How to Play and Record Audio in Python

Learn how to play and record sound files using different libraries such as playsound, Pydub and PyAudio in Python.
Abdou Rockikz · 4 min read · Updated feb 2020 · General Python Topics


Many of the applications out there records your voice as well as playing sounds, if you want to do that as well, then you came into the right place, in this tutorial, we will be using different Python libraries to play and record audio in Python.

Let's install the required libraries for this tutorial:

pip3 install playsound pyaudio pydub ffmpeg-python

Audio Player

First, we gonna start with the most straightforward module here, playsound:

from playsound import playsound

playsound("audio_file.mp3")

Yes, that's it required for this module, it is basically a pure Python, cross platform, single function module. The documentation says that WAV and MP3 extensions are known to work, and it may work for other formats as well.

playsound() function plays the sound in the audio file and blocks until the file reading is completed, you can pass block=False to make the function run asynchronously.

Another alternative is to use Pydub library:

from pydub import AudioSegment
from pydub.playback import play

# read MP3 file
song = AudioSegment.from_mp3("audio_file.mp3")
# song = AudioSegment.from_wav("audio_file.wav")
# you can also read from other formats such as MP4
# song = AudioSegment.from_file("audio_file.mp4", "mp4")
play(song)

Note: You need ffmpeg installed in your machine in order to use AudioSegment.from_file() function that supports all formats that is supported by ffmpeg.

Pydub is quite a popular library, as it isn't only for playing sound, you can use it for different purposes, such as converting audio files, slicing audio, boosting or reducing volume, and much more, check their repository for more information.

If you wish to play audio using PyAudio, check this link.

Audio Recorder

To record voice, we gonna use PyAudio library, as it is the most convenient approach:

import pyaudio
import wave

# the file name output you want to record into
filename = "recorded.wav"
# set the chunk size of 1024 samples
chunk = 1024
# sample format
FORMAT = pyaudio.paInt16
# mono, change to 2 if you want stereo
channels = 1
# 44100 samples per second
sample_rate = 44100
record_seconds = 5
# initialize PyAudio object
p = pyaudio.PyAudio()
# open stream object as input & output
stream = p.open(format=FORMAT,
                channels=channels,
                rate=sample_rate,
                input=True,
                output=True,
                frames_per_buffer=chunk)
frames = []
print("Recording...")
for i in range(int(44100 / chunk * record_seconds)):
    data = stream.read(chunk)
    # if you want to hear your voice while recording
    # stream.write(data)
    frames.append(data)
print("Finished recording.")
# stop and close stream
stream.stop_stream()
stream.close()
# terminate pyaudio object
p.terminate()
# save audio file
# open the file in 'write bytes' mode
wf = wave.open(filename, "wb")
# set the channels
wf.setnchannels(channels)
# set the sample format
wf.setsampwidth(p.get_sample_size(FORMAT))
# set the sample rate
wf.setframerate(sample_rate)
# write the frames as bytes
wf.writeframes(b"".join(frames))
# close the file
wf.close()

The above code basically intializes the PyAudio object, and then we open up a stream object that allow us to record from the microphone using stream.read() method. After we finish recording, we use the built-in wave module to write that WAV audio file into the disk.

When you set input=True in p.open() method, you will be able to use stream.read() to read from the microphone. Also, when you set output=True, you'll be able to use stream.write() to write to the speaker.

Conclusion

Alright, in this tutorial, you learned how you can play audio files using playsound, Pydub and PyAudio libraries as well as recording voice using PyAudio.

A great challenge for you, is to combine this with a screen recorder, and you'll come up with a Python tool that records your voice and screen simultaneously, you will need to use a thread that records audio, and another one for the screen recorder, good luck with that !

Learn alsoHow to Convert Python Files into Executables.

Happy Coding ♥

View Full Code
Sharing is caring!



Read Also





Comment panel

   
Comment system is still in Beta, if you find any bug, please consider contacting us here.