fix: fixing issue with microphone channels numbering and status recovery on stop_recording

This commit is contained in:
CarolinePascal
2025-04-11 18:51:34 +02:00
parent 0a6ca58299
commit 9a5c96b2b1
+6 -6
View File
@@ -188,7 +188,7 @@ class Microphone:
else: else:
self.sample_rate = int(actual_microphone["default_samplerate"]) self.sample_rate = int(actual_microphone["default_samplerate"])
if self.channels is not None: if self.channels is not None and len(self.channels) > 0:
if any(c > actual_microphone["max_input_channels"] for c in self.channels): if any(c > actual_microphone["max_input_channels"] for c in self.channels):
raise OSError( raise OSError(
f"Some of the provided channels {self.channels} are outside the maximum channel range of the microphone {actual_microphone['max_input_channels']}." f"Some of the provided channels {self.channels} are outside the maximum channel range of the microphone {actual_microphone['max_input_channels']}."
@@ -197,13 +197,13 @@ class Microphone:
self.channels = np.arange(1, actual_microphone["max_input_channels"] + 1) self.channels = np.arange(1, actual_microphone["max_input_channels"] + 1)
# Get channels index instead of number for slicing # Get channels index instead of number for slicing
self.channels = np.array(self.channels) - 1 self.channels_index = np.array(self.channels) - 1
# Create the audio stream # Create the audio stream
self.stream = sd.InputStream( self.stream = sd.InputStream(
device=self.microphone_index, device=self.microphone_index,
samplerate=self.sample_rate, samplerate=self.sample_rate,
channels=max(self.channels) + 1, channels=max(self.channels),
dtype="float32", dtype="float32",
callback=self._audio_callback, callback=self._audio_callback,
) )
@@ -221,8 +221,8 @@ class Microphone:
# Slicing makes copy unnecessary # Slicing makes copy unnecessary
# Two separate queues are necessary because .get() also pops the data from the queue # Two separate queues are necessary because .get() also pops the data from the queue
if self.is_writing: if self.is_writing:
self.record_queue.put(indata[:, self.channels]) self.record_queue.put(indata[:, self.channels_index])
self.read_queue.put(indata[:, self.channels]) self.read_queue.put(indata[:, self.channels_index])
@staticmethod @staticmethod
def _record_loop(queue, event: Event, sample_rate: int, channels: list[int], output_file: Path) -> None: def _record_loop(queue, event: Event, sample_rate: int, channels: list[int], output_file: Path) -> None:
@@ -234,7 +234,7 @@ class Microphone:
output_file, output_file,
mode="x", mode="x",
samplerate=sample_rate, samplerate=sample_rate,
channels=max(channels) + 1, channels=max(channels),
subtype=sf.default_subtype(output_file.suffix[1:]), subtype=sf.default_subtype(output_file.suffix[1:]),
) as file: ) as file:
while not event.is_set(): while not event.is_set():