Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

voice - cleanup disposables #193199

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ abstract class VoiceTranscriber extends Disposable {
this._register(toDisposable(() => this.port.off('message', requestHandler)));

this.port.start();
this._register(toDisposable(() => this.port.close()));

let closed = false;
this.port.on('close', () => {
this.logService.info(`[voice] transcriber: closed connection`);

cts.dispose(true);
closed = true;
this.dispose();
});

this._register(toDisposable(() => {
if (!closed) {
this.port.close();
}
}));
}

protected abstract handleRequest(data: Float32Array, cancellation: CancellationToken): Promise<void>;
Expand All @@ -93,7 +100,7 @@ abstract class VoiceTranscriber extends Disposable {

class SlidingWindowVoiceTranscriber extends VoiceTranscriber {

private readonly transcriptionQueue = new Queue();
private readonly transcriptionQueue = this._register(new Queue());

private transcribedResults: string[] = [];
private data: Float32Array = new Float32Array(0);
Expand All @@ -103,7 +110,7 @@ class SlidingWindowVoiceTranscriber extends VoiceTranscriber {
this.logService.info(`[voice] transcriber: voice detected, storing in buffer`);

this.data = this.data ? this.joinFloat32Arrays([this.data, data]) : data;
} else if (this.data) {
} else {
this.logService.info(`[voice] transcriber: silence detected, transcribing window...`);

const data = this.data.slice(0);
Expand Down Expand Up @@ -136,6 +143,12 @@ class SlidingWindowVoiceTranscriber extends VoiceTranscriber {

this.port.postMessage(this.transcribedResults.join(' '));
}

override dispose(): void {
super.dispose();

this.data = new Float32Array(0);
}
}
Comment on lines +147 to 152
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: overriding dispose is a little dangerous as it's not enforced that super.dispose() is called. Can you do this instead in the ctor?

this._register(toDisposable(() => this.data = new Float32Array(0)));


class FullWindowVoiceTranscriber extends VoiceTranscriber {
Expand Down Expand Up @@ -190,4 +203,10 @@ class FullWindowVoiceTranscriber extends VoiceTranscriber {

this.port.postMessage(result);
}

override dispose(): void {
super.dispose();

this.data = undefined;
}
}