Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6e31f69

Browse files
authored
Reuse single PlaybackWorker between Playback instances (#12520)
* Terminate playback worker on destroy Signed-off-by: Michael Telatynski <[email protected]> * Reuse single PlaybackWorker between all Playbacks Signed-off-by: Michael Telatynski <[email protected]> * ... Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 51f7156 commit 6e31f69

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/PlaybackEncoder.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2024 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// @ts-ignore - `.ts` is needed here to make TS happy
18+
import { Request, Response } from "./workers/playback.worker";
19+
import { WorkerManager } from "./WorkerManager";
20+
import playbackWorkerFactory from "./workers/playbackWorkerFactory";
21+
22+
export class PlaybackEncoder {
23+
private static internalInstance = new PlaybackEncoder();
24+
25+
public static get instance(): PlaybackEncoder {
26+
return PlaybackEncoder.internalInstance;
27+
}
28+
29+
private readonly worker = new WorkerManager<Request, Response>(playbackWorkerFactory());
30+
31+
public getPlaybackWaveform(input: Float32Array): Promise<number[]> {
32+
return this.worker.call({ data: Array.from(input) }).then((resp) => resp.waveform);
33+
}
34+
}

src/audio/Playback.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ import { SimpleObservable } from "matrix-widget-api";
1919
import { logger } from "matrix-js-sdk/src/logger";
2020
import { defer } from "matrix-js-sdk/src/utils";
2121

22-
// @ts-ignore - `.ts` is needed here to make TS happy
23-
import { Request, Response } from "../workers/playback.worker.ts";
2422
import { UPDATE_EVENT } from "../stores/AsyncStore";
2523
import { arrayFastResample } from "../utils/arrays";
2624
import { IDestroyable } from "../utils/IDestroyable";
2725
import { PlaybackClock } from "./PlaybackClock";
2826
import { createAudioContext, decodeOgg } from "./compat";
2927
import { clamp } from "../utils/numbers";
30-
import { WorkerManager } from "../WorkerManager";
3128
import { DEFAULT_WAVEFORM, PLAYBACK_WAVEFORM_SAMPLES } from "./consts";
32-
import playbackWorkerFactory from "../workers/playbackWorkerFactory";
29+
import { PlaybackEncoder } from "../PlaybackEncoder";
3330

3431
export enum PlaybackState {
3532
Decoding = "decoding",
@@ -64,7 +61,6 @@ export class Playback extends EventEmitter implements IDestroyable, PlaybackInte
6461
private waveformObservable = new SimpleObservable<number[]>();
6562
private readonly clock: PlaybackClock;
6663
private readonly fileSize: number;
67-
private readonly worker = new WorkerManager<Request, Response>(playbackWorkerFactory());
6864

6965
/**
7066
* Creates a new playback instance from a buffer.
@@ -209,7 +205,9 @@ export class Playback extends EventEmitter implements IDestroyable, PlaybackInte
209205

210206
// Update the waveform to the real waveform once we have channel data to use. We don't
211207
// exactly trust the user-provided waveform to be accurate...
212-
this.resampledWaveform = await this.makePlaybackWaveform(this.audioBuf.getChannelData(0));
208+
this.resampledWaveform = await PlaybackEncoder.instance.getPlaybackWaveform(
209+
this.audioBuf.getChannelData(0),
210+
);
213211
}
214212

215213
this.waveformObservable.update(this.resampledWaveform);
@@ -222,10 +220,6 @@ export class Playback extends EventEmitter implements IDestroyable, PlaybackInte
222220
this.emit(PlaybackState.Stopped); // signal that we're not decoding anymore
223221
}
224222

225-
private makePlaybackWaveform(input: Float32Array): Promise<number[]> {
226-
return this.worker.call({ data: Array.from(input) }).then((resp) => resp.waveform);
227-
}
228-
229223
private onPlaybackEnd = async (): Promise<void> => {
230224
await this.context.suspend();
231225
this.emit(PlaybackState.Stopped);

0 commit comments

Comments
 (0)