Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Midi Note Numbers To Name and Octave

Does anybody know of anything that exists in the Java world to map midi note numbers to specific note names and octave numbers. For example, see the reference table:

http://www.harmony-central.com/MIDI/Doc/table2.html

I want to map a midi note number 60 to it's corresponding note name (MiddleC) in octave 4. I could write a utility class/enum for this, but it would be rather tedious. Does anybody know of anything?

I'm specifically using this to write a Tenori-On/Monome clone in Java, so far so good...

Solution

This was what I ended up using:

String[] noteString = new String[] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };

int octave = (initialNote / 12) - 1;
int noteIndex = (initialNote % 12);
String note = noteString[noteIndex];
like image 608
Jon Avatar asked Apr 03 '09 05:04

Jon


People also ask

What MIDI number is middle C?

Middle C (the fourth C key from left on a standard 88-key piano keyboard) is designated C4 in scientific pitch notation, and c′ in Helmholtz pitch notation; it is note number 60 in MIDI notation.

What is the highest MIDI note number?

Note pitches are represented as in the MIDI specification, using integers from 0 (lowest pitch) to 127 (highest pitch).

How many MIDI numbers are there?

The General MIDI standard includes 47 percussive sounds, using note numbers 35-81 (of the possible 128 numbers from 0-127), as follows: 35 Acoustic Bass Drum.


1 Answers

I'm not convinced your suggestion is that tedious. It's really just a divide-and-modulo operation, one gets the octave, the other gets the note.

octave = int (notenum / 12) - 1;
note = substring("C C#D D#E F F#G G#A A#B ",(notenum % 12) * 2, 2);

In real Java, as opposed to that pseudo-code above, you can use something like:

public class Notes {
  public static void main(String [] args) {
    String notes = "C C#D D#E F F#G G#A A#B ";
    int octv;
    String nt;
    for (int noteNum = 0; noteNum < 128; noteNum++) {
      octv = noteNum / 12 - 1;
      nt = notes.substring((noteNum % 12) * 2, (noteNum % 12) * 2 + 2);
      System.out.println("Note # " + noteNum + " = octave " + octv + ", note " + nt);
    }
  }
}
like image 60
paxdiablo Avatar answered Oct 21 '22 07:10

paxdiablo