Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.


00:00 simpleaudio is another cross-platform library that’s made for playing back WAV files. A thing to note is that you can wait until the sound stops playing to continue on to the next line of code. Go ahead and install it into your environment using pip, and just say install simpleaudio.

00:25 While that’s installing, we can head over to the text editor and try it out. So, import simpleaudio as sa. Then define a filename, which in my case is going to be 'hello.wav', and then make a wave_obj, which will be sa.WaveObject, and you’re going to make this from a WAV file, and pass in that filename.

00:59 From here, you can go ahead and say play_obj and make this equal to the wave_obj and call the .play() method off of it. And if you want to wait until that’s completed, you can then say play_obj.wait_done().

01:19 That’ll make sure that the sound file has finished playing before continuing. So, save this and try it out!

01:31 “Hey there, this is a WAV file.” All right! That’s pretty cool! So, WAV files are sequences of bits with metadata stored as header information. There’s always a trade-off between sound quality and file size, however, so you’ll have to decide which is more critical for your application.

01:49 WAV files are considered uncompressed, but are defined by their sample rate and the size of each sample. The standard for music on a CD is a 16-bit sample, recorded at 44,100 samples per second, but this might not be necessary for things like speech. You could, for example, drop the sampling rate down to maybe 8,000 per second and enjoy some large file size improvements.

02:14 You might be wondering what this has to do with us using Python to play sounds, and it’s a good question. Some of the libraries you’ll learn about treat audio files as bytes, while others use NumPy arrays. simpleaudio can use both, so let’s see how this works in the editor. Go ahead and install NumPy,

02:37 and then import it into your script.

02:44 I’m going to delete the rest of this here, ‘cause we’re not going to need it—because instead, you’re going to be generating your own sound by creating it in a NumPy array.

02:53 NumPy arrays are mutable, unlike the bytes objects that you’d be getting from a WAV file, which make it better suited for making sounds and any type of audio processing.

03:04 So in your editor, go ahead and define a frequency and set this equal to 440, and then make your sampling size, which will be 44100.

03:17 And then for seconds, let’s say 3. Now make an array, which will just be an np and a linearly-spaced array from 0 to seconds, and it will contain seconds times the sampling rate.

03:40 All right, now make that 440 Hz sine wave, so note is just going to equal an np.sin() function, and in here pass in your frequency * t * 2, and then multiply that by pi.

04:02 And you’re going to want to make sure that the highest value is in that 16-bit range,

04:10 so you can say note, and then multiply this by 2**15 - 1, and then divide that by the largest absolute value inside note.

04:28 Make this 16-bit data by just reassigning audio to audio.as_type(np.int16). So with this, you can make your play_obj, which will now be an sa.play_buffer(),

04:55 which you’ll pass in audio, 1, 2, and then your sampling rate. And then like before, take that play_obj and call .wait_done() off of it.

05:08 All right. Let’s see if this works.

05:17 All right! So if you heard anything, that’s your computer generating a 440 Hz tone—or an A4 note, if you’re into music. If you’ve ever tuned a guitar off of a tuning fork, there’s a good chance that’s the note that was playing. All right!

05:32 So now you not only know how to play audio files, you can create your own audio sounds using NumPy arrays. In the next video, you’re going to learn how to use winsound, which only works with WAV files on Windows machines.

05:45 But it’s pretty straightforward, so it’s still definitely worth covering. Thanks for watching.

Pygator on March 1, 2020

What is 2**15 -1 doing? and it’s not clear what you did with the play buffer call. Thanks.

Joe Tatusko RP Team on March 2, 2020

Hi Pygator,

This one is a bit more complicated as it synthesizes a sound from a sine function. Since the goal is to produce a 16-bit sound, 2**15 -1 will create the ‘ceiling’ for the volume, applied to the note array made earlier to scale the data.

Because audio is now an array that maxes out at 16 bits, it can be passed into play_buffer to generate a tone. It accepts arguments for the audio data, number of channels, number of bytes per sample, and the sample rate.

Pygator on March 2, 2020

Thanks the 16 bits part makes more sense.

Become a Member to join the conversation.