Class MidiUtilities
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addTrackNameEvent
(Track t, String name) Add a TrackName MetaMessage (type=3) to the beginning of track.static ShortMessage
buildMessage
(int command, int channel, int d1, int d2) Build a ShortMessage.static boolean
checkMidiFileTypeSupport
(Sequence sequence, int fileType, boolean notifyUser) Check if the Midi sequence supports the specified Midi file type.static void
clearTrack
(Track track) Remove all MidiEvents from a track, but leave the End Of Track MetaEvent unchanged.static long
convertTick
(long srcTick, long srcPPQresolution) Convert srcTick in srcPPQresolution into a tick for the JJazz program's PPQ resolution.static ShortMessage
getChorusMessage
(int channel, int data) static String
getControllerChangeString
(int controllerId) Convert a ShortMessage controller id into an understandable string.static MetaMessage
static SysexMessage
static SysexMessage
static SysexMessage
static <T> MidiEvent
getLastMidiEvent
(Track track, Class<T> msgClass, Predicate<T> tester, long beforeTick) Get the last MidiEvent before beforeTick which satisfies predicate.static MetaMessage
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.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 ShortMessage
getNoteOffMessage
(int channel, int pitch) static ShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_OFF or ShortMessage.NOTE_ON with velocity = 0.static ShortMessage
getNoteOnMessage
(int channel, int pitch, int velocity) static ShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON with a velocity > 0.static ShortMessage
Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON or NOTE_OFF.static ShortMessage
getPanoramicMessage
(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 ShortMessage
getResetAllControllersMessage
(int channel) static ShortMessage
getReverbMessage
(int channel, int data) static String
getShortMessageCommandString
(int command) Convert the ShortMessage command number into an understandable string.static int
getTempoInBPM
(MetaMessage tempoMsg) Get the tempo in BPM coded in a Tempo Midi message.static MetaMessage
getTempoMessage
(int channel, int bpm) static String
Retrieve the text from the specified MidiEvent message, when possible.static MetaMessage
getTextMetaMessage
(String txt) static MetaMessage
static String
getTrackName
(Track track) Search for the first TrackName MetaEvent in the specified track and return its name.static MetaMessage
getUsedChannels
(Track track) Return the channels used in the specified track.static ShortMessage
getVolumeMessage
(int channel, int data) static SysexMessage
static int
limit
(int x) Make sure x is a in the range [0-127].static MidiMessage
Retrieve a MidiMessage from a save string.static List<MidiMessage>
Retrieve a list of MidiMessages from a save string.static void
rerouteShortMessages
(Sequence sequence, List<Integer> fromChannels, int destChannel) Change the channel of ShortMessage events in the sequence.static String
Convert a MidiMessage as a String for backup purpose.static String
Convert a list of MidiMessages to a save string.static void
Send the specified SysExMessage.static boolean
setEndOfTrackPosition
(Track t, long tick) Set the track's EndOfTrack MetaMessage position to tick.static void
setSequenceDuration
(Sequence sequence, long tickEnd) Change the global duration of the sequence.static String
toString
(MidiMessage msg, long tick) Provide an explicit string for a MidiMessage.static String
static String
static double
toTempoMPQ
(double tempoBPM) Convert a tempo in BPM (beat per minute) into a tempo in microseconds per quarter.static long
toTickInPPQ
(long tickInUs, double tempoMPQ, int resolution) Convert micro-second tick to PPQ tickstatic long
toTickInUs
(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:
ParseException
InvalidMidiDataException
- See Also:
-
loadMidiMessageFromString
public static MidiMessage loadMidiMessageFromString(String s) throws ParseException, InvalidMidiDataException Retrieve a MidiMessage from a save string.- Parameters:
s
-- Returns:
- Throws:
ParseException
InvalidMidiDataException
- 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:
-