WarrenWeckesser / wavio

A Python module for reading and writing WAV files using numpy arrays.
133 stars 19 forks source link

normalize option added for scale parameter in `write` #17

Closed nj-vs-vh closed 2 years ago

nj-vs-vh commented 3 years ago

My quick attempt to solve issues #12 and #13. Usage:

wavio.write("smth.wav", data, 44100, scale="normalize")

In this case the behavior mimics standard normalize feature found in any DAW: make audio as loud as possible while keeping zeroes at zero, i.e. avoiding DC offset. This may even be the default setting as it is the most fitting for dealing with recorded audio which is almost never symmetric.

Tests for the behavior are included.

nj-vs-vh commented 3 years ago

Would you be able to add a test for 24bit as well? Its not an uncommon bit rate in music.

I have just added one, though it really is similar to 32 and 16 bit as they both map analog zero to digital zero. One thing I just noticed is that writing 32-bit Numpy array to 24-bit .wav file results in hard clipping, not scaling down, but this seems to be preferred method for the library and I think I should not change that.

Exitare commented 3 years ago

Thanks for adding the test and for mentioning the 32 bit to 24 bit hard clipping.

WarrenWeckesser commented 2 years ago

@nj-vs-vh, a very belated "thanks" for the contribution! Back when you submitted this, I was considering more radical changes to the API, based on feedback in other issues. After a long delay, I've finally implemented a new API. I just pushed the changes today. Those changes make this pull request obsolete, so I'm closing it. But I think you were right about "keeping zeros at zero"--that is what the new (and simplified) API does.

Note that the new changes are not backwards compatible, so if you are still using wavio, you'll probably have to make some changes to handle the new API.