feed_audio.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. if __name__ == "__main__":
  2. import threading
  3. import pyaudio
  4. from RealtimeSTT import AudioToTextRecorder
  5. # Audio stream configuration constants
  6. CHUNK = 1024 # Number of audio samples per buffer
  7. FORMAT = pyaudio.paInt16 # Sample format (16-bit integer)
  8. CHANNELS = 1 # Mono audio
  9. RATE = 16000 # Sampling rate in Hz (expected by the recorder)
  10. # Initialize the audio-to-text recorder without using the microphone directly
  11. # Since we are feeding audio data manually, set use_microphone to False
  12. recorder = AudioToTextRecorder(
  13. use_microphone=False, # Disable built-in microphone usage
  14. spinner=False # Disable spinner animation in the console
  15. )
  16. # Event to signal when to stop the threads
  17. stop_event = threading.Event()
  18. def feed_audio_thread():
  19. """Thread function to read audio data and feed it to the recorder."""
  20. p = pyaudio.PyAudio()
  21. # Open an input audio stream with the specified configuration
  22. stream = p.open(
  23. format=FORMAT,
  24. channels=CHANNELS,
  25. rate=RATE,
  26. input=True,
  27. frames_per_buffer=CHUNK
  28. )
  29. try:
  30. print("Speak now")
  31. while not stop_event.is_set():
  32. # Read audio data from the stream (in the expected format)
  33. data = stream.read(CHUNK)
  34. # Feed the audio data to the recorder
  35. recorder.feed_audio(data)
  36. except Exception as e:
  37. print(f"feed_audio_thread encountered an error: {e}")
  38. finally:
  39. # Clean up the audio stream
  40. stream.stop_stream()
  41. stream.close()
  42. p.terminate()
  43. print("Audio stream closed.")
  44. def recorder_transcription_thread():
  45. """Thread function to handle transcription and process the text."""
  46. def process_text(full_sentence):
  47. """Callback function to process the transcribed text."""
  48. print("Transcribed text:", full_sentence)
  49. # Check for the stop command in the transcribed text
  50. if "stop recording" in full_sentence.lower():
  51. print("Stop command detected. Stopping threads...")
  52. stop_event.set()
  53. recorder.abort()
  54. try:
  55. while not stop_event.is_set():
  56. # Get transcribed text and process it using the callback
  57. recorder.text(process_text)
  58. except Exception as e:
  59. print(f"transcription_thread encountered an error: {e}")
  60. finally:
  61. print("Transcription thread exiting.")
  62. # Create and start the audio feeding thread
  63. audio_thread = threading.Thread(target=feed_audio_thread)
  64. audio_thread.daemon = False # Ensure the thread doesn't exit prematurely
  65. audio_thread.start()
  66. # Create and start the transcription thread
  67. transcription_thread = threading.Thread(target=recorder_transcription_thread)
  68. transcription_thread.daemon = False # Ensure the thread doesn't exit prematurely
  69. transcription_thread.start()
  70. # Wait for both threads to finish
  71. audio_thread.join()
  72. transcription_thread.join()
  73. print("Recording and transcription have stopped.")
  74. recorder.shutdown()