-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordFromMicrophone.py
64 lines (49 loc) · 1.7 KB
/
recordFromMicrophone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pyaudio
import wave
import uuid
import os
import sys
# Settings for audio recording
if len(sys.argv) == 1:
print(f"Usage: {sys.argv[0]} \"<KeyWord>\" (best without spaces - use 'underscore' instead, or put at least \" \" around")
sys.exit()
KEYWORD=sys.argv[1]
KEYWORD=KEYWORD.replace(" ","_")
print(f"using keyword: {KEYWORD}")
FORMAT = pyaudio.paInt16 # Format for audio input
CHANNELS = 1 # Number of audio channels (1 for mono)
RATE = 16000 # Sample rate in Hz
CHUNK = 1280 # Size of each audio chunk
RECORD_SECONDS = 2 # Duration of the recording in seconds
OUTPUT_FILENAME = KEYWORD+"_{COUNT}.wav" # Name of the output file
OUTPUT_DIR=KEYWORD
os.system(f"mkdir -p {OUTPUT_DIR}")
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Open a new stream
while True:
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("Recording...")
frames = []
# Record audio in chunks
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# Stop and close the stream
stream.stop_stream()
stream.close()
ID=uuid.uuid4().hex
# Save the recorded audio to a file
with wave.open(OUTPUT_FILENAME.format(COUNT=ID), 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
print(f"Audio saved as {OUTPUT_FILENAME.format(COUNT=ID)}")
input("Press Enter to continue...")
# Terminate the PyAudio object
audio.terminate()