pyaudio (Part 1)
Congrats! You’ve made it to the last library we’ll cover for playing audio. In this video, you’ll learn how to use
pyaudio, which also provides bindings for PortAudio. PortAudio keeps it cross-platform, so it will work on many different operating systems.
pyaudio is a bit different from what we’ve talked about so far in that the audio you hear is played by writing to a stream. It might be easier to see this in action, so let’s get to installing it and head to the text editor.
So, like before, set a
filename, which in my case is
'hello.wav'. And then I’m going to need to set a chunk size as well, so let’s just do
1024. And a chunk is just how many samples are put into each data frame. Now it’s time to open the sound file, so we’ll call that
wf and set that equal to
wave.open(), and then pass in the
filename, and you’ll want to read this as a binary.
Next, create an interface to PortAudio. So, with
pyaudio, you can then call
PyAudio(). Okay. Now it’s time to create that stream object, so say
stream and set this equal to
p.open(), so it’ll open up that interface. And then you’ll want to set your
format, which is going to be
p.get_format_from_width(), and then with the WAV file, you’ll get that sample width.
There’s two main objects here. There’s the actual WAV file and then there’s the connection to the audio device. So,
stream is going to be through this audio device, and it’s going to need a number of attributes that come from the WAV file.
So you’ll see that most of the data here like the sample width, the number of channels, and the frame rate are all coming from the WAV file and being passed into this interface. Setting
output equal to
True means that the sound is going to be played instead of recorded.
you’ll want to do
stream.write(data), and then set
data equal to
wf.readframes(), and grab that next chunk. So that’ll keep running until the file’s done, and after that, you can close the stream, and then terminate the interface. Okay.
03:39 So to recap that section there, you’re going to start chunking your WAV file into these little data frames, and as long as there’s another data frame, you’re going to write that data to the stream and then select the next chunk for the next frame. Once everything’s completed, you’re going to want to close that stream and then terminate your interface to the audio device. Okay! Let’s save this and see if it works.
04:08 “Hey there, this is a WAV file.” And there you go! Now, you might have picked up that this is pretty complex compared to what we’ve seen before, and it might seem strange to have to use, what, 24 lines of code? When different libraries only used a single line of code.
And there is a reason for this.
pyaudio is going to give you a lot more low-level control over how you play your sounds, which can be helpful depending on the demands of your application. Now, let’s say you have a little desktop application that you want to have some sound effects in there to notify a user of something.
Those might be better handled using a higher-level library where you just need a couple lines of code. Another case might be a more audio-centric program that would require the level of control that
pyaudio is going to give you a lot more control over input and output devices and can actually check the CPU load for latency. So if you’re making something like a digital audio workstation used in music production, that’s the kind of project that would benefit greatly from the level of control that
pyaudio will give you. And that’s it for playing audio!
Become a Member to join the conversation.