const ChannelStripDevice = ({ input, output, send1, send2, send1Gain, send2Gain, send1Pre, send2Pre, gain, balance }) => {
  for (let c = 0; c < input.length; c++) {
    const inputChannel = input[c];
    const outputChannel = output[c];
    const send1Channel = send1[c];
    const send2Channel = send2[c];

    if (send1Pre) {
      multiply(inputChannel, send1Gain, send1Channel);
    }
    if (send2Pre) {
      multiply(inputChannel, send2Gain, send2Channel);
    }
    
    for (let s = 0; s < outputChannel.length; s++) {
      const balanceMultiplier = c === 0
        ? Math.min(1, -1 * (balance[s] - 1))
        : Math.min(1, balance[s] + 1);
      outputChannel[s] = inputChannel[s] * gain[s] * balanceMultiplier;
    }

    if (!send1Pre) {
      multiply(outputChannel, send1Gain, send1Channel);
    }
    if (!send2Pre) {
      multiply(outputChannel, send2Gain, send2Channel);
    }
  }
}

this.constructDevice = () => constructPerBufferDevice({
  implementation: ChannelStripDevice,
  inputs: {
    input: stereo('In'),
    gain: dbSetting('Gain', { defaultValue: 0, min: -Infinity, max: 12 }),
    balance: numberSetting('Balance', { defaultValue: 0, min: -1, max: 1 }),
    send1Gain: dbSetting('Send 1', { defaultValue: -Infinity, min: -Infinity, max: 12 }),
    send2Gain: dbSetting('Send 2', { defaultValue: -Infinity, min: -Infinity, max: 12 }),
  },
  constants: {
    send1Pre: booleanConstant('Send 1 Pre-Fader', false),
    send2Pre: booleanConstant('Send 2 Pre-Fader', false),
  },
  outputs: {
    output: stereo('Out'),
    send1: stereo('Send 1'),
    send2: stereo('Send 2')
  },
  layout: row([
    titled.knob('gain'),
    titled.knob('balance'),
    titled.box(
      'Sends',
      row([
        col([
          spacer(15),
          altCheckbox('send1Pre', 'Pre-Fader', knob('send1Gain', { size: 25 })),
        ]),
        col([
          altCheckbox('send2Pre', 'Pre-Fader', knob('send2Gain', { size: 25 })),
          spacer(15),
        ]),
      ])
    )
  ])
});
