@@ -93,7 +93,7 @@ def recognizer(Q):
9393
9494 def recognize (signal , lastid , states ):
9595 if signal .shape [0 ] < args .blocksize :
96- signal = np .pad (signal , [[0 , args .blocksize - signal .shape [0 ]]])
96+ signal = tf .pad (signal , [[0 , args .blocksize - signal .shape [0 ]]])
9797 tflitemodel .set_tensor (input_details [0 ]["index" ], signal )
9898 tflitemodel .set_tensor (input_details [1 ]["index" ], lastid )
9999 tflitemodel .set_tensor (input_details [2 ]["index" ], states )
@@ -104,8 +104,8 @@ def recognize(signal, lastid, states):
104104 text = "" .join ([chr (u ) for u in upoints ])
105105 return text , lastid , states
106106
107- lastid = args .blank * np .ones (shape = [], dtype = np .int32 )
108- states = np .zeros (shape = [args .num_rnns , args .nstates , 1 , args .statesize ], dtype = np .float32 )
107+ lastid = args .blank * tf .ones (shape = [], dtype = tf .int32 )
108+ states = tf .zeros (shape = [args .num_rnns , args .nstates , 1 , args .statesize ], dtype = tf .float32 )
109109 transcript = ""
110110
111111 while True :
@@ -122,51 +122,56 @@ def recognize(signal, lastid, states):
122122tflite_process .start ()
123123
124124
125- def callback (outdata , frames , time , status ):
126- assert frames == args .blocksize
127- if status .output_underflow :
128- print ('Output underflow: increase blocksize?' , file = sys .stderr )
129- raise sd .CallbackAbort
130- assert not status
125+ def send (q , Q , E ):
126+ def callback (outdata , frames , time , status ):
127+ assert frames == args .blocksize
128+ if status .output_underflow :
129+ print ('Output underflow: increase blocksize?' , file = sys .stderr )
130+ raise sd .CallbackAbort
131+ assert not status
132+ try :
133+ data = q .get_nowait ()
134+ Q .put (np .frombuffer (data , dtype = np .float32 ))
135+ except queue .Empty as e :
136+ print ('Buffer is empty: increase buffersize?' , file = sys .stderr )
137+ raise sd .CallbackAbort from e
138+ if len (data ) < len (outdata ):
139+ outdata [:len (data )] = data
140+ outdata [len (data ):] = b'\x00 ' * (len (outdata ) - len (data ))
141+ raise sd .CallbackStop
142+ else :
143+ outdata [:] = data
144+
131145 try :
132- data = q .get_nowait ()
133- Q .put (np .frombuffer (data , dtype = np .float32 ))
134- except queue .Empty as e :
135- print ('Buffer is empty: increase buffersize?' , file = sys .stderr )
136- raise sd .CallbackAbort from e
137- if len (data ) < len (outdata ):
138- outdata [:len (data )] = data
139- outdata [len (data ):] = b'\x00 ' * (len (outdata ) - len (data ))
140- raise sd .CallbackStop
141- else :
142- outdata [:] = data
143-
144-
145- try :
146- with sf .SoundFile (args .filename ) as f :
147- for _ in range (args .buffersize ):
148- data = f .buffer_read (args .blocksize , dtype = 'float32' )
149- if not data :
150- break
151- q .put_nowait (data ) # Pre-fill queue
152- stream = sd .RawOutputStream (
153- samplerate = f .samplerate , blocksize = args .blocksize ,
154- device = args .device , channels = f .channels , dtype = 'float32' ,
155- callback = callback , finished_callback = E .set )
156- with stream :
157- timeout = args .blocksize * args .buffersize / f .samplerate
158- while data :
146+ with sf .SoundFile (args .filename ) as f :
147+ for _ in range (args .buffersize ):
159148 data = f .buffer_read (args .blocksize , dtype = 'float32' )
160- q .put (data , timeout = timeout )
161- E .wait ()
162-
163- except KeyboardInterrupt :
164- parser .exit ('\n Interrupted by user' )
165- except queue .Full :
166- # A timeout occurred, i.e. there was an error in the callback
167- parser .exit (1 )
168- except Exception as e :
169- parser .exit (type (e ).__name__ + ': ' + str (e ))
170-
171- tflite_process .join ()
172- tflite_process .close ()
149+ if not data :
150+ break
151+ q .put_nowait (data ) # Pre-fill queue
152+ stream = sd .RawOutputStream (
153+ samplerate = f .samplerate , blocksize = args .blocksize ,
154+ device = args .device , channels = f .channels , dtype = 'float32' ,
155+ callback = callback , finished_callback = E .set )
156+ with stream :
157+ timeout = args .blocksize * args .buffersize / f .samplerate
158+ while data :
159+ data = f .buffer_read (args .blocksize , dtype = 'float32' )
160+ q .put (data , timeout = timeout )
161+ E .wait ()
162+
163+ except KeyboardInterrupt :
164+ parser .exit ('\n Interrupted by user' )
165+ except queue .Full :
166+ # A timeout occurred, i.e. there was an error in the callback
167+ parser .exit (1 )
168+ except Exception as e :
169+ parser .exit (type (e ).__name__ + ': ' + str (e ))
170+
171+
172+ send_process = Process (target = send , args = [q , Q , E ])
173+ send_process .start ()
174+ send_process .join ()
175+ send_process .close ()
176+
177+ tflite_process .terminate ()
0 commit comments