File size: 701 Bytes
6ee6b5e
 
 
 
a6fc220
6ee6b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class PCMProcessor extends AudioWorkletProcessor {
  constructor(options) {
    super();
    this.sampleBuffer = [];
    this.chunkSize = options.processorOptions.chunkSize || 16_000;
  }

  process(inputs) {
    const input = inputs[0];
    if (input.length > 0) {
      const inputData = input[0];
      for (let i = 0; i < inputData.length; ++i) {
        this.sampleBuffer.push(inputData[i]);
        if (this.sampleBuffer.length >= this.chunkSize) {
          this.port.postMessage(this.sampleBuffer.slice(0, this.chunkSize));
          this.sampleBuffer = this.sampleBuffer.slice(this.chunkSize);
        }
      }
    }
    return true;
  }
}

registerProcessor("pcm-processor", PCMProcessor);