import type { AudioProcessor, ProcessorCategory } from "./AudioProcessor";

export class DCOffsetRemover implements AudioProcessor {
  getName(): string {
    return "DC Offset Remover";
  }

  getDescription(): string {
    return "Removes DC offset bias from the audio signal";
  }

  getCategory(): ProcessorCategory {
    return 'Utility';
  }

  async process(
    leftChannel: Float32Array,
    rightChannel: Float32Array
  ): Promise<[Float32Array, Float32Array]> {
    const leftOffset = this.calculateDCOffset(leftChannel);
    const rightOffset = this.calculateDCOffset(rightChannel);

    const newLeft = new Float32Array(leftChannel.length);
    const newRight = new Float32Array(rightChannel.length);

    for (let i = 0; i < leftChannel.length; i++) {
      newLeft[i] = leftChannel[i] - leftOffset;
      newRight[i] = rightChannel[i] - rightOffset;
    }

    return [newLeft, newRight];
  }

  private calculateDCOffset(channel: Float32Array): number {
    let sum = 0;
    for (let i = 0; i < channel.length; i++) {
      sum += channel[i];
    }
    return sum / channel.length;
  }
}
