class AudioProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this._buffer = new Float32Array(4096);
    this._bufferIndex = 0;
  }

  process(inputs) {
    const input = inputs[0];
    if (input.length > 0) {
      const samples = input[0];
      for (let i = 0; i < samples.length; i++) {
        this._buffer[this._bufferIndex++] = samples[i];
        if (this._bufferIndex >= 4096) {
          // Transfer ownership (zero-copy) to main thread
          this.port.postMessage(this._buffer.buffer, [this._buffer.buffer]);
          this._buffer = new Float32Array(4096);
          this._bufferIndex = 0;
        }
      }
    }
    return true;
  }
}

registerProcessor('audio-processor', AudioProcessor);
