From 373a169bd212ce2fdfa36b26822904d93f3d44c5 Mon Sep 17 00:00:00 2001 From: CarolinePascal Date: Mon, 7 Apr 2025 16:33:16 +0200 Subject: [PATCH] Fixing sounddevice stream active state recovery and adding corresponding exceptions --- src/lerobot/microphones/microphone.py | 16 +++++++++++++--- src/lerobot/utils/errors.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/lerobot/microphones/microphone.py b/src/lerobot/microphones/microphone.py index c9eaf4b9a..e1d151ce1 100644 --- a/src/lerobot/microphones/microphone.py +++ b/src/lerobot/microphones/microphone.py @@ -31,7 +31,9 @@ import soundfile as sf from lerobot.errors import ( DeviceAlreadyConnectedError, + DeviceAlreadyRecordingError, DeviceNotConnectedError, + DeviceNotRecordingError, ) from lerobot.microphones.configs import MicrophoneConfig from lerobot.utils.utils import capture_timestamp_utc @@ -145,6 +147,7 @@ class Microphone: self.logs = {} self.is_connected = False + self.is_recording = False def connect(self) -> None: if self.is_connected: @@ -261,6 +264,8 @@ class Microphone: def start_recording(self, output_file: str | None = None) -> None: if not self.is_connected: raise DeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.") + if self.is_recording: + raise DeviceAlreadyRecordingError(f"Microphone {self.microphone_index} is already recording.") self.read_queue = Queue() with self.read_queue.mutex: @@ -283,13 +288,14 @@ class Microphone: self.record_thread.daemon = True self.record_thread.start() + self.is_recording = True self.stream.start() def stop_recording(self) -> None: if not self.is_connected: raise DeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.") - - self.logs["stop_timestamp"] = capture_timestamp_utc() + if not self.is_recording: + raise DeviceNotRecordingError(f"Microphone {self.microphone_index} is not recording.") if self.record_thread is not None: # self.record_queue.join() @@ -302,11 +308,15 @@ class Microphone: self.stream.stop() # Wait for all buffers to be processed # Remark : stream.abort() flushes the buffers ! + self.is_recording = False + + self.logs["stop_timestamp"] = capture_timestamp_utc() + def disconnect(self) -> None: if not self.is_connected: raise DeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.") - if self.stream.active: + if self.is_recording: self.stop_recording() self.stream.close() diff --git a/src/lerobot/utils/errors.py b/src/lerobot/utils/errors.py index 31b73eaca..f0576f9ed 100644 --- a/src/lerobot/utils/errors.py +++ b/src/lerobot/utils/errors.py @@ -30,3 +30,22 @@ class DeviceAlreadyConnectedError(ConnectionError): ): self.message = message super().__init__(self.message) + + +class DeviceNotRecordingError(Exception): + """Exception raised when the robot device is not recording.""" + + def __init__(self, message="This robot device is not recording. Try calling `start_recording()` first."): + self.message = message + super().__init__(self.message) + + +class DeviceAlreadyRecordingError(Exception): + """Exception raised when the robot device is already recording.""" + + def __init__( + self, + message="This robot device is already recording. Try not calling `start_recording()` twice.", + ): + self.message = message + super().__init__(self.message)