- How to Record Audio using Python
- Install
- Find Available Microphones
- Record Audio
- Save Audio to File
- How to Play and Record Audio in Python?
- How to Play and Record Audio in Python?
- Install Required Libraries
- 1) Python playsound library
- 2) Python sounddevice library
- Python Program to Play Audio Files
- How to Record Audio in Python?
- #Python program to record an audio file.
- Conclusion
- Play and Record Sound with Python§
How to Record Audio using Python
Recording audio from a microphone using Python is tricky! Why? Because Python doesn’t provide a standard library for it. Existing third-party libraries (e.g. PyAudio) are not cross-platform and have external dependencies. We learned this the hard way as we needed microphone recording functionality in our voice recognition demos.
Hence we created PvRecorder, a cross-platform library that supports Linux, macOS, Windows, Raspberry Pi, NVIDIA Jetson, and BeagleBone. PvRecorder has SDKs for Python, Node.js, .NET, Go, and Rust.
Below we learn how to record audio in Python using PvRecorder . The Python SDK captures audio suitable for speech recognition, meaning the audio captured is already 16 kHz and 16-bit. PvRecorder Python SDK runs on Linux, macOS, Windows, Raspberry Pi, NVIDIA Jetson, and BeagleBone.
Install
Install PvRecorder using PIP:
Find Available Microphones
A computer can have multiple microphones. For example, a laptop has a built-in microphone and might have a headset attached to it. The first step is to find the microphone we want to record.
from pvrecorder import PvRecorder for index, device in enumerate(PvRecorder.get_audio_devices()): print(f"[index>] device>")
Running above on a Dell XPS laptop gives:
[0] Monitor of sof-hda-dsp HDMI3/DP3 Output[1] Monitor of sof-hda-dsp HDMI2/DP2 Output[2] Monitor of sof-hda-dsp HDMI1/DP1 Output[3] Monitor of sof-hda-dsp Speaker + Headphones[4] sof-hda-dsp Headset Mono Microphone + Headphones Stereo Microphone[5] sof-hda-dsp Digital Microphone
Take note of the index of your target microphone. We pass this to the constructor of PvRecorder . When unsure, pass -1 to the constructor to use the default microphone.
Record Audio
First, create an instance of PvRecoder . You need to provide a device_index (see above) and a frame_length . frame_length is the number of audio samples you wish to receive at each read. We set it to 512 (32 milliseconds of 16 kHz audio). Then call .start() to start the recording. Once recording, keep calling .read() in a loop to receive audio. Invoke .stop() to stop recording and then .delete() to release resources when done.
recorder = PvRecorder(device_index=-1, frame_length=512) try: recorder.start() while True: frame = recorder.read() # Do something . except KeyboardInterrupt: recorder.stop() finally: recorder.delete()
Save Audio to File
You can do whatever you wish using the code snippet above. Whether you want to detect wake words, recognize voice commands, transcribe speech to text, index audio for search, or save it to a file. The code snippet below shows how to save audio into a WAVE file format.
recorder = PvRecorder(device_index=-1, frame_length=512) audio = [] try: recorder.start() while True: frame = recorder.read() audio.extend(frame) except KeyboardInterrupt: recorder.stop() with wave.open(path, 'w') as f: f.setparams((1, 2, 16000, 512, "NONE", "NONE")) f.writeframes(struct.pack("h" * len(audio), *audio)) finally: recorder.delete()
Subscribe to our newsletter
How to Play and Record Audio in Python?
There are many applications out there on the internet that can play and record audio files such as mp3, wav, and mp4. If you are a Python developer and wish to write code that can record or play audio for you, then continue reading this article.
In this Python tutorial, I will walk you through two Python programs that can play and record audio.
How to Play and Record Audio in Python?
Install Required Libraries
Before we can code in Python to play and record audio, we need to install three Python libraries, namely playsound, sounddevice, and Scipy.
1) Python playsound library
As the library name suggests, the playsound library is used to play different types of audio files. It is an open-source Python library, and you can install it using the following pip command:
2) Python sounddevice library
The Python sounddevice library is another open-source library that is used to play and record NumPy arrays containing sound singles. This means it uses NumPy arrays to generate and structure audio file data. To install the sounddevice library, run the following pip command on your terminal or command prompt:
3) Python Scipy library
Scipy is a scientific computing library for Python, and in this tutorial, we will be using this library to save or write the data generated by the sounddevice library. Install the Python library using the following command:
Python Program to Play Audio Files
Playing an audio file is very straightforward with the Python playsound library. Check out the following code:
from playsound import playsound filename = "music.mp3" print(f"Playing . ") playsound(filename) #play audio
In this example, the music.mp3 audio file is located in the same directory where the Python script is, and that’s why we have only specified the file name, not the complete path.
Thus, if your audio file is located in some directory other than the directory having the Python script, then you need to specify the full path, like: filename =r»C:\Users\tsmehra\music\music.mp3″
How to Record Audio in Python?
Now you know how to play audio in Python using the Python playsound library. Next, let’s write a Python program that will record audio from your mic. Let’s start with importing the required modules.
import sounddevice as sd from scipy.io.wavfile import write from playsound import playsound import time
With the sounddevice module, we will record the audio in the wav format. Using the scipy.io.wavfile write module, we will save the recorded wav audio file locally, and the playsound module will allow us to play the recorded file. With the time module, we will create a recording timer.
Now, let’s define a timer function that will print the timer while the audio is recording.
def timer(duration): while duration: mins, secs = divmod(duration, 60) timer = f" mins: seconds Left" print(timer, end=" \r") time.sleep(1) duration -= 1
Next, let’s define the audio_record function that will record the audio and save it locally:
def record_audio(filename): #frequency fs=44100 #frames per second duration = 10 # seconds in integer print("Recording. ") #start recording myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2) timer(duration) #call timer function sd.wait() #write the data in filename and save it write(filename, fs, myrecording)
The fs variable specifies the frequency of the audio in frames per second, and its value could either be 44,100 or 48,000. The duration variable defines the recording duration in seconds.
rec() initializes the recording object while the wait() function holds the recording screen for the specified duration . Also, the write() function writes the recorded data and saves it in the same directory where the Python script is located, with the specified filename .
Now, let’s declare a variable that represents the recorded audio file name and calls the record_audio function.
filename ="new_record.wav" record_audio(filename) listen = input("Do you want to listen the recorded audio? [y/n]") if listen.lower() =="y": playsound(filename)
Finally, let’s put all the code together and execute it.
#Python program to record an audio file.
import sounddevice as sd from scipy.io.wavfile import write from playsound import playsound import time def timer(duration): while duration: mins, secs = divmod(duration, 60) timer = f" mins: seconds Left" print(timer, end=" \r") time.sleep(1) duration -= 1 def record_audio(filename): #frequency fs=44100 #frames per second duration = 10 # seconds in integer print("Recording. ") #start recording myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2) timer(duration) #call timer function sd.wait() #write the data in filename and save it write(filename, fs, myrecording) filename ="new_record.wav" record_audio(filename) listen = input("Do you want to listen the recorded audio? [y/n]") if listen.lower() =="y": playsound(filename)
When you execute the program, look in the directory where your Python script is located. There you will find a new wav audio file with the name new_record.wav .
Conclusion
In this Python tutorial, you learned » How to Play audio in Python? » and » How to Record Audio in Python? » In this tutorial, we have used three Python libraries, which are playsound , sounddevice , and Scipy .
We would recommend you read the official documentation of these three libraries if you want to perform more audio-related functions in Python. Let us know in the comments if you come across any issues.
People are also reading:
Play and Record Sound with Python§
This Python module provides bindings for the PortAudio library and a few convenience functions to play and record NumPy arrays containing audio signals.
The sounddevice module is available for Linux, macOS and Windows.
Source code repository and issue tracker:
MIT – see the file LICENSE for details.
- Installation
- Usage
- Playback
- Recording
- Simultaneous Playback and Recording
- Device Selection
- Callback Streams
- Blocking Read/Write Streams
- Play a Sound File
- Play a Very Long Sound File
- Play a Very Long Sound File without Using NumPy
- Play a Web Stream
- Play a Sine Signal
- Input to Output Pass-Through
- Plot Microphone Signal(s) in Real-Time
- Real-Time Text-Mode Spectrogram
- Recording with Arbitrary Duration
- Using a stream in an asyncio coroutine
- Creating an asyncio generator for audio blocks
- Reporting Problems
- Development Installation
- Building the Documentation
- Convenience Functions using NumPy Arrays
- play()
- rec()
- playrec()
- wait()
- stop()
- get_status()
- get_stream()
- query_devices()
- DeviceList
- query_hostapis()
- check_input_settings()
- check_output_settings()
- default
- default.device
- default.channels
- default.dtype
- default.latency
- default.extra_settings
- default.samplerate
- default.blocksize
- default.clip_off
- default.dither_off
- default.never_drop_input
- default.prime_output_buffers_using_stream_callback
- default.hostapi
- default.reset()
- AsioSettings
- CoreAudioSettings
- WasapiSettings
- Stream
- Stream.abort()
- Stream.active
- Stream.blocksize
- Stream.channels
- Stream.close()
- Stream.closed
- Stream.cpu_load
- Stream.device
- Stream.dtype
- Stream.latency
- Stream.read()
- Stream.read_available
- Stream.samplerate
- Stream.samplesize
- Stream.start()
- Stream.stop()
- Stream.stopped
- Stream.time
- Stream.write()
- Stream.write_available
- RawStream
- RawStream.read()
- RawStream.write()
- sleep()
- get_portaudio_version()
- CallbackFlags
- CallbackFlags.input_underflow
- CallbackFlags.input_overflow
- CallbackFlags.output_underflow
- CallbackFlags.output_overflow
- CallbackFlags.priming_output
- PortAudioError.args
- _initialize()
- _terminate()
- _split()
- _StreamBase