Create a Voice Recorder using Python

Python can be used to perform a variety of tasks. One of them is creating a voice recorder. We can use python’s sounddevice module to record and play audio. This module along with the wavio or the scipy module provides the way to save recorded audio.

Installation

  • sounddevice: This module provides functions to play and record NumPy arrays containing audio signals. Let’s install it by running the following command:
$ pip3 install sounddevice
  • We can use either wavio and scipy to save the recorded audio in file format. We will see both of them here.
  • To install wavio:
$ pip3 install wavio
  • To install scipy:
$ pip3 install scipy

Now, we are done with installing the required modules. So, let’s write the code.

Getting Started

First, import the required libraries.

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv

Now, before starting the recorder, we have to declare a few variables. The first one is the sampling frequency of the audio (in most cases this will be 44100 or 48000 frames per second) and the second is recording duration. We have to specify the duration in seconds so that it stops recording after that duration.

So, let’s declare them too.



# Sampling frequency
freq = 44100

# Recording duration
duration = 5

Now, we are ready to start the recorder. It will create a NumPy array of the recorded audio.

# Start recorder with the given values of 
# duration and sample frequency
recording = sd.rec(int(duration * freq), 
                   samplerate=freq, channels=2)

# Record audio for the given number of seconds
sd.wait()

Now, we are done with recording the audio. So, let’s save it. To save the audio file, we can either use the scipy module or the wavio module. Let’s go through them one by one.

Using Scipy:

We will use the write function from scipy.io.wavfile to convert the NumPy array to an audio file.

# This will convert the NumPy array to an audio
# file with the given sampling frequency
write("recording0.wav", freq, recording)

Using wavio:

We can also use the write function from the wavio library.

# Convert the NumPy array to audio file
wv.write("recording1.wav", recording, freq, sampwidth=2)

Complete Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv
  
# Sampling frequency
freq = 44100
  
# Recording duration
duration = 5
  
# Start recorder with the given values 
# of duration and sample frequency
recording = sd.rec(int(duration * freq), 
                   samplerate=freq, channels=2)
  
# Record audio for the given number of seconds
sd.wait()
  
# This will convert the NumPy array to an audio
# file with the given sampling frequency
write("recording0.wav", freq, recording)
  
# Convert the NumPy array to audio file
wv.write("recording1.wav", recording, freq, sampwidth=2)

chevron_right


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Wear your failure as a badge of honour

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.