-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.py
47 lines (40 loc) · 1.23 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pyaudio
import wave
import audio_options
def record(duration, filename):
"""
Record sound from the microphone.
Params:
duration - How long should the recording be.
filename - Name of the audio file where to write the audio.
"""
audio = pyaudio.PyAudio()
frames = []
stream = audio.open(
format=audio_options.FORMAT,
channels=audio_options.CHANNELS,
rate=audio_options.RATE,
input=True,
frames_per_buffer=audio_options.CHUNK
)
for i in range(0, int(audio_options.RATE / audio_options.CHUNK * duration)):
data = stream.read(audio_options.CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
audio.terminate()
__write_audio_file(audio, frames, filename)
def __write_audio_file(audio, frames, filename):
"""
Write audio from a PyAudio object to a file.
Params:
audio - PyAudio Object.
frames - Frames with the audio data.
filename - Name of the file where to store the audio.
"""
audio_file = wave.open(filename, 'wb')
audio_file.setnchannels(audio_options.CHANNELS)
audio_file.setsampwidth(audio.get_sample_size(audio_options.FORMAT))
audio_file.setframerate(audio_options.RATE)
audio_file.writeframes(b''.join(frames))
audio_file.close()