-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_recorder.py
33 lines (25 loc) · 876 Bytes
/
voice_recorder.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
import wave
import pyaudio
class VoiceRecorder:
def record(record_active, frames):
audio = pyaudio.PyAudio()
stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024,
)
while record_active.is_set(): # Multithreading
data = stream.read(num_frames=1024, exception_on_overflow=False)
frames.append(data)
stream.stop_stream()
stream.close()
audio.terminate()
# save audiofile with same format
audio_file = wave.open("voice_prompt.wav", "wb")
audio_file.setnchannels(1)
audio_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
audio_file.setframerate(44100)
audio_file.writeframes(b"".join(frames))
audio_file.close()