Class MidiUtilities

java.lang.Object
org.jjazz.midi.api.MidiUtilities

public class MidiUtilities extends Object
Convenience MIDI functions.
  • 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 resolution
      srcPPQ - E.g. 480
      destPPQ - 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 event
      trackTickRange - 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 class
      msgTester - Test the MidiMessage of the MidiEvent
      trackTickRange - 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

      public static Set<Integer> getUsedChannels(Track track)
      Return the channels used in the specified track.
      Parameters:
      track -
      Returns:
    • clearTrack

      public static void clearTrack(Track track)
      Remove all MidiEvents from a track, but leave the End Of Track MetaEvent unchanged.
      Parameters:
      track -
    • getMidiEventsCopy

      public static List<MidiEvent> getMidiEventsCopy(Track track)
      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

      public static ShortMessage getNoteShortMessage(MidiMessage mm)
      Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON or NOTE_OFF.
      Parameters:
      mm -
      Returns:
    • getNoteOnShortMessage

      public static ShortMessage getNoteOnShortMessage(MidiMessage mm)
      Return a non-null ShortMessage only if mm is a ShortMessage.NOTE_ON with a velocity > 0.
      Parameters:
      mm -
      Returns:
    • getNoteOffShortMessage

      public static ShortMessage getNoteOffShortMessage(MidiMessage mm)
      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

      public static SysexMessage getGmModeOnSysExMessage()
    • getGm2ModeOnSysExMessage

      public static SysexMessage getGm2ModeOnSysExMessage()
    • getXgModeOnSysExMessage

      public static SysexMessage getXgModeOnSysExMessage()
    • getGsModeOnSysExMessage

      public static SysexMessage getGsModeOnSysExMessage()
    • sendSysExMessage

      public static void sendSysExMessage(SysexMessage sm)
      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

      public static ShortMessage buildMessage(int command, int channel, int d1, int d2)
      Build a ShortMessage.
      Parameters:
      command -
      channel -
      d1 -
      d2 -
      Returns:
    • getVolumeMessage

      public static ShortMessage getVolumeMessage(int channel, int data)
    • getChorusMessage

      public static ShortMessage getChorusMessage(int channel, int data)
    • getReverbMessage

      public static ShortMessage getReverbMessage(int channel, int data)
    • getPanoramicMessage

      public static ShortMessage getPanoramicMessage(int channel, int data)
    • getNoteOnMessage

      public static ShortMessage getNoteOnMessage(int channel, int pitch, int velocity)
    • getNoteOffMessage

      public static ShortMessage getNoteOffMessage(int channel, int pitch)
    • getTimeSignatureMessage

      public static MetaMessage getTimeSignatureMessage(TimeSignature ts)
    • getTempoMessage

      public static MetaMessage getTempoMessage(int channel, int bpm)
    • getPatchMessages

      public static ShortMessage[] getPatchMessages(int channel, Instrument ins)
      Build the messages depending of the specified instrument's bank BankSelectionMethod.
      Parameters:
      channel -
      ins -
      Returns:
    • getPitchBendMessages

      public static ShortMessage[] getPitchBendMessages(int channel, int pitchDelta)
      Get the messages to apply a pitch bend of pitchDelta semitons.
      Parameters:
      pitchDelta - In semitons, can be negative or positive.
      Returns:
    • getResetAllControllersMessage

      public static ShortMessage getResetAllControllersMessage(int channel)
    • 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 quarter
      resolution -
      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 quarter
      resolution -
      Returns:
    • getTrackName

      public static String getTrackName(Track track)
      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

      public static MetaMessage getTrackNameMetaMessage(String txt)
    • getTextMetaMessage

      public static MetaMessage getTextMetaMessage(String txt)
    • getMarkerMetaMessage

      public static MetaMessage getMarkerMetaMessage(String txt)
    • getCopyrightMetaMessage

      public static MetaMessage getCopyrightMetaMessage(String txt)
    • checkMidiFileTypeSupport

      public static boolean checkMidiFileTypeSupport(Sequence sequence, int fileType, boolean notifyUser)
      Check if the Midi sequence supports the specified Midi file type.
      Parameters:
      sequence -
      fileType - 0 or 1
      notifyUser - 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

      public static void setSequenceDuration(Sequence sequence, long tickEnd)
      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

      public static int getTempoInBPM(MetaMessage tempoMsg)
      Get the tempo in BPM coded in a Tempo Midi message.
      Parameters:
      tempoMsg - Must be a tempo MetaMessage (type=81)
      Returns:
    • getText

      public static String getText(MidiEvent me)
      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

      public static String toString(MidiMessage msg, long tick)
      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

      public static String toString(Track track)
    • toString

      public static String toString(Sequence seq)
    • saveMidiMessagesAsString

      public static String saveMidiMessagesAsString(List<MidiMessage> mms)
      Convert a list of MidiMessages to a save string.

      Example: "SM.A1.27.C3-MM.BA.3.12"

      Parameters:
      mms -
      Returns:
      See Also:
    • saveMidiMessageAsString

      public static String saveMidiMessageAsString(MidiMessage mm)
      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

      public static void addTrackNameEvent(Track t, String name)
      Add a TrackName MetaMessage (type=3) to the beginning of track.
      Parameters:
      t -
      name - Name of the track
    • setEndOfTrackPosition

      public static boolean setEndOfTrackPosition(Track t, long tick)
      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

      public static String getControllerChangeString(int controllerId)
      Convert a ShortMessage controller id into an understandable string.

      For ex. if controller == 7 return "VOLUME_MSB".

      Parameters:
      controllerId -
      Returns:
    • getShortMessageCommandString

      public static String getShortMessageCommandString(int command)
      Convert the ShortMessage command number into an understandable string.

      For example if command==ShortMessage.CONTROL_CHANGE, return "CONTROL_CHANGE".

      Parameters:
      command -
      Returns: