-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
if __name__ == "__main__": | ||
import threading | ||
import pyaudio | ||
from RealtimeSTT import AudioToTextRecorder | ||
|
||
CHUNK = 1024 | ||
FORMAT = pyaudio.paInt16 | ||
CHANNELS = 1 | ||
RATE = 16000 | ||
|
||
recorder = AudioToTextRecorder( | ||
use_microphone=False, | ||
spinner=False | ||
) | ||
|
||
stop_event = threading.Event() | ||
|
||
def feed_audio_thread(): | ||
p = pyaudio.PyAudio() | ||
|
||
stream = p.open( | ||
format=FORMAT, | ||
channels=CHANNELS, | ||
rate=RATE, | ||
input=True, | ||
frames_per_buffer=CHUNK | ||
) | ||
|
||
try: | ||
print("Speak now") | ||
while not stop_event.is_set(): | ||
data = stream.read(CHUNK) | ||
recorder.feed_audio(data) | ||
except Exception as e: | ||
print(f"feed_audio_thread encountered an error: {e}") | ||
finally: | ||
stream.stop_stream() | ||
stream.close() | ||
p.terminate() | ||
print("Audio stream closed.") | ||
|
||
def recorder_transcription_thread(): | ||
def process_text(full_sentence): | ||
print("Transcribed text:", full_sentence) | ||
if "stop recording" in full_sentence.lower(): | ||
print("Stop command detected. Stopping threads...") | ||
stop_event.set() | ||
recorder.abort() | ||
try: | ||
while not stop_event.is_set(): | ||
recorder.text(process_text) | ||
except Exception as e: | ||
print(f"transcription_thread encountered an error: {e}") | ||
finally: | ||
print("Transcription thread exiting.") | ||
|
||
audio_thread = threading.Thread(target=feed_audio_thread) | ||
audio_thread.daemon = False | ||
audio_thread.start() | ||
|
||
transcription_thread = threading.Thread(target=recorder_transcription_thread) | ||
transcription_thread.daemon = False | ||
transcription_thread.start() | ||
|
||
audio_thread.join() | ||
transcription_thread.join() | ||
|
||
print("Recording and transcription have stopped.") | ||
recorder.shutdown() |