forked from Sma1033/Realtime-beat-tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_interface_control.py
242 lines (191 loc) · 9.92 KB
/
audio_interface_control.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Created on Thu Jul 13 00:23:37 2017 @ author: Sma1033
use this function to find bast match local dtw path
"""
#import Tkinter
import numpy as np
import time
from datetime import datetime
#import os, dill, ast
import multiprocessing
#import threading
#import dtw_job_control as djc
#import ast
import pyaudio, wave
import librosa
def save_wave_16bit(data, file_name, rate):
# get the max value of np.int16 data
max_v = np.iinfo(np.int16).max
# convert float data into np.int16 format for saving
librosa.output.write_wav(file_name, (data * max_v).astype(np.int16), rate)
class record_service():
def __init__(self):
self.saved_sampling_data = []
self.saved_sampling_data_str = []
self.saved_sampling_frame = 0
self.sync_saved_sampling_frame = 0
self.SAMPLING_WINDOW = 0.020 # sampling chunk size = 20 ms
self.SAMPLING_CHANNELS = 1
self.SAMPLING_RATE = 44100
self.MONITORING_SAMPLING_INPUT = False #False
self.NUM_SAMPLES = int(self.SAMPLING_RATE * float(self.SAMPLING_WINDOW))
self.pyaud = pyaudio.PyAudio()
self.sampling_is_running = False
#print "recording service is initialized."
def save_wave_file(self, filename, save_data):
self.wf = wave.open(filename, 'wb')
self.wf.setnchannels(self.SAMPLING_CHANNELS)
self.wf.setsampwidth(2)
self.wf.setframerate(self.SAMPLING_RATE)
self.wf.writeframes("".join(save_data))
self.wf.close()
print("file:", filename, "is saved.")
def callback(self, in_data, frame_count, time_info, status):
#global saved_sampling_frame
self.saved_sampling_frame += 1
self.saved_sampling_data.append(in_data)
#obacht: unlimited memory usage.
self.saved_sampling_data_str.append( np.fromstring(in_data, dtype=np.int16) )
return (in_data, pyaudio.paContinue)
def run_sampling_stream(self,audio_device_index = None):
#global sampling_is_running
if (self.sampling_is_running == False):
self.audio_stream = self.pyaud.open(format=pyaudio.paInt16,
channels=self.SAMPLING_CHANNELS,
rate=self.SAMPLING_RATE,
input=True,
input_device_index = audio_device_index,
output=self.MONITORING_SAMPLING_INPUT,
frames_per_buffer=self.NUM_SAMPLES,
stream_callback=self.callback)
self.audio_stream.start_stream()
self.sampling_is_running = True
print("[audio interface] Start recording.")
else:
print("[audio interface] Recording is already running")
#self.recording_service_update()
#print "sampling is Started."
def close_sampling_stream(self):
#global sampling_is_running
if (self.sampling_is_running == True):
self.audio_stream.stop_stream()
self.audio_stream.close()
self.pyaud.terminate()
self.sampling_is_running = False
print("[audio interface] Audio frame saved : {0}".format(self.saved_sampling_frame))
print("[audio interface] Stop recording.")
# else:
# print "Recording is already stopped"
def save_data_2_file(self):
if len(self.saved_sampling_data) > 0:
filename = datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + ".wav"
self.save_wave_file(filename, self.saved_sampling_data)
def update_frame_array(self,
ai_process_sm_data_chunk,
ai_process_sm_data_array_end,
ai_process_sm_data_array):
mirror_saved_sampling_frame = self.saved_sampling_frame
#mirror_saved_sampling_data =
# when get new frame, save data into "self.input_data_array"
if (mirror_saved_sampling_frame > self.sync_saved_sampling_frame):
self.frame_unit = int(self.SAMPLING_RATE * self.SAMPLING_WINDOW) # 2205 here
if (self.sync_saved_sampling_frame == 0):
self.input_data_array = np.asarray(self.saved_sampling_data_str[0]) / 32768.0
ai_process_sm_data_array[0 : self.frame_unit] = self.input_data_array[0 : self.frame_unit]
ai_process_sm_data_array_end.value = self.frame_unit
self.sync_saved_sampling_frame = 1
ai_process_sm_data_chunk.value = 1
else:
frame_difference = mirror_saved_sampling_frame - self.sync_saved_sampling_frame
for index in range(frame_difference):
self.input_data_array = np.hstack((self.input_data_array,
np.asarray(self.saved_sampling_data_str[min(mirror_saved_sampling_frame-1,
len(self.saved_sampling_data_str)-1,
self.sync_saved_sampling_frame+index)]) / 32768.0))
start_frame = int(self.frame_unit * (self.sync_saved_sampling_frame+index))
end_frame = start_frame + self.frame_unit
if len(ai_process_sm_data_array[start_frame:end_frame]) == len(self.input_data_array[start_frame:end_frame]) :
ai_process_sm_data_array[start_frame:end_frame] = self.input_data_array[start_frame:end_frame]
ai_process_sm_data_array_end.value = end_frame
self.sync_saved_sampling_frame += 1
ai_process_sm_data_chunk.value = self.sync_saved_sampling_frame
#print ("frame sync done")
pass
else:
#print ("no frame need to be sync")
pass
def audio_interface_control(ai_proc_sm_pstop,
ai_proc_sm_rec_running,
ai_proc_sm_data_chunk,
ai_proc_sm_data_chunk_size,
ai_proc_sm_data_array_end,
ai_proc_sm_data_array,
audio_device_index = None
):
# set recording service
audio_service = record_service()
ai_proc_sm_data_chunk_size.value = audio_service.SAMPLING_WINDOW
# start run recording
try :
audio_service.run_sampling_stream(audio_device_index)
except IOError:
print ("[audio interface] Microphone is not ready!")
audio_service.__init__()
# update chunk status
while (ai_proc_sm_pstop.value == 0):
time.sleep(0.002)
audio_service.update_frame_array(ai_proc_sm_data_chunk,
ai_proc_sm_data_array_end,
ai_proc_sm_data_array)
if (ai_proc_sm_data_chunk.value > 0):
if (ai_proc_sm_rec_running.value == 0):
ai_proc_sm_rec_running.value = 1
# stop recording
audio_service.close_sampling_stream()
audio_service.__init__()
#audio_service.save_data_2_file()
print ("[audio interface] sampling string stopped.")
# test program here
if __name__ == '__main__':
samp_rate = 44100
sm_audio_data_size = samp_rate * 60 * 30 # space for 30 min long audio
# create sheared memory for audio interface process
ai_proc_sm_pstop = multiprocessing.Value('i', 0)
ai_proc_sm_rec_running = multiprocessing.Value('i', 0)
ai_proc_sm_data_chunk = multiprocessing.Value('i', 0)
ai_proc_sm_data_chunk_size = multiprocessing.Value('d', 0)
ai_proc_sm_data_array_end = multiprocessing.Value('i', 0)
ai_proc_sm_data_array = multiprocessing.Array('d', sm_audio_data_size)
#ai_process_sm_data_queue = multiprocessing.Queue()
ai_process_main = multiprocessing.Process(target = audio_interface_control, \
args = (ai_proc_sm_pstop, \
ai_proc_sm_rec_running, \
ai_proc_sm_data_chunk, \
ai_proc_sm_data_chunk_size, \
ai_proc_sm_data_array_end, \
ai_proc_sm_data_array, \
) \
)
ai_process_main.daemon = True
ai_process_main.start()
# waiting for Rec. process start
while (ai_proc_sm_rec_running.value == 0):
time.sleep(0.01)
for i in range(0, 40):
time.sleep(0.5)
print((ai_proc_sm_data_chunk.value * ai_proc_sm_data_chunk_size.value))
rec_audio_data = np.array(ai_proc_sm_data_array[0: ai_proc_sm_data_array_end.value])
# check if all data is good
if (ai_proc_sm_data_chunk.value * ai_proc_sm_data_chunk_size.value * samp_rate == ai_proc_sm_data_array_end.value):
buffer_is_good = 1
else:
buffer_is_good = 0
if buffer_is_good == 1:
print ("great, no data loss.")
else:
print ("sad, some data is missing.")
# save audio data into file
save_file_name = "temp.wav"
save_wave_16bit(rec_audio_data, save_file_name, samp_rate)
# stop Rec process
ai_proc_sm_pstop.value = 1