Skip to content

Commit 962089a

Browse files
committed
Adds fix to flush queue on ctrl + c.
This change mitigates missing the cached caption for any final speech when user presses ctrl + c. Removes redundant np.array() call.
1 parent 5689bdf commit 962089a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

moonshine/demo/live_captions.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Live captions from microphone using Moonshine and SileroVAD ONNX models."""
22

3+
import argparse
34
import os
45
import sys
56
import time
6-
import argparse
77

88
from queue import Queue
99

@@ -67,7 +67,7 @@ def create_input_callback(q):
6767
def input_callback(data, frames, time, status):
6868
if status:
6969
print(status)
70-
q.put((data.copy(), status))
70+
q.put((data.copy().flatten(), status))
7171

7272
return input_callback
7373

@@ -144,12 +144,10 @@ def print_captions(text, new_cached_caption=False):
144144
print_captions("Ready...")
145145
try:
146146
while True:
147-
data, status = q.get()
147+
chunk, status = q.get()
148148
if VERBOSE and status:
149149
print(status)
150150

151-
chunk = np.array(data).flatten()
152-
153151
speech = np.concatenate((speech, chunk))
154152
if not recording:
155153
speech = speech[-lookback_size:]
@@ -182,6 +180,12 @@ def print_captions(text, new_cached_caption=False):
182180
except KeyboardInterrupt:
183181
stream.close()
184182

183+
if recording:
184+
while not q.empty():
185+
chunk, _ = q.get()
186+
speech = np.concatenate((speech, chunk))
187+
end_recording(speech, "<END.>")
188+
185189
print(f"""
186190
187191
model_name : {model_name}

0 commit comments

Comments
 (0)