@livekit/rtc-node 0.13.22 → 0.13.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/dist/audio_frame.cjs +6 -1
  2. package/dist/audio_frame.cjs.map +1 -1
  3. package/dist/audio_frame.d.cts +4 -1
  4. package/dist/audio_frame.d.ts +4 -1
  5. package/dist/audio_frame.d.ts.map +1 -1
  6. package/dist/audio_frame.js +6 -1
  7. package/dist/audio_frame.js.map +1 -1
  8. package/dist/audio_mixer.cjs +11 -4
  9. package/dist/audio_mixer.cjs.map +1 -1
  10. package/dist/audio_mixer.d.ts.map +1 -1
  11. package/dist/audio_mixer.js +11 -4
  12. package/dist/audio_mixer.js.map +1 -1
  13. package/dist/audio_stream.cjs +20 -5
  14. package/dist/audio_stream.cjs.map +1 -1
  15. package/dist/audio_stream.d.cts +4 -3
  16. package/dist/audio_stream.d.ts +4 -3
  17. package/dist/audio_stream.d.ts.map +1 -1
  18. package/dist/audio_stream.js +20 -5
  19. package/dist/audio_stream.js.map +1 -1
  20. package/dist/data_streams/stream_reader.cjs.map +1 -1
  21. package/dist/data_streams/stream_reader.js.map +1 -1
  22. package/dist/frame_processor.cjs +38 -0
  23. package/dist/frame_processor.cjs.map +1 -0
  24. package/dist/frame_processor.d.cts +29 -0
  25. package/dist/frame_processor.d.ts +29 -0
  26. package/dist/frame_processor.d.ts.map +1 -0
  27. package/dist/frame_processor.js +14 -0
  28. package/dist/frame_processor.js.map +1 -0
  29. package/dist/index.cjs +3 -0
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +1 -0
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +4 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/log.cjs +1 -10
  37. package/dist/log.cjs.map +1 -1
  38. package/dist/log.d.cts +4 -0
  39. package/dist/log.d.ts +4 -0
  40. package/dist/log.d.ts.map +1 -1
  41. package/dist/log.js +1 -10
  42. package/dist/log.js.map +1 -1
  43. package/dist/proto/audio_frame_pb.cjs.map +1 -1
  44. package/dist/proto/audio_frame_pb.js.map +1 -1
  45. package/dist/proto/e2ee_pb.cjs.map +1 -1
  46. package/dist/proto/e2ee_pb.js.map +1 -1
  47. package/dist/proto/ffi_pb.cjs +4 -2
  48. package/dist/proto/ffi_pb.cjs.map +1 -1
  49. package/dist/proto/ffi_pb.d.cts +13 -1
  50. package/dist/proto/ffi_pb.d.ts +13 -1
  51. package/dist/proto/ffi_pb.d.ts.map +1 -1
  52. package/dist/proto/ffi_pb.js +5 -3
  53. package/dist/proto/ffi_pb.js.map +1 -1
  54. package/dist/proto/handle_pb.cjs.map +1 -1
  55. package/dist/proto/handle_pb.js.map +1 -1
  56. package/dist/proto/participant_pb.cjs +26 -4
  57. package/dist/proto/participant_pb.cjs.map +1 -1
  58. package/dist/proto/participant_pb.d.cts +31 -2
  59. package/dist/proto/participant_pb.d.ts +31 -2
  60. package/dist/proto/participant_pb.d.ts.map +1 -1
  61. package/dist/proto/participant_pb.js +24 -3
  62. package/dist/proto/participant_pb.js.map +1 -1
  63. package/dist/proto/room_pb.cjs +29 -2
  64. package/dist/proto/room_pb.cjs.map +1 -1
  65. package/dist/proto/room_pb.d.cts +30 -7
  66. package/dist/proto/room_pb.d.ts +30 -7
  67. package/dist/proto/room_pb.d.ts.map +1 -1
  68. package/dist/proto/room_pb.js +28 -2
  69. package/dist/proto/room_pb.js.map +1 -1
  70. package/dist/proto/rpc_pb.cjs.map +1 -1
  71. package/dist/proto/rpc_pb.js.map +1 -1
  72. package/dist/proto/stats_pb.cjs.map +1 -1
  73. package/dist/proto/stats_pb.js.map +1 -1
  74. package/dist/proto/track_pb.cjs.map +1 -1
  75. package/dist/proto/track_pb.js.map +1 -1
  76. package/dist/proto/track_publication_pb.cjs +66 -2
  77. package/dist/proto/track_publication_pb.cjs.map +1 -1
  78. package/dist/proto/track_publication_pb.d.cts +56 -1
  79. package/dist/proto/track_publication_pb.d.ts +56 -1
  80. package/dist/proto/track_publication_pb.d.ts.map +1 -1
  81. package/dist/proto/track_publication_pb.js +62 -1
  82. package/dist/proto/track_publication_pb.js.map +1 -1
  83. package/dist/proto/video_frame_pb.cjs +3 -1
  84. package/dist/proto/video_frame_pb.cjs.map +1 -1
  85. package/dist/proto/video_frame_pb.d.cts +5 -1
  86. package/dist/proto/video_frame_pb.d.ts +5 -1
  87. package/dist/proto/video_frame_pb.d.ts.map +1 -1
  88. package/dist/proto/video_frame_pb.js +3 -1
  89. package/dist/proto/video_frame_pb.js.map +1 -1
  90. package/dist/room.cjs +44 -26
  91. package/dist/room.cjs.map +1 -1
  92. package/dist/room.d.cts +19 -11
  93. package/dist/room.d.ts +19 -11
  94. package/dist/room.d.ts.map +1 -1
  95. package/dist/room.js +45 -27
  96. package/dist/room.js.map +1 -1
  97. package/dist/version.cjs +1 -1
  98. package/dist/version.cjs.map +1 -1
  99. package/dist/version.d.cts +1 -1
  100. package/dist/version.d.ts +1 -1
  101. package/dist/version.js +1 -1
  102. package/dist/version.js.map +1 -1
  103. package/package.json +11 -7
  104. package/src/audio_frame.ts +15 -1
  105. package/src/audio_mixer.ts +15 -4
  106. package/src/audio_stream.ts +23 -7
  107. package/src/data_streams/stream_reader.ts +4 -4
  108. package/src/frame_processor.ts +29 -0
  109. package/src/index.ts +5 -0
  110. package/src/log.ts +14 -10
  111. package/src/proto/audio_frame_pb.ts +1 -1
  112. package/src/proto/e2ee_pb.ts +1 -1
  113. package/src/proto/ffi_pb.ts +16 -2
  114. package/src/proto/handle_pb.ts +1 -1
  115. package/src/proto/participant_pb.ts +45 -1
  116. package/src/proto/room_pb.ts +52 -8
  117. package/src/proto/rpc_pb.ts +1 -1
  118. package/src/proto/stats_pb.ts +1 -1
  119. package/src/proto/track_pb.ts +1 -1
  120. package/src/proto/track_publication_pb.ts +105 -1
  121. package/src/proto/video_frame_pb.ts +7 -1
  122. package/src/room.ts +65 -38
  123. package/src/tests/e2e.test.ts +514 -0
  124. package/src/version.ts +1 -1
@@ -30,11 +30,12 @@ class AudioFrame {
30
30
  // noise being added to the end.
31
31
  // it is recommended to use buffer.subarray instead.
32
32
  // XXX(nbsp): add this when writing proper docs
33
- constructor(data, sampleRate, channels, samplesPerChannel) {
33
+ constructor(data, sampleRate, channels, samplesPerChannel, userdata = {}) {
34
34
  this.data = data;
35
35
  this.sampleRate = sampleRate;
36
36
  this.channels = channels;
37
37
  this.samplesPerChannel = samplesPerChannel;
38
+ this._userdata = userdata;
38
39
  }
39
40
  static create(sampleRate, channels, samplesPerChannel) {
40
41
  const data = new Int16Array(channels * samplesPerChannel);
@@ -62,6 +63,10 @@ class AudioFrame {
62
63
  samplesPerChannel: this.samplesPerChannel
63
64
  });
64
65
  }
66
+ /** Returns the user data associated with the audio frame. */
67
+ get userdata() {
68
+ return this._userdata;
69
+ }
65
70
  }
