Example usage for javax.sound.midi Sequencer getTransmitter

List of usage examples for javax.sound.midi Sequencer getTransmitter

Introduction

In this page you can find the example usage for javax.sound.midi Sequencer getTransmitter.

Prototype

Transmitter getTransmitter() throws MidiUnavailableException;

Source Link

Document

Obtains a MIDI OUT connection from which the MIDI device will transmit MIDI data.

Usage

From source file:PlayerPiano.java

public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, IOException {
    int instrument = 0;
    int tempo = 120;
    String filename = null;//from   w w  w  .j  a va 2 s . co  m

    // 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 {//from   w  ww . j  av  a2  s. co  m
        // 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();
    }
}