Java examples for javax.sound.midi:MidiEvent
Return the number of note on and note off events in track for MIDI.
//package com.java2s; import javax.sound.midi.MidiMessage; import javax.sound.midi.MidiEvent; import javax.sound.midi.Track; public class Main { /**//from w w w . ja v a 2s . c o m * Return the number of note on and note off events in track. */ public static int noteOnOffsInTrack(Track track) { int count = 0; for (int i = 0; i < track.size(); i++) { MidiEvent event = track.get(i); if (isNoteOnEvent(event) || isNoteOffEvent(event)) { count++; } } return count; } /** * Return true if event is a Note On event. */ public static boolean isNoteOnEvent(MidiEvent event) { return isNoteOnMessage(event.getMessage()); } /** * Return true if event is a Note Off event. */ public static boolean isNoteOffEvent(MidiEvent event) { return isNoteOffMessage(event.getMessage()); } /** * Return true if message is a Note On message. */ public static boolean isNoteOnMessage(MidiMessage message) { // The status can be a range of values, depending on what channel it is on. // Also, the velocity cannot be zero, otherwise it is a note off message. return message.getStatus() >= 144 && message.getStatus() < 160 && getVelocity(message) > 0; } /** * Return true if message is a Note Off message. */ public static boolean isNoteOffMessage(MidiMessage message) { // It is a note off event if the status indicates it is a // note off message. Or, if it is a note on message and // the velocity is zero. return (message.getStatus() >= 128 && message.getStatus() < 144) || (message.getStatus() >= 144 && message.getStatus() < 160 && getVelocity(message) == 0); } /** * Return the velocity for a note on or note off event. */ public static int getVelocity(MidiEvent noteOnOff) { // I can't check for this because isNoteOnMessage calls getVelocity //assert isNoteOnEvent(noteOnOff) || isNoteOffEvent(noteOnOff); return getVelocity(noteOnOff.getMessage()); } /** * Return the velocity for a note on or off message. */ public static int getVelocity(MidiMessage noteOnOff) { // I can't check for this because isNoteOnMessage calls getVelocity //assert isNoteOnMessage(noteOnOff) || isNoteOffMessage(noteOnOff); return noteOnOff.getMessage()[2]; } }