Skip to content

Commit

Permalink
doc: add warning about buffer.slice
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp committed Jul 4, 2024
1 parent 7098253 commit 7f5550d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-schools-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": patch
---

doc: add warning about buffer.slice
7 changes: 5 additions & 2 deletions packages/livekit-rtc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ const track = LocalAudioTrack.createAudioTrack('audio', source);
const options = new TrackPublishOptions();
options.source = TrackSource.SOURCE_MICROPHONE;

// read file into Uint16Array
// note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!
// it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of
// noise being added to the end.
// it is recommended to use buffer.subarray instead.
const sample = readFileSync(pathToFile);
var buffer = new Uint16Array(sample.buffer);
var buffer = new Int16Array(sample.buffer);

await room.localParticipant.publishTrack(track, options);
await source.captureFrame(new AudioFrame(buffer, 16000, 1, buffer.byteLength / 2));
Expand Down
5 changes: 5 additions & 0 deletions packages/livekit-rtc/src/audio_frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export class AudioFrame {
channels: number;
samplesPerChannel: number;

// note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!
// it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of
// noise being added to the end.
// it is recommended to use buffer.subarray instead.
// XXX(nbsp): add this when writing proper docs
constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number) {
this.data = data;
this.sampleRate = sampleRate;
Expand Down

0 comments on commit 7f5550d

Please sign in to comment.