Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord.py
More file actions
Latest commit
125 lines (99 loc) · 3.09 KB
/
Copy pathrecord.py
File metadata and controls
125 lines (99 loc) · 3.09 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# для установки pyaudio
# pip install --global-option='build_ext' --global-option='-I/usr/local/include' --global-option='-L/usr/local/lib' pyaudio
fromsysimportbyteorder
fromarrayimportarray
fromstructimportpack
importpyaudio
importwave
THRESHOLD=500
CHUNK_SIZE=4096
FORMAT=pyaudio.paInt16
RATE=44100
defis_silent(snd_data):
"Returns 'True' if below the 'silent' threshold"
returnmax(snd_data) <THRESHOLD
defnormalize(snd_data):
"Average the volume out"
MAXIMUM=16384
times=float(MAXIMUM)/max(abs(i) foriinsnd_data)
r=array('h')
foriinsnd_data:
r.append(int(i*times))
returnr
deftrim(snd_data):
"Trim the blank spots at the start and end"
def_trim(snd_data):
snd_started=False
r=array('h')
foriinsnd_data:
ifnotsnd_startedandabs(i)>THRESHOLD:
snd_started=True
r.append(i)
elifsnd_started:
r.append(i)
returnr
# Trim to the left
snd_data=_trim(snd_data)
# Trim to the right
snd_data.reverse()
snd_data=_trim(snd_data)
snd_data.reverse()
returnsnd_data
defadd_silence(snd_data, seconds):
"Add silence to the start and end of 'snd_data' of length 'seconds' (float)"
r=array('h', [0foriinrange(int(seconds*RATE))])
r.extend(snd_data)
r.extend([0foriinrange(int(seconds*RATE))])
returnr
defrecord():
"""
Record a word or words from the microphone and
return the data as an array of signed shorts.
Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
"""
p=pyaudio.PyAudio()
stream=p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)
num_silent=0
snd_started=False
r=array('h')
while1:
# little endian, signed short
snd_data=array('h', stream.read(CHUNK_SIZE))
ifbyteorder=='big':
snd_data.byteswap()
r.extend(snd_data)
silent=is_silent(snd_data)
ifsilentandsnd_started:
num_silent+=1
elifnotsilentandnotsnd_started:
snd_started=True
ifsnd_startedandnum_silent>30:
break
sample_width=p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
r=normalize(r)
r=trim(r)
r=add_silence(r, 0.5)
returnsample_width, r
defrecord_to_file(path):
"Records from the microphone and outputs the resulting data to 'path'"
print("recording")
sample_width, data=record()
data=pack('<'+ ('h'*len(data)), *data)
wf=wave.open(path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
if__name__=='__main__':
print("please speak a word into the microphone")
record_to_file('demo.wav')
print("done - result written to demo.wav")