Class MidiUtilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddTrackNameEvent(Track t, String name) Add a TrackName MetaMessage (type=3) to the beginning of track.static ShortMessagebuildMessage(int command, int channel, int d1, int d2) Build a ShortMessage.static booleancheckMidiFileTypeSupport(Sequence sequence, int fileType, boolean notifyUser) Check if the Midi sequence supports the specified Midi file type.static voidclearTrack(Track track) Remove all MidiEvents from a track, but leave the End Of Track MetaEvent unchanged.static longconvertTick(long srcTick, long srcPPQresolution) Convert srcTick in srcPPQresolution into a tick for the JJazz program's PPQ resolution.static ShortMessagegetChorusMessage(int channel, int data) static StringgetControllerChangeString(int controllerId) Convert a ShortMessage controller id into an understandable string.static MetaMessagestatic SysexMessagestatic SysexMessagestatic SysexMessagestatic <T> MidiEventgetLastMidiEvent(Track track, Class<T> msgClass, Predicate<T> tester, long beforeTick) Get the last MidiEvent before beforeTick which satisfies predicate.static MetaMessagestatic <T extends MidiMessage>
List<MidiEvent>getMidiEvents(Track track, Class<T> msgClass, Predicate<T> msgTester, LongRange trackTickRange) Get track MidiEvents whose MidiMessage is instance of msgClass, which satisfy the specified MidiMessage tester, and whose tick position is within trackTickRange.getMidiEvents(Track track, Predicate<MidiEvent> tester, LongRange trackTickRange) Get track MidiEvents whose tick position is within trackTickRange and which satisfy the specified tester.getMidiEventsAtPPQ(List<MidiEvent> srcEvents, int srcPPQ, int destPPQ) Get MidiEvents converted from MidiEvents with a different PPQ resolution (Midi Pulses Per Quarter).getMidiEventsCopy(Track track) Get a clone copy of all MidiEvents found in track.static ShortMessagegetNoteOffMessage(int channel, int pitch) static ShortMessageReturn a non-null ShortMessage only if mm is a ShortMessage.NOTE_OFF or ShortMessage.NOTE_ON with velocity = 0.static ShortMessagegetNoteOnMessage(int channel, int pitch, int velocity) static ShortMessageReturn a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON with a velocity > 0.static ShortMessageReturn a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON or NOTE_OFF.static ShortMessagegetPanoramicMessage(int channel, int data) static ShortMessage[]getPatchMessages(int channel, Instrument ins) Build the messages depending of the specified instrument's bank BankSelectionMethod.static ShortMessage[]getPitchBendMessages(int channel, int pitchDelta) Get the messages to apply a pitch bend of pitchDelta semitons.static ShortMessagegetResetAllControllersMessage(int channel) static ShortMessagegetReverbMessage(int channel, int data) static StringgetShortMessageCommandString(int command) Convert the ShortMessage command number into an understandable string.static intgetTempoInBPM(MetaMessage tempoMsg) Get the tempo in BPM coded in a Tempo Midi message.static MetaMessagegetTempoMessage(int channel, int bpm) static StringRetrieve the text from the specified MidiEvent message, when possible.static MetaMessagegetTextMetaMessage(String txt) static MetaMessagestatic StringgetTrackName(Track track) Search for the first TrackName MetaEvent in the specified track and return its name.static MetaMessagegetUsedChannels(Track track) Return the channels used in the specified track.static ShortMessagegetVolumeMessage(int channel, int data) static SysexMessagestatic intlimit(int x) Make sure x is a in the range [0-127].static MidiMessageRetrieve a MidiMessage from a save string.static List<MidiMessage>Retrieve a list of MidiMessages from a save string.static voidrerouteShortMessages(Sequence sequence, List<Integer> fromChannels, int destChannel) Change the channel of ShortMessage events in the sequence.static StringConvert a MidiMessage as a String for backup purpose.static StringConvert a list of MidiMessages to a save string.static voidSend the specified SysExMessage.static booleansetEndOfTrackPosition(Track t, long tick) Set the track's EndOfTrack MetaMessage position to tick.static voidsetSequenceDuration(Sequence sequence, long tickEnd) Change the global duration of the sequence.static StringtoString(MidiMessage msg, long tick) Provide an explicit string for a MidiMessage.static Stringstatic Stringstatic doubletoTempoMPQ(double tempoBPM) Convert a tempo in BPM (beat per minute) into a tempo in microseconds per quarter.static longtoTickInPPQ(long tickInUs, double tempoMPQ, int resolution) Convert micro-second tick to PPQ tickstatic longtoTickInUs(long tickInPPQ, double tempoMPQ, int resolution) Convert PPQ tick to microsecond tick.
-
Constructor Details
-
MidiUtilities
public MidiUtilities()
-
-
Method Details
-
limit
public static int limit(int x) Make sure x is a in the range [0-127].- Parameters:
x-- Returns:
-
getMidiEventsAtPPQ
public static List<MidiEvent> getMidiEventsAtPPQ(List<MidiEvent> srcEvents, int srcPPQ, int destPPQ) Get MidiEvents converted from MidiEvents with a different PPQ resolution (Midi Pulses Per Quarter).- Parameters:
srcEvents- A list of MidiEvents at srcPPQ resolutionsrcPPQ- E.g. 480destPPQ- E.g. 960- Returns:
- A list of new MidiEvents at destPPQ resolution.
-
getMidiEvents
public static List<MidiEvent> getMidiEvents(Track track, Predicate<MidiEvent> tester, LongRange trackTickRange) Get track MidiEvents whose tick position is within trackTickRange and which satisfy the specified tester.- Parameters:
track-tester- Test the Midi eventtrackTickRange- If null there is no filtering on tick position. The range must be based on the track's resolution.- Returns:
- A list of track MidiEvents
-
getMidiEvents
public static <T extends MidiMessage> List<MidiEvent> getMidiEvents(Track track, Class<T> msgClass, Predicate<T> msgTester, LongRange trackTickRange) Get track MidiEvents whose MidiMessage is instance of msgClass, which satisfy the specified MidiMessage tester, and whose tick position is within trackTickRange.- Type Parameters:
T-- Parameters:
track-msgClass- MidiMessage classmsgTester- Test the MidiMessage of the MidiEventtrackTickRange- If null there is no filtering on tick position. The range must be based on the track's resolution.- Returns:
- A list of track MidiEvents
-
getUsedChannels
Return the channels used in the specified track.- Parameters:
track-- Returns:
-
clearTrack
Remove all MidiEvents from a track, but leave the End Of Track MetaEvent unchanged.- Parameters:
track-
-
getMidiEventsCopy
Get a clone copy of all MidiEvents found in track.- Parameters:
track-- Returns:
- The list will contain at least 1 MidiEvent, the special MetaEvent (type=47) marking the end of the track.
-
getNoteShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON or NOTE_OFF.- Parameters:
mm-- Returns:
-
getNoteOnShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON with a velocity > 0.- Parameters:
mm-- Returns:
-
getNoteOffShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_OFF or ShortMessage.NOTE_ON with velocity = 0.- Parameters:
mm-- Returns:
-
getLastMidiEvent
public static <T> MidiEvent getLastMidiEvent(Track track, Class<T> msgClass, Predicate<T> tester, long beforeTick) Get the last MidiEvent before beforeTick which satisfies predicate.- Type Parameters:
T-- Parameters:
track-msgClass-tester-beforeTick-- Returns:
-
getGmModeOnSysExMessage
-
getGm2ModeOnSysExMessage
-
getXgModeOnSysExMessage
-
getGsModeOnSysExMessage
-
sendSysExMessage
Send the specified SysExMessage.Add a little Thread.sleep() to give time for harware to execute before sending other possible Midi messages.
- Parameters:
sm-
-
buildMessage
Build a ShortMessage.- Parameters:
command-channel-d1-d2-- Returns:
-
getVolumeMessage
-
getChorusMessage
-
getReverbMessage
-
getPanoramicMessage
-
getNoteOnMessage
-
getNoteOffMessage
-
getTimeSignatureMessage
-
getTempoMessage
-
getPatchMessages
Build the messages depending of the specified instrument's bank BankSelectionMethod.- Parameters:
channel-ins-- Returns:
-
getPitchBendMessages
Get the messages to apply a pitch bend of pitchDelta semitons.- Parameters:
pitchDelta- In semitons, can be negative or positive.- Returns:
-
getResetAllControllersMessage
-
toTempoMPQ
public static double toTempoMPQ(double tempoBPM) Convert a tempo in BPM (beat per minute) into a tempo in microseconds per quarter.- Parameters:
tempoBPM-- Returns:
-
toTickInUs
public static long toTickInUs(long tickInPPQ, double tempoMPQ, int resolution) Convert PPQ tick to microsecond tick.- Parameters:
tickInPPQ-tempoMPQ- Tempo in microseconds per quarterresolution-- Returns:
-
toTickInPPQ
public static long toTickInPPQ(long tickInUs, double tempoMPQ, int resolution) Convert micro-second tick to PPQ tick- Parameters:
tickInUs-tempoMPQ- Tempo in microseconds per quarterresolution-- Returns:
-
getTrackName
Search for the first TrackName MetaEvent in the specified track and return its name.- Parameters:
track-- Returns:
- Can be null if no track name found
-
getTrackNameMetaMessage
-
getTextMetaMessage
-
getMarkerMetaMessage
-
getCopyrightMetaMessage
-
checkMidiFileTypeSupport
Check if the Midi sequence supports the specified Midi file type.- Parameters:
sequence-fileType- 0 or 1notifyUser- If true and fileType is not supported, notify end user.- Returns:
- True if fileType is supported
-
rerouteShortMessages
public static void rerouteShortMessages(Sequence sequence, List<Integer> fromChannels, int destChannel) Change the channel of ShortMessage events in the sequence.All ShortMessages belonging to one of the fromChannels are reassigned to channel destChannel.
This can be used for example to reroute percussion notes to the GM drums channel.
- Parameters:
sequence-fromChannels-destChannel-
-
convertTick
public static long convertTick(long srcTick, long srcPPQresolution) Convert srcTick in srcPPQresolution into a tick for the JJazz program's PPQ resolution.Example: srcTick=200, srcPPQresolution=400, MidiConst.PPQ_RESOLUTION=1920
then return tick=960.- Parameters:
srcTick-srcPPQresolution-- Returns:
- A tick with MidiConst.PPQ_RESOLUTION
-
setSequenceDuration
Change the global duration of the sequence.If tickEnd is shorter than current sequence duration, remove all MidiEvents after tickEnd and adjust notes onsets to stop no later than tickEnd. If tickEnd is greater than current sequence duration, just change end event position.
- Parameters:
sequence-tickEnd-
-
getTempoInBPM
Get the tempo in BPM coded in a Tempo Midi message.- Parameters:
tempoMsg- Must be a tempo MetaMessage (type=81)- Returns:
-
getText
Retrieve the text from the specified MidiEvent message, when possible.The event's message should be a text-based MetaMessage such TrackName, Text, Marker, Lyrics, Copyright, Instrument.
- Parameters:
me-- Returns:
- Null if the event's message is not a text-based MetaMessage
-
toString
Provide an explicit string for a MidiMessage.- Parameters:
msg- A MidiMessage.tick- The tick of the MidiMessage. Ignore if < 0.- Returns:
- A string representing the MidiMessage.
-
toString
-
toString
-
saveMidiMessagesAsString
Convert a list of MidiMessages to a save string.Example: "SM.A1.27.C3-MM.BA.3.12"
- Parameters:
mms-- Returns:
- See Also:
-
saveMidiMessageAsString
Convert a MidiMessage as a String for backup purpose.Example: "SM.A1.27.C3"
- Parameters:
mm-- Returns:
- See Also:
-
loadMidiMessagesFromString
public static List<MidiMessage> loadMidiMessagesFromString(String s) throws ParseException, InvalidMidiDataException Retrieve a list of MidiMessages from a save string.- Parameters:
s-- Returns:
- Throws:
ParseExceptionInvalidMidiDataException- See Also:
-
loadMidiMessageFromString
public static MidiMessage loadMidiMessageFromString(String s) throws ParseException, InvalidMidiDataException Retrieve a MidiMessage from a save string.- Parameters:
s-- Returns:
- Throws:
ParseExceptionInvalidMidiDataException- See Also:
-
addTrackNameEvent
Add a TrackName MetaMessage (type=3) to the beginning of track.- Parameters:
t-name- Name of the track
-
setEndOfTrackPosition
Set the track's EndOfTrack MetaMessage position to tick.- Parameters:
t-tick- The tick position must be equal or above the last tick in the sequence.- Returns:
- True if EndOfTrack position was correctly set, false if there was a problem.
-
getControllerChangeString
Convert a ShortMessage controller id into an understandable string.For ex. if controller == 7 return "VOLUME_MSB".
- Parameters:
controllerId-- Returns:
-
getShortMessageCommandString
Convert the ShortMessage command number into an understandable string.For example if command==ShortMessage.CONTROL_CHANGE, return "CONTROL_CHANGE".
- Parameters:
command-- Returns:
-