AudioData
Represents audio data with format information.
Properties:
Property | Type | Description |
|---|---|---|
|
| Audio metadata header |
|
| Number of channels |
|
| Sample rate in |
|
| Encoding or container format, e.g., |
|
| PCM sample format, see "Sensor and State Snapshot APIs / PcmSampleFormat" |
|
| Bitrate in |
Read-only properties:
Property | Type | Description |
|---|---|---|
|
| Raw audio binary data |
|
| Alias property for |
|
| Bit depth derived from |
|
| Timestamp in seconds, equivalent to |
|
| Audio duration; precisely calculated from byte length for PCM, estimated from |
AudioData
Signature:
AudioData(
data: bytes,
header: Header,
channels: int = 1,
sample_rate: int = 16000,
coding_format: str = "pcm",
sample_format: PcmSampleFormat = "S16LE",
bitrate: int = 0,
)
Description:
Constructs an AudioData instance.
Parameters:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Raw audio binary data |
|
| Yes | Audio metadata header |
|
| No | Number of channels |
|
| No | Sample rate in |
|
| No | Encoding or container format, e.g. |
|
| No | PCM sample format; see "Sensor and State Snapshot APIs / PcmSampleFormat" |
|
| No | Bit rate in |
Returns:
AudioData instance.
Raises:
None.
save
Signature:
AudioData.save(path: str) -> None
Description:
Save audio to a file. The file extension must be .wav, .pcm, or .mp3, and the data must be PCM; saving as .mp3 also requires ffmpeg to be installed on the system.
Parameters:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Output file path; the save format is selected automatically based on the extension |
Returns:
None.
Raises:
ValueError: Unsupported extension, or coding_format is not "pcm".
RuntimeError: ffmpeg not found or encoding failed when saving as .mp3.
with_data
Signature:
AudioData.with_data(data: bytes) -> AudioData
Description:
Copy the current object and replace the audio data; all other format fields retain their original values. Returns a new object; the original is unchanged. Commonly used for chunk-based splitting or streaming.
Parameters:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | New raw audio binary data |
Returns:
AudioData, the new audio object.
Raises:
None.
concat
Signature:
@classmethod
AudioData.concat(
chunks: Iterable[AudioData],
*,
header: Header | None = None,
) -> AudioData
Description:
Merge multiple audio chunks with identical formats. All chunks must share the same sample_rate / channels / sample_format / coding_format.
Parameters:
Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Sequence of audio chunks to merge; at least one required |
|
| No | Metadata header for the merged audio; if omitted, the first chunk's |
Returns:
AudioData, the new merged object.
Raises:
ValueError: chunks is empty, or chunks have inconsistent formats.
to_numpy
Signature:
AudioData.to_numpy() -> np.ndarray
Description:
Convert to a floating-point array in the range [-1.0, 1.0]. Shape is (n_samples, channels) for multi-channel audio, or (n_samples,) for mono.
Parameters:
No parameters.
Returns:
np.ndarray, dtype=np.float32.
Raises:
ValueError: coding_format is not "pcm".