Skip to content

Commit

Permalink
feed audio example
Browse files Browse the repository at this point in the history
  • Loading branch information
KoljaB committed Oct 21, 2024
1 parent 839c5fa commit cd235f5
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/feed_audio.py
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()

0 comments on commit cd235f5

Please sign in to comment.