Fixing sounddevice stream active state recovery and adding corresponding exceptions
This commit is contained in:
parent
17ad249335
commit
96ed10f90d
|
@ -35,6 +35,8 @@ from lerobot.common.robot_devices.microphones.configs import MicrophoneConfig
|
||||||
from lerobot.common.robot_devices.utils import (
|
from lerobot.common.robot_devices.utils import (
|
||||||
RobotDeviceAlreadyConnectedError,
|
RobotDeviceAlreadyConnectedError,
|
||||||
RobotDeviceNotConnectedError,
|
RobotDeviceNotConnectedError,
|
||||||
|
RobotDeviceNotRecordingError,
|
||||||
|
RobotDeviceAlreadyRecordingError,
|
||||||
busy_wait,
|
busy_wait,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -152,6 +154,7 @@ class Microphone:
|
||||||
|
|
||||||
self.logs = {}
|
self.logs = {}
|
||||||
self.is_connected = False
|
self.is_connected = False
|
||||||
|
self.is_recording = False
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
if self.is_connected:
|
if self.is_connected:
|
||||||
|
@ -250,8 +253,8 @@ class Microphone:
|
||||||
|
|
||||||
if not self.is_connected:
|
if not self.is_connected:
|
||||||
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
||||||
if not self.stream.active:
|
if not self.is_recording:
|
||||||
raise RuntimeError(f"Microphone {self.microphone_index} is not recording.")
|
raise RobotDeviceNotRecordingError(f"Microphone {self.microphone_index} is not recording.")
|
||||||
|
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
|
|
||||||
|
@ -269,6 +272,8 @@ class Microphone:
|
||||||
|
|
||||||
if not self.is_connected:
|
if not self.is_connected:
|
||||||
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
||||||
|
if self.is_recording:
|
||||||
|
raise RobotDeviceAlreadyRecordingError(f"Microphone {self.microphone_index} is already recording.")
|
||||||
|
|
||||||
self.read_queue = Queue()
|
self.read_queue = Queue()
|
||||||
with self.read_queue.mutex:
|
with self.read_queue.mutex:
|
||||||
|
@ -291,12 +296,15 @@ class Microphone:
|
||||||
self.record_thread.daemon = True
|
self.record_thread.daemon = True
|
||||||
self.record_thread.start()
|
self.record_thread.start()
|
||||||
|
|
||||||
|
self.is_recording = True
|
||||||
self.stream.start()
|
self.stream.start()
|
||||||
|
|
||||||
def stop_recording(self) -> None:
|
def stop_recording(self) -> None:
|
||||||
|
|
||||||
if not self.is_connected:
|
if not self.is_connected:
|
||||||
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
||||||
|
if not self.is_recording:
|
||||||
|
raise RobotDeviceNotRecordingError(f"Microphone {self.microphone_index} is not recording.")
|
||||||
|
|
||||||
if self.record_thread is not None:
|
if self.record_thread is not None:
|
||||||
#self.record_queue.join()
|
#self.record_queue.join()
|
||||||
|
@ -309,12 +317,14 @@ class Microphone:
|
||||||
self.stream.stop() #Wait for all buffers to be processed
|
self.stream.stop() #Wait for all buffers to be processed
|
||||||
#Remark : stream.abort() flushes the buffers !
|
#Remark : stream.abort() flushes the buffers !
|
||||||
|
|
||||||
|
self.is_recording = False
|
||||||
|
|
||||||
def disconnect(self) -> None:
|
def disconnect(self) -> None:
|
||||||
|
|
||||||
if not self.is_connected:
|
if not self.is_connected:
|
||||||
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
raise RobotDeviceNotConnectedError(f"Microphone {self.microphone_index} is not connected.")
|
||||||
|
|
||||||
if self.stream.active:
|
if self.is_recording:
|
||||||
self.stop_recording()
|
self.stop_recording()
|
||||||
|
|
||||||
self.stream.close()
|
self.stream.close()
|
||||||
|
|
|
@ -63,3 +63,23 @@ class RobotDeviceAlreadyConnectedError(Exception):
|
||||||
):
|
):
|
||||||
self.message = message
|
self.message = message
|
||||||
super().__init__(self.message)
|
super().__init__(self.message)
|
||||||
|
|
||||||
|
|
||||||
|
class RobotDeviceNotRecordingError(Exception):
|
||||||
|
"""Exception raised when the robot device is not recording."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, message="This robot device is not recording. Try calling `robot_device.start_recording()` first."
|
||||||
|
):
|
||||||
|
self.message = message
|
||||||
|
super().__init__(self.message)
|
||||||
|
|
||||||
|
class RobotDeviceAlreadyRecordingError(Exception):
|
||||||
|
"""Exception raised when the robot device is already recording."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message="This robot device is already recording. Try not calling `robot_device.start_recording()` twice.",
|
||||||
|
):
|
||||||
|
self.message = message
|
||||||
|
super().__init__(self.message)
|
||||||
|
|
Loading…
Reference in New Issue