From 4d477e35e7ad36f6fe8a5b6bb659569593e69a97 Mon Sep 17 00:00:00 2001 From: FlippFuzz <41221030+FlippFuzz@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:43:53 +0800 Subject: [PATCH] Handle failure on systems without microphones Catch the OSError and print a WARN log. --- whisper_live/client.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/whisper_live/client.py b/whisper_live/client.py index a759eae..5133266 100644 --- a/whisper_live/client.py +++ b/whisper_live/client.py @@ -66,13 +66,17 @@ def __init__( self.timestamp_offset = 0.0 self.audio_bytes = None self.p = pyaudio.PyAudio() - self.stream = self.p.open( - format=self.format, - channels=self.channels, - rate=self.rate, - input=True, - frames_per_buffer=self.chunk, - ) + try: + self.stream = self.p.open( + format=self.format, + channels=self.channels, + rate=self.rate, + input=True, + frames_per_buffer=self.chunk, + ) + except OSError as error: + print(f"[WARN]: Unable to access microphone. {error}") + self.stream = None if host is not None and port is not None: socket_url = f"ws://{host}:{port}"