Python simpleaudio

This article covers the audio library, simpleaudio in Python.

Audio files can play a significant role in the creation of software, most notably in large GUI programs or Games. Python with it’s vast community and resources has multiple Libraries which can be used to play high quality sound files. One of these many Python libraries in called simpleaudio.

Be sure to download and install simpleaudio before continuing. If you’re using pip, you can use the pip install simpleaudio command in the command prompt.


Playing audio with simpleaudio

import simpleaudio

filename = "background.wav"

sound_object = simpleaudio.WaveObject.from_wave_file(filename)
play_object = sound_object.play()
play_object.wait_done()

And that’s it. Pretty “simple” right? If you copy and run the above 5 lines of code, the audio you selected will begin to run. Now for the line by line explanation.

import simpleaudio

Imports the classes and functions inside the simpleaudio module for use in our program.

filename = "background.wav"

To keep things simple we’re going to place the file path of the sound file into a variable. Since the sound file is in the same location as our python script, we simply used it’s name, otherwise the full path would be required.

We typically use wav files with the audio libraries in Python. They are uncompressed “wave” sound files unlike something like mp3, which is compressed.

sound_object = simpleaudio.WaveObject.from_wave_file(filename)

You need to use the above function to create a “sound object” which we’ll be using later on. Remember to pass the filename variable, or just type in the filepath manually.

play_object = sound_object.play()

Creates a play-object and begins playing the sound file.

play_object.wait_done()

Using the play object we can modify the playing audio file a bit. Using the wait_done() method, we can have our program halt (while the audio is playing) until the audio track is completed.


Other Functions

Another useful function is the is_playing() method that’s used on place objects. It returns a True or False value depending on whether the audio is still playing or not.

In case you want to stop your audio clip halfway, you can use the stop() function on the play object.

Another rather complex function is the play_buffer() function. It’s a way to run audio directly without the need of any wav file. The last parameter represents the frequency. Try tweaking the values to change the sound produced. The second and third parameters represent the number of audio channels and bytes per sample respectively.

play_obj = simpleaudio.play_buffer(audio_data, 2, 2, 44100)

The above example was taken from the official documentation. Try running for it (with the code we showed you earlier) yourself and seeing the result.

Check out other sound libraries in Python:


This marks the end of the Python simpleaudio article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments