AudioData
The WebCodecs AudioData and the dictionaries that create and copy it.
- class webrtc.models.audio_data.AudioDataInit(format, sample_rate, number_of_frames, number_of_channels, timestamp, data, transfer=<factory>)
Options to create an
AudioData.- Parameters:
format (
webrtc.AudioSampleFormat) β The sample type and whether channels are interleaved or planar.sample_rate (
float) β The number of frames per second. It must be positive and finite.number_of_frames (
int) β The number of frames (samples per channel), which must be positive.number_of_channels (
int) β The number of channels, which must be positive.timestamp (
int) β The presentation time in microseconds.data (Buffer) β A bytes-like buffer that holds at least the samples described above. Extra bytes are ignored. The buffer is copied unless it is transferred.
transfer (
listof bytes-like buffers, optional) β Buffers to give up. Whendatais among them its memory is kept without copying, so it must not be modified afterwards. Transferredmemoryviewobjects are released, but other buffers stay usable.
- class webrtc.models.audio_data.AudioDataCopyToOptions(plane_index, frame_offset=0, frame_count=None, format=None)
Options for
AudioData.copy_to()andAudioData.allocation_size().- Parameters:
plane_index (
int) β The channel to copy when the destination format is planar. It must be 0 for an interleaved format, which copies all channels.frame_offset (
int, optional) β The index of the first frame to copy, 0 by default.frame_count (
int, optional) β How many frames to copy. By default, all frames fromframe_offseton.format (
webrtc.AudioSampleFormat, optional) β The format to convert the samples to. By default, the dataβs own format.
- class webrtc.models.audio_data.AudioData(init)
A block of audio samples with their format and presentation time.
The samples are held until
close()is called or awithblock around the data ends. Data that is garbage collected while open emits aResourceWarning. Data read from a track is interleavedu8,s16ors32.See MDN: AudioData.
- Parameters:
init (
AudioDataInit) β The samples and their format.- Raises:
TypeError β If the options are invalid, or the data is too small for them.
Example:
data = webrtc.AudioData( webrtc.AudioDataInit( format='s16', sample_rate=48000, number_of_frames=480, number_of_channels=1, timestamp=0, data=bytes(960), ) )- property format: AudioSampleFormat | None
The sample type and layout, or
Noneonce closed.- Type:
webrtc.AudioSampleFormat, optional
- property sample_rate: float
The number of frames per second, or 0 once closed.
See MDN: AudioData/sampleRate.
- Type:
- property number_of_frames: int
The number of frames (samples per channel), or 0 once closed.
See MDN: AudioData/numberOfFrames.
- Type:
- property number_of_channels: int
The number of channels, or 0 once closed.
See MDN: AudioData/numberOfChannels.
- Type:
- property duration: int
The duration in microseconds, rounded down, or 0 once closed.
- Type:
- property timestamp: int
The presentation time in microseconds, kept after closing.
- Type:
- allocation_size(options)
Computes the minimum destination size for
copy_to()with the same options.See MDN: AudioData/allocationSize.
- Parameters:
options (
AudioDataCopyToOptions) β What to copy.- Returns:
intβ The number of bytes.- Raises:
webrtc.InvalidStateError β If the data is closed.
TypeError β If an option has the wrong type or a negative value.
webrtc.InvalidRangeError β If the plane or the frames are out of range.
- Return type:
- copy_to(destination, options)
Copies samples into a buffer, converting them to another format if asked.
Float samples converted to an integer format are clamped to the range from -1 to 1 first.
- Parameters:
destination (
bytearrayor writablememoryview) β The buffer to write to, at leastallocation_size()bytes long.options (
AudioDataCopyToOptions) β What to copy.
- Raises:
webrtc.InvalidStateError β If the data is closed.
TypeError β If an option has the wrong type or a negative value.
webrtc.InvalidRangeError β If the plane or the frames are out of range, or the buffer is too small.
webrtc.NotSupportedError β If the samples canβt be converted to the format.
- clone()
Creates another data that shares these samples without copying them. Each must be closed separately.
See MDN: AudioData/clone.
- Returns:
AudioDataβ The new data.- Raises:
webrtc.InvalidStateError β If the data is closed.
- Return type:
- close()
Releases this dataβs hold on the samples. Closing a closed data does nothing.
After that, the format and size attributes read as empty, and the methods raise
webrtc.InvalidStateError.See MDN: AudioData/close.