62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
import os
|
|
import importlib
|
|
import threading
|
|
import time
|
|
import whisper
|
|
import traceback
|
|
|
|
import audio
|
|
|
|
print('Loading whisper model...')
|
|
start = time.time()
|
|
model = whisper.load_model('medium')
|
|
print('Done after', time.time() - start, 's')
|
|
|
|
# array of audio chunks
|
|
audio_chunks = [bytearray()]
|
|
|
|
def read_audio_thread():
|
|
global audio_chunks
|
|
|
|
while True:
|
|
with open('whispercppexample.pcm', 'rb') as f:
|
|
while True:
|
|
data = f.read(1920)
|
|
if not data:
|
|
break
|
|
audio.process_pcm(audio_chunks, data)
|
|
time.sleep(0.04)
|
|
|
|
def process_stream_thread():
|
|
global audio_chunks
|
|
|
|
while True:
|
|
try:
|
|
audio.process_stream(audio_chunks, model)
|
|
except BaseException as e:
|
|
print('exception')
|
|
traceback.print_exc()
|
|
print('sleeping...')
|
|
time.sleep(5)
|
|
|
|
def monitor_module():
|
|
mod_time = os.path.getmtime('audio.py')
|
|
|
|
while True:
|
|
if os.path.getmtime('audio.py') > mod_time:
|
|
mod_time = os.path.getmtime('audio.py')
|
|
print('Change detected, reloading.')
|
|
importlib.reload(audio)
|
|
time.sleep(1)
|
|
|
|
t1 = threading.Thread(target=read_audio_thread)
|
|
t2 = threading.Thread(target=process_stream_thread)
|
|
t3 = threading.Thread(target=monitor_module)
|
|
t1.start()
|
|
t2.start()
|
|
t3.start()
|
|
|
|
while True:
|
|
pass
|
|
|