-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create recorder.py helper for getting audio clips (#1010)
This helper script will record audio from microphone. Co-authored-by: Alexander Suvorov <[email protected]>
- Loading branch information
1 parent
7dec393
commit 2240669
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Setup Instruction | ||
#1. Install Dependencies with `pip install pyaudio` | ||
#2. Run python recorder.py | ||
#3. Record for 5 seconds and wait for the process to complete | ||
#4. Check directory to see the recording file (output.wav) | ||
|
||
import pyaudio | ||
import wave | ||
|
||
chunk = 1024 # Record in chunks of 1024 samples | ||
sample_format = pyaudio.paInt16 # 16 bits per sample | ||
channels = 1 | ||
fs = 16000 # Record at 16k samples per second | ||
seconds = 5 | ||
filename = "output.wav" | ||
|
||
p = pyaudio.PyAudio() # Create an interface to PortAudio | ||
|
||
print('Recording') | ||
stream = p.open(format=sample_format, | ||
channels=channels, | ||
rate=fs, | ||
frames_per_buffer=chunk, | ||
input=True) | ||
|
||
frames = [] # Initialize array to store frames | ||
|
||
# Store data in chunks for 3 seconds | ||
for i in range(0, int(fs / chunk * seconds)): | ||
data = stream.read(chunk) | ||
frames.append(data) | ||
|
||
# Stop and close the stream | ||
stream.stop_stream() | ||
stream.close() | ||
# Terminate the PortAudio interface | ||
p.terminate() | ||
print('Finished recording') | ||
|
||
# Save the recorded data as a WAV file | ||
wf = wave.open(filename, 'wb') | ||
wf.setnchannels(channels) | ||
wf.setsampwidth(p.get_sample_size(sample_format)) | ||
wf.setframerate(fs) | ||
wf.writeframes(b''.join(frames)) | ||
wf.close() |