How to Build a Simple Sound Recorder in Python?

Let us see how to build a simple sound recorder in Python. For this, we use the sounddevice and scipy modules.

sounddevice module:

The sounddevice module includes methods for playing and recording NumPy arrays containing audio signals. we should first install the module in our system before working with it.

Installation

pip install sounddevice

scipy module:

Scipy is an abbreviation for Scientific Python, and it is used to save the audio as a file.

Installation

pip install scipy

NOTE: We can save the recorded audio in file format using either wavio or scipy.

Variables Used:

Before we start the recorder, we need to declare a few variables.

1)Sampling frequency: 

This is the sampling frequency of the audio, which is set at 48000 or 44100 by default.

2)duration:

The second variable is the recording duration; we can record the audio for as long as we wish. We must define the duration in seconds so that the recording ends after that time.

Building a Simple Sound Recorder in Python

Approach:

  • Import sounddevice module using the import keyword.
  • Import write function from scipy.io.wavfile module using the import keyword.
  • Give the sampling frequency as static input and store it in a variable.
  • Give the duration of recording as static input and store it in another variable.
  • Pass the integer value of duration multipled with frequency, samplerate, channels as the arguments to the rec() function of sounddevice module.
  • Store it in a variable.
  • Here it converts the NumPy array to an audio file for the given sampling frequency.
  • The wait method, sounddevice.wait(), is used to wait until the recording is finished.
  • Save the audio in the .wav file format using the write() function by passing the audioname, sampling frequency, and the above-recorded audio to it.
  • The Exit of the Program.

Below is the implementation:

# Import sounddevice module using the import keyword.
import sounddevice 
# Import write function from scipy.io.wavfile module using the import keyword.
from scipy.io.wavfile import write
# Give the sampling frequency as static input and store it in a variable.
sampling_freq = 44100
# Give the duration of recording as static input and store it in another variable.
audio_duration= 5
print('The audio started to record......')
# Pass the integer value of duration multipled with frequency, samplerate, channels as the 
# arguments to the rec() function of sounddevice module.
# Store it in a variable.
# Here it converts the numpy array to an audio file for the given sampling frequency
recorded_audio = sounddevice.rec(int(audio_duration*sampling_freq), samplerate = sampling_freq , channels = 2)
# The wait method, sounddevice.wait(), is used to wait until the recording is finished.
sounddevice.wait()
print('The recording is finished!')
# Save the audio in the .wav file format using the write() function by passing the 
# audioname, sampling frequency and the above recorded audio to it
write("Audio.wav" , sampling_freq, recorded_audio)

Output:

The audio started to record......
The recording is finished!

Output Recording i.e Audio.wav:

Explanation:

After printing the message ‘The audio started to record……’ speak something in so that you may check for output. After 5 seconds, you will get the text ‘The recording is finished!’