Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

wavio

00:00 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?

00:14 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 wavio.

00:30 And now instead of importing the SciPy module, you can just go ahead and import wavio.

00:40 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.

00:54 You would instead do wavio.write(), pass in that filename—so 'output.wav'.

01:02 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 2.

01:16 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.

01:47 This makes 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.