In this tutorial, we will look at the various ways in which we can play audio using python. There are numerous python libraries that can play audio in your system. We will use a beep noise to test the various methods.
Let’s look at some of the ways in which we can make a beep sound in python.
Using the Bell character to make a beep noise.
This is one of the simplest ways in which we can make a beep sound in python. Just open your python prompt or your anaconda prompt depending upon your usage.
Run the following code:
This method doesn’t need any external package installation or imports hence makes audio playing easier.
Note: In some terminals this might not work.
Using pygame to play audio in Python
Before we can use pygame let’s install it in your system by running the following code in the terminal:
pip install pygame
If you’re using conda distribution, run the following code instead:
conda install -c cogsci pygame
Pygame is a game development tool that is used for creating games using python. This module contains a function called mixer that can play specific audios. I’ll use a beep.wav file that will be played using the mixer function.
#importing required modules
from pygame import mixer
#mixer function call
#storing the sound after accessing it
#playing the sound
You can directly do this in your python terminal or store it in a file and then run it. Either way the sound will be played.
I’ve directly played the sound in the python idle shell. It is shown below:
Using the winsound built in windows function
This winsound function doesn’t need any extra installations because it is an built in function in windows systems. Let’s look at it’s implementation:
This method just like the previous one can be directly used in the python shell or can be saved as a program file and then run. I have directly played and used the winsound function in the terminal.
Using playsound in Python
The playsound module is also used to play different sounds both in “wav” or “mp3” format. It contains only one function which is also called playsound().
We need to install the playsound module before we can use it. Run the following code in your command prompt to install playsound.
pip install playsound
Let’s look at how we can play both .wav and .mp3 files using playsound().
# importing module
from playsound import playsound
# for playing beep.wav file
print('playing sound using the playsound module.')
This can be done directly in the python shell or can be saved as a file before running.
The same code can be used to play .mp3 files. The only change would be to replace the “.wav” extension to “.mp3” or by downloading an audio in the “.mp3” format.
Using the pydub module to play sounds
The pydub module is a high level audio manipulation interface that enables users to easily edit and play audios and sounds in python. We can install pydub by running the simple command in our command prompt:
pip install pydub
Now let’s look at how we can use this library to play different sounds:
# importing required modules
from pydub import AudioSegment
from pydub.playback import play
# for playing our own wav file
noise = AudioSegment.from_wav("C:/Users/SHREYA/Downloads/beep-01a.wav")
print('playing a beep noise using the pydub library.')
The following would be displayed in your python shell and the audio will be played.
playing a beep noise using the pydub library.
For more advanced articles, visit: How to Extract Audio from Video files (Mp3 From Mp4)
This article covers the various ways in which we can play sounds with python and python modules. Python offers various high level audio manipulation interfaces that might be used according to our own use. From winsound, a built in function to pydub, a variety of functions can be used to play audios when coupled with python. To more about audio manipulation with python, start here.