Separating Midi Keyboard into Octaves/Sections

Hey Guys,

Fairly new to coding so apologies if this is an easy fix, I’ve been stumbling my way through this code up until this point.

Essentially, the code produces a circle - with size depending on what note it pressed and colour depending on how hard the note is pressed.

My question is: Is it possible to have an if(),while(),or for() statement that would identify a note that has been pressed, greater than one number BUT less than another, and run the code in that section.

i.e

if(note < 10 > 20)
ellipseMode(CENTER);
ellipse(x, y,note2, note2);
fill(HSB,vel2, vel2, 255);
noStroke();

import themidibus.*;
import javax.sound.midi.MidiMessage; 

MidiBus myBus; 

int currentColor = 0;
int midiDevice  = 0;
int note = 0 ;
int vel = 0 ;
float x = random(1, 1920);
float y = random(1, 1080);
float left = 320 ;
float right = 960;



void setup() {
  size(1920, 1080);
  MidiBus.list(); 
  myBus = new MidiBus(this, midiDevice, 1); 
  background(255);
}

void draw() {
 
    ellipseMode(CENTER);
    ellipse(x, y,note*2, note*2);
    fill(HSB,vel*2, vel*2, 255);
    noStroke();
    
    rectMode(CENTER);
    rect(x*2, y*2, note*2, note*2);
    fill(HSB,vel*2, 255, vel*2);
    noStroke();
  
}


void midiMessage(MidiMessage message, long timestamp, String bus_name) { 
  if(message.getMessage().length > 2){

    note = (int)(message.getMessage()[1] & 0xFF) ;
    vel = (int)(message.getMessage()[2] & 0xFF);
    x = random(1, 1920);
    y = random(1, 1080);
    
  if(note == 64){
    left = (vel * 5.04) + 640 ;
    right = 1920 - left ;
  }
 
    println("Bus " + bus_name + ": Note "+ note + ", vel " + vel);
    
  }
}

Any General help or ideas would be greatly appreciated!
(Ps. sorry if this is in the wrong section - new to the forum!)

1 Like

Please be more explicit about what you are trying to accomplish, not just how you are trying to do it. Is your goal to have the middle octave be circles, and the upper octave be rectangles? Is your the goal to have all the D keys be green, and all the E keys be blue?

To answer your question literally, yes. You can find the MIDI piano-equivalent key numbers here:

So the first full octave starts at 24 and goes to 35. It continues like this:

24-35
36-47
48-59
60-71
72-83
84-95
96-107

So, you can replace your if(note == 64) with

if(note >= 24 && note <= 35){
  // do something
}
if(note >= 36 && note <= 47){
  // do something
}
// etc.

Or you can cascade through the options with else:

if(note <= 35){
  // do something
}
else if(note <= 47){
  // do something
}
else if(note <= 59){
}
// etc.

Note that your “do something” should probably use the note variable. I’m not sure, but left = (vel * 5.04) + 640 ; should probably be (?) left = (vel * 5.04) + note * 10 ; …? Then you won’t be writing a code block for each individual note.

1 Like

Hey Jeremy! Thanks for replying!

My goal is exactly what you’ve outlined, to section off the octaves into different colours. I hadn’t thought about using &&.

The if(note == 64) section is actual controlling something else already! Strangely enough the keyboard I’m using has the pedal assigned as note 64, as well as E4 on the keyboard being note 64.

Thanks for the answer, i’ll reply when i’ve updated the code to what i want it to do!

Keep in mind that you can also generate an octave number

So, if you had a list of colors:

int[] myColors = new int[]{
  color(0),
  color(32),
  color(64),
  color(96),
  color(128),
  color(160),
  color(192),
  color(224),
  color(255)
};

Then you could just assign the color based on the note’s current octave, something like this:

int octaveNum = (note - 23)/11;
fill(myColors[octaveNum]);
1 Like