Fixing sounddevice stream active state recovery and adding corresponding exceptions

This commit is contained in:
CarolinePascal
2025-04-07 16:33:16 +02:00
parent 00536c6c5b
commit 373a169bd2
2 changed files with 32 additions and 3 deletions
+13 -3
View File
@@ -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()
+19
View File
@@ -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)