Data mismatches when reading and writing to a wav file

54 Views Asked by At

I am working on a audio using the libsndfile library. First, I have loaded my data from an existing wav file and then I stored those data into an std::vector. Next, I passed those data to sf_write_float but before I opened the new file with this function sf_open in write mode and store the data in that new file into a new std::vector.

The following is a function that writes:

SF_INFO sfInfo;
sfInfo.samplerate = sampleRate;
sfInfo.channels = nbrChannels;
sfInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;

SNDFILE* sndFile = sf_open(path.c_str(), SFM_WRITE, &sfInfo);
if (sndFile == nullptr) {
     std::cout << sf_strerror(sndFile);
}

sf_count_t count = sf_write_float(sndFile, samples.data(), samples.size());

sf_write_sync(sndFile);
sf_close(sndFile);

And this the function to read:

SF_INFO input;
SNDFILE* infile;

infile = sf_open(filePath.c_str(), SFM_READ, &input);
if (infile == nullptr) {
     std::cout << sf_strerror(infile);
}

float audio[input.channels * input.frames];
sf_read_float(infile, audio, input.channels * input.frames);

I was expecting to get similar data for both vectors but they are slightly different (difference of 10^-4).

Can someone explain this?

NB: I inspected the samplerate, the frames, the channels, the format, the sections and the seekable fields and they all are the same.

0

There are 0 best solutions below