Creating Piano with Processing

I've learn Processing for a month, I think Processing is GREAT tool for multimedia programming. There is many library that can help us to create a multimedia project like webcam application, music player, and the last i've creating piano like my piano with blender called piandu. But the library not user friendly for many operation system. They can work easily on Mac, but not on Linux and Windows, you need more trying and searching :D.

There is i want share my Piano application with processing. It's different with piandu, because Piandu creating with Blender and 3D visualization. But my piano with processing just with 2D visualization. There is step by step creating piano with processing.
piano application with processing


First, install soundcipher library on your desktop. If you using Ubuntu / Linux like me, change the SoundCipher.jar to be soundcipher. If you don't change the name, your library not recognized with Processing.


Second, after installed soundcipher open the Examples of soundcipher and open KeyTrigger by Andrew R. Brown like this
/**
* Create a music keyboard from the qwerty keyboard.
* Capture key presses and map the ascii key number 
* to a MIDI pitch number and play a note immediatly.
*
* A SoundCipher example by Andrew R. Brown
*/

import arb.soundcipher.*;

SoundCipher sc = new SoundCipher(this);

void keyPressed()
{
  sc.playNote(key - 40, 100, 0.5);
}

// keep processing 'alive'
void draw() {}

We will modifications this script for creating basic piano.

Third, inizialitation the variable like this

SoundCipher sc;
boolean[] keysNotePlay;
int[] keysNoteMap;

Fourth, write the code below for set the keyboard key with the sound. So you can set up the button for creating "do" sound. And don't forget to set the size of canvas like this script
keysNotePlay = new boolean[127];
  keysNoteMap = new int[127];
  keysNoteMap['a'] = 59;
  keysNoteMap['s'] = 60;
  keysNoteMap['d'] = 62;
  keysNoteMap['f'] = 64;
  keysNoteMap['g'] = 65;
  keysNoteMap['h'] = 67;
  keysNoteMap['j'] = 69;
  keysNoteMap['w'] = 61;
  keysNoteMap['e'] = 63;
  keysNoteMap['t'] = 66;
  keysNoteMap['y'] = 68;
  keysNoteMap['u'] = 70;
  size (250,150);

Fifth, write this script for stop the sound whenever you released the key on keyboard
void keyReleased(){
  keysNotePlay[key] = false;
}

Sixth, set the long of tut and color whenever you press or don't the key. You must set the position of rectangle too. So the script like this, i hope you can made this script more simple. can you :D? Please share to me ;)
void draw() {
  fill(255);
  if( keyPressed && keysNotePlay['a'] == true){
    fill(204);
  }
  rect (10, 10, 30, 100);
    
  fill(255);
    if( keyPressed && keysNotePlay['s'] == true){
    fill(204);
  }
  rect (40, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['d'] == true){
    fill(204);
  }
  rect (70, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['f'] == true){
    fill(204);
  }
  rect (100, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['g'] == true){
    fill(204);
  }
  
  rect (130, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['h'] == true){
    fill(204);
  }
  
  rect (160, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['j'] == true){
    fill(204);
  }
  
  rect (190, 10, 30, 100);

  //ireng
  fill(0);
  if( keyPressed && keysNotePlay['w'] == true){
    fill(204);
  }
  rect (32,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['e'] == true){
    fill(204);
  }
  rect (62,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['t'] == true){
    fill(204);
  }
  rect (122,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['y'] == true){
    fill(204);
  }
  rect (152,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['u'] == true){
    fill(204);
  }

Seventh, write this script for play the sound :D
if( keyPressed && keysNotePlay[key] == false){
    sc.playNote(keysNoteMap[key], 100, 1);
    keysNotePlay[key] = true; 

There is final code for creating piano with processing and soundcipher library.

/**
* Create a music keyboard from the qwerty keyboard.
* Capture key presses and map the ascii key number 
* to a MIDI pitch number and play a note immediatly.
*
* A SoundCipher example by Andrew R. Brown
*/

import arb.soundcipher.*;

SoundCipher sc;
boolean[] keysNotePlay;
int[] keysNoteMap;

void setup(){
  sc = new SoundCipher(this);
  keysNotePlay = new boolean[127];
  keysNoteMap = new int[127];
  keysNoteMap['a'] = 59;
  keysNoteMap['s'] = 60;
  keysNoteMap['d'] = 62;
  keysNoteMap['f'] = 64;
  keysNoteMap['g'] = 65;
  keysNoteMap['h'] = 67;
  keysNoteMap['j'] = 69;
  keysNoteMap['w'] = 61;
  keysNoteMap['e'] = 63;
  keysNoteMap['t'] = 66;
  keysNoteMap['y'] = 68;
  keysNoteMap['u'] = 70;
  size (250,150);
}

void keyReleased(){
  keysNotePlay[key] = false;
}

// keep processing 'alive'
void draw() {
//white tut 
  fill(255);
  if( keyPressed && keysNotePlay['a'] == true){
    fill(204);
  }
  rect (10, 10, 30, 100);
    
  fill(255);
    if( keyPressed && keysNotePlay['s'] == true){
    fill(204);
  }
  rect (40, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['d'] == true){
    fill(204);
  }
  rect (70, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['f'] == true){
    fill(204);
  }
  rect (100, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['g'] == true){
    fill(204);
  }
  
  rect (130, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['h'] == true){
    fill(204);
  }
  
  rect (160, 10, 30, 100);
    fill(255);
    if( keyPressed && keysNotePlay['j'] == true){
    fill(204);
  }
  
  rect (190, 10, 30, 100);
  
  //black tut
  fill(0);
  if( keyPressed && keysNotePlay['w'] == true){
    fill(204);
  }
  rect (32,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['e'] == true){
    fill(204);
  }
  rect (62,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['t'] == true){
    fill(204);
  }
  rect (122,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['y'] == true){
    fill(204);
  }
  rect (152,10,15,60);
  fill(0);
  if( keyPressed && keysNotePlay['u'] == true){
    fill(204);
  }
  rect (182,10,15,60);
  if( keyPressed && keysNotePlay[key] == false){
    sc.playNote(keysNoteMap[key], 100, 1);
    keysNotePlay[key] = true; 
  }
}

I hope you can playing this piano and enjoy this tutorial. Happy programming ;)

0 Response to "Creating Piano with Processing"

Post a Comment