const ConvolutionReverb = ({ input, mix, output, impulseResponse }) => {
  const leftReverb = useReverb(impulseResponse.channelDataList[0]);
  const rightResponse = impulseResponse.channelDataList.length > 1 ? impulseResponse.channelDataList[1] : impulseResponse.channelDataList[0];
  const rightReverb = useReverb(rightResponse);
  const leftReverbOutput = leftReverb(input[0]);
  const rightReverbOutput = rightReverb(input[1]);
  multiply(leftReverbOutput, mix, leftReverbOutput);
  multiply(rightReverbOutput, mix, rightReverbOutput);

  const impulseScale = useConstantBuffer(0.0625);
  multiply(leftReverbOutput, impulseScale, leftReverbOutput);
  multiply(rightReverbOutput, impulseScale, rightReverbOutput);

  const inverseMix = useMonoSignal();
  const one = useConstantBuffer(1);
  subtract(one, mix, inverseMix);

  const scaledInput = useStereoSignal();
  multiply(input[0], inverseMix, scaledInput[0]);
  multiply(input[1], inverseMix, scaledInput[1]);

  add(leftReverbOutput, scaledInput[0], output[0]);
  add(rightReverbOutput, scaledInput[1], output[1]);
};

this.constructDevice = () => constructPerBufferDevice({
  implementation: ConvolutionReverb,
  inputs: {
    input: stereo('In'),
    mix: zeroOneSetting('Mix', 0.5)
  },
  constants: {
    impulseResponse: audioBufferConstant('Impulse Response', '5a0b3743-4356-4d99-b0c7-8371c165ffe2'),
  },
  outputs: {
    output: stereo('Out'),
  },
  sharedArrays: {
    inputLevel: 1,
    reverbLevel: 1,
  },
  layout: themed(
    DeviceLayoutTheme.Dark,
    col([
      row([
        audioBufferPicker('impulseResponse'),
        titled.knob('mix')
      ])
    ])
  ),
  presetTags: {
    'Length': [
      'Long',
      'Short'
    ],
    'Environment': [
      'Church',
      'Concert Hall',
      'Spring',
      'Plate',
      'Synthetic',
      'Other'
    ]
  },
});
