Java – converts MIDI counts to names and octaves

Does anyone know anything in the Java world that maps MIDI note numbers to specific note names and octaves 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 its corresponding note name (middlec) in octave 4 I can write a utility class / enumeration for this, but it will be quite boring Does anyone know anything?

I'm using this method to write tenori on / mone clones in Java, so good so far

solution

This is my last use:

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];

Solution

I don't believe your suggestion is boring It's really just a modulo operation, one gets an octave and the other gets a 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, contrary to the pseudo code above, you can use something similar to the following:

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);
    }
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>