-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyRecorder.py
62 lines (43 loc) · 1.42 KB
/
pyRecorder.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
import pyaudio
##############################################
## last updated: Dec, 12 2008
##
## Charles Bock - Cbock at ASU dot EDU
##
##############################################
import wave
import sys
class Recorder(object):
def __init__(self):
pass
def record(self,seconds,filename):
self.chunk = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 8000
self.RECORD_SECONDS = seconds
self.WAVE_OUTPUT_FILENAME = filename
self.p = pyaudio.PyAudio()
stream = self.p.open(format = self.FORMAT,
channels = self.CHANNELS,
rate = self.RATE,
input = True,
frames_per_buffer = self.chunk)
#print "recording"
all = []
for i in range(0, self.RATE / self.chunk * self.RECORD_SECONDS):
data = stream.read(self.chunk)
all.append(data)
#print "done recording"
stream.close()
self.p.terminate()
# write data to WAVE file
data = ''.join(all)
wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
wf.writeframes(data)
wf.close()
if __name__ == '__main__':
print "Wanker"