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.

Implements: Disposable

Used by ​

Constructor ​

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

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

See AudioSampleResource.

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.

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.

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.

setTimestamp() ​

ts
setTimestamp(
	newTimestamp: number,
): void;

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

toAudioBuffer() ​

ts
toAudioBuffer(): AudioBuffer;

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

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.

trim() ​

ts
trim(
	startFrame: number,
	endFrame: number = this.numberOfFrames,
): AudioSample;

Returns a new AudioSample containing only the frames in the range [startFrame, endFrame). Both bounds must lie within this sample's range of frames. The returned sample's timestamp is shifted to match the start of the trimmed section.

[Symbol.dispose]() ​

ts
[Symbol.dispose](): void;

Calls .close().

Released under the Mozilla Public License 2.0.