# Separating Midi Keyboard into Octaves/Sections

**URL:** https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944
**Category:** Libraries
**Created:** [May 5, 2019, 1:51pm UTC](https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944 "2019-05-05T13:51:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![KIE13579](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@KIE13579](https://discourse.processing.org/u/KIE13579)
#### Post date: [May 5, 2019, 1:51pm UTC](https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944/1 "2019-05-05T13:51:50Z")

</div>

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,note_2, note_2);  
fill(HSB,vel_2, vel_2, 255);  
noStroke();

```auto
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!)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 6, 2019, 4:36pm UTC](https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944/2 "2019-05-06T16:36:21Z")

</div>

> [@KIE13579](#):
>
> 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.

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:

> **[Note names, MIDI numbers and frequencies](https://newt.phys.unsw.edu.au/jw/notes.html)**
>
> Note names, MIDI numbers and frequencies

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e7af0caea1df5a563aa99e6ac37a4e9c47a50cf5.gif)

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

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

```

Or you can cascade through the options with else:

```auto
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.

---

<div class="post-metadata">

### Author: ![KIE13579](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@KIE13579](https://discourse.processing.org/u/KIE13579)
#### Post date: [May 6, 2019, 5:27pm UTC](https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944/3 "2019-05-06T17:27:41Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 6, 2019, 10:26pm UTC](https://discourse.processing.org/t/separating-midi-keyboard-into-octaves-sections/10944/4 "2019-05-06T22:26:33Z")

</div>

Keep in mind that you can also generate an octave number

So, if you had a list of colors:

```auto
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:

```auto
int octaveNum = (note - 23)/11;
fill(myColors[octaveNum]);

```
