Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Destructable } from "../Common/Destructable";
import { ComputeWorker } from "../Common/Worker";
 
export class SplitChannel extends Destructable {
  static usage = 0;
  channelCount = 1;
  static worker: ComputeWorker | undefined;
 
  constructor(channelCount: number) {
    super();
    SplitChannel.usage++;
    if (!SplitChannel.worker) {
      // eslint-disable-next-line
      // @ts-ignore
      SplitChannel.worker = new ComputeWorker(new Worker(new URL("./SplitChannelWorker.ts", import.meta.url)));
    }
    this.channelCount = channelCount;
  }
 
  destroy() {
    SplitChannel.usage--;
    if (SplitChannel.usage === 0) {
      SplitChannel.worker?.destroy();
      SplitChannel.worker = undefined;
    }
    super.destroy();
  }
 
  async split(value: Float32Array): Promise<Float32Array[]> {
    if (!SplitChannel.worker) throw new Error("AudioDecoder: worker not initialized");
 
    return SplitChannel.worker.compute({
      value,
      channelCount: this.channelCount,
    });
  }
}