Example usage for javax.sound.midi MidiSystem getSequencer

List of usage examples for javax.sound.midi MidiSystem getSequencer

Introduction

In this page you can find the example usage for javax.sound.midi MidiSystem getSequencer.

Prototype

public static Sequencer getSequencer() throws MidiUnavailableException 

Source Link

Document

Obtains the default Sequencer , connected to a default device.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();//  w w  w .j a v a  2  s .c om
    double seconds = sequencer.getMicrosecondPosition() / 1000000.0;
}

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  va2s  . c  o m*/
    if (sequencer instanceof Synthesizer) {
        Synthesizer synthesizer = (Synthesizer) sequencer;
        MidiChannel[] channels = synthesizer.getChannels();

        // gain is a value between 0 and 1 (loudest)
        double gain = 0.9D;
        for (int i = 0; i < channels.length; i++) {
            channels[i].controlChange(7, (int) (gain * 127.0));
        }
    }
}

From source file:Main.java

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();/* w  ww. ja v  a2  s  .  c om*/

    // From file
    InputStream input = new BufferedInputStream(new FileInputStream(new File("midiaudiofile")));

    // From URL
    input = new BufferedInputStream(new URL("http://hostname/rmffile").openStream());

    sequencer.setSequence(input);

    // Start playing
    sequencer.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();// w ww . ja  v  a  2 s .  co  m

    // From file
    InputStream is = new BufferedInputStream(new FileInputStream(new File("midifile")));

    // From URL
    //    is = new BufferedInputStream(new URL("http://hostname/rmffile")
    //      .openStream());

    sequencer.setSequence(is);

    // Start playing
    sequencer.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequence sequence = MidiSystem.getSequence(new File("midiaudiofile"));

    sequence = MidiSystem.getSequence(new URL("http://hostname/midiaudiofile"));

    // Create a sequencer for the sequence
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();/*from  w  ww.j ava  2  s.  c  o m*/
    sequencer.setSequence(sequence);

    double durationInSecs = sequencer.getMicrosecondLength() / 1000000.0;

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequence sequence = MidiSystem.getSequence(new File("midifile"));

    // From URL//from  w w  w.j a  v  a2 s .co m
    sequence = MidiSystem.getSequence(new URL("http://hostname/midifile"));

    // Create a sequencer for the sequence
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    sequencer.setSequence(sequence);

    // Start playing
    sequencer.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // From file//from  w w  w .  ja  va 2s. c o  m
    Sequence sequence = MidiSystem.getSequence(new File("midiaudiofile"));

    // From URL
    // sequence = MidiSystem.getSequence(new
    // URL("http://hostname/midiaudiofile"));

    // Create a sequencer for the sequence
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    sequencer.setSequence(sequence);

    // Start playing
    sequencer.start();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    DrawPanel dp = new DrawPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(dp);//  w w w  . ja v  a2  s  . c om
    frame.pack();
    frame.setLocationByPlatform(true);
    try {
        Sequence seq = new Sequence(Sequence.PPQ, 4);
        Track track = seq.createTrack();
        for (int i = 0; i < 120; i += 4) {
            int d = (int) Math.abs(new Random().nextGaussian() * 24) + 32;
            track.add(makeEvent(ShortMessage.NOTE_ON, 1, d, 127, i));
            track.add(makeEvent(ShortMessage.CONTROL_CHANGE, 1, 127, 0, i));
            track.add(makeEvent(ShortMessage.NOTE_OFF, 1, d, 127, i));
        }
        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();
        sequencer.setSequence(seq);
        sequencer.addControllerEventListener(dp, new int[] { 127 });
        sequencer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    frame.setVisible(true);
}

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  av a 2s  . 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);
        }
    }
}