Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
App_Function_Libraries/Audio_Transcription_Lib.py
CHANGED
|
@@ -17,9 +17,11 @@ import gc
|
|
| 17 |
import json
|
| 18 |
import logging
|
| 19 |
import os
|
|
|
|
| 20 |
import sys
|
| 21 |
import subprocess
|
| 22 |
import tempfile
|
|
|
|
| 23 |
import time
|
| 24 |
import configparser
|
| 25 |
# DEBUG Imports
|
|
@@ -197,10 +199,29 @@ def record_audio(duration, sample_rate=16000, chunk_size=1024):
|
|
| 197 |
|
| 198 |
print("Recording...")
|
| 199 |
frames = []
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
print("Recording finished.")
|
| 206 |
|
|
|
|
| 17 |
import json
|
| 18 |
import logging
|
| 19 |
import os
|
| 20 |
+
import queue
|
| 21 |
import sys
|
| 22 |
import subprocess
|
| 23 |
import tempfile
|
| 24 |
+
import threading
|
| 25 |
import time
|
| 26 |
import configparser
|
| 27 |
# DEBUG Imports
|
|
|
|
| 199 |
|
| 200 |
print("Recording...")
|
| 201 |
frames = []
|
| 202 |
+
stop_recording = threading.Event()
|
| 203 |
+
audio_queue = queue.Queue()
|
| 204 |
|
| 205 |
+
def audio_callback():
|
| 206 |
+
for _ in range(0, int(sample_rate / chunk_size * duration)):
|
| 207 |
+
if stop_recording.is_set():
|
| 208 |
+
break
|
| 209 |
+
data = stream.read(chunk_size)
|
| 210 |
+
audio_queue.put(data)
|
| 211 |
+
|
| 212 |
+
audio_thread = threading.Thread(target=audio_callback)
|
| 213 |
+
audio_thread.start()
|
| 214 |
+
|
| 215 |
+
return p, stream, audio_queue, stop_recording, audio_thread
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def stop_recording(p, stream, audio_queue, stop_recording_event, audio_thread):
|
| 219 |
+
stop_recording_event.set()
|
| 220 |
+
audio_thread.join()
|
| 221 |
+
|
| 222 |
+
frames = []
|
| 223 |
+
while not audio_queue.empty():
|
| 224 |
+
frames.append(audio_queue.get())
|
| 225 |
|
| 226 |
print("Recording finished.")
|
| 227 |
|