Example usage for javax.sound.midi ShortMessage setMessage

List of usage examples for javax.sound.midi ShortMessage setMessage

Introduction

In this page you can find the example usage for javax.sound.midi ShortMessage setMessage.

Prototype

public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException 

Source Link

Document

Sets the short message parameters for a channel message which takes up to two data bytes.

Usage

From source file:Main.java

private static MidiEvent makeEvent(int cmd, int chan, int d1, int d2, int tick) {
    MidiEvent event = null;//from w w w  .ja  va 2s.  co m
    try {
        ShortMessage sm = new ShortMessage();
        sm.setMessage(cmd, chan, d1, 127);
        event = new MidiEvent(sm, tick);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    return event;
}

From source file:PlayerPiano.java

public static void addNote(Track track, int startTick, int tickLength, int key, int velocity)
        throws InvalidMidiDataException {
    ShortMessage on = new ShortMessage();
    on.setMessage(ShortMessage.NOTE_ON, 0, key, velocity);
    ShortMessage off = new ShortMessage();
    off.setMessage(ShortMessage.NOTE_OFF, 0, key, velocity);
    track.add(new MidiEvent(on, startTick));
    track.add(new MidiEvent(off, startTick + tickLength));
}

From source file:PlayerPiano.java

public static void addTrack(Sequence s, int instrument, int tempo, char[] notes)
        throws InvalidMidiDataException {
    Track track = s.createTrack(); // Begin with a new track

    // Set the instrument on channel 0
    ShortMessage sm = new ShortMessage();
    sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);
    track.add(new MidiEvent(sm, 0));

    int n = 0; // current character in notes[] array
    int t = 0; // time in ticks for the composition

    // These values persist and apply to all notes 'till changed
    int notelength = 16; // default to quarter notes
    int velocity = 64; // default to middle volume
    int basekey = 60; // 60 is middle C. Adjusted up and down by octave
    boolean sustain = false; // is the sustain pedal depressed?
    int numnotes = 0; // How many notes in current chord?

    while (n < notes.length) {
        char c = notes[n++];

        if (c == '+')
            basekey += 12; // increase octave
        else if (c == '-')
            basekey -= 12; // decrease octave
        else if (c == '>')
            velocity += 16; // increase volume;
        else if (c == '<')
            velocity -= 16; // decrease volume;
        else if (c == '/') {
            char d = notes[n++];
            if (d == '2')
                notelength = 32; // half note
            else if (d == '4')
                notelength = 16; // quarter note
            else if (d == '8')
                notelength = 8; // eighth note
            else if (d == '3' && notes[n++] == '2')
                notelength = 2;/*w  w w.j a v a2 s  .  c om*/
            else if (d == '6' && notes[n++] == '4')
                notelength = 1;
            else if (d == '1') {
                if (n < notes.length && notes[n] == '6')
                    notelength = 4; // 1/16th note
                else
                    notelength = 64; // whole note
            }
        } else if (c == 's') {
            sustain = !sustain;
            // Change the sustain setting for channel 0
            ShortMessage m = new ShortMessage();
            m.setMessage(ShortMessage.CONTROL_CHANGE, 0, DAMPER_PEDAL, sustain ? DAMPER_ON : DAMPER_OFF);
            track.add(new MidiEvent(m, t));
        } else if (c >= 'A' && c <= 'G') {
            int key = basekey + offsets[c - 'A'];
            if (n < notes.length) {
                if (notes[n] == 'b') { // flat
                    key--;
                    n++;
                } else if (notes[n] == '#') { // sharp
                    key++;
                    n++;
                }
            }

            addNote(track, t, notelength, key, velocity);
            numnotes++;
        } else if (c == ' ') {
            // Spaces separate groups of notes played at the same time.
            // But we ignore them unless they follow a note or notes.
            if (numnotes > 0) {
                t += notelength;
                numnotes = 0;
            }
        } else if (c == '.') {
            // Rests are like spaces in that they force any previous
            // note to be output (since they are never part of chords)
            if (numnotes > 0) {
                t += notelength;
                numnotes = 0;
            }
            // Now add additional rest time
            t += notelength;
        }
    }
}

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   ww  w  .  ja  v  a  2 s  .  c  om*/
    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;
}

