Introduction

In this tutorial we will see how to perform voice recording through microphone for arbitrary time using PyAudio in Python. PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. You can easily use Python with PyAudio to play and record audio on a variety of platforms.

This application does not use UI or frontend to start and stop the recordings.

To start the recording you just need to play the Python script and speak through microphone to record your voice as long as you want.

To stop recording you just need to press Ctrl+C keys from the keyboard in Windows operating system. For other operating system you need to check the key combination.

The final output is written into a .wav file which you can play and listen to your recording.

Prerequisites

Python 3.7.4, PyAudio 0.2.11, Windows 10 64 bit

Installing PyAudio

If you don’t have PyAudio installed in your system then you can install it by executing the following command:

pip install pyaudio

If you face any issue similar to the following while executing the above command:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

Then execute below commands one by one – when the first finishes execution then run the second command.

pip install pipwin
pipwin install pyaudio

Creating Project Directory

Create a project root directory called python-record-my-voice as per your chosen location.

We may not mention the project’s root directory name in the subsequent sections but we will assume that we are creating files with respect to the project’s root directory.

Creating Recording Script

Now we will create a Python script called record.py and put the below content into it.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100

def record():

	p = pyaudio.PyAudio()

	stream = p.open(format=FORMAT,
					channels=CHANNELS,
					rate=RATE,
					input=True,
					frames_per_buffer=CHUNK)

	print("Start recording")

	frames = []

	try:
		while True:
			data = stream.read(CHUNK)
			frames.append(data)
	except KeyboardInterrupt:
		print("Done recording")
	except Exception as e:
		print(str(e))

	sample_width = p.get_sample_size(FORMAT)
	
	stream.stop_stream()
	stream.close()
	p.terminate()
	
	return sample_width, frames	

def record_to_file(file_path):
	wf = wave.open(file_path, 'wb')
	wf.setnchannels(CHANNELS)
	sample_width, frames = record()
	wf.setsampwidth(sample_width)
	wf.setframerate(RATE)
	wf.writeframes(b''.join(frames))
	wf.close()

if __name__ == '__main__':
	print('#' * 80)
	print("Please speak word(s) into the microphone")
	print('Press Ctrl+C to stop the recording')
	
	record_to_file('output.wav')
	
	print("Result written to output.wav")
	print('#' * 80)

To use PyAudio, first we need to instantiate PyAudio using pyaudio.PyAudio(), which sets up the portaudio system.

To record or play audio, we need to open a stream on the desired device with the desired audio parameters using pyaudio.PyAudio.open(). This sets up a pyaudio.Stream to record audio.

To read audio data from the stream, we are using pyaudio.Stream.read().

We use pyaudio.Stream.stop_stream() to pause recording, and pyaudio.Stream.close() to terminate the stream.

Finally, we terminate the portaudio session using pyaudio.PyAudio.terminate().

The output is written to the output.wav file. You can play this file to listen to your recording.

Testing the Application

Execute the record.py script using command python record.py from command line tool.

Now speak through microphone to record your voice. When done just press Ctrl+C key combination from keyboard.

The final output is written into output.wav file. You can play the output.wav file to listen to your voice recording.

Source Code

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *