Build a simple Sound Recorder in Python

Hey techie, in this tutorial we are going to learn how to build a simple sound recorder in Python by importing some modules.

For building this application, we need to install sounddevice and scipy in our system.

You can also check: Voice Command Calculator in Python using speech recognition and PyAudio

Installing packages to record sound in Python

We need to go to the terminal and we need to type the following commands.

Sounddevice: Basically sounddevice is a module, which provides functions to record and play numpy arrays.

For installing sounddevice, we need to write the following command.

pip install sounddevice

Scipy:  Stands for Scientific Python, which is used to save the audio as the file.

The command for installing scipy is…

pip install scipy

Defining variables

Frequency: The first variable is frequency, by default the frequency is 48000 or 44100.
We are going to use the frequency as 44100, we can also use 48000.

Duration: The second variable is the duration of the recording, we can record the audio for the amount of time we want.
Let us take the duration to be 12 seconds.

Python Program to record sound

import sounddevice
import import write
fps = 44100
duration = 12 
recording = sounddevice.rec(int(duration*fps),samplerate = fps , channels = 2)
write("Output.wav" , fps, recording)     # for saving our recording in wav file

sounddevice.rec(int(duration*fps),samplerate = fps , channels = 2),  this function is used send the audio data to the array in numpy.

sounddevice.wait(), the wait method is used to wait until the recording is complete.

For saving the recording as the wav file we wrote the write() function.

In the write function, you can choose your own filename but  .wav extension is compulsory.



In Output, after printing the message ‘Recording…’ speak something so that we can check for the output. After 12 seconds we will get the message as ‘Done!’.

Then, right-click on the Output.wav file, select Reveal in file explorer.

By selecting the reveal in file explorer we will be entering into our file manager, then right-click on the Output.wav file.

Hurrah! we have learned how to build a simple recorder in Python.

Also, refer to these articles on your interest:

Leave a Reply

Your email address will not be published.