Example usage for javax.sound.midi ShortMessage CONTROL_CHANGE

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

Introduction

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

Prototype

int CONTROL_CHANGE

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

Click Source Link

Document

Command value for Control Change message (0xB0, or 176).

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 .  ja v  a 2s  .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 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;//from   www .  j a  va2 s. c o  m
            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:com.rockhoppertech.music.midi.js.MIDIEvent.java

public String toReadableString() {
    StringBuilder sb = new StringBuilder();

    // switch(se.getType()) {
    // case MidiEvent.CHANNEL_VOICE_MESSAGE:

    switch (status & 0xF0) {
    case ShortMessage.NOTE_OFF:
        sb.append("Note Off Key=").append(bytes[0]).append(" Velocity=").append(bytes[1]);
        break;//from  w w w  .j av a  2 s .com

    case ShortMessage.NOTE_ON:
        sb.append("Note On Key=").append(bytes[0]).append(" Velocity=").append(bytes[1]);
        break;

    case ShortMessage.POLY_PRESSURE:
        sb.append("Polyphonic key pressure key=").append(bytes[0]).append(" pressure=").append(bytes[1]);
        break;

    case ShortMessage.CONTROL_CHANGE:
        sb.append("Control Change controller=").append(bytes[0]).append(" value=").append(bytes[1]);
        sb.append(" ").append(MIDIControllers.getControllerName((int) bytes[0]));

        break;

    case ShortMessage.PROGRAM_CHANGE:
        sb.append("Program Change program=").append(bytes[0]).append(" name=")
                .append(MIDIGMPatch.getName(bytes[0]));
        break;

    case ShortMessage.CHANNEL_PRESSURE:
        sb.append("Channel Pressure pressure=").append(bytes[0]);
        break;

    case ShortMessage.PITCH_BEND:
        // int val = (this.bytes[0] & 0x7f) | ((this.bytes[1] & 0x7f) << 7);
        // short centered = 0x2000;
        short s14bit;
        s14bit = bytes[1];
        s14bit <<= 7;
        s14bit |= bytes[0];

        sb.append("Pitch Bend one=").append(bytes[0]).append(" two=").append(bytes[1]);
        sb.append(" val=").append(s14bit);
        break;
    default:
        sb.append(" Unhandled=").append(status & 0xF0);
        break;
    }
    sb.append(" Channel=").append(status & 0x0F);
    sb.append(" status=").append(Integer.toHexString(status));

    /*
     * case MidiEvent.CHANNEL_MODE_MESSAGE: printChannelModeMessage(se);
     * break;
     * 
     * case MidiEvent.SYSTEM_COMMON_MESSAGE : System.out.print(" system
     * common message "); break;
     * 
     * case MidiEvent.SYSTEM_REALTIME_MESSAGE : System.out.print(" system
     * realtime message "); break;
     * 
     * default: strMessage = "unknown event: status = " + se.getStatus() +
     * ", byte1 = " + this.bytes[0] + ", byte2 = " + this.bytes[1];
     */
    // System.out.print(sw.toString());
    return sb.toString();

}

From source file:org.monome.pages.configuration.MonomeConfiguration.java

/**
 * Called every time a MIDI message is received, the messages are passed along to each page.
 * /*  w w  w . ja  v a 2 s.c  o  m*/
 * @param message The MIDI message received
 * @param timeStamp The timestamp of the MIDI message
 */
public synchronized void send(MidiDevice device, MidiMessage message, long timeStamp) {
    if (this.useMIDIPageChanging) {
        if (message instanceof ShortMessage) {
            ShortMessage msg = (ShortMessage) message;
            int velocity = msg.getData1();
            if (msg.getCommand() == ShortMessage.NOTE_ON && velocity > 0) {
                int channel = msg.getChannel();
                int note = msg.getData1();

                for (int j = 0; j < this.pageChangeMidiInDevices.length; j++) {
                    if (this.pageChangeMidiInDevices[j] == null) {
                        continue;
                    }
                    if (this.pageChangeMidiInDevices[j].compareTo(device.getDeviceInfo().getName()) == 0) {
                        for (int i = 0; i < this.midiPageChangeRules.size(); i++) {
                            MIDIPageChangeRule mpcr = this.midiPageChangeRules.get(i);
                            if (mpcr.checkNoteRule(note, channel) == true) {
                                int switchToPageIndex = mpcr.getPageIndex();
                                Page page = this.pages.get(switchToPageIndex);
                                this.switchPage(page, switchToPageIndex, true);
                            }
                        }
                    }
                }
            }
            if (msg.getCommand() == ShortMessage.CONTROL_CHANGE) {
                int cc = msg.getData1();
                int ccVal = msg.getData2();
                int channel = msg.getChannel();
                for (int j = 0; j < this.pageChangeMidiInDevices.length; j++) {
                    if (this.pageChangeMidiInDevices[j] == null) {
                        continue;
                    }
                    if (this.pageChangeMidiInDevices[j].compareTo(device.getDeviceInfo().getName()) == 0) {
                        for (int i = 0; i < this.midiPageChangeRules.size(); i++) {
                            MIDIPageChangeRule mpcr = this.midiPageChangeRules.get(i);
                            if (mpcr.checkCCRule(cc, ccVal, channel) == true) {
                                int switchToPageIndex = mpcr.getPageIndex();
                                Page page = this.pages.get(switchToPageIndex);
                                this.switchPage(page, switchToPageIndex, true);
                            }
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < this.numPages; i++) {
        for (int j = 0; j < this.midiInDevices[i].length; j++) {
            if (this.midiInDevices[i][j] == null) {
                continue;
            }
            if (this.midiInDevices[i][j].compareTo(device.getDeviceInfo().getName()) == 0) {
                this.pages.get(i).send(message, timeStamp);
            }
        }
    }
}

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.
 *
 *//* w  w  w  .  j  ava  2s. co  m*/
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();
    }
}