Skip to content
Class

AudioSample

Represents a raw, unencoded audio sample. Mainly used as an expressive wrapper around WebCodecs API's AudioData, but can also be used standalone.

Used by

Constructor

ts
constructor(
	init: AudioData | AudioSampleInit,
): AudioSample;

Creates a new AudioSample, either from an existing AudioData or from raw bytes specified in AudioSampleInit.

Static methods

fromAudioBuffer()

ts
static fromAudioBuffer(
	audioBuffer: AudioBuffer,
	timestamp: number,
): AudioSample[];

Creates AudioSamples from an AudioBuffer, starting at the given timestamp in seconds. Typically creates exactly one sample, but may create multiple if the AudioBuffer is exceedingly large.

Properties

duration

ts
readonly duration: number;

The duration of the sample in seconds.

format

ts
readonly format: AudioSampleFormat;

The audio sample format. See sample formats

microsecondDuration

ts
get microsecondDuration(): number;

The duration of the sample in microseconds.

microsecondTimestamp

ts
get microsecondTimestamp(): number;

The presentation timestamp of the sample in microseconds.

numberOfChannels

ts
readonly numberOfChannels: number;

The number of audio channels.

numberOfFrames

ts
readonly numberOfFrames: number;

The number of audio frames in the sample, per channel. In other words, the length of this audio sample in frames.

sampleRate

ts
readonly sampleRate: number;

The audio sample rate in hertz.

timestamp

ts
readonly timestamp: number;

The presentation timestamp of the sample in seconds. May be negative. Samples with negative end timestamps should not be presented.

Methods

allocationSize()

ts
allocationSize(
	options: AudioSampleCopyToOptions,
): number;

Returns the number of bytes required to hold the audio sample's data as specified by the given options.

See AudioSampleCopyToOptions.

copyTo()

ts
copyTo(
	destination: AllowSharedBufferSource,
	options: AudioSampleCopyToOptions,
): void;

Copies the audio sample's data to an ArrayBuffer or ArrayBufferView as specified by the given options.

See AudioSampleCopyToOptions.

clone()

ts
clone(): AudioSample;

Clones this audio sample.

close()

ts
close(): void;

Closes this audio sample, releasing held resources. Audio samples should be closed as soon as they are not needed anymore.

toAudioData()

ts
toAudioData(): AudioData;

Converts this audio sample to an AudioData for use with the WebCodecs API. The AudioData returned by this method must be closed separately from this audio sample.

toAudioBuffer()

ts
toAudioBuffer(): AudioBuffer;

Convert this audio sample to an AudioBuffer for use with the Web Audio API.

setTimestamp()

ts
setTimestamp(
	newTimestamp: number,
): void;

Sets the presentation timestamp of this audio sample, in seconds.

Released under the Mozilla Public License 2.0.