Quellcode durchsuchen

reworked mic reconnection on errors

KoljaB vor 6 Monaten
Ursprung
Commit
8f65cfb9ee
1 geänderte Dateien mit 37 neuen und 24 gelöschten Zeilen
  1. 37 24
      RealtimeSTT/audio_recorder.py

+ 37 - 24
RealtimeSTT/audio_recorder.py

@@ -123,7 +123,7 @@ class TranscriptionWorker:
     def run(self):
         if __name__ == "__main__":
              system_signal.signal(system_signal.SIGINT, system_signal.SIG_IGN)
-            # __builtins__['print'] = self.custom_print
+             __builtins__['print'] = self.custom_print
 
         logging.info(f"Initializing faster_whisper main transcription model {self.model_path}")
 
@@ -172,7 +172,7 @@ class TranscriptionWorker:
                 except Exception as e:
                     logging.error(f"General error in processing queue item: {e}")
         finally:
-            # __builtins__['print'] = print  # Restore the original print function
+            __builtins__['print'] = print  # Restore the original print function
             self.conn.close()
             self.stdout_pipe.close()
             self.shutdown_event.set()  # Ensure the polling thread will stop
@@ -920,21 +920,31 @@ class AudioToTextRecorder:
                 logging.warning(f"Failed to get highest sample rate: {e}")
                 return 48000  # Fallback to a common high sample rate
 
-        def initialize_audio_stream(audio_interface, device_index, sample_rate, chunk_size):
+        def initialize_audio_stream(audio_interface, sample_rate, chunk_size):
+            nonlocal input_device_index
             """Initialize the audio stream with error handling."""
-            try:
-                stream = audio_interface.open(
-                    format=pyaudio.paInt16,
-                    channels=1,
-                    rate=sample_rate,
-                    input=True,
-                    frames_per_buffer=chunk_size,
-                    input_device_index=device_index,
-                )
-                return stream
-            except Exception as e:
-                logging.error(f"Error initializing audio stream: {e}")
-                raise
+            while not shutdown_event.is_set():
+                try:
+                    # Check and assign the input device index if it is not set
+                    if input_device_index is None:
+                        default_device = audio_interface.get_default_input_device_info()
+                        input_device_index = default_device['index']
+
+                    stream = audio_interface.open(
+                        format=pyaudio.paInt16,
+                        channels=1,
+                        rate=sample_rate,
+                        input=True,
+                        frames_per_buffer=chunk_size,
+                        input_device_index=input_device_index,
+                    )
+                    logging.info("Microphone connected successfully.")
+                    return stream
+
+                except Exception as e:
+                    logging.error(f"Microphone connection failed: {e}. Retrying...")
+                    input_device_index = None
+                    time.sleep(3)  # Wait for 3 seconds before retrying
 
         def preprocess_audio(chunk, original_sample_rate, target_sample_rate):
             """Preprocess audio chunk similar to feed_audio method."""
@@ -989,7 +999,7 @@ class AudioToTextRecorder:
                 for rate in sample_rates_to_try:
                     try:
                         device_sample_rate = rate
-                        stream = initialize_audio_stream(audio_interface, input_device_index, device_sample_rate, chunk_size)
+                        stream = initialize_audio_stream(audio_interface, device_sample_rate, chunk_size)
                         if stream is not None:
                             logging.debug(f"Audio recording initialized successfully at {device_sample_rate} Hz, reading {chunk_size} frames at a time")
                             # logging.error(f"Audio recording initialized successfully at {device_sample_rate} Hz, reading {chunk_size} frames at a time")
@@ -997,8 +1007,6 @@ class AudioToTextRecorder:
                     except Exception as e:
                         logging.warning(f"Failed to initialize audio23 stream at {device_sample_rate} Hz: {e}")
                         continue
-                    
-                    
 
                 # If we reach here, none of the sample rates worked
                 raise Exception("Failed to initialize audio stream12 with all sample rates.")
@@ -1047,12 +1055,17 @@ class AudioToTextRecorder:
                     if e.errno == pyaudio.paInputOverflowed:
                         logging.warning("Input overflowed. Frame dropped.")
                     else:
-                        logging.error(f"Error during recording: {e}")
+                        logging.error(f"OSError during recording: {e}")
                         # Attempt to reinitialize the stream
                         logging.info("Attempting to reinitialize the audio stream...")
-                        if stream:
-                            stream.stop_stream()
-                            stream.close()
+
+                        try:
+                            if stream:
+                                stream.stop_stream()
+                                stream.close()
+                        except Exception as e:
+                            pass
+
                         if audio_interface:
                             audio_interface.terminate()
                         
@@ -1067,7 +1080,7 @@ class AudioToTextRecorder:
                     continue
 
                 except Exception as e:
-                    logging.error(f"Error during recording: {e}")
+                    logging.error(f"Unknown error during recording: {e}")
                     tb_str = traceback.format_exc()
                     logging.error(f"Traceback: {tb_str}")
                     logging.error(f"Error: {e}")