Skip to content

Commit a155c25

Browse files
Optimizing applyToStream to re-use existing voice focus node in WebAudio graph (#3117)
* Updated applyToStream to use the existing web audio node
1 parent 2250ea5 commit a155c25

13 files changed

Lines changed: 94 additions & 12 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added additional APIs to underlying VoiceFocus library
13+
1214
### Removed
1315

1416
### Changed

docs/globals.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ <h3>AGCOptions</h3>
482482
<div class="tsd-signature tsd-kind-icon">AGCOptions<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">EnabledAGCOptions</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">DisabledAGCOptions</span></div>
483483
<aside class="tsd-sources">
484484
<ul>
485-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L91">libs/voicefocus/types.d.ts:91</a></li>
485+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L93">libs/voicefocus/types.d.ts:93</a></li>
486486
</ul>
487487
</aside>
488488
</section>

docs/interfaces/voicefocuspaths.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ <h3>models</h3>
9797
<div class="tsd-signature tsd-kind-icon">models<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
9898
<aside class="tsd-sources">
9999
<ul>
100-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L154">libs/voicefocus/types.d.ts:154</a></li>
100+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L156">libs/voicefocus/types.d.ts:156</a></li>
101101
</ul>
102102
</aside>
103103
</section>
@@ -107,7 +107,7 @@ <h3>processors</h3>
107107
<div class="tsd-signature tsd-kind-icon">processors<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
108108
<aside class="tsd-sources">
109109
<ul>
110-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L151">libs/voicefocus/types.d.ts:151</a></li>
110+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L153">libs/voicefocus/types.d.ts:153</a></li>
111111
</ul>
112112
</aside>
113113
</section>
@@ -117,7 +117,7 @@ <h3>wasm</h3>
117117
<div class="tsd-signature tsd-kind-icon">wasm<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
118118
<aside class="tsd-sources">
119119
<ul>
120-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L153">libs/voicefocus/types.d.ts:153</a></li>
120+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L155">libs/voicefocus/types.d.ts:155</a></li>
121121
</ul>
122122
</aside>
123123
</section>
@@ -127,7 +127,7 @@ <h3>workers</h3>
127127
<div class="tsd-signature tsd-kind-icon">workers<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
128128
<aside class="tsd-sources">
129129
<ul>
130-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L152">libs/voicefocus/types.d.ts:152</a></li>
130+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L154">libs/voicefocus/types.d.ts:154</a></li>
131131
</ul>
132132
</aside>
133133
</section>

docs/modules.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ <h3>AGCOptions</h3>
482482
<div class="tsd-signature tsd-kind-icon">AGCOptions<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">EnabledAGCOptions</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">DisabledAGCOptions</span></div>
483483
<aside class="tsd-sources">
484484
<ul>
485-
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L91">libs/voicefocus/types.d.ts:91</a></li>
485+
<li>Defined in <a href="https://github.com/aws/amazon-chime-sdk-js/blob/main/libs/voicefocus/types.d.ts#L93">libs/voicefocus/types.d.ts:93</a></li>
486486
</ul>
487487
</aside>
488488
</section>

libs/voicefocus/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export declare abstract class VoiceFocusAudioWorkletNode extends VoiceFocusAudio
7878
abstract setMode(mode: string): Promise<void>;
7979
abstract stop(): Promise<void>;
8080
abstract getModelMetrics(): ModelMetrics | undefined;
81+
abstract reset(): void;
82+
abstract isEnabled(): boolean;
8183
}
8284
export interface EnabledAGCOptions {
8385
useVoiceFocusAGC: true;

libs/voicefocus/voicefocus.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export declare class VoiceFocus {
5252
private internal;
5353
private constructor();
5454
getModelMetrics(): import("./types.js").ModelMetrics | undefined;
55+
reset(): void;
5556
enable(): void;
5657
disable(): void;
5758
setMode(mode: string): void;
@@ -67,7 +68,7 @@ export declare class VoiceFocus {
6768
logger?: Logger;
6869
}): Promise<VoiceFocus>;
6970
createNode(context: AudioContext, options?: NodeArguments): Promise<VoiceFocusAudioWorkletNode>;
70-
applyToStream(stream: MediaStream, context: AudioContext, options?: NodeArguments): Promise<{
71+
applyToStream(stream: MediaStream, context: AudioContext, options?: NodeArguments, useExistingNode?: boolean): Promise<{
7172
node: VoiceFocusAudioWorkletNode;
7273
source: MediaStreamAudioSourceNode;
7374
destination: MediaStreamAudioDestinationNode;

libs/voicefocus/voicefocus.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ class VoiceFocus {
107107
var _a;
108108
return (_a = this.internal.voiceFocusNode) === null || _a === void 0 ? void 0 : _a.getModelMetrics();
109109
}
110+
reset() {
111+
var _a, _b, _c;
112+
(_a = this.internal.voiceFocusNode) === null || _a === void 0 ? void 0 : _a.reset();
113+
(_b = this.internal.sourceNode) === null || _b === void 0 ? void 0 : _b.disconnect();
114+
(_c = this.internal.destinationNode) === null || _c === void 0 ? void 0 : _c.disconnect();
115+
}
110116
enable() {
111117
var _a;
112118
(_a = this.internal.voiceFocusNode) === null || _a === void 0 ? void 0 : _a.enable();
@@ -325,18 +331,27 @@ class VoiceFocus {
325331
.addModule(url)
326332
.then(() => new (this.nodeConstructor)(context, Object.assign(Object.assign({}, this.nodeOptions), { processorOptions })));
327333
}
328-
applyToStream(stream, context, options) {
334+
applyToStream(stream, context, options, useExistingNode = false) {
335+
var _a;
329336
return __awaiter(this, void 0, void 0, function* () {
330337
if (this.internal.isDestroyed) {
331338
throw new Error("Unable to apply stream because VoiceFocus worker has been destroyed");
332339
}
340+
let voiceFocusNode;
341+
if (useExistingNode && this.internal.voiceFocusNode && this.internal.voiceFocusNode.isEnabled()) {
342+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Re-using existing voice focus node");
343+
voiceFocusNode = this.internal.voiceFocusNode;
344+
}
345+
else {
346+
voiceFocusNode = yield this.createNode(context, options);
347+
}
333348
const source = context.createMediaStreamSource(stream);
334-
const node = yield this.applyToSourceNode(source, context, options);
349+
source.connect(voiceFocusNode);
335350
const destination = context.createMediaStreamDestination();
336-
node.connect(destination);
337-
this.internal = Object.assign(Object.assign({}, this.internal), { voiceFocusNode: node, sourceNode: source, destinationNode: destination, audioContext: context });
351+
voiceFocusNode.connect(destination);
352+
this.internal = Object.assign(Object.assign({}, this.internal), { voiceFocusNode: voiceFocusNode, sourceNode: source, destinationNode: destination, audioContext: context });
338353
return {
339-
node,
354+
node: voiceFocusNode,
340355
source,
341356
destination,
342357
stream: destination.stream,

libs/voicefocus/worklet-inline-node.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ declare class VoiceFocusInlineNode extends VoiceFocusAudioWorkletNode {
77
private cpuWarningCount;
88
private metricsLastRecorded;
99
private metrics;
10+
private enabled;
1011
constructor(context: AudioContext, options: VoiceFocusNodeOptions);
1112
onModuleBufferLoaded(buffer: ArrayBuffer, key: string): void;
1213
onModuleLoaded(module: WebAssembly.Module, key: string): void;
1314
enable(): Promise<void>;
1415
disable(): Promise<void>;
1516
setMode(mode: string): Promise<void>;
1617
stop(): Promise<void>;
18+
reset(): void;
19+
isEnabled(): boolean;
1720
onProcessorMessage(event: ProcessorMessage): void;
1821
getModelMetrics(): ModelMetrics | undefined;
1922
onWorkerMessage(event: WorkerMessage): void;

libs/voicefocus/worklet-inline-node.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class VoiceFocusInlineNode extends types_js_1.VoiceFocusAudioWorkletNode {
4242
longInvoke: 0,
4343
},
4444
};
45+
this.enabled = false;
4546
this.channelCountMode = 'explicit';
4647
this.channelCount = 1;
4748
const { modelURL, worker, fetchBehavior, logger, delegate, } = options;
@@ -59,6 +60,7 @@ class VoiceFocusInlineNode extends types_js_1.VoiceFocusAudioWorkletNode {
5960
fetchBehavior,
6061
path: modelURL,
6162
});
63+
this.enabled = true;
6264
}
6365
onModuleBufferLoaded(buffer, key) {
6466
this.port.postMessage({ message: 'module-buffer', buffer, key });
@@ -69,11 +71,13 @@ class VoiceFocusInlineNode extends types_js_1.VoiceFocusAudioWorkletNode {
6971
enable() {
7072
return __awaiter(this, void 0, void 0, function* () {
7173
this.port.postMessage({ message: 'enable' });
74+
this.enabled = true;
7275
});
7376
}
7477
disable() {
7578
return __awaiter(this, void 0, void 0, function* () {
7679
this.port.postMessage({ message: 'disable' });
80+
this.enabled = false;
7781
});
7882
}
7983
setMode(mode) {
@@ -92,8 +96,37 @@ class VoiceFocusInlineNode extends types_js_1.VoiceFocusAudioWorkletNode {
9296
console.error("failed to terminate worker:", e);
9397
}
9498
this.disconnect();
99+
this.enabled = false;
95100
});
96101
}
102+
reset() {
103+
this.metrics = {
104+
latencyMillisAverage: 0,
105+
snr: {
106+
average: 0,
107+
averageActive: 0,
108+
variance: 0,
109+
varianceActive: 0,
110+
},
111+
drr: {
112+
average: 0,
113+
variance: 0,
114+
averageActive: 0,
115+
varianceActive: 0,
116+
},
117+
vad: {
118+
average: 0,
119+
},
120+
cpu: {
121+
lateInvoke: 0,
122+
longInvoke: 0,
123+
},
124+
};
125+
this.port.postMessage({ message: 'reset' });
126+
}
127+
isEnabled() {
128+
return this.enabled;
129+
}
97130
onProcessorMessage(event) {
98131
var _a, _b, _c, _d, _e, _f;
99132
const { data } = event;

libs/voicefocus/worklet-worker-postMessage-node.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { ModelMetrics, ProcessorMessage, VoiceFocusAudioWorkletNode, VoiceFocusN
22
declare class VoiceFocusWorkerPostMessageNode extends VoiceFocusAudioWorkletNode {
33
private worker;
44
private delegate?;
5+
private enabled;
56
constructor(context: AudioContext, options: VoiceFocusNodeOptions);
67
enable(): Promise<void>;
78
disable(): Promise<void>;
89
setMode(mode: string): Promise<void>;
910
stop(): Promise<void>;
1011
getModelMetrics(): ModelMetrics | undefined;
12+
reset(): void;
13+
isEnabled(): boolean;
1114
onWorkerMessage(event: WorkerMessage): void;
1215
onProcessorMessage(event: ProcessorMessage): void;
1316
}

0 commit comments

Comments
 (0)