Java examples for javax.sound.midi:MidiMessage
Creates a human-readable string for a MIDI ShortMessage
//package com.java2s; import java.util.Arrays; import java.util.List; import javax.sound.midi.MidiMessage; import javax.sound.midi.ShortMessage; public class Main { private static List<String> numList = Arrays.asList(new String[] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }); /**/*w w w. ja v a 2 s.c om*/ * Creates a human-readable string for a MIDI ShortMessage * @param sm - the ShortMessage to use * @return a String representation of the ShortMessage */ public static String toString(MidiMessage mm) { if (mm instanceof ShortMessage) { final ShortMessage sm = (ShortMessage) mm; if (isNoteOn(sm)) { return "Note On ch=" + sm.getChannel() + " note=" + sm.getData1() + " (" + noteNumberToName(sm.getData1()) + ") vel=" + sm.getData2(); } else if (isNoteOff(sm)) { return "Note Off ch=" + sm.getChannel() + " note=" + sm.getData1() + " (" + noteNumberToName(sm.getData1()) + ")"; } else if (isControlChange(sm)) { return "Control Change ch=" + sm.getChannel() + " control=" + sm.getData1() + " value=" + sm.getData2(); } else if (isPitchBend(sm)) { return "Pitch Bend ch=" + sm.getChannel() + " value=" + sm.getData2(); } return "ShortMessage[ch=" + (sm.getChannel() + 1) + " cmd=" + sm.getCommand() + " d1=" + sm.getData1() + " d2=" + sm.getData2() + "]"; } else { return mm.getClass().getSimpleName() + " " + Arrays.toString(mm.getMessage()); } } /** * Determines if the given ShortMessage is a MIDI NOTE ON message. Note * that the MIDI specification says that a NOTE ON with velocity=0 is * equivalent to a NOTE OFF. * @param sm - the ShortMessage to check * @return <tt>true</tt> if the ShortMessage is a MIDI NOTE ON message */ public static boolean isNoteOn(ShortMessage sm) { return sm.getCommand() == ShortMessage.NOTE_ON && sm.getData2() > 0; } /** * Converts a MIDI number into a note name in scientific pitch notation * @param number - the MIDI number of the note * @return the note name in scientific pitch notation (e.g., A4, C#3, F#5) */ public static String noteNumberToName(int number) { int octave = (number / 12) - 1; int noteIdx = number % 12; return (numList.get(noteIdx)).toUpperCase() + octave; } /** * Determines if the given ShortMessage is a MIDI NOTE OFF message. Note * that the MIDI specification says that a NOTE ON with velocity=0 is * equivalent to a NOTE OFF. * @param sm - the ShortMessage to check * @return <tt>true</tt> if the ShortMessage is a MIDI NOTE OFF message */ public static boolean isNoteOff(ShortMessage sm) { final int command = sm.getCommand(); return command == ShortMessage.NOTE_OFF || (command == ShortMessage.NOTE_ON && sm.getData2() == 0); } /** * Determines if the given ShortMessage is a MIDI Control Change message * @param sm - the ShortMessage to check * @return <tt>true</tt> if the ShortMessage is a MIDI Control Change message */ public static boolean isControlChange(ShortMessage sm) { return sm.getCommand() == ShortMessage.CONTROL_CHANGE; } /** * Determines if the given ShortMessage is a MIDI Pitch Bend message * @param sm the ShortMessage to check * @return <tt>true</tt> if the ShortMessage is a MIDI Pitch Bend message */ public static boolean isPitchBend(ShortMessage sm) { return sm.getCommand() == ShortMessage.PITCH_BEND; } }