Minim Clipping When Changing Notes

I'm trying to build a polyphonic synth in Minim using an array of Oscil ugens that I've put into classes. My problem is that whenever I change note I get a short click sound. If I play a 'x' amounts of notes simultaneously it's fine, but adding one by one causes a click to appear.

void KeyPress()
{
  keysPressed=0; //FOR EACH KEYPRESSED, ADD ONE TO TO AMP
  for (int i = 0; i < keys.length; i++) osc[i].adsr.unpatch(out); //Reset patches when keyPressed

  for (int i = 0; i < keys.length; i++)
  {

    if (key == keys[i]) //if keypressed is equal to an element from my array keys
    {
      keyState[i] = true; //set corresponding boolean from keyState array to true
    }
    if (keyState[i]) //for each active keyState add one to keypressed
    {

      keysPressed++;
    }
  }

  if (keysPressed == 0) amp = MAXAMP; //no keyspressed amplitude is equal to maxamp
  else amp=MAXAMP/keysPressed; //divide amplitude by amount of keyspressed, otherwise sound distorts

  for (int i = 0; i < keys.length; i++) osc[i].oscil.setAmplitude(amp); //set amplitude of each class individually to current amp

  for (int i=0;i<keys.length;i++)
  {
    if (keyState[i] && key == keys[i]) //if keyState[i] is true and corresponding key is active
    {
      osc[i].adsr.unpatch(out); //unpatch
      freq = Frequency.ofPitch(notes[i]+oct); //set freq to frequency
      osc[i].oscil.setFrequency(freq); //set freq
      osc[i].noteOn(); //initialise noteOn
    }
    else if (keyState[i]) { //if only keyState[i] is true, reset patch and turn noteOn, this ensures polyphony for keypressed
      osc[i].adsr.unpatch(out);
      osc[i].noteOn();
    }
  }  
}

Thanks for any assistance!

Sign In or Register to comment.