wavio is a useful module that is focused on converting between WAV files and NumPy arrays. Remember how in the recording with the
python-sounddevice example, you had to use a
SciPy module to convert the NumPy array to a WAV file?
If you don’t want to bring in all of
SciPy, you can instead use
wavio. I’ve copied over the example from before so we can see how it would look. Let’s go ahead and install
And now instead of importing the
SciPy module, you can just go ahead and
Using the same sample rate and the same length of time, just about everything stays the same until you get to this
write() line. So let’s go ahead and comment this out and see how
wavio would look.
You would instead do
wavio.write(), pass in that filename—so
Pass in the NumPy array, which is
myrecording. You’ll need to add the sample rate, so you can put
fs in there. And then the sample width—so for a stereo recording, that’ll be
And actually, this is just
sampwidth. And that’s all there is to it! To reiterate, if you don’t want to use
SciPy to convert your NumPy arrays to WAV files, you can use
wavio. And looking at its usage, it’s not that much different. You’re still going to define the filename, the sample rate, the array, and then you do have to define the sample width again, but that’s not too difficult. Remember that NumPy arrays are great when you need to create or modify sound data, due to their mutability.
wavio a great way to convert a
bytes object WAV file into a NumPy array, and then convert it back into a WAV file when you’re done. Next, we’ll talk about
soundfile, which will let you convert a WAV file into a FLAC, AIFF, and a couple other formats. Thanks for watching.
Become a Member to join the conversation.