Example usage for javax.sound.midi ShortMessage NOTE_ON

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

Introduction

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

Prototype

int NOTE_ON

To view the source code for javax.sound.midi ShortMessage NOTE_ON.

Click Source Link

Document

Command value for Note On message (0x90, or 144).

Usage

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.
 * //from w  w  w  .  jav  a 2 s.  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.MIDISequencerPage.java

/**
 * Send MIDI note messages based on the sequence position.  If on = 0, note off will be sent.
 * /*from w w  w. j ava  2 s . c  o m*/
 * @param seq_pos The sequence position to play notes for
 * @param on Whether to turn notes on or off, a value of 1 means play notes
 */
public void playNotes(int seq_pos, int on) {
    ShortMessage note_out = new ShortMessage();
    int note_num;
    int velocity;
    int midiChannel = Integer.parseInt(this.midiChannel) - 1;
    for (int y = 0; y < 16; y++) {
        // hold mode
        if (this.getHoldModeCB().isSelected()) {
            if (on == 0) {
                return;
            }
            if (sequence[this.bank][seq_pos][y] > 0) {
                velocity = (this.sequence[this.bank][seq_pos][y] * 64) - 1;
            } else {
                velocity = 0;
            }
            note_num = this.getNoteNumber(y);
            try {
                if (velocity == 0 && this.heldNotes[y] == 1) {
                    this.heldNotes[y] = 0;
                    note_out.setMessage(ShortMessage.NOTE_OFF, midiChannel, note_num, velocity);
                    this.recv.send(note_out, -1);
                } else if (velocity > 0 && this.heldNotes[y] == 0) {
                    this.heldNotes[y] = 1;
                    note_out.setMessage(ShortMessage.NOTE_ON, midiChannel, note_num, velocity);
                    this.recv.send(note_out, -1);
                }
            } catch (InvalidMidiDataException e) {
                e.printStackTrace();
            }
            // normal mode 
        } else {
            if (sequence[this.bank][seq_pos][y] > 0) {
                if (on > 0) {
                    velocity = (this.sequence[this.bank][seq_pos][y] * 64) - 1;
                } else {
                    velocity = 0;
                }
                note_num = this.getNoteNumber(y);
                try {
                    if (velocity == 0) {
                        note_out.setMessage(ShortMessage.NOTE_OFF, midiChannel, note_num, velocity);
                        this.heldNotes[y] = 0;
                    } else {
                        note_out.setMessage(ShortMessage.NOTE_ON, midiChannel, note_num, velocity);
                        this.heldNotes[y] = 1;
                    }
                    this.recv.send(note_out, -1);
                } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

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

/**
 * Send MIDI note messages based on the sequence position.  If on = 0, note off will be sent.
 * //from w w  w . j av a2  s  . co m
 * @param seq_pos The sequence position to play notes for
 * @param on Whether to turn notes on or off, a value of 1 means play notes
 * @throws InterruptedException 
 */
public void playNotes(int seq_pos, int on, int quantPat) {
    ShortMessage note_out = new ShortMessage();
    int note_num;
    int velocity;
    int seq_pos_pat = 0;
    int midiChannel = Integer.parseInt(this.midiChannel) - 1;
    int noteOnOff;
    int globalPitch = 0;
    int globalPitchOct = 0;
    boolean onOff = true;
    int globalRandomGateValue = 1;
    int globalRandomNoteValue = 1;
    int globalRandomVelocityValue = 1;
    boolean globalHold;

    /*if(this.globalRandomGate16) globalRandomGateValue=(int)(Math.random() + 0.5); else 
            
    globalRandomGateValue=1; 
    if(this.globalRandomNote16) globalRandomNoteValue=(int)(Math.random() * (8)) -4; else 
            
    globalRandomNoteValue=0;
    if(this.globalRandomVelocity16) globalRandomVelocityValue=(int)(Math.random() * (63+1)); else 
            
    globalRandomVelocityValue=0;*/

    if (stopNotesRequest) {
        for (int i = 0; i < 256; i++) {
            this.stopNotes(i);
        }
        stopNotesRequest = false;
    }

    if (quantPat == 0) {
        globalPitch = this.globalPitch0;
        globalPitchOct = this.globalPitch0oct;
        onOff = this.onOff0;
        globalHold = this.globalHold0;
    } else {
        globalPitch = this.globalPitch1;
        globalPitchOct = this.globalPitch1oct;
        onOff = this.onOff1;
        globalHold = this.globalHold1;
    }
    //System.out.println("playnotes " + quantPat + " seq_pos " + seq_pos);         
    for (int y = 0; y < 16; y++) {
        if (y < 15 && !this.noteSwitchs[y])
            noteOnOff = 0;
        else
            noteOnOff = 1;
        for (int iSeq = 0; iSeq < this.bankSel.length; iSeq++) {
            // pat length
            seq_pos_pat = seq_pos % this.patlength[iSeq];
            // hold mode
            if (this.patHold[iSeq] == 1 && this.patSpeed[iSeq] == quantPat) {

                if (this.bankSel[iSeq] && on == 127 && ((this.patGate[iSeq] == 1 && this.gate

                [iSeq]) || (this.patGate[iSeq] == 0))) {
                    note_num = this.getNoteNumber(y) + globalPitch + this.patOctgUp[iSeq]

                            * 24 + globalPitchOct;
                    if (onOff)
                        velocity = ((this.sequence[iSeq][seq_pos_pat][y] * 64) -

                                1) * noteOnOff;
                    else
                        velocity = 0;

                    if ((seq_pos_pat > 0 && this.sequence[iSeq][seq_pos_pat - 1][y] > 0 &&

                            velocity <= 0) || (seq_pos_pat == 0
                                    && this.sequence[iSeq][this.patlength[iSeq] - 1][y] > 0 && velocity <= 0)) {
                        try {
                            if (!globalHold) {
                                heldNotes[iSeq][y] = false;
                                //note_out.setMessage(ShortMessage.NOTE_OFF, midiChannel, note_num, 0);   
                                note_out.setMessage(ShortMessage.NOTE_OFF, midiChannel, heldNotesNum[iSeq][y],
                                        0);
                                if (this.bankMode == 1)
                                    this.monome.led(y %

                                            (this.monome.sizeX - 1),
                                            this.monome.sizeY - 3 + (y / (this.monome.sizeX - 1)), 0,
                                            this.index);
                                this.recv.send(note_out, -1);
                            }
                        } catch (InvalidMidiDataException e) {
                            e.printStackTrace();
                        }
                    } else if (((seq_pos_pat > 0 && this.sequence[iSeq][seq_pos_pat - 1][y]

                    == 0) || seq_pos_pat == 0) && velocity > 0) {
                        try {
                            heldNotes[iSeq][y] = true;
                            note_out.setMessage(ShortMessage.NOTE_ON,

                                    midiChannel, note_num, velocity);
                            heldNotesNum[iSeq][y] = note_num;
                            if (this.bankMode == 1)
                                this.monome.led(y %

                                        (this.monome.sizeX - 1),
                                        this.monome.sizeY - 3 + (y / (this.monome.sizeX - 1)), 1, this.index);
                            this.recv.send(note_out, -1);
                        } catch (InvalidMidiDataException e) {
                            e.printStackTrace();
                        }
                    }
                }
                // normal mode 
            } else if (this.patSpeed[iSeq] == quantPat) {
                if (this.bankSel[iSeq] && on == 127 && ((this.patGate[iSeq] == 1 && this.gate

                [iSeq]) || (this.patGate[iSeq] == 0))) {
                    note_num = this.getNoteNumber(y) + globalPitch + this.patOctgUp[iSeq]

                            * 24 + globalPitchOct;
                    if (onOff)
                        velocity = ((this.sequence[iSeq][seq_pos_pat][y] * 64) -

                                1) * noteOnOff;
                    else
                        velocity = 0;
                    if ((seq_pos_pat > 0 && this.sequence[iSeq][seq_pos_pat - 1][y] > 0) ||

                            (seq_pos_pat == 0 && this.sequence[iSeq][this.patlength[iSeq] - 1][y] > 0)) {
                        try {
                            if (!globalHold) {
                                heldNotes[iSeq][y] = false;
                                //note_out.setMessage(ShortMessage.NOTE_OFF, midiChannel, note_num, 0);   
                                note_out.setMessage(ShortMessage.NOTE_OFF,

                                        midiChannel, heldNotesNum[iSeq][y], 0);
                                this.recv.send(note_out, -1);
                            }
                            if (this.bankMode == 1)
                                this.monome.led(y %

                                        (this.monome.sizeX - 1),
                                        this.monome.sizeY - 3 + (y / (this.monome.sizeX - 1)), 0, this.index);
                        } catch (InvalidMidiDataException e) {
                            e.printStackTrace();
                        }
                    }

                    if (velocity > 0) {
                        try {
                            heldNotes[iSeq][y] = true;
                            note_out.setMessage(ShortMessage.NOTE_ON,

                                    midiChannel, note_num, velocity);
                            heldNotesNum[iSeq][y] = note_num;
                            this.recv.send(note_out, -1);
                            if (this.bankMode == 1)
                                this.monome.led(y %

                                        (this.monome.sizeX - 1),
                                        this.monome.sizeY - 3 + (y / (this.monome.sizeX - 1)), 1, this.index);
                        } catch (InvalidMidiDataException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

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

/**
 * Converts a button press into a MIDI note event
 * //from w  w w  .j a  va  2  s  .com
 * @param x The x value of the button pressed
 * @param y The y value of the button pressed
 * @param value The state, 1 = pressed, 0 = released
 */
public void playNote(int x, int y, int value) {
    int note_num = x + 12;
    int channel = y;
    int velocity = value * 127;
    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);
        }
    } catch (InvalidMidiDataException e) {
        e.printStackTrace();
    }
    if (this.recv != null) {
        this.recv.send(note_out, -1);
    }
}