Implement software resampling for microphone input - #431
Open
imatrisciano wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses microphone acquisition failures on systems that can’t open an input device at 16kHz (e.g., PyAudio -9997) by adding a software-resampling fallback, and by deferring microphone access until record() is called (closing the stream when recording ends).
Changes:
- Add an on-demand microphone open/close path with a fallback to the device’s default sample rate when 16kHz is unsupported.
- Resample captured audio to 16kHz client-side (via
librosa) before sending it over the websocket. - Update output WAV metadata and client requirements to reflect the new sampling-rate handling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
whisper_live/client.py |
Adds microphone open/close helpers, invalid-sample-rate fallback, and per-chunk software resampling before sending audio. |
requirements/client.txt |
Adds librosa to client requirements. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Collaborator
|
@copilot apply changes based on the comments in this thread |
| def _open_microphone(self): | ||
| self.microphoneSampleRate = self.targetSampleRate | ||
|
|
||
| def open_stream(self): |
| ) | ||
|
|
||
| try: | ||
| open_stream(self) |
|
|
||
| try: | ||
| self.microphoneSampleRate = int(self.p.get_default_input_device_info()["defaultSampleRate"]) | ||
| open_stream(self) |
Comment on lines
+472
to
+473
| def _close_audio_stream(self): | ||
| """Closes the audio stream and terminates the PyAudio instance.""" |
Comment on lines
625
to
+626
| output_container = av.open(save_file, mode="w") | ||
| output_audio_stream = output_container.add_stream(codec_name="pcm_s16le", rate=self.rate) | ||
| output_audio_stream = output_container.add_stream(codec_name="pcm_s16le", rate=self.microphoneSampleRate) |
Comment on lines
724
to
+725
| # save frames if more than a minute | ||
| if len(self.frames) > 60 * self.rate: | ||
| if len(self.frames) > 60 * self.microphoneSampleRate: |
Comment on lines
732
to
+735
| except KeyboardInterrupt: | ||
| self.finalize_recording(n_audio_file) | ||
| finally: | ||
| self._close_microphone() |
Comment on lines
+696
to
+700
| self._open_microphone() | ||
| if self.stream is None: | ||
| logging.error("Failed to open microphone stream; aborting recording.") | ||
| self.close_all_clients() | ||
| self.write_all_clients_srt() |
Collaborator
|
@imatrisciano can you please look into the copilot's review comments? |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On some systems, audio recording can fail due to error
-9997(invalid sample rate) while trying to acquire the input audio device. This has been observed to happen on a desktop computer runningKubuntu 25.10and a laptop runningUbuntu 24.04To solve this issue, this PR introduces a fallback mechanism for client-side software resampling through
librosa, whenever the audio input device does not natively support the needed 16kHz sample rate.This PR also ensures that the microphone is only accessed if the
record()method is invoked, and it is closed as soon as that method returns .