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

export class RingModulator implements AudioProcessor {
  getName(): string {
    return "Ring Modulator";
  }

  getDescription(): string {
    return "Frequency modulation for metallic, bell-like tones";
  }

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

  async process(
    leftChannel: Float32Array,
    rightChannel: Float32Array
  ): Promise<[Float32Array, Float32Array]> {
    const sampleRate = 44100;
    const carrierFreq = 20 + Math.random() * 400;
    const mix = 0.5 + Math.random() * 0.5;

    const freqModDepth = Math.random() * 50;
    const freqModRate = 0.1 + Math.random() * 2;

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

    for (let i = 0; i < leftChannel.length; i++) {
      const t = i / sampleRate;

      const modulation = Math.sin(2 * Math.PI * freqModRate * t);
      const modulatedFreq = carrierFreq + (modulation * freqModDepth);

      const carrier = Math.sin(2 * Math.PI * modulatedFreq * t);

      const modulatedL = leftChannel[i] * carrier;
      const modulatedR = rightChannel[i] * carrier;

      newLeft[i] = leftChannel[i] * (1 - mix) + modulatedL * mix;
      newRight[i] = rightChannel[i] * (1 - mix) + modulatedR * mix;
    }

    return [newLeft, newRight];
  }
}
