Skip to content

Commit bb98682

Browse files
authored
docs: add docstring to sound device connector (#548)
1 parent 7ad2668 commit bb98682

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/rai_core/rai/communication/sound_device/connector.py

+131
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ def __init__(
6161
super().__init__()
6262

6363
def get_audio_params(self, target: str) -> AudioParams:
64+
"""
65+
Retrieve audio parameters for a specified device.
66+
67+
Parameters
68+
----------
69+
target : str
70+
The name of the device for which to retrieve audio parameters.
71+
72+
Returns
73+
-------
74+
AudioParams
75+
An `AudioParams` object containing the sample rate, input channels,
76+
and output channels of the specified device.
77+
"""
6478
return AudioParams(
6579
self.devices[target].sample_rate,
6680
self.devices[target].in_channels,
@@ -72,9 +86,47 @@ def configure_device(
7286
target: str,
7387
config: SoundDeviceConfig,
7488
):
89+
"""
90+
Configure and register a new audio device.
91+
92+
Parameters
93+
----------
94+
target : str
95+
The name identifier for the device to configure.
96+
config : SoundDeviceConfig
97+
The configuration settings to initialize the device with.
98+
99+
Notes
100+
-----
101+
The configured device is stored in `self.devices` under the given target name.
102+
"""
75103
self.devices[target] = SoundDeviceAPI(config)
76104

77105
def send_message(self, message: SoundDeviceMessage, target: str, **kwargs) -> None:
106+
"""
107+
Send an audio message to a specified device.
108+
109+
Parameters
110+
----------
111+
message : SoundDeviceMessage
112+
The message containing audio data or control commands (e.g., stop or read request).
113+
target : str
114+
The name of the target device to which the message will be sent.
115+
**kwargs
116+
Additional keyword arguments (currently unused).
117+
118+
Raises
119+
------
120+
SoundDeviceError
121+
If the message requests reading (unsupported) or if no audio data is provided
122+
when attempting to play audio.
123+
124+
Notes
125+
-----
126+
- If `message.stop` is `True`, the device will be stopped.
127+
- If `message.read` is `True`, an error is raised (reading must use actions or services).
128+
- Otherwise, attempts to write audio data to the device.
129+
"""
78130
if message.stop:
79131
self.devices[target].stop()
80132
elif message.read:
@@ -90,6 +142,10 @@ def send_message(self, message: SoundDeviceMessage, target: str, **kwargs) -> No
90142
def receive_message(
91143
self, source: str, timeout_sec: float = 1.0, **kwargs
92144
) -> SoundDeviceMessage:
145+
"""
146+
SoundDeviceConnector does not support receiving messages. For recording use start_action or service_call with read=True.
147+
148+
"""
93149
raise SoundDeviceError(
94150
"SoundDeviceConnector does not support receiving messages. For recording use start_action or service_call with read=True."
95151
)
@@ -103,6 +159,41 @@ def service_call(
103159
duration: float = 1.0,
104160
**kwargs,
105161
) -> SoundDeviceMessage:
162+
"""
163+
Perform a blocking service call to a sound device for playback or recording.
164+
165+
Depending on the message content, either records audio from the device or plays
166+
provided audio data in a blocking manner.
167+
168+
Parameters
169+
----------
170+
message : SoundDeviceMessage
171+
The message specifying the operation: playback (with audio data) or recording (read flag).
172+
target : str
173+
The name of the target device on which to perform the action.
174+
timeout_sec : float, optional
175+
Timeout for the operation in seconds. Currently unused. Defaults to `1.0`.
176+
duration : float, optional
177+
Duration for recording audio in seconds. Only relevant when reading. Defaults to `1.0`.
178+
**kwargs
179+
Additional keyword arguments (currently unused).
180+
181+
Returns
182+
-------
183+
SoundDeviceMessage
184+
A new message containing recorded audio if reading, or an empty message after successful playback.
185+
186+
Raises
187+
------
188+
SoundDeviceError
189+
If stopping is requested (unsupported in this method) or if playback is attempted
190+
without providing audio data.
191+
192+
Notes
193+
-----
194+
- To stop a device, use `send_message` with `stop=True` instead.
195+
- Audio recording is done synchronously with `blocking=True`.
196+
"""
106197
if message.stop:
107198
raise SoundDeviceError("For stopping use send_message with stop=True.")
108199
elif message.read:
@@ -128,6 +219,46 @@ def start_action(
128219
timeout_sec: float = 1.0,
129220
**kwargs,
130221
) -> str:
222+
"""
223+
Start an asynchronous streaming action on a sound device.
224+
225+
Depending on the action data, either opens a read (recording) or write (playback)
226+
audio stream. Handles for ongoing actions are tracked internally.
227+
228+
Parameters
229+
----------
230+
action_data : SoundDeviceMessage, optional
231+
The action request containing operation details. Must not be `None`.
232+
If `read` is `True`, a read (recording) stream is opened; otherwise, a write (playback) stream.
233+
target : str
234+
The name of the target device to interact with.
235+
on_feedback : Callable
236+
Callback function to handle feedback during the stream.
237+
on_done : Callable
238+
Callback function called upon stream completion.
239+
timeout_sec : float, optional
240+
Timeout for starting the stream, in seconds. Defaults to `1.0`. (Currently unused.)
241+
**kwargs
242+
Additional parameters:
243+
- `sample_rate` (int, optional): Desired sample rate for playback streams.
244+
- `channels` (int, optional): Number of channels for playback streams.
245+
246+
Returns
247+
-------
248+
str
249+
A unique handle identifying the started action.
250+
251+
Raises
252+
------
253+
SoundDeviceError
254+
If `action_data` is not provided (`None`).
255+
256+
Notes
257+
-----
258+
- For recording streams, `open_read_stream` is used.
259+
- For playback streams, `open_write_stream` is used, optionally customized by `sample_rate` and `channels`.
260+
- Started actions are stored in `self.action_handles` with the handle as the key.
261+
"""
131262
handle = self._generate_handle()
132263
if action_data is None:
133264
raise SoundDeviceError("action_data must be provided")

0 commit comments

Comments
 (0)