WavAudioFormat approximate write issue

I’m trying to read a wave file no 1, slightly modify the data, and exporting it to wave file no 2. The issue is when writing data they’re modified again, and when reading wave file no 2, I don’t have what I’m supposed to wrote.

I’m using the juce method :

audioFormatWriter->writeFromAudioSampleBuffer(floatBuffer, 0, nSamples)

And I identified this code to be the source of my issue :

//juce_AudioFormatWriter.cpp line 64
static void convertFloatsToInts (int* dest, const float* src, int numSamples) noexcept
{
    while (--numSamples >= 0)
    {
        const double samp = *src++;

        if (samp <= -1.0)
            *dest = std::numeric_limits<int>::min();
        else if (samp >= 1.0)
            *dest = std::numeric_limits<int>::max();
        else
            *dest = roundToInt (std::numeric_limits<int>::max() * samp); //this specific line

        ++dest;
    }
}

For what I’m trying to do, this is important that the data I retrieve from the wave file no 2 is exactly the same I wrote.

Is there a way to do some exact write ?

Yep, i stumbled over the same code, searching for a way how to write 32-bit float wave files. It seems that Juce does not have this option right now and only saves int wave files. In the code you mention your float data is clipped to 0dB and then converted to int. When you convert it back to float after reading from the file it naturally is not exactly the same because of rounding errors occurring in the conversion process.

It would be incredible to have the option to save 32-bit float wave files as they are used these days. If this will be implemented, you will get your exact read.

1 Like