Example usage for javax.sound.midi MidiSystem write

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

Introduction

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

Prototype

public static int write(final Sequence in, final int type, final File out) throws IOException 

Source Link

Document

Writes a stream of bytes representing a file of the MIDI file type indicated to the external file provided.

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;/*  w ww . j a va 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);
        }
    }
}

From source file:at.ofai.music.util.WormFileParseException.java

public void writeMIDI(String fileName, EventList pedal) {
    try {/* w w w .j  a  v  a 2s  .  co  m*/
        MidiSystem.write(toMIDI(pedal), 1, new File(fileName));
    } catch (Exception e) {
        System.err.println("Error: Unable to write MIDI file " + fileName);
        e.printStackTrace();
    }
}