forked from halleytl/pyvad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordParser.py
79 lines (62 loc) · 1.68 KB
/
RecordParser.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
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
sys.path.append("../")
from audio import Audio
from vad import Vad
import threading
class StreamParser(Vad):
def __init__(self):
self.record = Audio()
self.active = False
self.play =Audio()
Vad.__init__(self)
def open_mic(self):
print "start recording"
t = threading.Thread(target=self.mic_record)
t.setDaemon(True)
t.start()
def mic_record(self):
self.record.record_stream_start()
self.active = True
print "The microphone has opened"
while self.active:
data = self.record.record_read()
self.add(data)
self.record.record_stream_end()
print "exit mic"
def close_mic(self):
print "stop recording"
if self.record:
print self.active
self.active = False
def play_stream(self, data):
self.play.play_stream(data)
def test():
stream_test = StreamParser()
stream_test.open_mic()
import time
time.sleep(5)
stream_test.close_mic()
time.sleep(5)
import util
data = "".join(stream_test.cache_frames)
util.save_file(data)
stream_test.play_stream(data)
def main():
stream_test = StreamParser()
stream_test.open_mic()
import time
while 1:
try:
time.sleep(5)
except KeyboardInterrupt, e:
#time.sleep(5)
stream_test.close_mic()
break
import util
data = "".join(stream_test.cache_frames)
util.save_file(data)
stream_test.play_stream(data)
if __name__ == "__main__":
main()