const TransientSplitter = ({ input, attack, sustain, speed, sensitivity }) => {
  const slowAmplitude = useVal(0);
  const transientness = useVal(0);
  const vuMeter = useVUFollower();

  for (let i = 0; i < input[0].length; i ++) {
    const fastAmplitude = Math.max(...input.map(channel => Math.abs(channel[i])));
    slowAmplitude.current = vuMeter(fastAmplitude);

    if ((fastAmplitude / slowAmplitude.current) > (1 - Math.pow(sensitivity[i] / 10, 2)) * 14) {
      transientness.current = 1;
    }

    for (let c = 0; c < input.length; c ++) {
      attack[c][i] = transientness.current * input[c][i];
      sustain[c][i] = (1 - transientness.current) * input[c][i];
    }

    transientness.current *= 1 - Math.pow(10 - speed[i], -4);
  }
};

this.constructDevice = () => constructPerBufferDevice({
  implementation: TransientSplitter,
  inputs: {
    input: stereo('In'),
    speed: numberSetting('Speed', { defaultValue: 4, min: 0, max: 10 }),
    sensitivity: numberSetting('Sensitivity', { defaultValue: 9, min: 0, max: 10 }),
  },
  outputs: {
    attack: stereo('Attack'),
    sustain: stereo('Sustain'),
  }
});
