const sampleRateMultiplier = sampleRate / 1000;

const FlangerEffect = ({ input, output, mix, feedback, lfoFreq, lfoAmount, milliseconds }) => {
  const delay = useDelay();
  const oscillator = useOscillator(Waveform.Sine, false);
  const oscillatorOutput = oscillator(lfoFreq);
  const delaySamples = useMonoSignal();
  multiply(milliseconds, useConstantBuffer(sampleRateMultiplier), delaySamples);
  multiply(oscillatorOutput, lfoAmount, oscillatorOutput);
  add(oscillatorOutput, delaySamples, delaySamples);
  const delayed = delay(input, delaySamples, feedback);
  for (let i = 0; i < input.length; i ++) {
    output[i] = ((1 - mix[i]) * input[i]) + (mix[i] * delayed[i]);
  }
};

this.constructDevice = () => constructPerChannelDevice({
  implementation: FlangerEffect,
  inputs: {
    input: stereo('In'),
    mix: numberSetting('Mix', { defaultValue: 0.5, min: 0, max: 1 }),
    lfoFreq: numberSetting('LFO Freq', { defaultValue: 1, min: 0.1, max: 10 }),
    lfoAmount: numberSetting('LFO Amount', { defaultValue: 7.5, min: 0, max: 10 }),
    feedback: numberSetting('Feedback', { defaultValue: 0.5, min: 0, max: 1 }),
    milliseconds: numberSetting('milliseconds', { defaultValue: 12, min: 0, max: 25 }),
  },
  outputs: {
    output: stereo('Out'),
  }
});
