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.

See MDN: AudioData/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 (list of bytes-like buffers, optional) – Buffers to give up. When data is among them its memory is kept without copying, so it must not be modified afterwards. Transferred memoryview objects 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() and AudioData.allocation_size().

See MDN: AudioData/copyTo.

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 from frame_offset on.

  • 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 a with block around the data ends. Data that is garbage collected while open emits a ResourceWarning. Data read from a track is interleaved u8, s16 or s32.

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 None once closed.

See MDN: AudioData/format.

Type:

webrtc.AudioSampleFormat, optional

property sample_rate: float

The number of frames per second, or 0 once closed.

See MDN: AudioData/sampleRate.

Type:

float

property number_of_frames: int

The number of frames (samples per channel), or 0 once closed.

See MDN: AudioData/numberOfFrames.

Type:

int

property number_of_channels: int

The number of channels, or 0 once closed.

See MDN: AudioData/numberOfChannels.

Type:

int

property duration: int

The duration in microseconds, rounded down, or 0 once closed.

See MDN: AudioData/duration.

Type:

int

property timestamp: int

The presentation time in microseconds, kept after closing.

See MDN: AudioData/timestamp.

Type:

int

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:
Return type:

int

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.

See MDN: AudioData/copyTo.

Parameters:
Raises:
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:

AudioData

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.