How to Create Your Own Voice Recorder Using Python

Voice recorders are used by many people for many purposes like: for recording songs, recitations and many more. But finding the perfect voice recorder may be difficult. So why not build our own voice recorder . A few lines of code can be used to make our own voice recorder.

So let’s get started right away.

Voice Recorder Project

Requirements:

Basic knowledge of python( Learn Python Tutorial) . We can use any ide (for example : https://code.visualstudio.com/download, https://jupyter.org/) .

Modules required:

1. Sounddevice : Sounddevice ,a cross-platform module in Python helps us to play and record sounds. This module is available for windows ,linux and macOS.

2. Wavio : It is a Python module. We will use it for reading and writing WAV files using numpy arrays.

Installation:

Firstly, we have to open command prompt in administrator mode.

Sounddevice :To install sounddevice we will type the below command in the terminal.

pip install sounddevice

pyautogui: Similarly ,for installing wavio, we will type the below command in the terminal.

pip install wavio

Approach:

1. Firstly, we will import the modules one by one.

 import sounddevice as sd
 import wavio as wv 

2. Secondly, we have to specify the frequency (44100 or 48000 frames per second) and recording duration (in seconds).

 # Sampling frequency
 freq = 48000
 # Recording duration
 duration = 10 

3. Thirdly, we will start recorder with the given values of duration and sample frequency.

 recording = sd.rec(int(duration * freq),
                    samplerate=freq, channels=2)
 sd.wait() 

This will create a numpy array of our recorded audio.

4. Finally, we are done with our recording and now we will save it using wavio module. We have to use the write function from wavio. The parameters are – recorded wav file, recording, frequency and sampwidth (sampling width of the audio, generally 1 or 2) .

wv.write("MyRecording.wav", recording, freq, sampwidth=2)

Source Code:

import sounddevice as sd 
import wavio as wv 
 
freq = 48000
duration = 10
 
recording = sd.rec(int(duration * freq),  samplerate=freq, channels=2) 
sd.wait() 
 
wv.write("MyRecording1.wav", recording, freq, sampwidth=2)

Another Approach:

Here,instead of using wavio module, we will use scipy module to convert the numpy array into an audio file.

Installation:

Firstly, we have to open command prompt in administrator mode and write —

pip install scipy

Source Code:

import sounddevice as sd 
from scipy.io.wavfile import write 
 
freq = 48000
duration = 10
 
recording = sd.rec(int(duration * freq), samplerate=freq ,channels=2) 
sd.wait() 
 
write("MyRecording.wav", freq, recording) 
 

Hope you liked this tutorial.