Why does this code introduce hiss/noise into wave sequence

73 Views Asked by At

I'm trying to extract PCM wave sequences from different files (files that are NOT audio files but contain audio data, and other data all in one file), and while this code does extract the expected audio, it seems to introduce a very audible hiss/noise into the sequence.

I've tried to find similar topics but the only thing I came across was either "aliasing" or "dither"? I've tried this code with a bunch of different files and formats, and it seems to add the same hiss noise into everything, so the hiss is definitely not contained in the original files that I'm trying to extract data from.

I know the original files contain MONO 16bit 48Khz PCM data.

int main()
{
const int               lenght  = 180; //file length, just a "long enough" number to make sure there's enough space for the whole sequence from the original file

const unsigned short    bits    = 16;
const unsigned int      bitrate = 48000;
const unsigned short    channels= 1;

std::ifstream file("samplesfile.img", std::ios::binary); //the original file that contains all sorts of data, plus a simple wave sequence

// Offset 1byte to align with the wave sequence bytes
unsigned int offsetAmount = 1;

if (offsetAmount > 0)
{
    unsigned char tmp[bits];
    file.read(reinterpret_cast<char*>(tmp), sizeof(unsigned char) * offsetAmount);
}


// Read everything from file after the offset and interpret it as a wave sequence
unsigned char* samples = new unsigned char[bitrate * lenght];
file.read(reinterpret_cast<char*>(samples), sizeof(unsigned char) * bitrate * lenght);
file.close();


// creating a .wav file, calculating chunk data

unsigned short  PCM         = 1;
unsigned int    chunk1size  = 16;
unsigned int    chunk2size  = lenght * bitrate;
unsigned int    chunkSize   = chunk2size + 40;
unsigned int    byteRate    = (bitrate * channels * bits) / 8;
unsigned short  blockAlign  = (bits * channels) / 8;    



std::ofstream file2("res.wav", std::ios::binary);

if (file2.good())
{
    file2.flush();
    file2.write("RIFF", sizeof(char) * 4);
    file2.write(reinterpret_cast<const char*>(&chunkSize), sizeof(unsigned int));
    file2.write("WAVE", sizeof(char) * 4);

    file2.write("fmt ", sizeof(char) * 4);
    file2.write(reinterpret_cast<const char*>(&chunk1size), sizeof(unsigned int));
    file2.write(reinterpret_cast<const char*>(&PCM),        sizeof(unsigned short));
    file2.write(reinterpret_cast<const char*>(&channels),   sizeof(unsigned short));
    file2.write(reinterpret_cast<const char*>(&bitrate),    sizeof(unsigned int));
    file2.write(reinterpret_cast<const char*>(&byteRate),   sizeof(unsigned int));
    file2.write(reinterpret_cast<const char*>(&blockAlign), sizeof(unsigned short));
    file2.write(reinterpret_cast<const char*>(&bits),       sizeof(unsigned short));

    file2.write("data", sizeof(char) * 4);
    file2.write(reinterpret_cast<const char*>(&chunk2size), sizeof(unsigned int)); // 40 bytes up to here
    file2.write(reinterpret_cast<const char*>(samples),     chunk2size);
}

file2.close();

delete[] samples;

return 0;
}

Result:

enter image description here

0

There are 0 best solutions below