[skip ci] feat(audio recording): handle folder creation in start_recording directly

This commit is contained in:
CarolinePascal 2025-04-16 11:36:31 +02:00
parent 0acda9facd
commit 9f3a424498
No known key found for this signature in database
3 changed files with 22 additions and 14 deletions

View File

@ -967,17 +967,8 @@ class LeRobotDataset(torch.utils.data.Dataset):
Starts recording audio data provided by the microphone and directly writes it in a .wav file. Starts recording audio data provided by the microphone and directly writes it in a .wav file.
""" """
audio_dir = self._get_raw_audio_file_path( audio_file = self._get_raw_audio_file_path(self.num_episodes, "observation.audio." + microphone_key)
self.num_episodes, "observation.audio." + microphone_key microphone.start_recording(output_file=audio_file)
).parent
if not audio_dir.is_dir():
audio_dir.mkdir(parents=True, exist_ok=True)
microphone.start_recording(
output_file=self._get_raw_audio_file_path(
self.num_episodes, "observation.audio." + microphone_key
)
)
def save_episode(self, episode_data: dict | None = None) -> None: def save_episode(self, episode_data: dict | None = None) -> None:
""" """

View File

@ -302,7 +302,12 @@ class Microphone:
return audio_readings return audio_readings
def start_recording(self, output_file: str | None = None, multiprocessing: bool | None = False) -> None: def start_recording(
self,
output_file: str | None = None,
multiprocessing: bool | None = False,
overwrite: bool | None = True,
) -> None:
""" """
Starts the recording of the microphone. If output_file is provided, the audio will be written to this file. Starts the recording of the microphone. If output_file is provided, the audio will be written to this file.
""" """
@ -323,8 +328,15 @@ class Microphone:
# Write recordings into a file if output_file is provided # Write recordings into a file if output_file is provided
if output_file is not None: if output_file is not None:
output_file = Path(output_file) output_file = Path(output_file)
output_file.parent.mkdir(parents=True, exist_ok=True)
if output_file.exists(): if output_file.exists():
output_file.unlink() if overwrite:
output_file.unlink()
else:
raise FileExistsError(
f"Output file {output_file} already exists. Set overwrite to True to overwrite it."
)
if multiprocessing: if multiprocessing:
self.record_stop_event = process_Event() self.record_stop_event = process_Event()

View File

@ -21,7 +21,12 @@ from lerobot.common.robot_devices.microphones.configs import MicrophoneConfig, M
class Microphone(Protocol): class Microphone(Protocol):
def connect(self): ... def connect(self): ...
def disconnect(self): ... def disconnect(self): ...
def start_recording(self, output_file: str | None = None, multiprocessing: bool | None = False): ... def start_recording(
self,
output_file: str | None = None,
multiprocessing: bool | None = False,
overwrite: bool | None = True,
): ...
def stop_recording(self): ... def stop_recording(self): ...