Example usage for javax.sound.midi Sequencer open

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

Introduction

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

Prototype

void open() throws MidiUnavailableException;

Source Link

Document

Opens the device, indicating that it should now acquire any system resources it requires and become operational.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    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();
    sequencer.addMetaEventListener(new MetaEventListener() {
        public void meta(MetaMessage event) {
            if (event.getType() == 47) {
                // Sequencer is done playing
            }/*w  w  w . j  a  v a 2s.c om*/
        }
    });

}

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();
    sequencer.setSequence(sequence);// w ww .j a  va  2  s.c  o m

    double durationInSecs = sequencer.getMicrosecondLength() / 1000000.0;

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    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   www .  jav  a  2 s.c  o  m
    }
}

From source file:Main.java

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

    // From URL/*from   w ww . ja  v a2 s  .c om*/
    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 www.ja  va  2 s. co 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[] argv) throws Exception {
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();

    // From file/*from w  w  w. j  av  a2  s. c o  m*/
    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();

    // From file//w  ww  .ja v a 2s .  co  m
    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[] args) {
    JFrame frame = new JFrame();
    DrawPanel dp = new DrawPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(dp);//  www .  jav a  2  s  . com
    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;/*  w  w  w.  j  ava2 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);
        }
    }
}