Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 91d8c168 authored by Simon Chan's avatar Simon Chan
Browse files

feat(pcm): add a stop method

parent 14abce47
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ export abstract class PcmPlayer<T> {
    #channelCount: number;
    #worklet: AudioWorkletNode | undefined;
    #buffers: T[] = [];
    #stopped = false;

    constructor(sampleRate: number, channelCount: number) {
        this.#context = new AudioContext({
@@ -17,6 +18,10 @@ export abstract class PcmPlayer<T> {
    protected abstract feedCore(worklet: AudioWorkletNode, source: T): void;

    feed(source: T) {
        if (this.#stopped) {
            throw new Error("PcmPlayer is stopped");
        }

        if (this.#worklet === undefined) {
            this.#buffers.push(source);
            return;
@@ -30,6 +35,10 @@ export abstract class PcmPlayer<T> {
            new URL("./worker.js", import.meta.url),
        );

        if (this.#stopped) {
            return;
        }

        this.#worklet = new AudioWorkletNode(this.#context, this.sourceName, {
            numberOfInputs: 0,
            numberOfOutputs: 1,
@@ -44,6 +53,11 @@ export abstract class PcmPlayer<T> {
    }

    async stop() {
        if (this.#stopped) {
            return;
        }
        this.#stopped = true;

        this.#worklet?.disconnect();
        this.#worklet = undefined;