-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_ssl.py
204 lines (161 loc) · 5.22 KB
/
record_ssl.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
always read, until wake up, choose part to compute gcc
"""
import pyaudio
import wave
from scipy.io import wavfile
import tensorflow as tf
import numpy as np
import sys
import os
import math
import time
import collections
import threading
import warnings
warnings.filterwarnings('ignore')
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
gpu_options = tf.GPUOptions(allow_growth=True)
pwd = os.path.abspath(os.path.abspath(__file__))
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(father_path)
import DigitalDriver.ControlDriver as CD
"""
Record Parameters
"""
CHUNK = 1024
RECORD_DEVICE_NAME = "USB Camera-B4.09.24.1"
RECORD_WIDTH = 2
CHANNELS = 4
RATE = 16000
RECORD_SECONDS = 3
FORMAT = pyaudio.paInt16
FORWARD_SECONDS = 2
WAV_PATH = "../resource/wav/"
def cal_volume(waveData, frameSize=256, overLap=128):
waveData = waveData * 1.0 / max(abs(waveData)) # normalization
wlen = len(waveData)
step = frameSize - overLap
frameNum = int(math.ceil(wlen * 1.0 / step))
volume = np.zeros((frameNum, 1))
for i in range(frameNum):
curFrame = waveData[np.arange(i * step, min(i * step + frameSize, wlen))]
curFrame = curFrame - np.median(curFrame) # zero-justified
volume[i] = np.sum(np.abs(curFrame))
return volume
def split_channels(wave_output_filename):
sampleRate, musicData = wavfile.read(wave_output_filename)
mic1 = []
mic2 = []
mic3 = []
mic4 = []
for item in musicData:
mic1.append(item[0])
mic2.append(item[1])
mic3.append(item[2])
mic4.append(item[3])
front = wave_output_filename[:len(wave_output_filename) - 4]
# physic mic number --- channel number
wavfile.write(front + '_mic1.wav', sampleRate, np.array(mic2))
wavfile.write(front + '_mic2.wav', sampleRate, np.array(mic3))
wavfile.write(front + '_mic3.wav', sampleRate, np.array(mic1))
wavfile.write(front + '_mic4.wav', sampleRate, np.array(mic4))
def judge_active(wave_output_filename):
sampleRate, musicData = wavfile.read(wave_output_filename)
d1 = []
d2 = []
d3 = []
d4 = []
for item in musicData:
d1.append(item[0])
d2.append(item[1])
d3.append(item[2])
d4.append(item[3])
v1 = np.average(np.abs(d1))
v2 = np.average(np.abs(d2))
v3 = np.average(np.abs(d3))
v4 = np.average(np.abs(d4))
threshold_v = 230
if v1 > threshold_v or v2 > threshold_v or v3 > threshold_v or v4 > threshold_v:
print(v1, v2, v3, v4)
return True
else:
return False
def loop_record(control):
device_index = -1
p = pyaudio.PyAudio()
"""
Recognize Mic device, before loop
"""
# scan to get usb device
print(p.get_device_count())
for index in range(0, p.get_device_count()):
info = p.get_device_info_by_index(index)
device_name = info.get("name")
print("device_name: ", device_name)
# find mic usb device
if device_name.find(RECORD_DEVICE_NAME) != -1:
device_index = index
# break
if device_index != -1:
print("find the device")
print(p.get_device_info_by_index(device_index))
else:
print("don't find the device")
saved_count = 0
# steps
while True:
"""
Record
"""
# active detection
while True:
print("start monitoring ... ")
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(RECORD_WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=device_index)
# 16 data
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
print("End monitoring ... ")
# temp store into file
wave_output_filename = "real_" + str(saved_count) + "_" + sys.argv[1] + ".wav"
wf = wave.open(os.path.join(WAV_PATH, wave_output_filename), 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(RECORD_WIDTH)
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# if exceed, break, split to process, then action. After action done, begin monitor
if judge_active(os.path.join(WAV_PATH, wave_output_filename)) is True:
print("active ... ")
break
"""
Split
"""
split_channels(os.path.join(WAV_PATH, wave_output_filename))
"""
use four mic file to be input to produce action
"""
print("apply movement ...")
time.sleep(FORWARD_SECONDS)
print("movement done.")
# begin next step
saved_count += 1
if __name__ == '__main__':
# cd = Control()
# loop_record(cd)
cd = CD.ControlDriver()
p1 = threading.Thread(target=loop_record, args=(cd,))
p2 = threading.Thread(target=cd.control_part, args=())
print("hehe")
p2.start()
p1.start()