Close

Synth implementation : key release handling

A project log for PC Keyboard becomes synthesizer / drum computer

Converted an old PS2 keyboard into a synthesizer/drum computer/... using arduino nano.

serdefserdef 07/27/2014 at 14:020 Comments

The teensy ps/2 keyboard library was extended to support notification of key releases.

I maintained backwards compatibility and added 2 new public methods :

add 'rel' to the usual method, and provide an iskeyrelease bool parameter. 

this parameters is referenced and set to true when the keypress was a release..

Only a few keys are supported right now. I plan to refactor  the code to eliminate redundancy and implement full playable keyboard. 

So the code below shows a simple example of sounding a synth sound as long as key 'c' is pressed.

void loop() {

...

if (keyboard.availablerel()) {

// read the next key

bool iskeyrelease = false;

char c = keyboard.readrel(iskeyrelease);

KeyboardHandler(c,iskeyrelease);

}

..

}

#define _SYNTHPRESS_STATUSCODE 0x90

#define _SYNTHRELEASE_STATUSCODE 0x80

void KeyboardHandler(char key,bool iskeyrelease)

{

  static bool c_pressed = false;
  byte synthstatuscode;
 byte volumecode;

 synthstatuscode = iskeyrelease ? _SYNTHRELEASE_STATUSCODE : _SYNTHPRESS_STATUSCODE;
 volumecode = iskeyrelease ? 0x00 : leadvolume;

   if(key=='c')

    {

       if(!c_pressed || iskeyrelease)

      {

        midiwrite(synthstatuscode, 40, volumecode);

      }

      c_pressed = !iskeyrelease;

      return;

   }

}

Discussions