-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.py
43 lines (39 loc) · 1.27 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
"""
recorder.py - A module for recording waveform files
:author: Elliot Miller
Usage: python recorder.py FILENAME [LENGTH]
FILENAME -- the path to write the new file to
LENGTH -- the duration in seconds of the recording (default is 5)
"""
import pyaudio, waveform, sys
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
def record(time, filename):
"""Records audio and writes to a file"""
# open a pyaudio stream for recording
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
# record the sound in frames
print "Recording Audio..."
frames = []
for i in range(0, int(RATE / CHUNK * time)):
data = stream.read(CHUNK)
frames.append(data)
print "Done"
# close the pyaudio stream
stream.stop_stream()
stream.close()
audio.terminate()
# save the waveform to a file
wav = waveform.from_string(b''.join(frames), rate=RATE, width=audio.get_sample_size(FORMAT), channels=CHANNELS)
waveform.to_file(filename, wav)
if __name__=="__main__":
if len(sys.argv) > 1:
t = float(sys.argv[2]) if len(sys.argv) > 2 else 5
record(t, sys.argv[1])
else:
print __doc__