Example usage for javax.sound.midi Sequence PPQ

List of usage examples for javax.sound.midi Sequence PPQ

Introduction

In this page you can find the example usage for javax.sound.midi Sequence PPQ.

Prototype

float PPQ

To view the source code for javax.sound.midi Sequence PPQ.

Click Source Link

Document

The tempo-based timing type, for which the resolution is expressed in pulses (ticks) per quarter note.

Usage

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);/*from  w  w w .  j a v  a 2  s.co  m*/
    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.  ja  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:at.ofai.music.util.WormFileParseException.java

public Sequence toMIDI(EventList pedal) throws InvalidMidiDataException {
    final int midiTempo = 1000000;
    Sequence s = new Sequence(Sequence.PPQ, 1000);
    Track[] tr = new Track[16];
    tr[0] = s.createTrack();/*from  w w  w .j  a  va  2s  . c o m*/
    MetaMessage mm = new MetaMessage();
    byte[] b = new byte[3];
    b[0] = (byte) ((midiTempo >> 16) & 0xFF);
    b[1] = (byte) ((midiTempo >> 8) & 0xFF);
    b[2] = (byte) (midiTempo & 0xFF);
    mm.setMessage(0x51, b, 3);
    tr[0].add(new MidiEvent(mm, 0L));
    for (Event e : l) { // from match or beatTrack file
        if (e.midiCommand == 0) // skip beatTrack file
            break;
        if (tr[e.midiTrack] == null)
            tr[e.midiTrack] = s.createTrack();
        //switch (e.midiCommand) 
        //case ShortMessage.NOTE_ON:
        //case ShortMessage.POLY_PRESSURE:
        //case ShortMessage.CONTROL_CHANGE:
        //case ShortMessage.PROGRAM_CHANGE:
        //case ShortMessage.CHANNEL_PRESSURE:
        //case ShortMessage.PITCH_BEND:
        ShortMessage sm = new ShortMessage();
        sm.setMessage(e.midiCommand, e.midiChannel, e.midiPitch, e.midiVelocity);
        tr[e.midiTrack].add(new MidiEvent(sm, (long) Math.round(1000 * e.keyDown)));
        if (e.midiCommand == ShortMessage.NOTE_ON) {
            sm = new ShortMessage();
            sm.setMessage(ShortMessage.NOTE_OFF, e.midiChannel, e.midiPitch, 0);
            tr[e.midiTrack].add(new MidiEvent(sm, (long) Math.round(1000 * e.keyUp)));
        }
    }
    if (pedal != null) { // from MIDI file
        //      if (t.size() > 0)   // otherwise beatTrack files leave an empty trk
        //         t = s.createTrack();
        for (Event e : pedal.l) {
            if (tr[e.midiTrack] == null)
                tr[e.midiTrack] = s.createTrack();
            ShortMessage sm = new ShortMessage();
            sm.setMessage(e.midiCommand, e.midiChannel, e.midiPitch, e.midiVelocity);
            tr[e.midiTrack].add(new MidiEvent(sm, (long) Math.round(1000 * e.keyDown)));
            if (e.midiCommand == ShortMessage.NOTE_ON) {
                sm = new ShortMessage();
                sm.setMessage(ShortMessage.NOTE_OFF, e.midiChannel, e.midiPitch, e.midiVelocity);
                tr[e.midiTrack].add(new MidiEvent(sm, (long) Math.round(1000 * e.keyUp)));
            }
            //catch (InvalidMidiDataException exception) {}
        }
    }
    return s;
}