Create a Set Tempo meta event for MIDI. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiEvent

Description

Create a Set Tempo meta event for MIDI.

Demo Code


import javax.sound.midi.ShortMessage;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Track;
import javax.sound.midi.Sequence;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MetaMessage;
import java.util.Vector;

public class Main{
    /** //from   www.ja v a 2 s.c  om
     * Create a Set Tempo meta event. Takes a tempo in BPMs.
     */
    public static MidiEvent createSetTempoEvent(long tick, long tempo) {
        // microseconds per quarternote
        long mpqn = DebugMidi.MICROSECONDS_PER_MINUTE / tempo;

        MetaMessage metaMessage = new MetaMessage();

        // create the tempo byte array
        byte[] array = new byte[] { 0, 0, 0 };

        for (int i = 0; i < 3; i++) {
            int shift = (3 - 1 - i) * 8;
            array[i] = (byte) (mpqn >> shift);
        }

        // now set the message
        try {
            metaMessage.setMessage(81, array, 3);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        return new MidiEvent(metaMessage, tick);
    }
}

Related Tutorials