Python Voice Recording through Microphone for Arbitrary Time using PyAudio


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.


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":

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 and put the below content into it.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 44100

def record():

	p = pyaudio.PyAudio()

	stream =,

	print("Start recording")

	frames = []

		while True:
			data =
	except KeyboardInterrupt:
		print("Done recording")
	except Exception as e:

	sample_width = p.get_sample_size(FORMAT)
	return sample_width, frames	

def record_to_file(file_path):
	wf =, 'wb')
	sample_width, frames = record()

if __name__ == '__main__':
	print('#' * 80)
	print("Please speak word(s) into the microphone")
	print('Press Ctrl+C to stop the recording')
	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 This sets up a pyaudio.Stream to record audio.

To read audio data from the stream, we are using

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 script using command python 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

Download Source Code

Thanks for reading.

Leave a Reply

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