66
71
  const combineAudioFrames = (buffer) => {
67
72
  if (!Array.isArray(buffer)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/audio_frame.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { FfiClient, FfiHandle } from './ffi_client.js';\nimport type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';\nimport { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';\n\nexport class AudioFrame {\n data: Int16Array;\n sampleRate: number;\n channels: number;\n samplesPerChannel: number;\n\n // note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!\n // it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of\n // noise being added to the end.\n // it is recommended to use buffer.subarray instead.\n // XXX(nbsp): add this when writing proper docs\n constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number) {\n this.data = data;\n this.sampleRate = sampleRate;\n this.channels = channels;\n this.samplesPerChannel = samplesPerChannel;\n }\n\n static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {\n const data = new Int16Array(channels * samplesPerChannel);\n return new AudioFrame(data, sampleRate, channels, samplesPerChannel);\n }\n\n /** @internal */\n static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {\n const info = owned.info!;\n const len = info.numChannels! * info.samplesPerChannel! * 2; // c_int16\n const data = FfiClient.instance.copyBuffer(info.dataPtr!, len);\n new FfiHandle(owned.handle!.id!).dispose();\n return new AudioFrame(\n new Int16Array(data.buffer),\n info.sampleRate!,\n info.numChannels!,\n info.samplesPerChannel!,\n );\n }\n\n /** @internal */\n protoInfo(): AudioFrameBufferInfo {\n return new AudioFrameBufferInfo({\n dataPtr: FfiClient.instance.retrievePtr(new Uint8Array(this.data.buffer)),\n sampleRate: this.sampleRate,\n numChannels: this.channels,\n samplesPerChannel: this.samplesPerChannel,\n });\n }\n}\n\n/**\n * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.\n *\n * This function concatenates the audio data from multiple frames, ensuring that all frames have\n * the same sample rate and number of channels. It efficiently merges the data by preallocating the\n * necessary memory and copying the frame data without unnecessary reallocations.\n *\n * @param buffer - a single AudioFrame or list thereof\n */\nexport const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {\n if (!Array.isArray(buffer)) {\n return buffer;\n }\n buffer = buffer as AudioFrame[];\n\n if (buffer.length === 0) {\n throw new Error('buffer is empty');\n }\n\n const sampleRate = buffer[0]!.sampleRate;\n const channels = buffer[0]!.channels;\n\n let totalSamplesPerChannel = 0;\n for (const frame of buffer) {\n if (frame.sampleRate != sampleRate) {\n throw new Error(`sample rate mismatch: expected ${sampleRate}, got ${frame.sampleRate}`);\n }\n\n if (frame.channels != channels) {\n throw new Error(`channel mismatch: expected ${channels}, got ${frame.channels}`);\n }\n\n totalSamplesPerChannel += frame.samplesPerChannel;\n }\n\n const data = new Int16Array(buffer.map((x) => [...x.data]).flat());\n return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;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,EAAG;AAE5B,MAAI,yBAAyB;AAC7B,aAAW,SAAS,QAAQ;AAC1B,QAAI,MAAM,cAAc,YAAY;AAClC,YAAM,IAAI,MAAM,kCAAkC,UAAU,SAAS,MAAM,UAAU,EAAE;AAAA,IACzF;AAEA,QAAI,MAAM,YAAY,UAAU;AAC9B,YAAM,IAAI,MAAM,8BAA8B,QAAQ,SAAS,MAAM,QAAQ,EAAE;AAAA,IACjF;AAEA,8BAA0B,MAAM;AAAA,EAClC;AAEA,QAAM,OAAO,IAAI,WAAW,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC;AACjE,SAAO,IAAI,WAAW,MAAM,YAAY,UAAU,sBAAsB;AAC1E;","names":[]}
1
+ {"version":3,"sources":["../src/audio_frame.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { FfiClient, FfiHandle } from './ffi_client.js';\nimport type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';\nimport { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';\n\nexport class AudioFrame {\n data: Int16Array;\n sampleRate: number;\n channels: number;\n samplesPerChannel: number;\n\n private _userdata: Record<string, unknown>;\n\n // note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!\n // it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of\n // noise being added to the end.\n // it is recommended to use buffer.subarray instead.\n // XXX(nbsp): add this when writing proper docs\n constructor(\n data: Int16Array,\n sampleRate: number,\n channels: number,\n samplesPerChannel: number,\n userdata: Record<string, unknown> = {},\n ) {\n this.data = data;\n this.sampleRate = sampleRate;\n this.channels = channels;\n this.samplesPerChannel = samplesPerChannel;\n this._userdata = userdata;\n }\n\n static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {\n const data = new Int16Array(channels * samplesPerChannel);\n return new AudioFrame(data, sampleRate, channels, samplesPerChannel);\n }\n\n /** @internal */\n static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {\n const info = owned.info!;\n const len = info.numChannels! * info.samplesPerChannel! * 2; // c_int16\n const data = FfiClient.instance.copyBuffer(info.dataPtr!, len);\n new FfiHandle(owned.handle!.id!).dispose();\n return new AudioFrame(\n new Int16Array(data.buffer),\n info.sampleRate!,\n info.numChannels!,\n info.samplesPerChannel!,\n );\n }\n\n /** @internal */\n protoInfo(): AudioFrameBufferInfo {\n return new AudioFrameBufferInfo({\n dataPtr: FfiClient.instance.retrievePtr(new Uint8Array(this.data.buffer)),\n sampleRate: this.sampleRate,\n numChannels: this.channels,\n samplesPerChannel: this.samplesPerChannel,\n });\n }\n\n /** Returns the user data associated with the audio frame. */\n get userdata() {\n return this._userdata;\n }\n}\n\n/**\n * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.\n *\n * This function concatenates the audio data from multiple frames, ensuring that all frames have\n * the same sample rate and number of channels. It efficiently merges the data by preallocating the\n * necessary memory and copying the frame data without unnecessary reallocations.\n *\n * @param buffer - a single AudioFrame or list thereof\n */\nexport const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {\n if (!Array.isArray(buffer)) {\n return buffer;\n }\n buffer = buffer as AudioFrame[];\n\n if (buffer.length === 0) {\n throw new Error('buffer is empty');\n }\n\n const sampleRate = buffer[0]!.sampleRate;\n const channels = buffer[0]!.channels;\n\n let totalSamplesPerChannel = 0;\n for (const frame of buffer) {\n if (frame.sampleRate != sampleRate) {\n throw new Error(`sample rate mismatch: expected ${sampleRate}, got ${frame.sampleRate}`);\n }\n\n if (frame.channels != channels) {\n throw new Error(`channel mismatch: expected ${channels}, got ${frame.channels}`);\n }\n\n totalSamplesPerChannel += frame.samplesPerChannel;\n }\n\n const data = new Int16Array(buffer.map((x) => [...x.data]).flat());\n return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAA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AM,aAAa,OAAO,CAAC,EAAG;AAC9B,QAAM,WAAW,OAAO,CAAC,EAAG;AAE5B,MAAI,yBAAyB;AAC7B,aAAW,SAAS,QAAQ;AAC1B,QAAI,MAAM,cAAc,YAAY;AAClC,YAAM,IAAI,MAAM,kCAAkC,UAAU,SAAS,MAAM,UAAU,EAAE;AAAA,IACzF;AAEA,QAAI,MAAM,YAAY,UAAU;AAC9B,YAAM,IAAI,MAAM,8BAA8B,QAAQ,SAAS,MAAM,QAAQ,EAAE;AAAA,IACjF;AAEA,8BAA0B,MAAM;AAAA,EAClC;AAEA,QAAM,OAAO,IAAI,WAAW,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC;AACjE,SAAO,IAAI,WAAW,MAAM,YAAY,UAAU,sBAAsB;AAC1E;","names":[]}
@@ -10,12 +10,15 @@ declare class AudioFrame {
10
10
  sampleRate: number;
11
11
  channels: number;
12
12
  samplesPerChannel: number;
13
- constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number);
13
+ private _userdata;
14
+ constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number, userdata?: Record<string, unknown>);
14
15
  static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame;
15
16
  /** @internal */
16
17
  static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame;
17
18
  /** @internal */
18
19
  protoInfo(): AudioFrameBufferInfo;
20
+ /** Returns the user data associated with the audio frame. */
21
+ get userdata(): Record<string, unknown>;
19
22
  }
20
23
  /**
21
24
  * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.
@@ -10,12 +10,15 @@ declare class AudioFrame {
10
10
  sampleRate: number;
11
11
  channels: number;
12
12
  samplesPerChannel: number;
13
- constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number);
13
+ private _userdata;
14
+ constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number, userdata?: Record<string, unknown>);
14
15
  static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame;
15
16
  /** @internal */
16
17
  static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame;
17
18
  /** @internal */
18
19
  protoInfo(): AudioFrameBufferInfo;
20
+ /** Returns the user data associated with the audio frame. */
21
+ get userdata(): Record<string, unknown>;
19
22
  }
20
23
  /**
21
24
  * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.
@@ -1 +1 @@
1
- {"version":3,"file":"audio_frame.d.ts","sourceRoot":"","sources":["../src/audio_frame.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,qBAAa,UAAU;IACrB,IAAI,EAAE,UAAU,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;gBAOd,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM;IAO7F,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,GAAG,UAAU;IAK1F,gBAAgB;IAChB,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,qBAAqB,GAAG,UAAU;IAa9D,gBAAgB;IAChB,SAAS,IAAI,oBAAoB;CAQlC;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,UAAU,GAAG,UAAU,EAAE,KAAG,UA4BtE,CAAC"}
1
+ {"version":3,"file":"audio_frame.d.ts","sourceRoot":"","sources":["../src/audio_frame.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,qBAAa,UAAU;IACrB,IAAI,EAAE,UAAU,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAE1B,OAAO,CAAC,SAAS,CAA0B;gBAQzC,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,iBAAiB,EAAE,MAAM,EACzB,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;IASxC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,GAAG,UAAU;IAK1F,gBAAgB;IAChB,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,qBAAqB,GAAG,UAAU;IAa9D,gBAAgB;IAChB,SAAS,IAAI,oBAAoB;IASjC,6DAA6D;IAC7D,IAAI,QAAQ,4BAEX;CACF;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,UAAU,GAAG,UAAU,EAAE,KAAG,UA4BtE,CAAC"}
@@ -6,11 +6,12 @@ class AudioFrame {
6
6
  // noise being added to the end.
7
7
  // it is recommended to use buffer.subarray instead.
8
8
  // XXX(nbsp): add this when writing proper docs
9
- constructor(data, sampleRate, channels, samplesPerChannel) {
9
+ constructor(data, sampleRate, channels, samplesPerChannel, userdata = {}) {
10
10
  this.data = data;
11
11
  this.sampleRate = sampleRate;
12
12
  this.channels = channels;
13
13
  this.samplesPerChannel = samplesPerChannel;
14
+ this._userdata = userdata;
14
15
  }
15
16
  static create(sampleRate, channels, samplesPerChannel) {
16
17
  const data = new Int16Array(channels * samplesPerChannel);
@@ -38,6 +39,10 @@ class AudioFrame {
38
39
  samplesPerChannel: this.samplesPerChannel
39
40
  });
40
41
  }
42
+ /** Returns the user data associated with the audio frame. */
43
+ get userdata() {
44
+ return this._userdata;
45
+ }
41
46
  }
