Build A Voice Recorder Using Python

Venkatalakshmi Last Updated : 19 Oct, 2021
4 min read
This article was published as a part of the Data Science Blogathon

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 few lines of code in Python.

Where do we use Voice Recorder?

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

  • Interviews
  • Handling Meetings
  • Lectures/Seminars in Educational Institutions
  • Audiobooks for kids
  • Learning foreign languages
  • Translating a word to another language
  • Google search by voice
  • A quick voice message to a person instead of typing
  • Podcasts
  • Voice assistants like Google assistants, Alexa, Siri, etc.,
Voice Recorder Using Python image

Essentials of Voice Recorder using Python

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

  1. sounddevice module
  2. write module from scipy.io.wavfile

Installation

The two modules needed to code a voice recorder must be installed first to make use of it in a program further. If you are using conda as the package manager like Miniconda or Anaconda for OS like Linux, Windows or macOS, then you can install using conda-forge channel. The command for installation is as follows,

conda install -c conda-forge python-sounddevice

These Python modules can also be installed using the command, 

pip install sounddevice

or

python -m pip install sounddevice

Installation of SciPy module in your device can be made by using the following command,

pip install scipy

or

python -m pip install scipy

To uninstall a module, specify the command as,

python3 -m pip uninstall sounddevice

sounddevice module to build Voice Recorder Using Python

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
  • Recording
  • Simultaneous Playback and Recording
  • Selection of a particular device
  • Callback Streams
  • Block read/write streams

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 sounddevice
from 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 is taken 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 command as,

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 into a file

The audio recorded is finally written into a file, where one can fetch or share that file for future use. write() method in Python is used to create a file with 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.

Other methods in sounddevice module

Some of the functions handled by the sounddevice module in Python are,

Usage

Function Name

Function Description

Playback sounddevice.play( myarray , fs ) This function plays audio data contained in a NumPy array. It also plays audio in the background.
sounddevice.stop() It stops the playback.
sounddevice.wait() It waits until the playback gets finished.
Recording

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

Records the audio given as an input.
Concurrent Playback and Recording sounddevice.playrec(myarray, fs, channels=2) It records and playback audio data at the same time.
Stream Callback get_status() Results in the status of the device, whether it is still recording or finished recording.
get_stream() It results in a stream whether the device is in the Input stream or the device is in the Output stream.
Blocking Streams Stream.read() It blocks the read stream.
Stream.write() It blocks the write stream.

Building a Voice Recorder in Python

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 Voice Recorder with the help of Python. Thanks for spending your valuable time here.

Happy learning!

Love Conquers All…!

 

About the author

Am Venkatalakshmi Subramaniam from Manchester of South India, am a passionate blogger, author as well as a doctoral student eager to learn new things day by day. 

Connect with me via 

LinkedIn : https://www.linkedin.com/in/venkatalakshmi-subramaniam-6ab4401a9/

Twitter: https://twitter.com/Venkat1144

Github: https://github.com/VenkatalakshmiS

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details