Skip to content

Conversation

@gitelope
Copy link

@gitelope gitelope commented May 10, 2025

Fixes issue (#96) with saving AudioFile<double> to 32-bit WAV producing loud, scratchy noise.

…s taken *before* reinterpret_cat to int32_t& followed by cast to int32_t when an AudioFile<double> is saved to IEEE 32-bit float WAV.
@adamstark
Copy link
Owner

Hi there, thank you for this. Do you think you'd be able to provide a bit of sample code that produces the issue?

This is not because I don't believe your experience - simply I just want to write a unit test to capture this problem and then demonstrate that the change fixes it. I tried writing a test myself but could re-create the problem (perhaps I don't understand entirely the use case).

Thanks!

@Dwebb22
Copy link

Dwebb22 commented May 24, 2025 via email

@gitelope
Copy link
Author

gitelope commented May 28, 2025

MAKE SURE TO READ VOLUME WARNING AT END OF COMMENT.

constexpr int channels {1};
constexpr int sampleRate {48000};
constexpr int bitDepth {32};

constexpr double lengthInSeconds {1};
constexpr double frequency {440};

constexpr int lengthInSamples = sampleRate * lengthInSeconds;

AudioFile<double> af;

af.setSampleRate (sampleRate);
af.setBitDepth (bitDepth);
af.setAudioBufferSize (channels, lengthInSamples);

for (int n=0; n<lengthInSamples; ++n)
    af.samples[0][n] = sin (2. * M_PI * frequency * static_cast<double>(n) /sampleRate);

// This may be VERY LOUD:
af.save("lower-your-volume-as-far-as-possible.wav");

If instead I use
AudioFile<float> af;
there's no issue.

The audio is actually quite loud for me so I recommend turning your volume as down as far as you can without turning it off and listening with some cheap headphones or speakers and get ready to mute or pause or something.

@adamstark
Copy link
Owner

Thanks for this! I managed to write some unit tests that failed by doing some 32-bit and double precision tests. I've also confirmed your change fixes the problem, nice work! :)

I'll merge this into develop now...

@adamstark adamstark merged commit 99e4ce6 into adamstark:develop May 30, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants