Skip to content

Commit ca51387

Browse files
authored
init (#492)
1 parent 2379275 commit ca51387

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/livekit-rtc/rust-sdks

Submodule rust-sdks updated 79 files

packages/livekit-rtc/src/audio_frame.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,58 @@ import { FfiClient, FfiHandle } from './ffi_client.js';
55
import type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';
66
import { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';
77

8+
/**
9+
* A class that represents a frame of audio data with specific properties such as sample rate,
10+
* number of channels, and samples per channel.
11+
*/
812
export class AudioFrame {
913
data: Int16Array;
1014
sampleRate: number;
1115
channels: number;
1216
samplesPerChannel: number;
17+
duration: number;
1318

1419
// note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!
1520
// it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of
1621
// noise being added to the end.
1722
// it is recommended to use buffer.subarray instead.
1823
// XXX(nbsp): add this when writing proper docs
24+
25+
/**
26+
* Initialize an AudioFrame instance.
27+
*
28+
* @param data - The raw audio data as Int16Array, which must be at least
29+
* `channels * samplesPerChannel` elements long.
30+
* @param sampleRate - The sample rate of the audio in Hz.
31+
* @param channels - The number of audio channels (e.g., 1 for mono, 2 for stereo).
32+
* @param samplesPerChannel - The number of samples per channel.
33+
*
34+
* @throws Error - If the length of `data` is smaller than the required size.
35+
*/
1936
constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number) {
37+
if (data.length < channels * samplesPerChannel) {
38+
throw new Error(
39+
`data length ${data.length} is smaller than required ${channels * samplesPerChannel}`,
40+
);
41+
}
42+
2043
this.data = data;
2144
this.sampleRate = sampleRate;
2245
this.channels = channels;
2346
this.samplesPerChannel = samplesPerChannel;
47+
this.duration = samplesPerChannel / sampleRate;
2448
}
2549

50+
/**
51+
* Create a new empty AudioFrame instance with specified sample rate, number of channels,
52+
* and samples per channel.
53+
*
54+
* @param sampleRate - The sample rate of the audio in Hz.
55+
* @param channels - The number of audio channels (e.g., 1 for mono, 2 for stereo).
56+
* @param samplesPerChannel - The number of samples per channel.
57+
*
58+
* @returns A new AudioFrame instance with uninitialized (zeroed) data.
59+
*/
2660
static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {
2761
const data = new Int16Array(channels * samplesPerChannel);
2862
return new AudioFrame(data, sampleRate, channels, samplesPerChannel);

0 commit comments

Comments
 (0)