Skip to main content

How to Create A Voice Recorder Using Python

 A voice recorder records a sound or a voice of a person and converts it into an audio file. The file can be stored in different audio formats like MP3 format, Waveform Audio File (WAV), Advanced Audio Coding (AAC), Audio Interchange File Format (AIFF), etc. and later it can be transferred to other devices. Any device that is capable of recording a sound or a voice is said to be a voice recorder by default.



In this article, you will learn to build a voice recorder with a few lines of code in Python.

Where do We Use Voice Recorder?

Some of the applications of a voice recorder are listed below:

  • Interviews

Essentials

The first and foremost thing needed to build a voice recorder using Python is two basic Python modules. They are:

  1. sounddevice module

Installation

The two modules needed to code a voice recorder must be installed first to make use of it in a program further. These Python modules can be installed using the command:

pip install sounddevicepip install scipy

sounddevice Module

The sounddevice module in Python has functions to play and record audio signals contained in the NumPy array. It can also provide bindings for the PortAudio library.

If you install the sounddevice module using the above pip command on Windows or macOS, then it automatically installs the PortAudio library on your device. You need to install the PortAudio library with other packages manually, only on other Operating Systems rather than the platforms mentioned above.

scipy.io.wavfile.write Module

It writes the NumPy array into a WAV file. The WAV file will be simple and compressed. It takes 3 parameters namely, filename, sample rate and data. To make multiple channels, use a 2D array of shapes.

Some of the functions carried out by this module are:

  • Playback

Importing Modules

The above modules need to be imported first to extract all its features and to be used inside our program. It can be imported with the help of the following commands:

import sounddevicefrom scipy.io.wavfile import write

Record an Audio

The sounddevice.rec() method from the sounddevice module is used to record audio data from a sound device into an array in NumPy. The sampling frequency is assigned by the programmer as 44100 frames per second.

The command to record audio is as follows:

record_voice = sounddevice.rec(int(second* fs), samplerate=fs, channels=2)

where second → the time duration needed to record an audio

fs → sampling frequency

The recording will be carried out in the background, so you can work on other commands in the meantime. To check whether the recording is finished, use the following command:

sounddevice.wait()

By default, the data type of the recorded array will be float32, you can change the datatype by specifying the command as:

record_voice= sounddevice.rec(second* fs, dtype='float64')

Selection of a Device

One can have one or more input and output devices connected to their computer so that its user’s wish to select a device from the list of devices connected.

To get the list of devices connected to your device, use the command as:

sounddevice.query_devices() 

To set a particular device as a default one, use the command as:

sounddevice.default.device = 'digital output'

Writing to a File

The audio recorded is finally written into a file, where one can fetch or share that file for future use. The write() method in Python is used to create a file with a filename passed as an argument. The file name must be specified with the extension of an audio format so that it won't crash with the input given.

The command to write the output into a file is as follows:

write("out.wav" , fs , record_voice )

where out.wav is the name of the output file. This output file will be saved in the same directory where the program code is saved.

Time to code!!


import sounddevice
from scipy.io.wavfile import write
fs= 44100
second =  int(input("Enter time duration in seconds: "))
print("Recording.....\n")
record_voice = sounddevice.rec( int ( second * fs ) , samplerate = fs , channels = 2 )
sounddevice.wait()
write("out.wav",fs,record_voice)
print("Finished.....\nPlease check your output file")

Conclusion

Hope you guys found an easy and informative article on creating a Voice Recorder with the help of Python. Thanks for spending your valuable time here. Comment if you have any queries or suggestions regarding this article.

Thanks for learning!!!

Comments

Popular posts from this blog

Easy Way To Build An Audiobook Using Python

  An audiobook is nothing but a book that was recorded in an audio format. It can also be stated as a book that is being read aloud. Audiobook helps in improvising one's vocabulary, comprehension and pronunciation of words. Hope some of you guys are bookaholic but lazy to read it on your own. So it's time to create your own audiobook by using few codes of Python. This will definitely make you enjoy audiobooks without any subscription fee, that was imposed on platforms like Audible, Scribd, etc. Prerequisite Python has a huge number of modules that contains reusable code which performs desired functions when invoked. It must be installed into your system before using its functionalities. So here we going to grab just 2 modules from a bunch of python modules prevailing around. Install PyPDF2 module, using                     pip install PyPDF2 Install pyttsx3 module, using                     pip install pyttsx3 PyPDF2 module It is one of the Pure-Python libraries that runs on an

Interacting With System Applications Through OS Module In Python

 As we all know, Operating System (OS) is a program that acts as an interface between computer hardware and user. Python has a standard utility module called "os module" to achieve a process of communicating directly to the operating system of your device. Installation The os module is one of the standard libraries in Python 3, so it doesn't need to be installed manually or externally. It comes along with the installation of Python. So it is mandatory to import this os module before using it in your program as it is not a built-in function. Importing module The os module can be used in a program by importing it initially. It is mandatory to import the os module as it is not a built-in function to use it without any importation. To import the os module, use the command as import os Built-in functions inside os module There are a bunch of built-in functions present inside the os module where one can invoke each function after the module gets imported inside a program.  To k