Loading...
Logo
Processing Forum
Hi guys, I'm currently working on editing a piece of code from  http://krazydad.com/processing_music/ I've managed to get it working roughly how I wanted, but there's a few gripes I'm thus far unable to get my head around. 

This is what I'm struggling with; 

1; I want the visual to draw in for however long I hold a note on my midi keyboard 
2; I don't want the visual to disappear when I strike the note for a second time

Here's the code I'm working with, once again many thanks to Jim (KrazyDad) for the original code! 

Copy code
  1. import rwmidi.*; //Import the library

  2. MidiOutput myOutput; // The MidiBus
  3. MidiInput  myInput;

  4. boolean midiThru = false;  // echo all received midi in messages to the midi out device

  5. int  maxNotes = 128;


  6. class MNote {
  7.   long   startTime;
  8.   float  ax;
  9.   float  ay;
  10.   float  bx;
  11.   float  by;
  12.   float  cx;
  13.   float  cy;
  14.   float  dx;
  15.   float  dy;
  16.   float  ex;
  17.   float  ey;
  18.   float  fx;
  19.   float  fy;
  20.   float  gx;
  21.   float  gy;
  22.   float  hx;
  23.   float  hy;
  24.   float  initRad;
  25.   float  weight;
  26.   float  kMinVelocity = 10;
  27.   float  kMaxVelocity = 100;
  28.   float a = 100;
  29.   int    pitch;
  30.   int    velocity;
  31.   
  32.   
  33.   boolean  isOn = false;

  34.   MNote(int pitch) {
  35.     this.pitch = pitch;
  36.   }
  37.   
  38.   void init(int pitch, int velocity)
  39.   {
  40.     startTime = millis();
  41.     int  rotation = 50000;
  42.     int currentTime = millis();
  43.     float m = (currentTime%rotation)* PI*2 / rotation; //creates motion
  44.     
  45.     this.ax = (sin(m)*a+screen.width/2);
  46.     this.ay = (cos(m)*a+screen.height/2);
  47.     this.bx = (sin(m)*(a+40)+screen.width/2);
  48.     this.by = (cos(m)*(a+40)+screen.height/2);
  49.     this.cx = (sin(m)*(a+80)+screen.width/2);
  50.     this.cy = (cos(m)*(a+80)+screen.height/2);
  51.     this.dx = (sin(m)*(a+120)+screen.width/2);
  52.     this.dy = (cos(m)*(a+120)+screen.height/2);
  53.     this.ex = (sin(m)*(a+160)+screen.width/2);
  54.     this.ey = (cos(m)*(a+160)+screen.height/2);
  55.     this.fx = (sin(m)*(a+200)+screen.width/2);
  56.     this.fy = (cos(m)*(a+200)+screen.height/2);
  57.     this.gx = (sin(m)*(a+240)+screen.width/2);
  58.     this.gy = (cos(m)*(a+240)+screen.height/2);
  59.     this.hx = (sin(m)*(a+280)+screen.width/2);
  60.     this.hy = (cos(m)*(a+280)+screen.height/2);
  61.     
  62.     this.pitch = pitch;
  63.     this.velocity = velocity;
  64.     this.isOn = true;
  65.   }
  66.   
  67.   void render()
  68.   {
  69.    if (!isOn)
  70.       return;
  71.       
  72.     float hueV = (this.pitch % 12) / 12.0;
  73.     float lum = constrain(1+pow(4,2), .05, 1);
  74.     float sat = lum;
  75.      
  76.     noStroke();
  77.     
  78.     fill(hueV,sat,lum,20);
  79.     ellipse(ax,ay,40,40);
  80.     
  81.     fill(hueV,sat,lum,20);
  82.     ellipse(bx,by,40,40);
  83.     
  84.     fill(hueV,sat,lum,20);
  85.     ellipse(cx,cy,40,40);
  86.     
  87.     fill(hueV,sat,lum,20);
  88.     ellipse(dx,dy,40,40);
  89.     
  90.     fill(hueV,sat,lum,20);
  91.     ellipse(ex,ey,40,40);
  92.     
  93.     fill(hueV,sat,lum,20);
  94.     ellipse(fx,fy,40,40);
  95.     
  96.     fill(hueV,sat,lum,20);
  97.     ellipse(gx,gy,40,40);
  98.       
  99.     fill(hueV,sat,lum,20);
  100.     ellipse(hx,hy,40,40);
  101.     
  102.   }
  103. }

Replies(3)

I don't know the Midi library (but because of it I moved the topic from Programming Questions to this one), so I can't help on your specific question. But here too, you could use an array to reduce the amount of code...
I'm pretty sure that MIDI devices send separate messages for key-on and key-off events. You could just write your code to begin drawing in your visual at the key-on event, and continue to draw in until a key-off event is detected.

Maybe you can find some useful info here:
Your MNote class can be shortened as follows.  You question pertains more to the way you are triggering it, which involves the parts of the code you haven't shown.  In your class, I've eliminated a lot of unnecessary variables, and used a loop to shorten the code.  Assuming you are drawing one MNote per note, there is no need to precompute the ellipse positions as you did.

Copy code
  1. class MNote {
  2.   int    pitch;
  3.   int    velocity;
  4.   boolean  isOn = false;

  5.   MNote(int pitch) {
  6.     this.pitch = pitch;
  7.   }
  8.   
  9.   void init(int pitch, int velocity)
  10.   {
  11.     startTime = millis();
  12.     
  13.     this.pitch = pitch;
  14.     this.velocity = velocity;
  15.     this.isOn = true;
  16.   }
  17.   
  18.   void render()
  19.   {
  20.    if (!isOn)
  21.       return;
  22.       
  23.     float hueV = (this.pitch % 12) / 12.0;
  24.     float lum = constrain(1+pow(4,2), .05, 1);
  25.     float sat = lum;
  26.      
  27.     noStroke();
  28.     
  29.     fill(hueV,sat,lum,20);
  30.     for (int i = 0; i < 8; ++i) {
  31.       ellipse(sin(m)*(a+40*i)+screen.width/2,
  32.               cos(m)*(a+40*i)+screen.height/2,40,40);
  33.     }
  34.   }
  35. }