Example usage for javax.sound.midi MetaMessage getType

List of usage examples for javax.sound.midi MetaMessage getType

Introduction

In this page you can find the example usage for javax.sound.midi MetaMessage getType.

Prototype

public int getType() 

Source Link

Document

Obtains the type of the MetaMessage .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();//from w  w w  . j a  v a  2 s .com
    sequencer.addMetaEventListener(new MetaEventListener() {
        public void meta(MetaMessage event) {
            if (event.getType() == 47) {
                // Sequencer is done playing
            }
        }
    });

}

From source file:PlayerPiano.java

public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, IOException {
    int instrument = 0;
    int tempo = 120;
    String filename = null;/*w w  w  .  ja  va  2  s . c om*/

    // Parse the options
    // -i <instrument number> default 0, a piano. Allowed values: 0-127
    // -t <beats per minute> default tempo is 120 quarter notes per minute
    // -o <filename> save to a midi file instead of playing
    int a = 0;
    while (a < args.length) {
        if (args[a].equals("-i")) {
            instrument = Integer.parseInt(args[a + 1]);
            a += 2;
        } else if (args[a].equals("-t")) {
            tempo = Integer.parseInt(args[a + 1]);
            a += 2;
        } else if (args[a].equals("-o")) {
            filename = args[a + 1];
            a += 2;
        } else
            break;
    }

    char[] notes = args[a].toCharArray();

    // 16 ticks per quarter note.
    Sequence sequence = new Sequence(Sequence.PPQ, 16);

    // Add the specified notes to the track
    addTrack(sequence, instrument, tempo, notes);

    if (filename == null) { // no filename, so play the notes
        // Set up the Sequencer and Synthesizer objects
        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());

        // Specify the sequence to play, and the tempo to play it at
        sequencer.setSequence(sequence);
        sequencer.setTempoInBPM(tempo);

        // Let us know when it is done playing
        sequencer.addMetaEventListener(new MetaEventListener() {
            public void meta(MetaMessage m) {
                // A message of this type is automatically sent
                // when we reach the end of the track
                if (m.getType() == END_OF_TRACK)
                    System.exit(0);
            }
        });
        // And start playing now.
        sequencer.start();
    } else { // A file name was specified, so save the notes
        int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence);
        if (allowedTypes.length == 0) {
            System.err.println("No supported MIDI file types.");
        } else {
            MidiSystem.write(sequence, allowedTypes[0], new File(filename));
            System.exit(0);
        }
    }
}

From source file:Main.java

public static void streamMidiSequence(URL url)
        throws IOException, InvalidMidiDataException, MidiUnavailableException {
    Sequencer sequencer = null; // Converts a Sequence to MIDI events
    Synthesizer synthesizer = null; // Plays notes in response to MIDI events

    try {/*w w w.jav a 2  s.  c  om*/
        // Create, open, and connect a Sequencer and Synthesizer
        // They are closed in the finally block at the end of this method.
        sequencer = MidiSystem.getSequencer();
        sequencer.open();
        synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());

        // Specify the InputStream to stream the sequence from
        sequencer.setSequence(url.openStream());

        // This is an arbitrary object used with wait and notify to
        // prevent the method from returning before the music finishes
        final Object lock = new Object();

        // Register a listener to make the method exit when the stream is
        // done. See Object.wait() and Object.notify()
        sequencer.addMetaEventListener(new MetaEventListener() {
            public void meta(MetaMessage e) {
                if (e.getType() == END_OF_TRACK) {
                    synchronized (lock) {
                        lock.notify();
                    }
                }
            }
        });

        // Start playing the music
        sequencer.start();

        // Now block until the listener above notifies us that we're done.
        synchronized (lock) {
            while (sequencer.isRunning()) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    } finally {
        // Always relinquish the sequencer, so others can use it.
        if (sequencer != null)
            sequencer.close();
        if (synthesizer != null)
            synthesizer.close();
    }
}

From source file:MidiTest.java

/**
 * This method is called by the sound system when a meta event occurs. In
 * this case, when the end-of-track meta event is received, the sequence is
 * restarted if looping is on.//from   www  .j  a  v a 2  s .c o m
 */
public void meta(MetaMessage event) {
    if (event.getType() == END_OF_TRACK_MESSAGE) {
        if (sequencer != null && sequencer.isOpen() && loop) {
            sequencer.start();
        }
    }
}

From source file:MidiTest.java

/**
 * This method is called by the sound system when a meta event occurs. In
 * this case, when the end-of-track meta event is received, the drum track
 * is turned on.//from w  w w. jav  a2s. c  o m
 */
public void meta(MetaMessage event) {
    if (event.getType() == MidiPlayer.END_OF_TRACK_MESSAGE) {
        Sequencer sequencer = player.getSequencer();
        if (sequencer.getTrackMute(DRUM_TRACK)) {
            // turn on the drum track
            System.out.println("Turning on drums...");
            sequencer.setTrackMute(DRUM_TRACK, false);
        } else {
            // close the sequencer and exit
            System.out.println("Exiting...");
            player.close();
            System.exit(0);
        }
    }
}

From source file:de.ailis.midi4js.MessageReceiver.java

/**
 * Processes a meta message./*ww  w .  j  a va 2s  . c  om*/
 *
 * @param message
 *            The message to process.
 * @param json
 *            The JSON stringer.
 * @throws JSONException
 *             When JSON output fails.
 */
private void processMetaMessage(final MetaMessage message, final JSONStringer json) throws JSONException {
    json.key("class").value("MetaMessage");
    json.key("type").value(message.getType());
    json.key("data");
    json.array();
    final byte[] data = message.getMessage();
    final int max = Math.min(data.length, message.getLength());
    for (int i = 0; i < max; i++)
        json.value(data[i] & 0xff);
    json.endArray();
}

From source file:edu.tsinghua.lumaqq.Sounder.java

public void meta(MetaMessage meta) {
    if (meta.getType() == 47) { // 47?
        midiEOM = true;
    }
}

From source file:SimpleSoundPlayer.java

public void meta(MetaMessage message) {
    if (message.getType() == 47) { // 47 is end of track
        midiEOM = true;//from   w ww  . j  av  a2 s . c o  m
    }
}

From source file:net.abumarkub.midi.MIDISequencer.java

public void meta(MetaMessage meta) {
    if (1 == 2) {//just a way of ignoring the AppletContext send method
        StringBuilder jsMsg = new StringBuilder();
        jsMsg.append(meta.getType());
        jsMsg.append(",");
        jsMsg.append(meta.getStatus());// w  w w. j a  v  a 2  s  .c  o  m

        byte[] message = meta.getMessage();
        for (int i = 0, maxi = message.length; i < maxi; i++) {
            jsMsg.append(",");
            jsMsg.append(message[i]);
        }

        sendMessageViaContext("javascript:midiBridge.sequencerMetaData(" + jsMsg.toString() + ")");
    } else {//currently sending via Live Connect is preferred
        Object[] args = { meta };
        _jsMetaEventListener.call("listener", args);
    }

}