@livekit/agents 0.4.6 → 0.5.0
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.
- package/README.md +17 -0
- package/dist/audio.cjs +77 -0
- package/dist/audio.cjs.map +1 -0
- package/dist/audio.js +48 -37
- package/dist/audio.js.map +1 -1
- package/dist/cli.cjs +131 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.js +96 -122
- package/dist/cli.js.map +1 -1
- package/dist/generator.cjs +36 -0
- package/dist/generator.cjs.map +1 -0
- package/dist/generator.js +8 -22
- package/dist/generator.js.map +1 -1
- package/dist/http_server.cjs +72 -0
- package/dist/http_server.cjs.map +1 -0
- package/dist/http_server.d.ts +1 -1
- package/dist/http_server.js +44 -47
- package/dist/http_server.js.map +1 -1
- package/dist/index.cjs +78 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +26 -28
- package/dist/index.js.map +1 -1
- package/dist/ipc/job_executor.cjs +33 -0
- package/dist/ipc/job_executor.cjs.map +1 -0
- package/dist/ipc/job_executor.js +7 -4
- package/dist/ipc/job_executor.js.map +1 -1
- package/dist/ipc/job_main.cjs +147 -0
- package/dist/ipc/job_main.cjs.map +1 -0
- package/dist/ipc/job_main.d.ts +1 -1
- package/dist/ipc/job_main.js +103 -103
- package/dist/ipc/job_main.js.map +1 -1
- package/dist/ipc/message.cjs +17 -0
- package/dist/ipc/message.cjs.map +1 -0
- package/dist/ipc/message.js +0 -1
- package/dist/ipc/message.js.map +1 -1
- package/dist/ipc/proc_job_executor.cjs +174 -0
- package/dist/ipc/proc_job_executor.cjs.map +1 -0
- package/dist/ipc/proc_job_executor.js +130 -126
- package/dist/ipc/proc_job_executor.js.map +1 -1
- package/dist/ipc/proc_pool.cjs +126 -0
- package/dist/ipc/proc_pool.cjs.map +1 -0
- package/dist/ipc/proc_pool.js +93 -96
- package/dist/ipc/proc_pool.js.map +1 -1
- package/dist/job.cjs +230 -0
- package/dist/job.cjs.map +1 -0
- package/dist/job.js +195 -198
- package/dist/job.js.map +1 -1
- package/dist/llm/chat_context.cjs +131 -0
- package/dist/llm/chat_context.cjs.map +1 -0
- package/dist/llm/chat_context.js +98 -86
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/function_context.cjs +103 -0
- package/dist/llm/function_context.cjs.map +1 -0
- package/dist/llm/function_context.js +72 -81
- package/dist/llm/function_context.js.map +1 -1
- package/dist/llm/function_context.test.cjs +218 -0
- package/dist/llm/function_context.test.cjs.map +1 -0
- package/dist/llm/function_context.test.js +209 -210
- package/dist/llm/function_context.test.js.map +1 -1
- package/dist/llm/index.cjs +43 -0
- package/dist/llm/index.cjs.map +1 -0
- package/dist/llm/index.js +22 -6
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.cjs +76 -0
- package/dist/llm/llm.cjs.map +1 -0
- package/dist/llm/llm.js +48 -42
- package/dist/llm/llm.js.map +1 -1
- package/dist/log.cjs +57 -0
- package/dist/log.cjs.map +1 -0
- package/dist/log.js +27 -26
- package/dist/log.js.map +1 -1
- package/dist/multimodal/agent_playout.cjs +228 -0
- package/dist/multimodal/agent_playout.cjs.map +1 -0
- package/dist/multimodal/agent_playout.d.ts +1 -1
- package/dist/multimodal/agent_playout.js +193 -180
- package/dist/multimodal/agent_playout.js.map +1 -1
- package/dist/multimodal/index.cjs +25 -0
- package/dist/multimodal/index.cjs.map +1 -0
- package/dist/multimodal/index.js +2 -5
- package/dist/multimodal/index.js.map +1 -1
- package/dist/multimodal/multimodal_agent.cjs +404 -0
- package/dist/multimodal/multimodal_agent.cjs.map +1 -0
- package/dist/multimodal/multimodal_agent.d.ts +1 -1
- package/dist/multimodal/multimodal_agent.js +351 -330
- package/dist/multimodal/multimodal_agent.js.map +1 -1
- package/dist/pipeline/agent_output.cjs +172 -0
- package/dist/pipeline/agent_output.cjs.map +1 -0
- package/dist/pipeline/agent_output.js +136 -138
- package/dist/pipeline/agent_output.js.map +1 -1
- package/dist/pipeline/agent_playout.cjs +169 -0
- package/dist/pipeline/agent_playout.cjs.map +1 -0
- package/dist/pipeline/agent_playout.js +126 -136
- package/dist/pipeline/agent_playout.js.map +1 -1
- package/dist/pipeline/human_input.cjs +158 -0
- package/dist/pipeline/human_input.cjs.map +1 -0
- package/dist/pipeline/human_input.js +124 -125
- package/dist/pipeline/human_input.js.map +1 -1
- package/dist/pipeline/index.cjs +31 -0
- package/dist/pipeline/index.cjs.map +1 -0
- package/dist/pipeline/index.js +8 -4
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/pipeline_agent.cjs +642 -0
- package/dist/pipeline/pipeline_agent.cjs.map +1 -0
- package/dist/pipeline/pipeline_agent.js +595 -651
- package/dist/pipeline/pipeline_agent.js.map +1 -1
- package/dist/pipeline/speech_handle.cjs +128 -0
- package/dist/pipeline/speech_handle.cjs.map +1 -0
- package/dist/pipeline/speech_handle.js +102 -100
- package/dist/pipeline/speech_handle.js.map +1 -1
- package/dist/plugin.cjs +46 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +20 -20
- package/dist/plugin.js.map +1 -1
- package/dist/stt/index.cjs +38 -0
- package/dist/stt/index.cjs.map +1 -0
- package/dist/stt/index.js +13 -5
- package/dist/stt/index.js.map +1 -1
- package/dist/stt/stream_adapter.cjs +87 -0
- package/dist/stt/stream_adapter.cjs.map +1 -0
- package/dist/stt/stream_adapter.js +58 -55
- package/dist/stt/stream_adapter.js.map +1 -1
- package/dist/stt/stt.cjs +98 -0
- package/dist/stt/stt.cjs.map +1 -0
- package/dist/stt/stt.js +63 -98
- package/dist/stt/stt.js.map +1 -1
- package/dist/tokenize/basic/basic.cjs +98 -0
- package/dist/tokenize/basic/basic.cjs.map +1 -0
- package/dist/tokenize/basic/basic.js +56 -45
- package/dist/tokenize/basic/basic.js.map +1 -1
- package/dist/tokenize/basic/hyphenator.cjs +425 -0
- package/dist/tokenize/basic/hyphenator.cjs.map +1 -0
- package/dist/tokenize/basic/hyphenator.js +66 -82
- package/dist/tokenize/basic/hyphenator.js.map +1 -1
- package/dist/tokenize/basic/index.cjs +35 -0
- package/dist/tokenize/basic/index.cjs.map +1 -0
- package/dist/tokenize/basic/index.js +7 -4
- package/dist/tokenize/basic/index.js.map +1 -1
- package/dist/tokenize/basic/paragraph.cjs +57 -0
- package/dist/tokenize/basic/paragraph.cjs.map +1 -0
- package/dist/tokenize/basic/paragraph.js +30 -35
- package/dist/tokenize/basic/paragraph.js.map +1 -1
- package/dist/tokenize/basic/sentence.cjs +83 -0
- package/dist/tokenize/basic/sentence.cjs.map +1 -0
- package/dist/tokenize/basic/sentence.js +56 -57
- package/dist/tokenize/basic/sentence.js.map +1 -1
- package/dist/tokenize/basic/word.cjs +44 -0
- package/dist/tokenize/basic/word.cjs.map +1 -0
- package/dist/tokenize/basic/word.js +17 -20
- package/dist/tokenize/basic/word.js.map +1 -1
- package/dist/tokenize/index.cjs +55 -0
- package/dist/tokenize/index.cjs.map +1 -0
- package/dist/tokenize/index.js +18 -7
- package/dist/tokenize/index.js.map +1 -1
- package/dist/tokenize/token_stream.cjs +164 -0
- package/dist/tokenize/token_stream.cjs.map +1 -0
- package/dist/tokenize/token_stream.js +133 -139
- package/dist/tokenize/token_stream.js.map +1 -1
- package/dist/tokenize/tokenizer.cjs +184 -0
- package/dist/tokenize/tokenizer.cjs.map +1 -0
- package/dist/tokenize/tokenizer.js +138 -99
- package/dist/tokenize/tokenizer.js.map +1 -1
- package/dist/transcription.cjs +131 -0
- package/dist/transcription.cjs.map +1 -0
- package/dist/transcription.js +99 -96
- package/dist/transcription.js.map +1 -1
- package/dist/tts/index.cjs +38 -0
- package/dist/tts/index.cjs.map +1 -0
- package/dist/tts/index.js +13 -5
- package/dist/tts/index.js.map +1 -1
- package/dist/tts/stream_adapter.cjs +78 -0
- package/dist/tts/stream_adapter.cjs.map +1 -0
- package/dist/tts/stream_adapter.js +50 -47
- package/dist/tts/stream_adapter.js.map +1 -1
- package/dist/tts/tts.cjs +127 -0
- package/dist/tts/tts.cjs.map +1 -0
- package/dist/tts/tts.js +90 -120
- package/dist/tts/tts.js.map +1 -1
- package/dist/utils.cjs +284 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.js +242 -247
- package/dist/utils.js.map +1 -1
- package/dist/vad.cjs +92 -0
- package/dist/vad.cjs.map +1 -0
- package/dist/vad.js +57 -52
- package/dist/vad.js.map +1 -1
- package/dist/version.cjs +29 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +4 -4
- package/dist/version.js.map +1 -1
- package/dist/worker.cjs +576 -0
- package/dist/worker.cjs.map +1 -0
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +511 -484
- package/dist/worker.js.map +1 -1
- package/package.json +18 -8
- package/src/ipc/job_main.ts +66 -64
- package/src/pipeline/pipeline_agent.ts +23 -23
package/dist/tts/tts.js
CHANGED
|
@@ -1,131 +1,101 @@
|
|
|
1
|
-
import { AsyncIterableQueue, mergeFrames } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
get sampleRate() {
|
|
24
|
-
return this.#sampleRate;
|
|
25
|
-
}
|
|
26
|
-
/** Returns the channel count of audio frames returned by this TTS */
|
|
27
|
-
get numChannels() {
|
|
28
|
-
return this.#numChannels;
|
|
29
|
-
}
|
|
1
|
+
import { AsyncIterableQueue, mergeFrames } from "../utils.js";
|
|
2
|
+
class TTS {
|
|
3
|
+
#capabilities;
|
|
4
|
+
#sampleRate;
|
|
5
|
+
#numChannels;
|
|
6
|
+
constructor(sampleRate, numChannels, capabilities) {
|
|
7
|
+
this.#capabilities = capabilities;
|
|
8
|
+
this.#sampleRate = sampleRate;
|
|
9
|
+
this.#numChannels = numChannels;
|
|
10
|
+
}
|
|
11
|
+
/** Returns this TTS's capabilities */
|
|
12
|
+
get capabilities() {
|
|
13
|
+
return this.#capabilities;
|
|
14
|
+
}
|
|
15
|
+
/** Returns the sample rate of audio frames returned by this TTS */
|
|
16
|
+
get sampleRate() {
|
|
17
|
+
return this.#sampleRate;
|
|
18
|
+
}
|
|
19
|
+
/** Returns the channel count of audio frames returned by this TTS */
|
|
20
|
+
get numChannels() {
|
|
21
|
+
return this.#numChannels;
|
|
22
|
+
}
|
|
30
23
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* @remarks
|
|
42
|
-
* This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that
|
|
43
|
-
* exports its own child SynthesizeStream class, which inherits this class's methods.
|
|
44
|
-
*/
|
|
45
|
-
export class SynthesizeStream {
|
|
46
|
-
static FLUSH_SENTINEL = Symbol('FLUSH_SENTINEL');
|
|
47
|
-
static END_OF_STREAM = Symbol('END_OF_STREAM');
|
|
48
|
-
input = new AsyncIterableQueue();
|
|
49
|
-
queue = new AsyncIterableQueue();
|
|
50
|
-
closed = false;
|
|
51
|
-
/** Push a string of text to the TTS */
|
|
52
|
-
pushText(text) {
|
|
53
|
-
if (this.input.closed) {
|
|
54
|
-
throw new Error('Input is closed');
|
|
55
|
-
}
|
|
56
|
-
if (this.closed) {
|
|
57
|
-
throw new Error('Stream is closed');
|
|
58
|
-
}
|
|
59
|
-
this.input.put(text);
|
|
24
|
+
class SynthesizeStream {
|
|
25
|
+
static FLUSH_SENTINEL = Symbol("FLUSH_SENTINEL");
|
|
26
|
+
static END_OF_STREAM = Symbol("END_OF_STREAM");
|
|
27
|
+
input = new AsyncIterableQueue();
|
|
28
|
+
queue = new AsyncIterableQueue();
|
|
29
|
+
closed = false;
|
|
30
|
+
/** Push a string of text to the TTS */
|
|
31
|
+
pushText(text) {
|
|
32
|
+
if (this.input.closed) {
|
|
33
|
+
throw new Error("Input is closed");
|
|
60
34
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (this.input.closed) {
|
|
64
|
-
throw new Error('Input is closed');
|
|
65
|
-
}
|
|
66
|
-
if (this.closed) {
|
|
67
|
-
throw new Error('Stream is closed');
|
|
68
|
-
}
|
|
69
|
-
this.input.put(SynthesizeStream.FLUSH_SENTINEL);
|
|
35
|
+
if (this.closed) {
|
|
36
|
+
throw new Error("Stream is closed");
|
|
70
37
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
throw new Error('Stream is closed');
|
|
78
|
-
}
|
|
79
|
-
this.input.close();
|
|
38
|
+
this.input.put(text);
|
|
39
|
+
}
|
|
40
|
+
/** Flush the TTS, causing it to process all pending text */
|
|
41
|
+
flush() {
|
|
42
|
+
if (this.input.closed) {
|
|
43
|
+
throw new Error("Input is closed");
|
|
80
44
|
}
|
|
81
|
-
|
|
82
|
-
|
|
45
|
+
if (this.closed) {
|
|
46
|
+
throw new Error("Stream is closed");
|
|
83
47
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
48
|
+
this.input.put(SynthesizeStream.FLUSH_SENTINEL);
|
|
49
|
+
}
|
|
50
|
+
/** Mark the input as ended and forbid additional pushes */
|
|
51
|
+
endInput() {
|
|
52
|
+
if (this.input.closed) {
|
|
53
|
+
throw new Error("Input is closed");
|
|
89
54
|
}
|
|
90
|
-
|
|
91
|
-
|
|
55
|
+
if (this.closed) {
|
|
56
|
+
throw new Error("Stream is closed");
|
|
92
57
|
}
|
|
58
|
+
this.input.close();
|
|
59
|
+
}
|
|
60
|
+
next() {
|
|
61
|
+
return this.queue.next();
|
|
62
|
+
}
|
|
63
|
+
/** Close both the input and output of the TTS stream */
|
|
64
|
+
close() {
|
|
65
|
+
this.input.close();
|
|
66
|
+
this.queue.close();
|
|
67
|
+
this.closed = true;
|
|
68
|
+
}
|
|
69
|
+
[Symbol.asyncIterator]() {
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
93
72
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* ```
|
|
103
|
-
*
|
|
104
|
-
* @remarks
|
|
105
|
-
* This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that
|
|
106
|
-
* exports its own child ChunkedStream class, which inherits this class's methods.
|
|
107
|
-
*/
|
|
108
|
-
export class ChunkedStream {
|
|
109
|
-
queue = new AsyncIterableQueue();
|
|
110
|
-
closed = false;
|
|
111
|
-
/** Collect every frame into one in a single call */
|
|
112
|
-
async collect() {
|
|
113
|
-
const frames = [];
|
|
114
|
-
for await (const event of this) {
|
|
115
|
-
frames.push(event.frame);
|
|
116
|
-
}
|
|
117
|
-
return mergeFrames(frames);
|
|
118
|
-
}
|
|
119
|
-
next() {
|
|
120
|
-
return this.queue.next();
|
|
121
|
-
}
|
|
122
|
-
/** Close both the input and output of the TTS stream */
|
|
123
|
-
close() {
|
|
124
|
-
this.queue.close();
|
|
125
|
-
this.closed = true;
|
|
126
|
-
}
|
|
127
|
-
[Symbol.asyncIterator]() {
|
|
128
|
-
return this;
|
|
73
|
+
class ChunkedStream {
|
|
74
|
+
queue = new AsyncIterableQueue();
|
|
75
|
+
closed = false;
|
|
76
|
+
/** Collect every frame into one in a single call */
|
|
77
|
+
async collect() {
|
|
78
|
+
const frames = [];
|
|
79
|
+
for await (const event of this) {
|
|
80
|
+
frames.push(event.frame);
|
|
129
81
|
}
|
|
82
|
+
return mergeFrames(frames);
|
|
83
|
+
}
|
|
84
|
+
next() {
|
|
85
|
+
return this.queue.next();
|
|
86
|
+
}
|
|
87
|
+
/** Close both the input and output of the TTS stream */
|
|
88
|
+
close() {
|
|
89
|
+
this.queue.close();
|
|
90
|
+
this.closed = true;
|
|
91
|
+
}
|
|
92
|
+
[Symbol.asyncIterator]() {
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
130
95
|
}
|
|
96
|
+
export {
|
|
97
|
+
ChunkedStream,
|
|
98
|
+
SynthesizeStream,
|
|
99
|
+
TTS
|
|
100
|
+
};
|
|
131
101
|
//# sourceMappingURL=tts.js.map
|
package/dist/tts/tts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/tts/tts.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { AsyncIterableQueue, mergeFrames } from '../utils.js';\n\n/** SynthesizedAudio is a packet of speech synthesis as returned by the TTS. */\nexport interface SynthesizedAudio {\n /** Request ID (one segment could be made up of multiple requests) */\n requestId: string;\n /** Segment ID, each segment is separated by a flush */\n segmentId: string;\n /** Synthesized audio frame */\n frame: AudioFrame;\n /** Current segment of the synthesized audio */\n deltaText?: string;\n}\n\n/**\n * Describes the capabilities of the TTS provider.\n *\n * @remarks\n * At present, only `streaming` is supplied to this interface, and the framework only supports\n * providers that do have a streaming endpoint.\n */\nexport interface TTSCapabilities {\n streaming: boolean;\n}\n\n/**\n * An instance of a text-to-speech adapter.\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child TTS class, which inherits this class's methods.\n */\nexport abstract class TTS {\n #capabilities: TTSCapabilities;\n #sampleRate: number;\n #numChannels: number;\n\n constructor(sampleRate: number, numChannels: number, capabilities: TTSCapabilities) {\n this.#capabilities = capabilities;\n this.#sampleRate = sampleRate;\n this.#numChannels = numChannels;\n }\n\n /** Returns this TTS's capabilities */\n get capabilities(): TTSCapabilities {\n return this.#capabilities;\n }\n\n /** Returns the sample rate of audio frames returned by this TTS */\n get sampleRate(): number {\n return this.#sampleRate;\n }\n\n /** Returns the channel count of audio frames returned by this TTS */\n get numChannels(): number {\n return this.#numChannels;\n }\n\n /**\n * Receives text and returns synthesis in the form of a {@link ChunkedStream}\n */\n abstract synthesize(text: string): ChunkedStream;\n\n /**\n * Returns a {@link SynthesizeStream} that can be used to push text and receive audio data\n */\n abstract stream(): SynthesizeStream;\n}\n\n/**\n * An instance of a text-to-speech stream, as an asynchronous iterable iterator.\n *\n * @example Looping through frames\n * ```ts\n * for await (const event of stream) {\n * await source.captureFrame(event.frame);\n * }\n * ```\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child SynthesizeStream class, which inherits this class's methods.\n */\nexport abstract class SynthesizeStream\n implements AsyncIterableIterator<SynthesizedAudio | typeof SynthesizeStream.END_OF_STREAM>\n{\n protected static readonly FLUSH_SENTINEL = Symbol('FLUSH_SENTINEL');\n static readonly END_OF_STREAM = Symbol('END_OF_STREAM');\n protected input = new AsyncIterableQueue<string | typeof SynthesizeStream.FLUSH_SENTINEL>();\n protected queue = new AsyncIterableQueue<\n SynthesizedAudio | typeof SynthesizeStream.END_OF_STREAM\n >();\n protected closed = false;\n\n /** Push a string of text to the TTS */\n pushText(text: string) {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(text);\n }\n\n /** Flush the TTS, causing it to process all pending text */\n flush() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(SynthesizeStream.FLUSH_SENTINEL);\n }\n\n /** Mark the input as ended and forbid additional pushes */\n endInput() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.close();\n }\n\n next(): Promise<IteratorResult<SynthesizedAudio | typeof SynthesizeStream.END_OF_STREAM>> {\n return this.queue.next();\n }\n\n /** Close both the input and output of the TTS stream */\n close() {\n this.input.close();\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): SynthesizeStream {\n return this;\n }\n}\n\n/**\n * An instance of a text-to-speech response, as an asynchronous iterable iterator.\n *\n * @example Looping through frames\n * ```ts\n * for await (const event of stream) {\n * await source.captureFrame(event.frame);\n * }\n * ```\n *\n * @remarks\n * This class is abstract, and as such cannot be used directly. Instead, use a provider plugin that\n * exports its own child ChunkedStream class, which inherits this class's methods.\n */\nexport abstract class ChunkedStream implements AsyncIterableIterator<SynthesizedAudio> {\n protected queue = new AsyncIterableQueue<SynthesizedAudio>();\n protected closed = false;\n\n /** Collect every frame into one in a single call */\n async collect(): Promise<AudioFrame> {\n const frames = [];\n for await (const event of this) {\n frames.push(event.frame);\n }\n return mergeFrames(frames);\n }\n\n next(): Promise<IteratorResult<SynthesizedAudio>> {\n return this.queue.next();\n }\n\n /** Close both the input and output of the TTS stream */\n close() {\n this.queue.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): ChunkedStream {\n return this;\n }\n}\n"],"mappings":"AAIA,SAAS,oBAAoB,mBAAmB;AAgCzC,MAAe,IAAI;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,YAAoB,aAAqB,cAA+B;AAClF,SAAK,gBAAgB;AACrB,SAAK,cAAc;AACnB,SAAK,eAAe;AAAA,EACtB;AAAA;AAAA,EAGA,IAAI,eAAgC;AAClC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,aAAqB;AACvB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAsB;AACxB,WAAO,KAAK;AAAA,EACd;AAWF;AAgBO,MAAe,iBAEtB;AAAA,EACE,OAA0B,iBAAiB,OAAO,gBAAgB;AAAA,EAClE,OAAgB,gBAAgB,OAAO,eAAe;AAAA,EAC5C,QAAQ,IAAI,mBAAoE;AAAA,EAChF,QAAQ,IAAI,mBAEpB;AAAA,EACQ,SAAS;AAAA;AAAA,EAGnB,SAAS,MAAc;AAC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,IAAmB;AACtC,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var utils_exports = {};
|
|
20
|
+
__export(utils_exports, {
|
|
21
|
+
AsyncIterableQueue: () => AsyncIterableQueue,
|
|
22
|
+
AudioEnergyFilter: () => AudioEnergyFilter,
|
|
23
|
+
CancellablePromise: () => CancellablePromise,
|
|
24
|
+
ExpFilter: () => ExpFilter,
|
|
25
|
+
Future: () => Future,
|
|
26
|
+
Queue: () => Queue,
|
|
27
|
+
findMicroTrackId: () => findMicroTrackId,
|
|
28
|
+
gracefullyCancel: () => gracefullyCancel,
|
|
29
|
+
mergeFrames: () => mergeFrames
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(utils_exports);
|
|
32
|
+
var import_rtc_node = require("@livekit/rtc-node");
|
|
33
|
+
var import_node_events = require("node:events");
|
|
34
|
+
const mergeFrames = (buffer) => {
|
|
35
|
+
if (Array.isArray(buffer)) {
|
|
36
|
+
buffer = buffer;
|
|
37
|
+
if (buffer.length == 0) {
|
|
38
|
+
throw new TypeError("buffer is empty");
|
|
39
|
+
}
|
|
40
|
+
const sampleRate = buffer[0].sampleRate;
|
|
41
|
+
const channels = buffer[0].channels;
|
|
42
|
+
let samplesPerChannel = 0;
|
|
43
|
+
let data = new Int16Array();
|
|
44
|
+
for (const frame of buffer) {
|
|
45
|
+
if (frame.sampleRate !== sampleRate) {
|
|
46
|
+
throw new TypeError("sample rate mismatch");
|
|
47
|
+
}
|
|
48
|
+
if (frame.channels !== channels) {
|
|
49
|
+
throw new TypeError("channel count mismatch");
|
|
50
|
+
}
|
|
51
|
+
data = new Int16Array([...data, ...frame.data]);
|
|
52
|
+
samplesPerChannel += frame.samplesPerChannel;
|
|
53
|
+
}
|
|
54
|
+
return new import_rtc_node.AudioFrame(data, sampleRate, channels, samplesPerChannel);
|
|
55
|
+
}
|
|
56
|
+
return buffer;
|
|
57
|
+
};
|
|
58
|
+
const findMicroTrackId = (room, identity) => {
|
|
59
|
+
var _a;
|
|
60
|
+
let p = room.remoteParticipants.get(identity);
|
|
61
|
+
if (identity === ((_a = room.localParticipant) == null ? void 0 : _a.identity)) {
|
|
62
|
+
p = room.localParticipant;
|
|
63
|
+
}
|
|
64
|
+
if (!p) {
|
|
65
|
+
throw new Error(`participant ${identity} not found`);
|
|
66
|
+
}
|
|
67
|
+
let trackId;
|
|
68
|
+
p.trackPublications.forEach((track) => {
|
|
69
|
+
if (track.source === import_rtc_node.TrackSource.SOURCE_MICROPHONE) {
|
|
70
|
+
trackId = track.sid;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
if (!trackId) {
|
|
75
|
+
throw new Error(`participant ${identity} does not have a microphone track`);
|
|
76
|
+
}
|
|
77
|
+
return trackId;
|
|
78
|
+
};
|
|
79
|
+
class Queue {
|
|
80
|
+
/** @internal */
|
|
81
|
+
items = [];
|
|
82
|
+
#limit;
|
|
83
|
+
#events = new import_node_events.EventEmitter();
|
|
84
|
+
constructor(limit) {
|
|
85
|
+
this.#limit = limit;
|
|
86
|
+
}
|
|
87
|
+
async get() {
|
|
88
|
+
if (this.items.length === 0) {
|
|
89
|
+
await (0, import_node_events.once)(this.#events, "put");
|
|
90
|
+
}
|
|
91
|
+
const item = this.items.shift();
|
|
92
|
+
this.#events.emit("get");
|
|
93
|
+
return item;
|
|
94
|
+
}
|
|
95
|
+
async put(item) {
|
|
96
|
+
if (this.#limit && this.items.length >= this.#limit) {
|
|
97
|
+
await (0, import_node_events.once)(this.#events, "get");
|
|
98
|
+
}
|
|
99
|
+
this.items.push(item);
|
|
100
|
+
this.#events.emit("put");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
class Future {
|
|
104
|
+
#await;
|
|
105
|
+
#resolvePromise;
|
|
106
|
+
#rejectPromise;
|
|
107
|
+
#done = false;
|
|
108
|
+
constructor() {
|
|
109
|
+
this.#await = new Promise((resolve, reject) => {
|
|
110
|
+
this.#resolvePromise = resolve;
|
|
111
|
+
this.#rejectPromise = reject;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
get await() {
|
|
115
|
+
return this.#await;
|
|
116
|
+
}
|
|
117
|
+
get done() {
|
|
118
|
+
return this.#done;
|
|
119
|
+
}
|
|
120
|
+
resolve() {
|
|
121
|
+
this.#done = true;
|
|
122
|
+
this.#resolvePromise();
|
|
123
|
+
}
|
|
124
|
+
reject(error) {
|
|
125
|
+
this.#done = true;
|
|
126
|
+
this.#rejectPromise(error);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
class CancellablePromise {
|
|
130
|
+
#promise;
|
|
131
|
+
#cancelFn;
|
|
132
|
+
#isCancelled = false;
|
|
133
|
+
#error = null;
|
|
134
|
+
constructor(executor) {
|
|
135
|
+
let cancel;
|
|
136
|
+
this.#promise = new Promise((resolve, reject) => {
|
|
137
|
+
executor(
|
|
138
|
+
resolve,
|
|
139
|
+
(reason) => {
|
|
140
|
+
this.#error = reason instanceof Error ? reason : new Error(String(reason));
|
|
141
|
+
reject(reason);
|
|
142
|
+
},
|
|
143
|
+
(cancelFn) => {
|
|
144
|
+
cancel = () => {
|
|
145
|
+
this.#isCancelled = true;
|
|
146
|
+
cancelFn();
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
this.#cancelFn = cancel;
|
|
152
|
+
}
|
|
153
|
+
get isCancelled() {
|
|
154
|
+
return this.#isCancelled;
|
|
155
|
+
}
|
|
156
|
+
get error() {
|
|
157
|
+
return this.#error;
|
|
158
|
+
}
|
|
159
|
+
then(onfulfilled, onrejected) {
|
|
160
|
+
return this.#promise.then(onfulfilled, onrejected);
|
|
161
|
+
}
|
|
162
|
+
catch(onrejected) {
|
|
163
|
+
return this.#promise.catch(onrejected);
|
|
164
|
+
}
|
|
165
|
+
finally(onfinally) {
|
|
166
|
+
return this.#promise.finally(onfinally);
|
|
167
|
+
}
|
|
168
|
+
cancel() {
|
|
169
|
+
this.#cancelFn();
|
|
170
|
+
}
|
|
171
|
+
static from(promise) {
|
|
172
|
+
return new CancellablePromise((resolve, reject) => {
|
|
173
|
+
promise.then(resolve).catch(reject);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async function gracefullyCancel(promise) {
|
|
178
|
+
if (!promise.isCancelled) {
|
|
179
|
+
promise.cancel();
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
await promise;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
class AsyncIterableQueue {
|
|
187
|
+
static CLOSE_SENTINEL = Symbol("CLOSE_SENTINEL");
|
|
188
|
+
#queue = new Queue();
|
|
189
|
+
#closed = false;
|
|
190
|
+
get closed() {
|
|
191
|
+
return this.#closed;
|
|
192
|
+
}
|
|
193
|
+
put(item) {
|
|
194
|
+
if (this.#closed) {
|
|
195
|
+
throw new Error("Queue is closed");
|
|
196
|
+
}
|
|
197
|
+
this.#queue.put(item);
|
|
198
|
+
}
|
|
199
|
+
close() {
|
|
200
|
+
this.#closed = true;
|
|
201
|
+
this.#queue.put(AsyncIterableQueue.CLOSE_SENTINEL);
|
|
202
|
+
}
|
|
203
|
+
async next() {
|
|
204
|
+
if (this.#closed && this.#queue.items.length === 0) {
|
|
205
|
+
return { value: void 0, done: true };
|
|
206
|
+
}
|
|
207
|
+
const item = await this.#queue.get();
|
|
208
|
+
if (item === AsyncIterableQueue.CLOSE_SENTINEL && this.#closed) {
|
|
209
|
+
return { value: void 0, done: true };
|
|
210
|
+
}
|
|
211
|
+
return { value: item, done: false };
|
|
212
|
+
}
|
|
213
|
+
[Symbol.asyncIterator]() {
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
class ExpFilter {
|
|
218
|
+
#alpha;
|
|
219
|
+
#max;
|
|
220
|
+
#filtered = void 0;
|
|
221
|
+
constructor(alpha, max) {
|
|
222
|
+
this.#alpha = alpha;
|
|
223
|
+
this.#max = max;
|
|
224
|
+
}
|
|
225
|
+
reset(alpha) {
|
|
226
|
+
if (alpha) {
|
|
227
|
+
this.#alpha = alpha;
|
|
228
|
+
}
|
|
229
|
+
this.#filtered = void 0;
|
|
230
|
+
}
|
|
231
|
+
apply(exp, sample) {
|
|
232
|
+
if (this.#filtered) {
|
|
233
|
+
const a = this.#alpha ** exp;
|
|
234
|
+
this.#filtered = a * this.#filtered + (1 - a) * sample;
|
|
235
|
+
} else {
|
|
236
|
+
this.#filtered = sample;
|
|
237
|
+
}
|
|
238
|
+
if (this.#max && this.#filtered > this.#max) {
|
|
239
|
+
this.#filtered = this.#max;
|
|
240
|
+
}
|
|
241
|
+
return this.#filtered;
|
|
242
|
+
}
|
|
243
|
+
get filtered() {
|
|
244
|
+
return this.#filtered;
|
|
245
|
+
}
|
|
246
|
+
set alpha(alpha) {
|
|
247
|
+
this.#alpha = alpha;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
class AudioEnergyFilter {
|
|
251
|
+
#cooldownSeconds;
|
|
252
|
+
#cooldown;
|
|
253
|
+
constructor(cooldownSeconds = 1) {
|
|
254
|
+
this.#cooldownSeconds = cooldownSeconds;
|
|
255
|
+
this.#cooldown = cooldownSeconds;
|
|
256
|
+
}
|
|
257
|
+
pushFrame(frame) {
|
|
258
|
+
const arr = Float32Array.from(frame.data, (x) => x / 32768);
|
|
259
|
+
const rms = (arr.map((x) => x ** 2).reduce((acc, x) => acc + x) / arr.length) ** 0.5;
|
|
260
|
+
if (rms > 4e-3) {
|
|
261
|
+
this.#cooldown = this.#cooldownSeconds;
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
const durationSeconds = frame.samplesPerChannel / frame.sampleRate;
|
|
265
|
+
this.#cooldown -= durationSeconds;
|
|
266
|
+
if (this.#cooldown > 0) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
273
|
+
0 && (module.exports = {
|
|
274
|
+
AsyncIterableQueue,
|
|
275
|
+
AudioEnergyFilter,
|
|
276
|
+
CancellablePromise,
|
|
277
|
+
ExpFilter,
|
|
278
|
+
Future,
|
|
279
|
+
Queue,
|
|
280
|
+
findMicroTrackId,
|
|
281
|
+
gracefullyCancel,
|
|
282
|
+
mergeFrames
|
|
283
|
+
});
|
|
284
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type {\n LocalParticipant,\n RemoteParticipant,\n Room,\n TrackPublication,\n} from '@livekit/rtc-node';\nimport { AudioFrame, TrackSource } from '@livekit/rtc-node';\nimport { EventEmitter, once } from 'node:events';\n\n/** Union of a single and a list of {@link AudioFrame}s */\nexport type AudioBuffer = AudioFrame[] | AudioFrame;\n\n/**\n * Merge one or more {@link AudioFrame}s into a single one.\n *\n * @param buffer Either an {@link AudioFrame} or a list thereof\n * @throws\n * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError\n * | TypeError} if sample rate or channel count are mismatched\n */\nexport const mergeFrames = (buffer: AudioBuffer): AudioFrame => {\n if (Array.isArray(buffer)) {\n buffer = buffer as AudioFrame[];\n if (buffer.length == 0) {\n throw new TypeError('buffer is empty');\n }\n\n const sampleRate = buffer[0]!.sampleRate;\n const channels = buffer[0]!.channels;\n let samplesPerChannel = 0;\n let data = new Int16Array();\n\n for (const frame of buffer) {\n if (frame.sampleRate !== sampleRate) {\n throw new TypeError('sample rate mismatch');\n }\n\n if (frame.channels !== channels) {\n throw new TypeError('channel count mismatch');\n }\n\n data = new Int16Array([...data, ...frame.data]);\n samplesPerChannel += frame.samplesPerChannel;\n }\n\n return new AudioFrame(data, sampleRate, channels, samplesPerChannel);\n }\n\n return buffer;\n};\n\nexport const findMicroTrackId = (room: Room, identity: string): string => {\n let p: RemoteParticipant | LocalParticipant | undefined = room.remoteParticipants.get(identity);\n\n if (identity === room.localParticipant?.identity) {\n p = room.localParticipant;\n }\n\n if (!p) {\n throw new Error(`participant ${identity} not found`);\n }\n\n // find first micro track\n let trackId: string | undefined;\n p.trackPublications.forEach((track: TrackPublication) => {\n if (track.source === TrackSource.SOURCE_MICROPHONE) {\n trackId = track.sid;\n return;\n }\n });\n\n if (!trackId) {\n throw new Error(`participant ${identity} does not have a microphone track`);\n }\n\n return trackId;\n};\n\n/** @internal */\nexport class Queue<T> {\n /** @internal */\n items: T[] = [];\n #limit?: number;\n #events = new EventEmitter();\n\n constructor(limit?: number) {\n this.#limit = limit;\n }\n\n async get(): Promise<T> {\n if (this.items.length === 0) {\n await once(this.#events, 'put');\n }\n const item = this.items.shift()!;\n this.#events.emit('get');\n return item;\n }\n\n async put(item: T) {\n if (this.#limit && this.items.length >= this.#limit) {\n await once(this.#events, 'get');\n }\n this.items.push(item);\n this.#events.emit('put');\n }\n}\n\n/** @internal */\nexport class Future {\n #await: Promise<void>;\n #resolvePromise!: () => void;\n #rejectPromise!: (error: Error) => void;\n #done: boolean = false;\n\n constructor() {\n this.#await = new Promise<void>((resolve, reject) => {\n this.#resolvePromise = resolve;\n this.#rejectPromise = reject;\n });\n }\n\n get await() {\n return this.#await;\n }\n\n get done() {\n return this.#done;\n }\n\n resolve() {\n this.#done = true;\n this.#resolvePromise();\n }\n\n reject(error: Error) {\n this.#done = true;\n this.#rejectPromise(error);\n }\n}\n\n/** @internal */\nexport class CancellablePromise<T> {\n #promise: Promise<T>;\n #cancelFn: () => void;\n #isCancelled: boolean = false;\n #error: Error | null = null;\n\n constructor(\n executor: (\n resolve: (value: T | PromiseLike<T>) => void,\n reject: (reason?: any) => void,\n onCancel: (cancelFn: () => void) => void,\n ) => void,\n ) {\n let cancel: () => void;\n\n this.#promise = new Promise<T>((resolve, reject) => {\n executor(\n resolve,\n (reason) => {\n this.#error = reason instanceof Error ? reason : new Error(String(reason));\n reject(reason);\n },\n (cancelFn) => {\n cancel = () => {\n this.#isCancelled = true;\n cancelFn();\n };\n },\n );\n });\n\n this.#cancelFn = cancel!;\n }\n\n get isCancelled(): boolean {\n return this.#isCancelled;\n }\n\n get error(): Error | null {\n return this.#error;\n }\n\n then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | Promise<TResult1>) | null,\n onrejected?: ((reason: any) => TResult2 | Promise<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n return this.#promise.then(onfulfilled, onrejected);\n }\n\n catch<TResult = never>(\n onrejected?: ((reason: any) => TResult | Promise<TResult>) | null,\n ): Promise<T | TResult> {\n return this.#promise.catch(onrejected);\n }\n\n finally(onfinally?: (() => void) | null): Promise<T> {\n return this.#promise.finally(onfinally);\n }\n\n cancel(): void {\n this.#cancelFn();\n }\n\n static from<T>(promise: Promise<T>): CancellablePromise<T> {\n return new CancellablePromise<T>((resolve, reject) => {\n promise.then(resolve).catch(reject);\n });\n }\n}\n\n/** @internal */\nexport async function gracefullyCancel<T>(promise: CancellablePromise<T>): Promise<void> {\n if (!promise.isCancelled) {\n promise.cancel();\n }\n try {\n await promise;\n } catch (error) {\n // Ignore the error, as it's expected due to cancellation\n }\n}\n\n/** @internal */\nexport class AsyncIterableQueue<T> implements AsyncIterableIterator<T> {\n private static readonly CLOSE_SENTINEL = Symbol('CLOSE_SENTINEL');\n #queue = new Queue<T | typeof AsyncIterableQueue.CLOSE_SENTINEL>();\n #closed = false;\n\n get closed(): boolean {\n return this.#closed;\n }\n\n put(item: T): void {\n if (this.#closed) {\n throw new Error('Queue is closed');\n }\n this.#queue.put(item);\n }\n\n close(): void {\n this.#closed = true;\n this.#queue.put(AsyncIterableQueue.CLOSE_SENTINEL);\n }\n\n async next(): Promise<IteratorResult<T>> {\n if (this.#closed && this.#queue.items.length === 0) {\n return { value: undefined, done: true };\n }\n const item = await this.#queue.get();\n if (item === AsyncIterableQueue.CLOSE_SENTINEL && this.#closed) {\n return { value: undefined, done: true };\n }\n return { value: item as T, done: false };\n }\n\n [Symbol.asyncIterator](): AsyncIterableQueue<T> {\n return this;\n }\n}\n\n/** @internal */\nexport class ExpFilter {\n #alpha: number;\n #max?: number;\n #filtered?: number = undefined;\n\n constructor(alpha: number, max?: number) {\n this.#alpha = alpha;\n this.#max = max;\n }\n\n reset(alpha?: number) {\n if (alpha) {\n this.#alpha = alpha;\n }\n this.#filtered = undefined;\n }\n\n apply(exp: number, sample: number): number {\n if (this.#filtered) {\n const a = this.#alpha ** exp;\n this.#filtered = a * this.#filtered + (1 - a) * sample;\n } else {\n this.#filtered = sample;\n }\n\n if (this.#max && this.#filtered > this.#max) {\n this.#filtered = this.#max;\n }\n\n return this.#filtered;\n }\n\n get filtered(): number | undefined {\n return this.#filtered;\n }\n\n set alpha(alpha: number) {\n this.#alpha = alpha;\n }\n}\n\n/** @internal */\nexport class AudioEnergyFilter {\n #cooldownSeconds: number;\n #cooldown: number;\n\n constructor(cooldownSeconds = 1) {\n this.#cooldownSeconds = cooldownSeconds;\n this.#cooldown = cooldownSeconds;\n }\n\n pushFrame(frame: AudioFrame): boolean {\n const arr = Float32Array.from(frame.data, (x) => x / 32768);\n const rms = (arr.map((x) => x ** 2).reduce((acc, x) => acc + x) / arr.length) ** 0.5;\n if (rms > 0.004) {\n this.#cooldown = this.#cooldownSeconds;\n return true;\n }\n\n const durationSeconds = frame.samplesPerChannel / frame.sampleRate;\n this.#cooldown -= durationSeconds;\n if (this.#cooldown > 0) {\n return true;\n }\n\n return false;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,sBAAwC;AACxC,yBAAmC;AAa5B,MAAM,cAAc,CAAC,WAAoC;AAC9D,MAAI,MAAM,QAAQ,MAAM,GAAG;AACzB,aAAS;AACT,QAAI,OAAO,UAAU,GAAG;AACtB,YAAM,IAAI,UAAU,iBAAiB;AAAA,IACvC;AAEA,UAAM,aAAa,OAAO,CAAC,EAAG;AAC9B,UAAM,WAAW,OAAO,CAAC,EAAG;AAC5B,QAAI,oBAAoB;AACxB,QAAI,OAAO,IAAI,WAAW;AAE1B,eAAW,SAAS,QAAQ;AAC1B,UAAI,MAAM,eAAe,YAAY;AACnC,cAAM,IAAI,UAAU,sBAAsB;AAAA,MAC5C;AAEA,UAAI,MAAM,aAAa,UAAU;AAC/B,cAAM,IAAI,UAAU,wBAAwB;AAAA,MAC9C;AAEA,aAAO,IAAI,WAAW,CAAC,GAAG,MAAM,GAAG,MAAM,IAAI,CAAC;AAC9C,2BAAqB,MAAM;AAAA,IAC7B;AAEA,WAAO,IAAI,2BAAW,MAAM,YAAY,UAAU,iBAAiB;AAAA,EACrE;AAEA,SAAO;AACT;AAEO,MAAM,mBAAmB,CAAC,M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