From source file:org.monome.pages.MIDIKeyboardJulienBPage.java

private void stopNotes() {
    ShortMessage note_out = new ShortMessage();
    for (int chan = 0; chan < 16; chan++) {
        for (int i = 0; i < 128; i++) {
            if (this.notesOn[chan][i] == 1) {
                try {
                    note_out.setMessage(ShortMessage.NOTE_OFF, chan, i, 0);
                    for (int j = 0; j < midiReceivers.size(); j++) {
                        midiReceivers.get(j).send(note_out, -1);
                    }/*from w  ww .jav a  2s  .c  om*/
                } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:org.monome.pages.MIDIKeyboardJulienBPage.java

/**
 * Plays a MIDI note.  0 velocity will send a note off, and > 0 velocity will send a note on.
 * //from   w  w w . j  av a  2s  .  c o  m
 * @param note_num
 * @param velocity
 * @param channel
 */
public void playNote(int note_num, int velocity, int channel) {
    ShortMessage note_out = new ShortMessage();
    try {
        if (velocity == 0) {
            note_out.setMessage(ShortMessage.NOTE_OFF, channel, note_num, velocity);
        } else {
            note_out.setMessage(ShortMessage.NOTE_ON, channel, note_num, velocity);
        }
        for (int i = 0; i < midiReceivers.size(); i++) {
            midiReceivers.get(i).send(note_out, -1);
        }
    } catch (InvalidMidiDataException e) {
        e.printStackTrace();
    }
}

From source file:org.monome.pages.MIDIKeyboardPage.java

private void stopNotes() {
    ShortMessage note_out = new ShortMessage();
    for (int chan = 0; chan < 16; chan++) {
        for (int i = 0; i < 128; i++) {
            if (this.notesOn[chan][i] == 1) {
                try {
                    note_out.setMessage(ShortMessage.NOTE_OFF, chan, i, 0);
                    recv.send(note_out, -1);
                } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
                }/*  w  w w . ja  v a  2 s  . c o m*/
            }
        }
    }
}

From source file:org.monome.pages.MIDIKeyboardPage.java

/**
 * Plays a MIDI note.  0 velocity will send a note off, and > 0 velocity will send a note on.
 * // www .j ava 2s.c o m
 * @param note_num
 * @param velocity
 * @param channel
 */
public void playNote(int note_num, int velocity, int channel) {
    ShortMessage note_out = new ShortMessage();
    if (this.recv == null) {
        return;
    }
    try {
        if (velocity == 0) {
            note_out.setMessage(ShortMessage.NOTE_OFF, channel, note_num, velocity);
        } else {
            note_out.setMessage(ShortMessage.NOTE_ON, channel, note_num, velocity);
        }
        recv.send(note_out, -1);

    } catch (InvalidMidiDataException e) {
        e.printStackTrace();
    }
}

From source file:org.monome.pages.MIDIKeyboardPage.java

/**
 * Sends CC64 (sustain).  0 velocity will send a note off, and > 0 velocity will send a note on.
 *
 *//* ww w.j av a 2s.c  om*/
public void doSustain() {
    ShortMessage sustain_out = new ShortMessage();
    if (this.recv == null) {
        return;
    }
    try {
        if (this.sustain == 1) {
            sustain_out.setMessage(ShortMessage.CONTROL_CHANGE, this.midiChannel, 64, 127);
        } else {
            sustain_out.setMessage(ShortMessage.CONTROL_CHANGE, this.midiChannel, 64, 0);
        }
        recv.send(sustain_out, -1);

    } catch (InvalidMidiDataException e) {
        e.printStackTrace();
    }
}

From source file:org.monome.pages.MIDISequencerPage.java

public void stopNotes() {
    ShortMessage note_out = new ShortMessage();
    for (int i = 0; i < 16; i++) {
        if (this.heldNotes[i] == 1) {
            this.heldNotes[i] = 0;
            int note_num = this.getNoteNumber(i);
            try {
                note_out.setMessage(ShortMessage.NOTE_OFF, 0, note_num, 0);
                this.recv.send(note_out, -1);
            } catch (InvalidMidiDataException e) {
                e.printStackTrace();/*from   ww w.  java2 s . c  om*/
            }
        }
    }
}