42
47
  const combineAudioFrames = (buffer) => {
43
48
  if (!Array.isArray(buffer)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/audio_frame.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { FfiClient, FfiHandle } from './ffi_client.js';\nimport type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';\nimport { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';\n\nexport class AudioFrame {\n data: Int16Array;\n sampleRate: number;\n channels: number;\n samplesPerChannel: number;\n\n // note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!\n // it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of\n // noise being added to the end.\n // it is recommended to use buffer.subarray instead.\n // XXX(nbsp): add this when writing proper docs\n constructor(data: Int16Array, sampleRate: number, channels: number, samplesPerChannel: number) {\n this.data = data;\n this.sampleRate = sampleRate;\n this.channels = channels;\n this.samplesPerChannel = samplesPerChannel;\n }\n\n static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {\n const data = new Int16Array(channels * samplesPerChannel);\n return new AudioFrame(data, sampleRate, channels, samplesPerChannel);\n }\n\n /** @internal */\n static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {\n const info = owned.info!;\n const len = info.numChannels! * info.samplesPerChannel! * 2; // c_int16\n const data = FfiClient.instance.copyBuffer(info.dataPtr!, len);\n new FfiHandle(owned.handle!.id!).dispose();\n return new AudioFrame(\n new Int16Array(data.buffer),\n info.sampleRate!,\n info.numChannels!,\n info.samplesPerChannel!,\n );\n }\n\n /** @internal */\n protoInfo(): AudioFrameBufferInfo {\n return new AudioFrameBufferInfo({\n dataPtr: FfiClient.instance.retrievePtr(new Uint8Array(this.data.buffer)),\n sampleRate: this.sampleRate,\n numChannels: this.channels,\n samplesPerChannel: this.samplesPerChannel,\n });\n }\n}\n\n/**\n * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.\n *\n * This function concatenates the audio data from multiple frames, ensuring that all frames have\n * the same sample rate and number of channels. It efficiently merges the data by preallocating the\n * necessary memory and copying the frame data without unnecessary reallocations.\n *\n * @param buffer - a single AudioFrame or list thereof\n */\nexport const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {\n if (!Array.isArray(buffer)) {\n return buffer;\n }\n buffer = buffer as AudioFrame[];\n\n if (buffer.length === 0) {\n throw new Error('buffer is empty');\n }\n\n const sampleRate = buffer[0]!.sampleRate;\n const channels = buffer[0]!.channels;\n\n let totalSamplesPerChannel = 0;\n for (const frame of buffer) {\n if (frame.sampleRate != sampleRate) {\n throw new Error(`sample rate mismatch: expected ${sampleRate}, got ${frame.sampleRate}`);\n }\n\n if (frame.channels != channels) {\n throw new Error(`channel mismatch: expected ${channels}, got ${frame.channels}`);\n }\n\n totalSamplesPerChannel += frame.samplesPerChannel;\n }\n\n const data = new Int16Array(buffer.map((x) => [...x.data]).flat());\n return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel);\n};\n"],"mappings":"AAGA,SAAS,WAAW,iBAAiB;AAErC,SAAS,4BAA4B;AAE9B,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWtB,YAAY,MAAkB,YAAoB,UAAkB,mBAA2B;AAC7F,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,WAAW;AAChB,SAAK,oBAAoB;AAAA,EAC3B;AAAA,EAEA,OAAO,OAAO,YAAoB,UAAkB,mBAAuC;AACzF,UAAM,OAAO,IAAI,WAAW,WAAW,iBAAiB;AACxD,WAAO,IAAI,WAAW,MAAM,YAAY,UAAU,iBAAiB;AAAA,EACrE;AAAA;AAAA,EAGA,OAAO,cAAc,OAA0C;AAC7D,UAAM,OAAO,MAAM;AACnB,UAAM,MAAM,KAAK,cAAe,KAAK,oBAAqB;AAC1D,UAAM,OAAO,UAAU,SAAS,WAAW,KAAK,SAAU,GA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License-Identifier: Apache-2.0\nimport { FfiClient, FfiHandle } from './ffi_client.js';\nimport type { OwnedAudioFrameBuffer } from './proto/audio_frame_pb.js';\nimport { AudioFrameBufferInfo } from './proto/audio_frame_pb.js';\n\nexport class AudioFrame {\n data: Int16Array;\n sampleRate: number;\n channels: number;\n samplesPerChannel: number;\n\n private _userdata: Record<string, unknown>;\n\n // note: if converting from Uint8Array to Int16Array, *do not* use buffer.slice!\n // it is marked unstable by Node and can cause undefined behaviour, such as massive chunks of\n // noise being added to the end.\n // it is recommended to use buffer.subarray instead.\n // XXX(nbsp): add this when writing proper docs\n constructor(\n data: Int16Array,\n sampleRate: number,\n channels: number,\n samplesPerChannel: number,\n userdata: Record<string, unknown> = {},\n ) {\n this.data = data;\n this.sampleRate = sampleRate;\n this.channels = channels;\n this.samplesPerChannel = samplesPerChannel;\n this._userdata = userdata;\n }\n\n static create(sampleRate: number, channels: number, samplesPerChannel: number): AudioFrame {\n const data = new Int16Array(channels * samplesPerChannel);\n return new AudioFrame(data, sampleRate, channels, samplesPerChannel);\n }\n\n /** @internal */\n static fromOwnedInfo(owned: OwnedAudioFrameBuffer): AudioFrame {\n const info = owned.info!;\n const len = info.numChannels! * info.samplesPerChannel! * 2; // c_int16\n const data = FfiClient.instance.copyBuffer(info.dataPtr!, len);\n new FfiHandle(owned.handle!.id!).dispose();\n return new AudioFrame(\n new Int16Array(data.buffer),\n info.sampleRate!,\n info.numChannels!,\n info.samplesPerChannel!,\n );\n }\n\n /** @internal */\n protoInfo(): AudioFrameBufferInfo {\n return new AudioFrameBufferInfo({\n dataPtr: FfiClient.instance.retrievePtr(new Uint8Array(this.data.buffer)),\n sampleRate: this.sampleRate,\n numChannels: this.channels,\n samplesPerChannel: this.samplesPerChannel,\n });\n }\n\n /** Returns the user data associated with the audio frame. */\n get userdata() {\n return this._userdata;\n }\n}\n\n/**\n * Combines one or more `rtc.AudioFrame` objects into a single `rtc.AudioFrame`.\n *\n * This function concatenates the audio data from multiple frames, ensuring that all frames have\n * the same sample rate and number of channels. It efficiently merges the data by preallocating the\n * necessary memory and copying the frame data without unnecessary reallocations.\n *\n * @param buffer - a single AudioFrame or list thereof\n */\nexport const combineAudioFrames = (buffer: AudioFrame | AudioFrame[]): AudioFrame => {\n if (!Array.isArray(buffer)) {\n return buffer;\n }\n buffer = buffer as AudioFrame[];\n\n if (buffer.length === 0) {\n throw new Error('buffer is empty');\n }\n\n const sampleRate = buffer[0]!.sampleRate;\n const channels = buffer[0]!.channels;\n\n let totalSamplesPerChannel = 0;\n for (const frame of buffer) {\n if (frame.sampleRate != sampleRate) {\n throw new Error(`sample rate mismatch: expected ${sampleRate}, got ${frame.sampleRate}`);\n }\n\n if (frame.channels != channels) {\n throw new Error(`channel mismatch: expected ${channels}, got ${frame.channels}`);\n }\n\n totalSamplesPerChannel += frame.samplesPerChannel;\n }\n\n const data = new Int16Array(buffer.map((x) => [...x.data]).flat());\n return new AudioFrame(data, sampleRate, channels, totalSamplesPerChannel);\n};\n"],"mappings":"AAGA,SAAS,WAAW,iBAAiB;AAErC,SAAS,4BAA4B;AAE9B,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAatB,YACE,MACA,YACA,UACA,mBACA,WAAoC,CAAC,GACrC;AACA,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,WAAW;AAChB,SAAK,oBAAoB;AACzB,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,OAAO,OAAO,YAAoB,UAAkB,mBAAuC;AACzF,UAAM,OAAO,IAAI,WAAW,WAAW,iBAAiB;AACxD,WAAO,IAAI,WAAW,MAAM,YAAY,UAAU,iBAAiB;AAAA,EACrE;AAAA;AAAA,EAGA,OAAO,cAAc,OAA0C;AAC7D,UAAM,OAAO,MAAM;AACnB,UAAM,MAAM,KAAK,cAAe,KAAK,oBAAqB;AAC1D,UAAM,OAAO,UAAU,SAAS,WAAW,KAAK,SAAU,GAAG;AAC7D,QAAI,UAAU,MAAM,OAAQ,EAAG,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  var import_async_queue = require("./async_queue.cjs");
26
26
  var import_audio_frame = require("./audio_frame.cjs");
27
+ var import_log = require("./log.cjs");
27
28
  var import_async_queue2 = require("./async_queue.cjs");
28
29
  class AudioMixer {
29
30
  /**
@@ -151,7 +152,7 @@ class AudioMixer {
151
152
  const removals = [];
152
153
  for (const result of results) {
153
154
  if (result.status !== "fulfilled") {
154
- console.warn("AudioMixer: Stream contribution failed:", result.reason);
155
+ import_log.log.warn("AudioMixer: Stream contribution failed:", result.reason);
155
156
  continue;
156
157
  }
157
158
  const contrib = result.value;
@@ -167,6 +168,9 @@ class AudioMixer {
167
168
  for (const stream of removals) {
168
169
  this.removeStream(stream);
169
170
  }
171
+ if (!this.ending && removals.length > 0 && this.streams.size === 0) {
172
+ this.ending = true;
173
+ }
170
174
  if (!anyData) {
171
175
  await this.sleep(1);
172
176
  continue;
@@ -247,21 +251,24 @@ class AudioMixer {
247
251
  return new Int16Array(this.chunkSize * this.numChannels);
248
252
  }
249
253
  const length = this.chunkSize * this.numChannels;
250
- const mixed = new Int16Array(length);
254
+ const acc = new Int32Array(length);
251
255
  for (const contrib of contributions) {
252
256
  for (let i = 0; i < length; i++) {
253
257
  const val = contrib[i];
254
258
  if (val !== void 0) {
255
- mixed[i] = (mixed[i] ?? 0) + val;
259
+ acc[i] = (acc[i] ?? 0) + val;
256
260
  }
257
261
  }
258
262
  }
263
+ const mixed = new Int16Array(length);
259
264
  for (let i = 0; i < length; i++) {
260
- const val = mixed[i] ?? 0;
265
+ const val = acc[i] ?? 0;
261
266
  if (val > 32767) {
262
267
  mixed[i] = 32767;
263
268
  } else if (val < -32768) {
264
269
  mixed[i] = -32768;
270
+ } else {
271
+ mixed[i] = val;
265
272
  }
266
273
  }
267
274
  return mixed;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/audio_mixer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncQueue } from './async_queue.js';\nimport { AudioFrame } from './audio_frame.js';\n\n// Re-export AsyncQueue for backward compatibility\nexport { AsyncQueue } from './async_queue.js';\n\n// Define types for async iteration (since lib: es2015 doesn't include them)\ntype AudioStream = {\n [Symbol.asyncIterator](): {\n next(): Promise<IteratorResult<AudioFrame>>;\n };\n};\n\ninterface Contribution {\n stream: AudioStream;\n data: Int16Array;\n buffer: Int16Array;\n hadData: boolean;\n exhausted: boolean;\n}\n\nexport interface AudioMixerOptions {\n /**\n * The size of the audio block (in samples) for mixing.\n * If not provided, defaults to sampleRate / 10 (100ms).\n */\n blocksize?: number;\n\n /**\n * The maximum wait time in milliseconds for each stream to provide\n * audio data before timing out. Defaults to 100 ms.\n */\n streamTimeoutMs?: number;\n\n /**\n * The maximum number of mixed frames to store in the output queue.\n * Defaults to 100.\n */\n capacity?: number;\n}\n\n/**\n * AudioMixer combines multiple async audio streams into a single output stream.\n *\n * The mixer accepts multiple async audio streams and mixes them into a single output stream.\n * Each output frame is generated with a fixed chunk size determined by the blocksize (in samples).\n * If blocksize is not provided (or 0), it defaults to 100ms.\n *\n * Each input stream is processed in parallel, accumulating audio data until at least one chunk\n * of samples is available. If an input stream does not provide data within the specified timeout,\n * a warning is logged. The mixer can be closed immediately\n * (dropping unconsumed frames) or allowed to flush remaining data using endInput().\n *\n * @example\n * ```typescript\n * const mixer = new AudioMixer(48000, 2);\n * mixer.addStream(stream1);\n * mixer.addStream(stream2);\n *\n * for await (const frame of mixer) {\n * // Process mixed audio frame\n * }\n * ```\n */\nexport class AudioMixer {\n private streams: Set<AudioStream>;\n private buffers: Map<AudioStream, Int16Array>;\n private streamIterators: Map<AudioStream, { next(): Promise<IteratorResult<AudioFrame>> }>;\n private sampleRate: number;\n private numChannels: number;\n private chunkSize: number;\n private streamTimeoutMs: number;\n private queue: AsyncQueue<AudioFrame>;\n private streamSignal: AsyncQueue<void>; // Signals when streams are added\n private ending: boolean;\n private mixerTask?: Promise<void>;\n private closed: boolean;\n\n /**\n * Initialize the AudioMixer.\n *\n * @param sampleRate - The audio sample rate in Hz.\n * @param numChannels - The number of audio channels.\n * @param options - Optional configuration for the mixer.\n */\n constructor(sampleRate: number, numChannels: number, options: AudioMixerOptions = {}) {\n this.streams = new Set();\n this.buffers = new Map();\n this.streamIterators = new Map();\n this.sampleRate = sampleRate;\n this.numChannels = numChannels;\n this.chunkSize =\n options.blocksize && options.blocksize > 0 ? options.blocksize : Math.floor(sampleRate / 10);\n this.streamTimeoutMs = options.streamTimeoutMs ?? 100;\n this.queue = new AsyncQueue<AudioFrame>(options.capacity ?? 100);\n this.streamSignal = new AsyncQueue<void>(1); // there should only be one mixer\n this.ending = false;\n this.closed = false;\n\n // Start the mixer task\n this.mixerTask = this.mixer();\n }\n\n /**\n * Add an audio stream to the mixer.\n *\n * The stream is added to the internal set of streams and an empty buffer is initialized for it,\n * if not already present.\n *\n * @param stream - An async iterable that produces AudioFrame objects.\n * @throws Error if the mixer has been closed.\n */\n addStream(stream: AudioStream): void {\n if (this.ending) {\n throw new Error('Cannot add stream after mixer has been closed');\n }\n\n this.streams.add(stream);\n if (!this.buffers.has(stream)) {\n this.buffers.set(stream, new Int16Array(0));\n }\n\n // Signal that a stream was added (non-blocking)\n this.streamSignal.put(undefined).catch(() => {\n // Ignore errors if signal queue is closed\n });\n }\n\n /**\n * Remove an audio stream from the mixer.\n *\n * This method removes the specified stream and its associated buffer from the mixer.\n *\n * @param stream - The audio stream to remove.\n */\n removeStream(stream: AudioStream): void {\n this.streams.delete(stream);\n this.buffers.delete(stream);\n this.streamIterators.delete(stream);\n }\n\n /**\n * Returns an async iterator for the mixed audio frames.\n */\n [Symbol.asyncIterator]() {\n return {\n next: async (): Promise<IteratorResult<AudioFrame>> => {\n const frame = await this.getNextFrame();\n if (frame === null) {\n return { done: true, value: undefined };\n }\n return { done: false, value: frame };\n },\n };\n }\n\n /**\n * Immediately stop mixing and close the mixer.\n *\n * This stops the mixing task, and any unconsumed output in the queue may be dropped.\n */\n async aclose(): Promise<void> {\n if (this.closed) {\n return;\n }\n this.closed = true;\n this.ending = true;\n\n // Close both queues to wake up any waiting operations\n this.streamSignal.close();\n this.queue.close();\n\n await this.mixerTask;\n }\n\n /**\n * Signal that no more streams will be added.\n *\n * This method marks the mixer as closed so that it flushes any remaining buffered output before ending.\n * Note that existing streams will still be processed until exhausted.\n */\n endInput(): void {\n this.ending = true;\n }\n\n private async getNextFrame(): Promise<AudioFrame | null> {\n while (true) {\n // Try to get an item from the queue (non-blocking)\n const frame = this.queue.get();\n\n if (frame !== undefined) {\n return frame;\n }\n\n // Check if mixer is closed or ending\n if (this.queue.closed || (this.ending && this.streams.size === 0)) {\n return null;\n }\n\n // Queue is empty but mixer is still running - wait for an item to be added\n await this.queue.waitForItem();\n }\n }\n\n private async mixer(): Promise<void> {\n // Main mixing loop that continuously processes streams and produces output frames\n while (true) {\n // If we're in ending mode and there are no more streams, exit\n if (this.ending && this.streams.size === 0) {\n break;\n }\n\n if (this.streams.size === 0) {\n // Wait for a stream to be added (signal queue will have an item)\n await this.streamSignal.waitForItem();\n // Consume the signal\n this.streamSignal.get();\n continue;\n }\n\n // Process all streams in parallel\n const streamArray = Array.from(this.streams);\n const promises = streamArray.map((stream) => this.getContribution(stream));\n const results = await Promise.all(\n promises.map((p) =>\n p\n .then((value) => ({ status: 'fulfilled' as const, value }))\n .catch((reason) => ({ status: 'rejected' as const, reason })),\n ),\n );\n\n const contributions: Int16Array[] = [];\n let anyData = false;\n const removals: AudioStream[] = [];\n\n for (const result of results) {\n if (result.status !== 'fulfilled') {\n console.warn('AudioMixer: Stream contribution failed:', result.reason);\n continue;\n }\n\n const contrib = result.value;\n contributions.push(contrib.data);\n this.buffers.set(contrib.stream, contrib.buffer);\n\n if (contrib.hadData) {\n anyData = true;\n }\n\n // Mark exhausted streams with no remaining buffer for removal\n if (contrib.exhausted && contrib.buffer.length === 0) {\n removals.push(contrib.stream);\n }\n }\n\n // Remove exhausted streams\n for (const stream of removals) {\n this.removeStream(stream);\n }\n\n if (!anyData) {\n // No data available from any stream, wait briefly before trying again\n await this.sleep(1);\n continue;\n }\n\n // Mix the audio data\n const mixed = this.mixAudio(contributions);\n const frame = new AudioFrame(mixed, this.sampleRate, this.numChannels, this.chunkSize);\n\n if (this.closed) {\n break;\n }\n\n try {\n // Add mixed frame to output queue\n await this.queue.put(frame);\n } catch {\n // Queue closed while trying to add frame\n break;\n }\n }\n\n // Close the queue to signal end of stream\n this.queue.close();\n }\n\n private async getContribution(stream: AudioStream): Promise<Contribution> {\n let buf = this.buffers.get(stream) ?? new Int16Array(0);\n const initialBufferLength = buf.length;\n let exhausted = false;\n let receivedDataInThisCall = false;\n\n // Get or create iterator for this stream\n let iterator = this.streamIterators.get(stream);\n if (!iterator) {\n iterator = stream[Symbol.asyncIterator]();\n this.streamIterators.set(stream, iterator);\n }\n\n // Accumulate data until we have at least chunkSize samples\n while (buf.length < this.chunkSize * this.numChannels && !exhausted && !this.closed) {\n try {\n const result = await Promise.race([iterator.next(), this.timeout(this.streamTimeoutMs)]);\n\n if (result === 'timeout') {\n console.warn(`AudioMixer: stream timeout after ${this.streamTimeoutMs}ms`);\n break;\n }\n\n if (result.done) {\n exhausted = true;\n break;\n }\n\n const frame = result.value;\n const newData = frame.data;\n\n // Mark that we received data in this call\n receivedDataInThisCall = true;\n\n // Concatenate buffers\n if (buf.length === 0) {\n buf = newData;\n } else {\n const combined = new Int16Array(buf.length + newData.length);\n combined.set(buf);\n combined.set(newData, buf.length);\n buf = combined;\n }\n } catch (error) {\n console.error(`AudioMixer: Error reading from stream:`, error);\n exhausted = true;\n break;\n }\n }\n\n // Extract contribution and update buffer\n let contrib: Int16Array;\n const samplesNeeded = this.chunkSize * this.numChannels;\n\n if (buf.length >= samplesNeeded) {\n // Extract the needed samples and keep the remainder in the buffer\n contrib = buf.subarray(0, samplesNeeded);\n buf = buf.subarray(samplesNeeded);\n } else {\n // Pad with zeros if we don't have enough data\n const padded = new Int16Array(samplesNeeded);\n padded.set(buf);\n contrib = padded;\n buf = new Int16Array(0);\n }\n\n // hadData means: we had data at start OR we received data during this call OR we have data remaining\n const hadData = initialBufferLength > 0 || receivedDataInThisCall || buf.length > 0;\n\n return {\n stream,\n data: contrib,\n buffer: buf,\n hadData,\n exhausted,\n };\n }\n\n private mixAudio(contributions: Int16Array[]): Int16Array {\n if (contributions.length === 0) {\n return new Int16Array(this.chunkSize * this.numChannels);\n }\n\n const length = this.chunkSize * this.numChannels;\n const mixed = new Int16Array(length);\n\n // Sum all contributions\n for (const contrib of contributions) {\n for (let i = 0; i < length; i++) {\n const val = contrib[i];\n if (val !== undefined) {\n mixed[i] = (mixed[i] ?? 0) + val;\n }\n }\n }\n\n // Clip to Int16 range\n for (let i = 0; i < length; i++) {\n const val = mixed[i] ?? 0;\n if (val > 32767) {\n mixed[i] = 32767;\n } else if (val < -32768) {\n mixed[i] = -32768;\n }\n }\n\n return mixed;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n private timeout(ms: number): Promise<'timeout'> {\n return new Promise((resolve) => setTimeout(() => resolve('timeout'), ms));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAA2B;AAC3B,yBAA2B;AAG3B,IAAAA,sBAA2B;AA4DpB,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBtB,YAAY,YAAoB,aAAqB,UAA6B,CAAC,GAAG;AACpF,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,kBAAkB,oBAAI,IAAI;AAC/B,SAAK,aAAa;AAClB,SAAK,cAAc;AACnB,SAAK,YACH,QAAQ,aAAa,QAAQ,YAAY,IAAI,QAAQ,YAAY,KAAK,MAAM,aAAa,EAAE;AAC7F,SAAK,kBAAkB,QAAQ,mBAAmB;AAClD,SAAK,QAAQ,IAAI,8BAAuB,QAAQ,YAAY,GAA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IAAI;AAAA,MACb,WAAW,MAAM,QAAQ;AACvB,cAAM,CAAC,IAAI;AAAA,MACb;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AAAA,EAEQ,QAAQ,IAAgC;AAC9C,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,MAAM,QAAQ,SAAS,GAAG,EAAE,CAAC;AAAA,EAC1E;AACF;","names":["import_async_queue"]}
1
+ {"version":3,"sources":["../src/audio_mixer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncQueue } from './async_queue.js';\nimport { AudioFrame } from './audio_frame.js';\nimport { log } from './log.js';\n\n// Re-export AsyncQueue for backward compatibility\nexport { AsyncQueue } from './async_queue.js';\n\n// Define types for async iteration (since lib: es2015 doesn't include them)\ntype AudioStream = {\n [Symbol.asyncIterator](): {\n next(): Promise<IteratorResult<AudioFrame>>;\n };\n};\n\ninterface Contribution {\n stream: AudioStream;\n data: Int16Array;\n buffer: Int16Array;\n hadData: boolean;\n exhausted: boolean;\n}\n\nexport interface AudioMixerOptions {\n /**\n * The size of the audio block (in samples) for mixing.\n * If not provided, defaults to sampleRate / 10 (100ms).\n */\n blocksize?: number;\n\n /**\n * The maximum wait time in milliseconds for each stream to provide\n * audio data before timing out. Defaults to 100 ms.\n */\n streamTimeoutMs?: number;\n\n /**\n * The maximum number of mixed frames to store in the output queue.\n * Defaults to 100.\n */\n capacity?: number;\n}\n\n/**\n * AudioMixer combines multiple async audio streams into a single output stream.\n *\n * The mixer accepts multiple async audio streams and mixes them into a single output stream.\n * Each output frame is generated with a fixed chunk size determined by the blocksize (in samples).\n * If blocksize is not provided (or 0), it defaults to 100ms.\n *\n * Each input stream is processed in parallel, accumulating audio data until at least one chunk\n * of samples is available. If an input stream does not provide data within the specified timeout,\n * a warning is logged. The mixer can be closed immediately\n * (dropping unconsumed frames) or allowed to flush remaining data using endInput().\n *\n * @example\n * ```typescript\n * const mixer = new AudioMixer(48000, 2);\n * mixer.addStream(stream1);\n * mixer.addStream(stream2);\n *\n * for await (const frame of mixer) {\n * // Process mixed audio frame\n * }\n * ```\n */\nexport class AudioMixer {\n private streams: Set<AudioStream>;\n private buffers: Map<AudioStream, Int16Array>;\n private streamIterators: Map<AudioStream, { next(): Promise<IteratorResult<AudioFrame>> }>;\n private sampleRate: number;\n private numChannels: number;\n private chunkSize: number;\n private streamTimeoutMs: number;\n private queue: AsyncQueue<AudioFrame>;\n private streamSignal: AsyncQueue<void>; // Signals when streams are added\n private ending: boolean;\n private mixerTask?: Promise<void>;\n private closed: boolean;\n\n /**\n * Initialize the AudioMixer.\n *\n * @param sampleRate - The audio sample rate in Hz.\n * @param numChannels - The number of audio channels.\n * @param options - Optional configuration for the mixer.\n */\n constructor(sampleRate: number, numChannels: number, options: AudioMixerOptions = {}) {\n this.streams = new Set();\n this.buffers = new Map();\n this.streamIterators = new Map();\n this.sampleRate = sampleRate;\n this.numChannels = numChannels;\n this.chunkSize =\n options.blocksize && options.blocksize > 0 ? options.blocksize : Math.floor(sampleRate / 10);\n this.streamTimeoutMs = options.streamTimeoutMs ?? 100;\n this.queue = new AsyncQueue<AudioFrame>(options.capacity ?? 100);\n this.streamSignal = new AsyncQueue<void>(1); // there should only be one mixer\n this.ending = false;\n this.closed = false;\n\n // Start the mixer task\n this.mixerTask = this.mixer();\n }\n\n /**\n * Add an audio stream to the mixer.\n *\n * The stream is added to the internal set of streams and an empty buffer is initialized for it,\n * if not already present.\n *\n * @param stream - An async iterable that produces AudioFrame objects.\n * @throws Error if the mixer has been closed.\n */\n addStream(stream: AudioStream): void {\n if (this.ending) {\n throw new Error('Cannot add stream after mixer has been closed');\n }\n\n this.streams.add(stream);\n if (!this.buffers.has(stream)) {\n this.buffers.set(stream, new Int16Array(0));\n }\n\n // Signal that a stream was added (non-blocking)\n this.streamSignal.put(undefined).catch(() => {\n // Ignore errors if signal queue is closed\n });\n }\n\n /**\n * Remove an audio stream from the mixer.\n *\n * This method removes the specified stream and its associated buffer from the mixer.\n *\n * @param stream - The audio stream to remove.\n */\n removeStream(stream: AudioStream): void {\n this.streams.delete(stream);\n this.buffers.delete(stream);\n this.streamIterators.delete(stream);\n }\n\n /**\n * Returns an async iterator for the mixed audio frames.\n */\n [Symbol.asyncIterator]() {\n return {\n next: async (): Promise<IteratorResult<AudioFrame>> => {\n const frame = await this.getNextFrame();\n if (frame === null) {\n return { done: true, value: undefined };\n }\n return { done: false, value: frame };\n },\n };\n }\n\n /**\n * Immediately stop mixing and close the mixer.\n *\n * This stops the mixing task, and any unconsumed output in the queue may be dropped.\n */\n async aclose(): Promise<void> {\n if (this.closed) {\n return;\n }\n this.closed = true;\n this.ending = true;\n\n // Close both queues to wake up any waiting operations\n this.streamSignal.close();\n this.queue.close();\n\n await this.mixerTask;\n }\n\n /**\n * Signal that no more streams will be added.\n *\n * This method marks the mixer as closed so that it flushes any remaining buffered output before ending.\n * Note that existing streams will still be processed until exhausted.\n */\n endInput(): void {\n this.ending = true;\n }\n\n private async getNextFrame(): Promise<AudioFrame | null> {\n while (true) {\n // Try to get an item from the queue (non-blocking)\n const frame = this.queue.get();\n\n if (frame !== undefined) {\n return frame;\n }\n\n // Check if mixer is closed or ending\n if (this.queue.closed || (this.ending && this.streams.size === 0)) {\n return null;\n }\n\n // Queue is empty but mixer is still running - wait for an item to be added\n await this.queue.waitForItem();\n }\n }\n\n private async mixer(): Promise<void> {\n // Main mixing loop that continuously processes streams and produces output frames\n while (true) {\n // If we're in ending mode and there are no more streams, exit\n if (this.ending && this.streams.size === 0) {\n break;\n }\n\n if (this.streams.size === 0) {\n // Wait for a stream to be added (signal queue will have an item)\n await this.streamSignal.waitForItem();\n // Consume the signal\n this.streamSignal.get();\n continue;\n }\n\n // Process all streams in parallel\n const streamArray = Array.from(this.streams);\n const promises = streamArray.map((stream) => this.getContribution(stream));\n const results = await Promise.all(\n promises.map((p) =>\n p\n .then((value) => ({ status: 'fulfilled' as const, value }))\n .catch((reason) => ({ status: 'rejected' as const, reason })),\n ),\n );\n\n const contributions: Int16Array[] = [];\n let anyData = false;\n const removals: AudioStream[] = [];\n\n for (const result of results) {\n if (result.status !== 'fulfilled') {\n log.warn('AudioMixer: Stream contribution failed:', result.reason);\n continue;\n }\n\n const contrib = result.value;\n contributions.push(contrib.data);\n this.buffers.set(contrib.stream, contrib.buffer);\n\n if (contrib.hadData) {\n anyData = true;\n }\n\n // Mark exhausted streams with no remaining buffer for removal\n if (contrib.exhausted && contrib.buffer.length === 0) {\n removals.push(contrib.stream);\n }\n }\n\n // Remove exhausted streams\n for (const stream of removals) {\n this.removeStream(stream);\n }\n\n // If all streams are exhausted, end the mixer automatically.\n // This keeps `for await...of` consumers from hanging indefinitely when inputs complete.\n if (!this.ending && removals.length > 0 && this.streams.size === 0) {\n this.ending = true;\n }\n\n if (!anyData) {\n // No data available from any stream, wait briefly before trying again\n await this.sleep(1);\n continue;\n }\n\n // Mix the audio data\n const mixed = this.mixAudio(contributions);\n const frame = new AudioFrame(mixed, this.sampleRate, this.numChannels, this.chunkSize);\n\n if (this.closed) {\n break;\n }\n\n try {\n // Add mixed frame to output queue\n await this.queue.put(frame);\n } catch {\n // Queue closed while trying to add frame\n break;\n }\n }\n\n // Close the queue to signal end of stream\n this.queue.close();\n }\n\n private async getContribution(stream: AudioStream): Promise<Contribution> {\n let buf = this.buffers.get(stream) ?? new Int16Array(0);\n const initialBufferLength = buf.length;\n let exhausted = false;\n let receivedDataInThisCall = false;\n\n // Get or create iterator for this stream\n let iterator = this.streamIterators.get(stream);\n if (!iterator) {\n iterator = stream[Symbol.asyncIterator]();\n this.streamIterators.set(stream, iterator);\n }\n\n // Accumulate data until we have at least chunkSize samples\n while (buf.length < this.chunkSize * this.numChannels && !exhausted && !this.closed) {\n try {\n const result = await Promise.race([iterator.next(), this.timeout(this.streamTimeoutMs)]);\n\n if (result === 'timeout') {\n console.warn(`AudioMixer: stream timeout after ${this.streamTimeoutMs}ms`);\n break;\n }\n\n if (result.done) {\n exhausted = true;\n break;\n }\n\n const frame = result.value;\n const newData = frame.data;\n\n // Mark that we received data in this call\n receivedDataInThisCall = true;\n\n // Concatenate buffers\n if (buf.length === 0) {\n buf = newData;\n } else {\n const combined = new Int16Array(buf.length + newData.length);\n combined.set(buf);\n combined.set(newData, buf.length);\n buf = combined;\n }\n } catch (error) {\n console.error(`AudioMixer: Error reading from stream:`, error);\n exhausted = true;\n break;\n }\n }\n\n // Extract contribution and update buffer\n let contrib: Int16Array;\n const samplesNeeded = this.chunkSize * this.numChannels;\n\n if (buf.length >= samplesNeeded) {\n // Extract the needed samples and keep the remainder in the buffer\n contrib = buf.subarray(0, samplesNeeded);\n buf = buf.subarray(samplesNeeded);\n } else {\n // Pad with zeros if we don't have enough data\n const padded = new Int16Array(samplesNeeded);\n padded.set(buf);\n contrib = padded;\n buf = new Int16Array(0);\n }\n\n // hadData means: we had data at start OR we received data during this call OR we have data remaining\n const hadData = initialBufferLength > 0 || receivedDataInThisCall || buf.length > 0;\n\n return {\n stream,\n data: contrib,\n buffer: buf,\n hadData,\n exhausted,\n };\n }\n\n private mixAudio(contributions: Int16Array[]): Int16Array {\n if (contributions.length === 0) {\n return new Int16Array(this.chunkSize * this.numChannels);\n }\n\n const length = this.chunkSize * this.numChannels;\n // Use a wider accumulator to avoid int16 overflow while summing.\n const acc = new Int32Array(length);\n\n // Sum all contributions\n for (const contrib of contributions) {\n for (let i = 0; i < length; i++) {\n const val = contrib[i];\n if (val !== undefined) {\n acc[i] = (acc[i] ?? 0) + val;\n }\n }\n }\n\n // Clip to Int16 range\n const mixed = new Int16Array(length);\n for (let i = 0; i < length; i++) {\n const val = acc[i] ?? 0;\n if (val > 32767) {\n mixed[i] = 32767;\n } else if (val < -32768) {\n mixed[i] = -32768;\n } else {\n mixed[i] = val;\n }\n }\n\n return mixed;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n private timeout(ms: number): Promise<'timeout'> {\n return new Promise((resolve) => setTimeout(() => resolve('timeout'), ms));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAA2B;AAC3B,yBAA2B;AAC3B,iBAAoB;AAGpB,IAAAA,sBAA2B;AA4DpB,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBtB,YAAY,YAAoB,aAAqB,UAA6B,CAAC,GAAG;AACpF,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,kBAAkB,oBAAI,IAAI;AAC/B,SAAK,aAAa;AAClB,SAAK,cAAc;AACnB,SAAK,YACH,QAAQ,aAAa,QAAQ,YAAY,IAAI,QAAQ,YAAY,KAAK,MAAM,aAAa,EAAE;AAC7F,SAAK,kBAAkB,QAAQ,mBAAmB;AAClD,SAAK,QAAQ,IAAI,8BAAuB,QAAQ,YAAY,GAAG;AAC/D,SAAK,eAAe,IAAI,8BAAiB,CAAC;AAC1C,SAAK,SAAS;AACd,SAAK,SAAS;AAGd,SAAK,YAAY,KAAK,MAAM;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,UAAU,QAA2B;AACnC,QAAI,KAAK,QAAQ;AACf,YAAM,IAAI,MAAM,+CAA+C;AAAA,IACjE;AAEA,SAAK,QAAQ,IAAI,MAAM;AACvB,QAAI,CAAC,KAAK,QAAQ,IAAI,MAAM,GAAG;AAC7B,WAAK,QAAQ,IAAI,QAAQ,IAAI,WAAW,CAAC,CAAC;AAAA,IAC5C;AAGA,SAAK,aAAa,IAAI,MAAS,EAAE,MAAM,MAAM;AAAA,IAE7C,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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MAAM,CAAU;IAExB;;;;;;OAMG;gBACS,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB;IAkBpF;;;;;;;;OAQG;IACH,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAgBpC;;;;;;OAMG;IACH,YAAY,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAMvC;;OAEG;IACH,CAAC,MAAM,CAAC,aAAa,CAAC;oBAEF,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;;IAUvD;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAc7B;;;;;OAKG;IACH,QAAQ,IAAI,IAAI;YAIF,YAAY;YAmBZ,KAAK;YAyFL,eAAe;IA8E7B,OAAO,CAAC,QAAQ;IAmChB,OAAO,CAAC,KAAK;IAIb,OAAO,CAAC,OAAO;CAGhB"}
@@ -1,5 +1,6 @@
1
1
  import { AsyncQueue } from "./async_queue.js";
2
2
  import { AudioFrame } from "./audio_frame.js";
3
+ import { log } from "./log.js";
3
4
  import { AsyncQueue as AsyncQueue2 } from "./async_queue.js";
4
5
  class AudioMixer {
5
6
  /**
@@ -127,7 +128,7 @@ class AudioMixer {
127
128
  const removals = [];
128
129
  for (const result of results) {
129
130
  if (result.status !== "fulfilled") {
130
- console.warn("AudioMixer: Stream contribution failed:", result.reason);
131
+ log.warn("AudioMixer: Stream contribution failed:", result.reason);
131
132
  continue;
132
133
  }
133
134
  const contrib = result.value;
@@ -143,6 +144,9 @@ class AudioMixer {
143
144
  for (const stream of removals) {
144
145
  this.removeStream(stream);
145
146
  }
147
+ if (!this.ending && removals.length > 0 && this.streams.size === 0) {
148
+ this.ending = true;
149
+ }
146
150
  if (!anyData) {
147
151
  await this.sleep(1);
148
152
  continue;
@@ -223,21 +227,24 @@ class AudioMixer {
223
227
  return new Int16Array(this.chunkSize * this.numChannels);
224
228
  }
225
229
  const length = this.chunkSize * this.numChannels;
226
- const mixed = new Int16Array(length);
230
+ const acc = new Int32Array(length);
227
231
  for (const contrib of contributions) {
228
232
  for (let i = 0; i < length; i++) {
229
233
  const val = contrib[i];
230
234
  if (val !== void 0) {
231
- mixed[i] = (mixed[i] ?? 0) + val;
235
+ acc[i] = (acc[i] ?? 0) + val;
232
236
  }
233
237
  }
234
238
  }
239
+ const mixed = new Int16Array(length);
235
240
  for (let i = 0; i < length; i++) {
236
- const val = mixed[i] ?? 0;
241
+ const val = acc[i] ?? 0;
237
242
  if (val > 32767) {
238
243
  mixed[i] = 32767;
239
244
  } else if (val < -32768) {
240
245
  mixed[i] = -32768;
246
+ } else {
247
+ mixed[i] = val;
241
248
  }
242
249
  }
243
250
  return mixed;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/audio_mixer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncQueue } from './async_queue.js';\nimport { AudioFrame } from './audio_frame.js';\n\n// Re-export AsyncQueue for backward compatibility\nexport { AsyncQueue } from './async_queue.js';\n\n// Define types for async iteration (since lib: es2015 doesn't include them)\ntype AudioStream = {\n [Symbol.asyncIterator](): {\n next(): Promise<IteratorResult<AudioFrame>>;\n };\n};\n\ninterface Contribution {\n stream: AudioStream;\n data: Int16Array;\n buffer: Int16Array;\n hadData: boolean;\n exhausted: boolean;\n}\n\nexport interface AudioMixerOptions {\n /**\n * The size of the audio block (in samples) for mixing.\n * If not provided, defaults to sampleRate / 10 (100ms).\n */\n blocksize?: number;\n\n /**\n * The maximum wait time in milliseconds for each stream to provide\n * audio data before timing out. Defaults to 100 ms.\n */\n streamTimeoutMs?: number;\n\n /**\n * The maximum number of mixed frames to store in the output queue.\n * Defaults to 100.\n */\n capacity?: number;\n}\n\n/**\n * AudioMixer combines multiple async audio streams into a single output stream.\n *\n * The mixer accepts multiple async audio streams and mixes them into a single output stream.\n * Each output frame is generated with a fixed chunk size determined by the blocksize (in samples).\n * If blocksize is not provided (or 0), it defaults to 100ms.\n *\n * Each input stream is processed in parallel, accumulating audio data until at least one chunk\n * of samples is available. If an input stream does not provide data within the specified timeout,\n * a warning is logged. The mixer can be closed immediately\n * (dropping unconsumed frames) or allowed to flush remaining data using endInput().\n *\n * @example\n * ```typescript\n * const mixer = new AudioMixer(48000, 2);\n * mixer.addStream(stream1);\n * mixer.addStream(stream2);\n *\n * for await (const frame of mixer) {\n * // Process mixed audio frame\n * }\n * ```\n */\nexport class AudioMixer {\n private streams: Set<AudioStream>;\n private buffers: Map<AudioStream, Int16Array>;\n private streamIterators: Map<AudioStream, { next(): Promise<IteratorResult<AudioFrame>> }>;\n private sampleRate: number;\n private numChannels: number;\n private chunkSize: number;\n private streamTimeoutMs: number;\n private queue: AsyncQueue<AudioFrame>;\n private streamSignal: AsyncQueue<void>; // Signals when streams are added\n private ending: boolean;\n private mixerTask?: Promise<void>;\n private closed: boolean;\n\n /**\n * Initialize the AudioMixer.\n *\n * @param sampleRate - The audio sample rate in Hz.\n * @param numChannels - The number of audio channels.\n * @param options - Optional configuration for the mixer.\n */\n constructor(sampleRate: number, numChannels: number, options: AudioMixerOptions = {}) {\n this.streams = new Set();\n this.buffers = new Map();\n this.streamIterators = new Map();\n this.sampleRate = sampleRate;\n this.numChannels = numChannels;\n this.chunkSize =\n options.blocksize && options.blocksize > 0 ? options.blocksize : Math.floor(sampleRate / 10);\n this.streamTimeoutMs = options.streamTimeoutMs ?? 100;\n this.queue = new AsyncQueue<AudioFrame>(options.capacity ?? 100);\n this.streamSignal = new AsyncQueue<void>(1); // there should only be one mixer\n this.ending = false;\n this.closed = false;\n\n // Start the mixer task\n this.mixerTask = this.mixer();\n }\n\n /**\n * Add an audio stream to the mixer.\n *\n * The stream is added to the internal set of streams and an empty buffer is initialized for it,\n * if not already present.\n *\n * @param stream - An async iterable that produces AudioFrame objects.\n * @throws Error if the mixer has been closed.\n */\n addStream(stream: AudioStream): void {\n if (this.ending) {\n throw new Error('Cannot add stream after mixer has been closed');\n }\n\n this.streams.add(stream);\n if (!this.buffers.has(stream)) {\n this.buffers.set(stream, new Int16Array(0));\n }\n\n // Signal that a stream was added (non-blocking)\n this.streamSignal.put(undefined).catch(() => {\n // Ignore errors if signal queue is closed\n });\n }\n\n /**\n * Remove an audio stream from the mixer.\n *\n * This method removes the specified stream and its associated buffer from the mixer.\n *\n * @param stream - The audio stream to remove.\n */\n removeStream(stream: AudioStream): void {\n this.streams.delete(stream);\n this.buffers.delete(stream);\n this.streamIterators.delete(stream);\n }\n\n /**\n * Returns an async iterator for the mixed audio frames.\n */\n [Symbol.asyncIterator]() {\n return {\n next: async (): Promise<IteratorResult<AudioFrame>> => {\n const frame = await this.getNextFrame();\n if (frame === null) {\n return { done: true, value: undefined };\n }\n return { done: false, value: frame };\n },\n };\n }\n\n /**\n * Immediately stop mixing and close the mixer.\n *\n * This stops the mixing task, and any unconsumed output in the queue may be dropped.\n */\n async aclose(): Promise<void> {\n if (this.closed) {\n return;\n }\n this.closed = true;\n this.ending = true;\n\n // Close both queues to wake up any waiting operations\n this.streamSignal.close();\n this.queue.close();\n\n await this.mixerTask;\n }\n\n /**\n * Signal that no more streams will be added.\n *\n * This method marks the mixer as closed so that it flushes any remaining buffered output before ending.\n * Note that existing streams will still be processed until exhausted.\n */\n endInput(): void {\n this.ending = true;\n }\n\n private async getNextFrame(): Promise<AudioFrame | null> {\n while (true) {\n // Try to get an item from the queue (non-blocking)\n const frame = this.queue.get();\n\n if (frame !== undefined) {\n return frame;\n }\n\n // Check if mixer is closed or ending\n if (this.queue.closed || (this.ending && this.streams.size === 0)) {\n return null;\n }\n\n // Queue is empty but mixer is still running - wait for an item to be added\n await this.queue.waitForItem();\n }\n }\n\n private async mixer(): Promise<void> {\n // Main mixing loop that continuously processes streams and produces output frames\n while (true) {\n // If we're in ending mode and there are no more streams, exit\n if (this.ending && this.streams.size === 0) {\n break;\n }\n\n if (this.streams.size === 0) {\n // Wait for a stream to be added (signal queue will have an item)\n await this.streamSignal.waitForItem();\n // Consume the signal\n this.streamSignal.get();\n continue;\n }\n\n // Process all streams in parallel\n const streamArray = Array.from(this.streams);\n const promises = streamArray.map((stream) => this.getContribution(stream));\n const results = await Promise.all(\n promises.map((p) =>\n p\n .then((value) => ({ status: 'fulfilled' as const, value }))\n .catch((reason) => ({ status: 'rejected' as const, reason })),\n ),\n );\n\n const contributions: Int16Array[] = [];\n let anyData = false;\n const removals: AudioStream[] = [];\n\n for (const result of results) {\n if (result.status !== 'fulfilled') {\n console.warn('AudioMixer: Stream contribution failed:', result.reason);\n continue;\n }\n\n const contrib = result.value;\n contributions.push(contrib.data);\n this.buffers.set(contrib.stream, contrib.buffer);\n\n if (contrib.hadData) {\n anyData = true;\n }\n\n // Mark exhausted streams with no remaining buffer for removal\n if (contrib.exhausted && contrib.buffer.length === 0) {\n removals.push(contrib.stream);\n }\n }\n\n // Remove exhausted streams\n for (const stream of removals) {\n this.removeStream(stream);\n }\n\n if (!anyData) {\n // No data available from any stream, wait briefly before trying again\n await this.sleep(1);\n continue;\n }\n\n // Mix the audio data\n const mixed = this.mixAudio(contributions);\n const frame = new AudioFrame(mixed, this.sampleRate, this.numChannels, this.chunkSize);\n\n if (this.closed) {\n break;\n }\n\n try {\n // Add mixed frame to output queue\n await this.queue.put(frame);\n } catch {\n // Queue closed while trying to add frame\n break;\n }\n }\n\n // Close the queue to signal end of stream\n this.queue.close();\n }\n\n private async getContribution(stream: AudioStream): Promise<Contribution> {\n let buf = this.buffers.get(stream) ?? new Int16Array(0);\n const initialBufferLength = buf.length;\n let exhausted = false;\n let receivedDataInThisCall = false;\n\n // Get or create iterator for this stream\n let iterator = this.streamIterators.get(stream);\n if (!iterator) {\n iterator = stream[Symbol.asyncIterator]();\n this.streamIterators.set(stream, iterator);\n }\n\n // Accumulate data until we have at least chunkSize samples\n while (buf.length < this.chunkSize * this.numChannels && !exhausted && !this.closed) {\n try {\n const result = await Promise.race([iterator.next(), this.timeout(this.streamTimeoutMs)]);\n\n if (result === 'timeout') {\n console.warn(`AudioMixer: stream timeout after ${this.streamTimeoutMs}ms`);\n break;\n }\n\n if (result.done) {\n exhausted = true;\n break;\n }\n\n const frame = result.value;\n const newData = frame.data;\n\n // Mark that we received data in this call\n receivedDataInThisCall = true;\n\n // Concatenate buffers\n if (buf.length === 0) {\n buf = newData;\n } else {\n const combined = new Int16Array(buf.length + newData.length);\n combined.set(buf);\n combined.set(newData, buf.length);\n buf = combined;\n }\n } catch (error) {\n console.error(`AudioMixer: Error reading from stream:`, error);\n exhausted = true;\n break;\n }\n }\n\n // Extract contribution and update buffer\n let contrib: Int16Array;\n const samplesNeeded = this.chunkSize * this.numChannels;\n\n if (buf.length >= samplesNeeded) {\n // Extract the needed samples and keep the remainder in the buffer\n contrib = buf.subarray(0, samplesNeeded);\n buf = buf.subarray(samplesNeeded);\n } else {\n // Pad with zeros if we don't have enough data\n const padded = new Int16Array(samplesNeeded);\n padded.set(buf);\n contrib = padded;\n buf = new Int16Array(0);\n }\n\n // hadData means: we had data at start OR we received data during this call OR we have data remaining\n const hadData = initialBufferLength > 0 || receivedDataInThisCall || buf.length > 0;\n\n return {\n stream,\n data: contrib,\n buffer: buf,\n hadData,\n exhausted,\n };\n }\n\n private mixAudio(contributions: Int16Array[]): Int16Array {\n if (contributions.length === 0) {\n return new Int16Array(this.chunkSize * this.numChannels);\n }\n\n const length = this.chunkSize * this.numChannels;\n const mixed = new Int16Array(length);\n\n // Sum all contributions\n for (const contrib of contributions) {\n for (let i = 0; i < length; i++) {\n const val = contrib[i];\n if (val !== undefined) {\n mixed[i] = (mixed[i] ?? 0) + val;\n }\n }\n }\n\n // Clip to Int16 range\n for (let i = 0; i < length; i++) {\n const val = mixed[i] ?? 0;\n if (val > 32767) {\n mixed[i] = 32767;\n } else if (val < -32768) {\n mixed[i] = -32768;\n }\n }\n\n return mixed;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n private timeout(ms: number): Promise<'timeout'> {\n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ACA;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,SAAS,eAAyC;AACxD,QAAI,cAAc,WAAW,GAAG;AAC9B,aAAO,IAAI,WAAW,KAAK,YAAY,KAAK,WAAW;AAAA,IACzD;AAEA,UAAM,SAAS,KAAK,YAAY,KAAK;AACrC,UAAM,QAAQ,IAAI,WAAW,MAAM;AAGnC,eAAW,WAAW,eAAe;AACnC,eAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,cAAM,MAAM,QAAQ,CAAC;AACrB,YAAI,QAAQ,QAAW;AACrB,gBAAM,CAAC,KAAK,MAAM,CAAC,KAAK,KAAK;AAAA,QAC/B;AAAA,MACF;AAAA,IACF;AAGA,aAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,YAAM,MAAM,MAAM,CAAC,KAAK;AACxB,UAAI,MAAM,OAAO;AACf,cAAM,CAAC,IAAI;AAAA,MACb,WAAW,MAAM,QAAQ;AACvB,cAAM,CAAC,IAAI;AAAA,MACb;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AAAA,EAEQ,QAAQ,IAAgC;AAC9C,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,MAAM,QAAQ,SAAS,GAAG,EAAE,CAAC;AAAA,EAC1E;AACF;","names":["AsyncQueue"]}
1
+ {"version":3,"sources":["../src/audio_mixer.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { AsyncQueue } from './async_queue.js';\nimport { AudioFrame } from './audio_frame.js';\nimport { log } from './log.js';\n\n// Re-export AsyncQueue for backward compatibility\nexport { AsyncQueue } from './async_queue.js';\n\n// Define types for async iteration (since lib: es2015 doesn't include them)\ntype AudioStream = {\n [Symbol.asyncIterator](): {\n next(): Promise<IteratorResult<AudioFrame>>;\n };\n};\n\ninterface Contribution {\n stream: AudioStream;\n data: Int16Array;\n buffer: Int16Array;\n hadData: boolean;\n exhausted: boolean;\n}\n\nexport interface AudioMixerOptions {\n /**\n * The size of the audio block (in samples) for mixing.\n * If not provided, defaults to sampleRate / 10 (100ms).\n */\n blocksize?: number;\n\n /**\n * The maximum wait time in milliseconds for each stream to provide\n * audio data before timing out. Defaults to 100 ms.\n */\n streamTimeoutMs?: number;\n\n /**\n * The maximum number of mixed frames to store in the output queue.\n * Defaults to 100.\n */\n capacity?: number;\n}\n\n/**\n * AudioMixer combines multiple async audio streams into a single output stream.\n *\n * The mixer accepts multiple async audio streams and mixes them into a single output stream.\n * Each output frame is generated with a fixed chunk size determined by the blocksize (in samples).\n * If blocksize is not provided (or 0), it defaults to 100ms.\n *\n * Each input stream is processed in parallel, accumulating audio data until at least one chunk\n * of samples is available. If an input stream does not provide data within the specified timeout,\n * a warning is logged. The mixer can be closed immediately\n * (dropping unconsumed frames) or allowed to flush remaining data using endInput().\n *\n * @example\n * ```typescript\n * const mixer = new AudioMixer(48000, 2);\n * mixer.addStream(stream1);\n * mixer.addStream(stream2);\n *\n * for await (const frame of mixer) {\n * // Process mixed audio frame\n * }\n * ```\n */\nexport class AudioMixer {\n private streams: Set<AudioStream>;\n private buffers: Map<AudioStream, Int16Array>;\n private streamIterators: Map<AudioStream, { next(): Promise<IteratorResult<AudioFrame>> }>;\n private sampleRate: number;\n private numChannels: number;\n private chunkSize: number;\n private streamTimeoutMs: number;\n private queue: AsyncQueue<AudioFrame>;\n private streamSignal: AsyncQueue<void>; // Signals when streams are added\n private ending: boolean;\n private mixerTask?: Promise<void>;\n private closed: boolean;\n\n /**\n * Initialize the AudioMixer.\n *\n * @param sampleRate - The audio sample rate in Hz.\n * @param numChannels - The number of audio channels.\n * @param options - Optional configuration for the mixer.\n */\n constructor(sampleRate: number, numChannels: number, options: AudioMixerOptions = {}) {\n this.streams = new Set();\n this.buffers = new Map();\n this.streamIterators = new Map();\n this.sampleRate = sampleRate;\n this.numChannels = numChannels;\n this.chunkSize =\n options.blocksize && options.blocksize > 0 ? options.blocksize : Math.floor(sampleRate / 10);\n this.streamTimeoutMs = options.streamTimeoutMs ?? 100;\n this.queue = new AsyncQueue<AudioFrame>(options.capacity ?? 100);\n this.streamSignal = new AsyncQueue<void>(1); // there should only be one mixer\n this.ending = false;\n this.closed = false;\n\n // Start the mixer task\n this.mixerTask = this.mixer();\n }\n\n /**\n * Add an audio stream to the mixer.\n *\n * The stream is added to the internal set of streams and an empty buffer is initialized for it,\n * if not already present.\n *\n * @param stream - An async iterable that produces AudioFrame objects.\n * @throws Error if the mixer has been closed.\n */\n addStream(stream: AudioStream): void {\n if (this.ending) {\n throw new Error('Cannot add stream after mixer has been closed');\n }\n\n this.streams.add(stream);\n if (!this.buffers.has(stream)) {\n this.buffers.set(stream, new Int16Array(0));\n }\n\n // Signal that a stream was added (non-blocking)\n this.streamSignal.put(undefined).catch(() => {\n // Ignore errors if signal queue is closed\n });\n }\n\n /**\n * Remove an audio stream from the mixer.\n *\n * This method removes the specified stream and its associated buffer from the mixer.\n *\n * @param stream - The audio stream to remove.\n */\n removeStream(stream: AudioStream): void {\n this.streams.delete(stream);\n this.buffers.delete(stream);\n this.streamIterators.delete(stream);\n }\n\n /**\n * Returns an async iterator for the mixed audio frames.\n */\n [Symbol.asyncIterator]() {\n return {\n next: async (): Promise<IteratorResult<AudioFrame>> => {\n const frame = await this.getNextFrame();\n if (frame === null) {\n return { done: true, value: undefined };\n }\n return { done: false, value: frame };\n },\n };\n }\n\n /**\n * Immediately stop mixing and close the mixer.\n *\n * This stops the mixing task, and any unconsumed output in the queue may be dropped.\n */\n async aclose(): Promise<void> {\n if (this.closed) {\n return;\n }\n this.closed = true;\n this.ending = true;\n\n // Close both queues to wake up any waiting operations\n this.streamSignal.close();\n this.queue.close();\n\n await this.mixerTask;\n }\n\n /**\n * Signal that no more streams will be added.\n *\n * This method marks the mixer as closed so that it flushes any remaining buffered output before ending.\n * Note that existing streams will still be processed until exhausted.\n */\n endInput(): void {\n this.ending = true;\n }\n\n private async getNextFrame(): Promise<AudioFrame | null> {\n while (true) {\n // Try to get an item from the queue (non-blocking)\n const frame = this.queue.get();\n\n if (frame !== undefined) {\n return frame;\n }\n\n // Check if mixer is closed or ending\n if (this.queue.closed || (this.ending && this.streams.size === 0)) {\n return null;\n }\n\n // Queue is empty but mixer is still running - wait for an item to be added\n await this.queue.waitForItem();\n }\n }\n\n private async mixer(): Promise<void> {\n // Main mixing loop that continuously processes streams and produces output frames\n while (true) {\n // If we're in ending mode and there are no more streams, exit\n if (this.ending && this.streams.size === 0) {\n break;\n }\n\n if (this.streams.size === 0) {\n // Wait for a stream to be added (signal queue will have an item)\n await this.streamSignal.waitForItem();\n // Consume the signal\n this.streamSignal.get();\n continue;\n }\n\n // Process all streams in parallel\n const streamArray = Array.from(this.streams);\n const promises = streamArray.map((stream) => this.getContribution(stream));\n const results = await Promise.all(\n promises.map((p) =>\n p\n .then((value) => ({ status: 'fulfilled' as const, value }))\n .catch((reason) => ({ status: 'rejected' as const, reason })),\n ),\n );\n\n const contributions: Int16Array[] = [];\n let anyData = false;\n const removals: AudioStream[] = [];\n\n for (const result of results) {\n if (result.status !== 'fulfilled') {\n log.warn('AudioMixer: Stream contribution failed:', result.reason);\n continue;\n }\n\n const contrib = result.value;\n contributions.push(contrib.data);\n this.buffers.set(contrib.stream, contrib.buffer);\n\n if (contrib.hadData) {\n anyData = true;\n }\n\n // Mark exhausted streams with no remaining buffer for removal\n if (contrib.exhausted && contrib.buffer.length === 0) {\n removals.push(contrib.stream);\n }\n }\n\n // Remove exhausted streams\n for (const stream of removals) {\n this.removeStream(stream);\n }\n\n // If all streams are exhausted, end the mixer automatically.\n // This keeps `for await...of` consumers from hanging indefinitely when inputs complete.\n if (!this.ending && removals.length > 0 && this.streams.size === 0) {\n this.ending = true;\n }\n\n if (!anyData) {\n // No data available from any stream, wait briefly before trying again\n await this.sleep(1);\n continue;\n }\n\n // Mix the audio data\n const mixed = this.mixAudio(contributions);\n const frame = new AudioFrame(mixed, this.sampleRate, this.numChannels, this.chunkSize);\n\n if (this.closed) {\n break;\n }\n\n try {\n // Add mixed frame to output queue\n await this.queue.put(frame);\n } catch {\n // Queue closed while trying to add frame\n break;\n }\n }\n\n // Close the queue to signal end of stream\n this.queue.close();\n }\n\n private async getContribution(stream: AudioStream): Promise<Contribution> {\n let buf = this.buffers.get(stream) ?? new Int16Array(0);\n const initialBufferLength = buf.length;\n let exhausted = false;\n let receivedDataInThisCall = false;\n\n // Get or create iterator for this stream\n let iterator = this.streamIterators.get(stream);\n if (!iterator) {\n iterator = stream[Symbol.asyncIterator]();\n this.streamIterators.set(stream, iterator);\n }\n\n // Accumulate data until we have at least chunkSize samples\n while (buf.length < this.chunkSize * this.numChannels && !exhausted && !this.closed) {\n try {\n const result = await Promise.race([iterator.next(), this.timeout(this.streamTimeoutMs)]);\n\n if (result === 'timeout') {\n console.warn(`AudioMixer: stream timeout after ${this.streamTimeoutMs}ms`);\n break;\n }\n\n if (result.done) {\n exhausted = true;\n break;\n }\n\n const frame = result.value;\n const newData = frame.data;\n\n // Mark that we received data in this call\n receivedDataInThisCall = true;\n\n // Concatenate buffers\n if (buf.length === 0) {\n buf = newData;\n } else {\n const combined = new Int16Array(buf.length + newData.length);\n combined.set(buf);\n combined.set(newData, buf.length);\n buf = combined;\n }\n } catch (error) {\n console.error(`AudioMixer: Error reading from stream:`, error);\n exhausted = true;\n break;\n }\n }\n\n // Extract contribution and update buffer\n let contrib: Int16Array;\n const samplesNeeded = this.chunkSize * this.numChannels;\n\n if (buf.length >= samplesNeeded) {\n // Extract the needed samples and keep the remainder in the buffer\n contrib = buf.subarray(0, samplesNeeded);\n buf = buf.subarray(samplesNeeded);\n } else {\n // Pad with zeros if we don't have enough data\n const padded = new Int16Array(samplesNeeded);\n padded.set(buf);\n contrib = padded;\n buf = new Int16Array(0);\n }\n\n // hadData means: we had data at start OR we received data during this call OR we have data remaining\n const hadData = initialBufferLength > 0 || receivedDataInThisCall || buf.length > 0;\n\n return {\n stream,\n data: contrib,\n buffer: buf,\n hadData,\n exhausted,\n };\n }\n\n private mixAudio(contributions: Int16Array[]): Int16Array {\n if (contributions.length === 0) {\n return new Int16Array(this.chunkSize * this.numChannels);\n }\n\n const length = this.chunkSize * this.numChannels;\n // Use a wider accumulator to avoid int16 overflow while summing.\n const acc = new Int32Array(length);\n\n // Sum all contributions\n for (const contrib of contributions) {\n for (let i = 0; i < length; i++) {\n const val = contrib[i];\n if (val !== undefined) {\n acc[i] = (acc[i] ?? 0) + val;\n }\n }\n }\n\n // Clip to Int16 range\n const mixed = new Int16Array(length);\n for (let i = 0; i < length; i++) {\n const val = acc[i] ?? 0;\n if (val > 32767) {\n mixed[i] = 32767;\n } else if (val < -32768) {\n mixed[i] = -32768;\n } else {\n mixed[i] = val;\n }\n }\n\n return mixed;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n private timeout(ms: number): Promise<'timeout'> {\n return new Promise((resolve) => setTimeout(() => resolve('timeout'), ms));\n }\n}\n"],"mappings":"AAGA,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,WAAW;AAGpB,SAAS,cAAAA,mBAAkB;AA4DpB,MAAM,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBtB,YAAY,YAAoB,aAAqB,UAA6B,CAAC,GAAG;AACpF,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,kBAAkB,oBAAI,IAAI;AAC/B,SAAK,aAAa;AAClB,SAAK,cAAc;AACnB,SAAK,YACH,QAAQ,aAAa,QAAQ,YAAY,IAAI,QAAQ,YAAY,KAAK,MAAM,aAAa,EAAE;AAC7F,SAAK,kBAAkB,QAAQ,mBAAmB;AAClD,SAAK,QAAQ,IAAI,WAAuB,QAAQ,YAAY,GAAG;AAC/D,SAAK,eAA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AAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,YAAM,MAAM,IAAI,CAAC,KAAK;AACtB,UAAI,MAAM,OAAO;AACf,cAAM,CAAC,IAAI;AAAA,MACb,WAAW,MAAM,QAAQ;AACvB,cAAM,CAAC,IAAI;AAAA,MACb,OAAO;AACL,cAAM,CAAC,IAAI;AAAA,MACb;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AAAA,EAEQ,QAAQ,IAAgC;AAC9C,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,MAAM,QAAQ,SAAS,GAAG,EAAE,CAAC;AAAA,EAC1E;AACF;","names":["AsyncQueue"]}
@@ -23,10 +23,13 @@ __export(audio_stream_exports, {
23
23
  module.exports = __toCommonJS(audio_stream_exports);
24
24
  var import_audio_frame = require("./audio_frame.cjs");
25
25
  var import_ffi_client = require("./ffi_client.cjs");
26
+ var import_frame_processor = require("./frame_processor.cjs");
27
+ var import_log = require("./log.cjs");
26
28
  var import_audio_frame_pb = require("./proto/audio_frame_pb.cjs");
27
29
  class AudioStreamSource {
28
30
  constructor(track, sampleRateOrOptions, numChannels) {
29
31
  this.onEvent = (ev) => {
32
+ var _a;
30
33
  if (!this.controller) {
31
34
  throw new Error("Stream controller not initialized");
32
35
  }
@@ -36,19 +39,31 @@ class AudioStreamSource {
36
39
  const streamEvent = ev.message.value.message;
37
40
  switch (streamEvent.case) {
38
41
  case "frameReceived":
39
- const frame = import_audio_frame.AudioFrame.fromOwnedInfo(streamEvent.value.frame);
42
+ let frame = import_audio_frame.AudioFrame.fromOwnedInfo(streamEvent.value.frame);
43
+ if (this.frameProcessor && this.frameProcessor.isEnabled()) {
44
+ try {
45
+ frame = this.frameProcessor.process(frame);
46
+ } catch (err) {
47
+ import_log.log.warn(`Frame processing failed, passing through original frame: ${err}`);
48
+ }
49
+ }
40
50
  this.controller.enqueue(frame);
41
51
  break;
42
52
  case "eos":
43
53
  import_ffi_client.FfiClient.instance.off(import_ffi_client.FfiClientEvent.FfiEvent, this.onEvent);
44
54
  this.controller.close();
55
+ (_a = this.frameProcessor) == null ? void 0 : _a.close();
45
56
  break;
46
57
  }
47
58
  };
48
59
  if (sampleRateOrOptions !== void 0 && typeof sampleRateOrOptions !== "number") {
49
60
  this.sampleRate = sampleRateOrOptions.sampleRate ?? 48e3;
50
61
  this.numChannels = sampleRateOrOptions.numChannels ?? 1;
51
- this.ncOptions = sampleRateOrOptions.noiseCancellation;
62
+ if (sampleRateOrOptions.noiseCancellation instanceof import_frame_processor.FrameProcessor) {
63
+ this.frameProcessor = sampleRateOrOptions.noiseCancellation;
64
+ } else {
65
+ this.legacyNcOptions = sampleRateOrOptions.noiseCancellation;
66
+ }
52
67
  this.frameSizeMs = sampleRateOrOptions.frameSizeMs;
53
68
  } else {
54
69
  this.sampleRate = sampleRateOrOptions ?? 48e3;
@@ -60,9 +75,9 @@ class AudioStreamSource {
60
75
  sampleRate: this.sampleRate,
61
76
  numChannels: this.numChannels,
62
77
  frameSizeMs: this.frameSizeMs,
63
- ...this.ncOptions ? {
64
- audioFilterModuleId: this.ncOptions.moduleId,
65
- audioFilterOptions: JSON.stringify(this.ncOptions.options)
78
+ ...this.legacyNcOptions ? {
79
+ audioFilterModuleId: this.legacyNcOptions.moduleId,
80
+ audioFilterOptions: JSON.stringify(this.legacyNcOptions.options)
66
81
  } : {}
67
82
  });
68
83
  const res = import_ffi_client.FfiClient.instance.request({
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/audio_stream.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { UnderlyingSource } from 'node:stream/web';\nimport { AudioFrame } from './audio_frame.js';\nimport type { FfiEvent } from './ffi_client.js';\nimport { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';\nimport type { NewAudioStreamResponse } from './proto/audio_frame_pb.js';\nimport { AudioStreamType, NewAudioStreamRequest } from './proto/audio_frame_pb.js';\nimport type { Track } from './track.js';\n\nexport interface AudioStreamOptions {\n noiseCancellation?: NoiseCancellationOptions;\n sampleRate?: number;\n numChannels?: number;\n frameSizeMs?: number;\n}\n\nexport interface NoiseCancellationOptions {\n moduleId: string;\n options: Record<string, any>;\n}\n\nclass AudioStreamSource implements UnderlyingSource<AudioFrame> {\n private controller?: ReadableStreamDefaultController<AudioFrame>;\n private ffiHandle: FfiHandle;\n private sampleRate: number;\n private numChannels: number;\n private ncOptions?: NoiseCancellationOptions;\n private frameSizeMs?: number;\n\n constructor(\n track: Track,\n sampleRateOrOptions?: number | AudioStreamOptions,\n numChannels?: number,\n ) {\n if (sampleRateOrOptions !== undefined && typeof sampleRateOrOptions !== 'number') {\n this.sampleRate = sampleRateOrOptions.sampleRate ?? 48000;\n this.numChannels = sampleRateOrOptions.numChannels ?? 1;\n this.ncOptions = sampleRateOrOptions.noiseCancellation;\n this.frameSizeMs = sampleRateOrOptions.frameSizeMs;\n } else {\n this.sampleRate = (sampleRateOrOptions as number) ?? 48000;\n this.numChannels = numChannels ?? 1;\n }\n\n const req = new NewAudioStreamRequest({\n type: AudioStreamType.AUDIO_STREAM_NATIVE,\n trackHandle: track.ffi_handle.handle,\n sampleRate: this.sampleRate,\n numChannels: this.numChannels,\n frameSizeMs: this.frameSizeMs,\n ...(this.ncOptions\n ? {\n audioFilterModuleId: this.ncOptions.moduleId,\n audioFilterOptions: JSON.stringify(this.ncOptions.options),\n }\n : {}),\n });\n\n const res = FfiClient.instance.request<NewAudioStreamResponse>({\n message: {\n case: 'newAudioStream',\n value: req,\n },\n });\n\n this.ffiHandle = new FfiHandle(res.stream!.handle!.id!);\n\n FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onEvent);\n }\n\n private onEvent = (ev: FfiEvent) => {\n if (!this.controller) {\n throw new Error('Stream controller not initialized');\n }\n\n if (\n ev.message.case != 'audioStreamEvent' ||\n ev.message.value.streamHandle != this.ffiHandle.handle\n ) {\n return;\n }\n\n const streamEvent = ev.message.value.message;\n switch (streamEvent.case) {\n case 'frameReceived':\n const frame = AudioFrame.fromOwnedInfo(streamEvent.value.frame!);\n this.controller.enqueue(frame);\n break;\n case 'eos':\n FfiClient.instance.off(FfiClientEvent.FfiEvent, this.onEvent);\n this.controller.close();\n break;\n }\n };\n\n start(controller: ReadableStreamDefaultController<AudioFrame>) {\n this.controller = controller;\n }\n\n cancel() {\n FfiClient.instance.off(FfiClientEvent.FfiEvent, this.onEvent);\n this.ffiHandle.dispose();\n }\n}\n\nexport class AudioStream extends ReadableStream<AudioFrame> {\n constructor(track: Track);\n constructor(track: Track, sampleRate: number);\n constructor(track: Track, sampleRate: number, numChannels: number);\n constructor(track: Track, options: AudioStreamOptions);\n constructor(\n track: Track,\n sampleRateOrOptions?: number | AudioStreamOptions,\n numChannels?: number,\n ) {\n super(new AudioStreamSource(track, sampleRateOrOptions, numChannels));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,yBAA2B;AAE3B,wBAAqD;AAErD,4BAAuD;AAevD,MAAM,kBAA0D;AAAA,EAQ9D,YACE,OACA,qBACA,aACA;AAqCF,SAAQ,UAAU,CAAC,OAAiB;AAClC,UAAI,CAAC,KAAK,YAAY;AACpB,cAAM,IAAI,MAAM,mCAAmC;AAAA,MACrD;AAEA,UACE,GAAG,QAAQ,QAAQ,sBACnB,GAAG,QAAQ,MAAM,gBAAgB,KAAK,UAAU,QAChD;AACA;AAAA,MACF;AAEA,YAAM,cAAc,GAAG,QAAQ,MAAM;AACrC,cAAQ,YAAY,MAAM;AAAA,QACxB,KAAK;AACH,gBAAM,QAAQ,8BAAW,cAAc,YAAY,MAAM,KAAM;AAC/D,eAAK,WAAW,QAAQ,KAAK;AAC7B;AAAA,QACF,KAAK;AACH,sCAAU,SAAS,IAAI,iCAAe,UAAU,KAAK,O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LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { UnderlyingSource } from 'node:stream/web';\nimport { AudioFrame } from './audio_frame.js';\nimport type { FfiEvent } from './ffi_client.js';\nimport { FfiClient, FfiClientEvent, FfiHandle } from './ffi_client.js';\nimport { FrameProcessor } from './frame_processor.js';\nimport { log } from './log.js';\nimport type { NewAudioStreamResponse } from './proto/audio_frame_pb.js';\nimport { AudioStreamType, NewAudioStreamRequest } from './proto/audio_frame_pb.js';\nimport type { Track } from './track.js';\n\nexport interface AudioStreamOptions {\n noiseCancellation?: NoiseCancellationOptions | FrameProcessor<AudioFrame>;\n sampleRate?: number;\n numChannels?: number;\n frameSizeMs?: number;\n}\n\nexport interface NoiseCancellationOptions {\n moduleId: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n options: Record<string, any>;\n}\n\nclass AudioStreamSource implements UnderlyingSource<AudioFrame> {\n private controller?: ReadableStreamDefaultController<AudioFrame>;\n private ffiHandle: FfiHandle;\n private sampleRate: number;\n private numChannels: number;\n private legacyNcOptions?: NoiseCancellationOptions;\n private frameProcessor?: FrameProcessor<AudioFrame>;\n private frameSizeMs?: number;\n\n constructor(\n track: Track,\n sampleRateOrOptions?: number | AudioStreamOptions,\n numChannels?: number,\n ) {\n if (sampleRateOrOptions !== undefined && typeof sampleRateOrOptions !== 'number') {\n this.sampleRate = sampleRateOrOptions.sampleRate ?? 48000;\n this.numChannels = sampleRateOrOptions.numChannels ?? 1;\n if (sampleRateOrOptions.noiseCancellation instanceof FrameProcessor) {\n this.frameProcessor = sampleRateOrOptions.noiseCancellation;\n } else {\n this.legacyNcOptions = sampleRateOrOptions.noiseCancellation;\n }\n this.frameSizeMs = sampleRateOrOptions.frameSizeMs;\n } else {\n this.sampleRate = (sampleRateOrOptions as number) ?? 48000;\n this.numChannels = numChannels ?? 1;\n }\n\n const req = new NewAudioStreamRequest({\n type: AudioStreamType.AUDIO_STREAM_NATIVE,\n trackHandle: track.ffi_handle.handle,\n sampleRate: this.sampleRate,\n numChannels: this.numChannels,\n frameSizeMs: this.frameSizeMs,\n ...(this.legacyNcOptions\n ? {\n audioFilterModuleId: this.legacyNcOptions.moduleId,\n audioFilterOptions: JSON.stringify(this.legacyNcOptions.options),\n }\n : {}),\n });\n\n const res = FfiClient.instance.request<NewAudioStreamResponse>({\n message: {\n case: 'newAudioStream',\n value: req,\n },\n });\n\n this.ffiHandle = new FfiHandle(res.stream!.handle!.id!);\n\n FfiClient.instance.on(FfiClientEvent.FfiEvent, this.onEvent);\n }\n\n private onEvent = (ev: FfiEvent) => {\n if (!this.controller) {\n throw new Error('Stream controller not initialized');\n }\n\n if (\n ev.message.case != 'audioStreamEvent' ||\n ev.message.value.streamHandle != this.ffiHandle.handle\n ) {\n return;\n }\n\n const streamEvent = ev.message.value.message;\n switch (streamEvent.case) {\n case 'frameReceived':\n let frame = AudioFrame.fromOwnedInfo(streamEvent.value.frame!);\n if (this.frameProcessor && this.frameProcessor.isEnabled()) {\n try {\n frame = this.frameProcessor.process(frame);\n } catch (err: unknown) {\n log.warn(`Frame processing failed, passing through original frame: ${err}`);\n }\n }\n this.controller.enqueue(frame);\n break;\n case 'eos':\n FfiClient.instance.off(FfiClientEvent.FfiEvent, this.onEvent);\n this.controller.close();\n this.frameProcessor?.close();\n break;\n }\n };\n\n start(controller: ReadableStreamDefaultController<AudioFrame>) {\n this.controller = controller;\n }\n\n cancel() {\n FfiClient.instance.off(FfiClientEvent.FfiEvent, this.onEvent);\n this.ffiHandle.dispose();\n }\n}\n\nexport class AudioStream extends ReadableStream<AudioFrame> {\n constructor(track: Track);\n constructor(track: Track, sampleRate: number);\n constructor(track: Track, sampleRate: number, numChannels: number);\n constructor(track: Track, options: AudioStreamOptions);\n constructor(\n track: Track,\n sampleRateOrOptions?: number | AudioStreamOptions,\n numChannels?: number,\n ) {\n super(new AudioStreamSource(track, sampleRateOrOptions, numChannels));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAA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AAM,IAAI,4CAAsB;AAAA,MACpC,MAAM,sCAAgB;AAAA,MACtB,aAAa,MAAM,WAAW;AAAA,MAC9B,YAAY,KAAK;AAAA,MACjB,aAAa,KAAK;AAAA,MAClB,aAAa,KAAK;AAAA,MAClB,GAAI,KAAK,kBACL;AAAA,QACE,qBAAqB,KAAK,gBAAgB;AAAA,QAC1C,oBAAoB,KAAK,UAAU,KAAK,gBAAgB,OAAO;AAAA,MACjE,IACA,CAAC;AAAA,IACP,CAAC;AAED,UAAM,MAAM,4BAAU,SAAS,QAAgC;AAAA,MAC7D,SAAS;AAAA,QACP,MAAM;AAAA,QACN,OAAO;AAAA,MACT;AAAA,IACF,CAAC;AAED,SAAK,YAAY,IAAI,4BAAU,IAAI,OAAQ,OAAQ,EAAG;AAEtD,gCAAU,SAAS,GAAG,iCAAe,UAAU,KAAK,OAAO;AAAA,EAC7D;AAAA,EAmCA,MAAM,YAAyD;AAC7D,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,SAAS;AACP,gCAAU,SAAS,IAAI,iCAAe,UAAU,KAAK,OAAO;AAC5D,SAAK,UAAU,QAAQ;AAAA,EACzB;AACF;AAEO,MAAM,oBAAoB,eAA2B;AAAA,EAK1D,YACE,OACA,qBACA,aACA;AACA,UAAM,IAAI,kBAAkB,OAAO,qBAAqB,WAAW,CAAC;AAAA,EACtE;AACF;","names":[]}
@@ -1,4 +1,5 @@
1
1
  import { AudioFrame } from './audio_frame.cjs';
2
+ import { FrameProcessor } from './frame_processor.cjs';
2
3
  import { Track } from './track.cjs';
3
4
  import './proto/audio_frame_pb.cjs';
4
5
  import '@bufbuild/protobuf';
@@ -6,6 +7,8 @@ import './proto/track_pb.cjs';
6
7
  import './proto/stats_pb.cjs';
7
8
  import './proto/e2ee_pb.cjs';
8
9
  import './proto/handle_pb.cjs';
10
+ import './video_frame.cjs';
11
+ import './proto/video_frame_pb.cjs';
9
12
  import './audio_source.cjs';
10
13
  import './napi/native.d.cjs';
11
14
  import './ffi_client.cjs';
@@ -13,15 +16,13 @@ import '@livekit/typed-emitter';
13
16
  import './proto/ffi_pb.cjs';
14
17
  import './proto/room_pb.cjs';
15
18
  import './proto/participant_pb.cjs';
16
- import './proto/video_frame_pb.cjs';
17
19
  import './proto/data_stream_pb.cjs';
18
20
  import './proto/rpc_pb.cjs';
19
21
  import './proto/track_publication_pb.cjs';
20
22
  import './video_source.cjs';
21
- import './video_frame.cjs';
22
23
 
23
24
  interface AudioStreamOptions {
24
- noiseCancellation?: NoiseCancellationOptions;
25
+ noiseCancellation?: NoiseCancellationOptions | FrameProcessor<AudioFrame>;
25
26
  sampleRate?: number;
26
27
  numChannels?: number;
27
28
  frameSizeMs?: number;
@@ -1,4 +1,5 @@
1
1
  import { AudioFrame } from './audio_frame.js';
2
+ import { FrameProcessor } from './frame_processor.js';
2
3
  import { Track } from './track.js';
3
4
  import './proto/audio_frame_pb.js';
4
5
  import '@bufbuild/protobuf';
@@ -6,6 +7,8 @@ import './proto/track_pb.js';
6
7
  import './proto/stats_pb.js';
7
8
  import './proto/e2ee_pb.js';
8
9
  import './proto/handle_pb.js';
10
+ import './video_frame.js';
11
+ import './proto/video_frame_pb.js';
9
12
  import './audio_source.js';
10
13
  import './napi/native.d.js';
11
14
  import './ffi_client.js';
@@ -13,15 +16,13 @@ import '@livekit/typed-emitter';
13
16
  import './proto/ffi_pb.js';
14
17
  import './proto/room_pb.js';
15
18
  import './proto/participant_pb.js';
16
- import './proto/video_frame_pb.js';
17
19
  import './proto/data_stream_pb.js';
18
20
  import './proto/rpc_pb.js';
19
21
  import './proto/track_publication_pb.js';
20
22
  import './video_source.js';
21
- import './video_frame.js';
22
23
 
23
24
  interface AudioStreamOptions {
24
- noiseCancellation?: NoiseCancellationOptions;
25
+ noiseCancellation?: NoiseCancellationOptions | FrameProcessor<AudioFrame>;
25
26
  sampleRate?: number;
26
27
  numChannels?: number;
27
28
  frameSizeMs?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"audio_stream.d.ts","sourceRoot":"","sources":["../src/audio_stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAK9C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B;AAsFD,qBAAa,WAAY,SAAQ,cAAc,CAAC,UAAU,CAAC;gBAC7C,KAAK,EAAE,KAAK;gBACZ,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM;gBAChC,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM;gBACrD,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,kBAAkB;CAQtD"}
1
+ {"version":3,"file":"audio_stream.d.ts","sourceRoot":"","sources":["../src/audio_stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAItD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,CAAC,EAAE,wBAAwB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;IAC1E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,MAAM,CAAC;IAEjB,OAAO,EA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