Build a Python Audio Recorder with PyAudio: Step-by-Step Guide
This tutorial explains how to install PyAudio, record and play back audio on Windows 10 using Python 3.7, covering stream callbacks, wave file handling, GUI integration, and keyboard hotkey control for a complete audio recording tool.
Hello, I’m 🌑 (the dark side of the moon). This article shows how to use PyAudio in Python to create an audio recording and playback tool on Windows 10.
Application Platform
Windows 10
Python 3.7
Audio Recording Section
Audio recording works like video recording by capturing data frames. Install PyAudio with: pip install PyAudio If the installation fails, download the matching .whl file (e.g., PyAudio‑xx.whl) and install it with: pip install PyAudio-xx.whl Core recording code using a callback stream:
from pyaudio import PyAudio, paInt16, paContinue, paComplete
# Fixed parameters
chunk = 1024 # frames per buffer
format_sample = paInt16 # sample format
channels = 2 # 1=mono, 2=stereo
fps = 44100 # sampling rate
def callback(in_data, frame_count, time_info, status):
"""Recording callback"""
wf.writeframes(in_data)
if xx: # when a certain condition is met
return in_data, paContinue
else:
return in_data, paComplete
p = PyAudio()
stream = p.open(format=format_sample,
channels=channels,
rate=fps,
frames_per_buffer=chunk,
input=True,
input_device_index=None, # default device
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1) # sensitivity
stream.stop_stream()
stream.close()
p.terminate()Save audio to a WAV file using the built‑in wave module:
import wave
wf = wave.open('test.wav', 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format_sample))
wf.setframerate(fps)For better reuse, the above logic is wrapped into an AudioRecord class (partial example shown):
from pyaudio import PyAudio
class AudioRecord(PyAudio):
def __init__(self):
super().__init__()
self.chunk = 1024
self.format_sample = paInt16
self.channels = 2
self.fps = 44100
self.input_dict = None
self.output_dict = None
self.stream = None
self.filename = '~test.wav'
self.duration = 0
self.flag = False
self.kill = False
# ... additional methods omitted for brevity ...Audio Playback Section
Playback code mirrors the recording code, using a callback to read frames from the WAV file:
wf = wave.open('test.wav', 'rb')
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
return data, paContinue
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
output_device_index=output_device_index,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)The tool supports both .wav and .mp3 formats for recording and playback.
GUI Window Attribute Code Section
To provide a user‑friendly interface, the script includes code for displaying audio duration and enumerating input/output devices:
# Audio duration
duration = wf.getnframes() / wf.getframerate() # Enumerate devices
dev_info = self.get_device_info_by_index(i)
default_rate = int(dev_info['defaultSampleRate'])
if not dev_info['hostApi'] and default_rate == fps and '映射器' not in dev_info['name']:
if dev_info['maxInputChannels']:
print('Input device:', dev_info['name'])
elif dev_info['maxOutputChannels']:
print('Output device:', dev_info['name'])pynput Keyboard Listener
The script uses pynput to listen for hotkeys that stop or cancel recording:
def hotkey(self):
"""Hotkey listener"""
with keyboard.Listener(on_press=self.on_press) as listener:
listener.join()
def on_press(self, key):
try:
if key.char == 't': # stop recording and save
self.flag = True
elif key.char == 'k': # cancel recording and delete file
self.flag = True
self.kill = True
except Exception as e:
print(e)Summary
The article demonstrates how to record and play back audio on Windows using PyAudio, covering installation, stream callbacks, wave file handling, device enumeration, GUI integration, and keyboard hotkey control, providing a solid foundation for building more advanced audio tools.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
