Search in sources :

Example 1 with ShortMessage

use of javax.sound.midi.ShortMessage in project jdk8u_jdk by JetBrains.

the class GetReceiver2 method main.

public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    Receiver recv = synth.getReceiver();
    assertTrue(recv != null);
    ShortMessage sm = new ShortMessage();
    sm.setMessage(ShortMessage.NOTE_OFF, 0, 64, 64);
    synth.open(new DummySourceDataLine(), null);
    recv.send(sm, -1);
    synth.close();
    try {
        recv.send(sm, -1);
        throw new RuntimeException("Exception not thrown!");
    } catch (Exception e) {
    // Just checking if exception is thrown
    }
}
Also used : ShortMessage(javax.sound.midi.ShortMessage) Receiver(javax.sound.midi.Receiver) SoftSynthesizer(com.sun.media.sound.SoftSynthesizer) AudioSynthesizer(com.sun.media.sound.AudioSynthesizer)

Example 2 with ShortMessage

use of javax.sound.midi.ShortMessage in project jdk8u_jdk by JetBrains.

the class EventDispatcher method processEvent.

/**
     * Invoked when there is at least one event in the queue.
     * Implement this as a callback to process one event.
     */
void processEvent(EventInfo eventInfo) {
    int count = eventInfo.getListenerCount();
    // process an LineEvent
    if (eventInfo.getEvent() instanceof LineEvent) {
        LineEvent event = (LineEvent) eventInfo.getEvent();
        if (Printer.debug)
            Printer.debug("Sending " + event + " to " + count + " listeners");
        for (int i = 0; i < count; i++) {
            try {
                ((LineListener) eventInfo.getListener(i)).update(event);
            } catch (Throwable t) {
                if (Printer.err)
                    t.printStackTrace();
            }
        }
        return;
    }
    // process a MetaMessage
    if (eventInfo.getEvent() instanceof MetaMessage) {
        MetaMessage event = (MetaMessage) eventInfo.getEvent();
        for (int i = 0; i < count; i++) {
            try {
                ((MetaEventListener) eventInfo.getListener(i)).meta(event);
            } catch (Throwable t) {
                if (Printer.err)
                    t.printStackTrace();
            }
        }
        return;
    }
    // process a Controller or Mode Event
    if (eventInfo.getEvent() instanceof ShortMessage) {
        ShortMessage event = (ShortMessage) eventInfo.getEvent();
        int status = event.getStatus();
        // c is the channel they are sent on.
        if ((status & 0xF0) == 0xB0) {
            for (int i = 0; i < count; i++) {
                try {
                    ((ControllerEventListener) eventInfo.getListener(i)).controlChange(event);
                } catch (Throwable t) {
                    if (Printer.err)
                        t.printStackTrace();
                }
            }
        }
        return;
    }
    Printer.err("Unknown event type: " + eventInfo.getEvent());
}
Also used : LineEvent(javax.sound.sampled.LineEvent) ControllerEventListener(javax.sound.midi.ControllerEventListener) ShortMessage(javax.sound.midi.ShortMessage) MetaEventListener(javax.sound.midi.MetaEventListener) LineListener(javax.sound.sampled.LineListener) MetaMessage(javax.sound.midi.MetaMessage)

Example 3 with ShortMessage

use of javax.sound.midi.ShortMessage in project blue by kunstmusik.

the class MidiImportUtilities method getMidiImportSettings.

private static MidiImportSettings getMidiImportSettings(Track[] tracks) {
    MidiImportSettings settings = new MidiImportSettings();
    for (int i = 0; i < tracks.length; i++) {
        Track track = tracks[i];
        boolean containsNotes = false;
        for (int j = 0; j < track.size(); j++) {
            MidiEvent me = track.get(j);
            MidiMessage message = me.getMessage();
            if (message instanceof ShortMessage) {
                ShortMessage shortMsg = (ShortMessage) message;
                int velocity;
                MNote n;
                switch(shortMsg.getCommand()) {
                    case ShortMessage.NOTE_ON:
                        velocity = shortMsg.getData2();
                        if (velocity == 0) {
                            containsNotes = true;
                        }
                        break;
                    case ShortMessage.NOTE_OFF:
                        containsNotes = true;
                        break;
                }
                if (containsNotes) {
                    break;
                }
            }
        }
        if (containsNotes) {
            TrackImportSettings trSettings = new TrackImportSettings();
            String instrId = Integer.toString(i);
            trSettings.setInstrId(instrId);
            trSettings.setTrackNumber(i);
            trSettings.setNoteTemplate(NOTE_TEMPLATE);
            settings.addTrackImportSetting(trSettings);
        }
    }
    return settings;
}
Also used : MidiMessage(javax.sound.midi.MidiMessage) ShortMessage(javax.sound.midi.ShortMessage) MidiEvent(javax.sound.midi.MidiEvent) Track(javax.sound.midi.Track)

Example 4 with ShortMessage

use of javax.sound.midi.ShortMessage in project blue by kunstmusik.

the class VirtualKeyboardPanel method handleKey.

protected void handleKey(char key, boolean keyDown) {
    int index = -1;
    switch(key) {
        case 'z':
            index = 0;
            break;
        case 's':
            index = 1;
            break;
        case 'x':
            index = 2;
            break;
        case 'd':
            index = 3;
            break;
        case 'c':
            index = 4;
            break;
        case 'v':
            index = 5;
            break;
        case 'g':
            index = 6;
            break;
        case 'b':
            index = 7;
            break;
        case 'h':
            index = 8;
            break;
        case 'n':
            index = 9;
            break;
        case 'j':
            index = 10;
            break;
        case 'm':
            index = 11;
            break;
        case 'q':
            index = 12;
            break;
        case '2':
            index = 13;
            break;
        case 'w':
            index = 14;
            break;
        case '3':
            index = 15;
            break;
        case 'e':
            index = 16;
            break;
        case 'r':
            index = 17;
            break;
        case '5':
            index = 18;
            break;
        case 't':
            index = 19;
            break;
        case '6':
            index = 20;
            break;
        case 'y':
            index = 21;
            break;
        case '7':
            index = 22;
            break;
        case 'u':
            index = 23;
            break;
        case 'i':
            index = 24;
            break;
        case '9':
            index = 25;
            break;
        case 'o':
            index = 26;
            break;
        case '0':
            index = 27;
            break;
        case 'p':
            index = 28;
            break;
        default:
            return;
    }
    if (index < 0) {
        return;
    }
    index = ((octave * 12) + index) - 21;
    if (index < 0 || index > 87) {
        return;
    }
    if (keyStates[index].get() != keyDown) {
        keyStates[index].set(keyDown);
        try {
            ShortMessage sme = new ShortMessage();
            int state = keyDown ? ShortMessage.NOTE_ON : ShortMessage.NOTE_OFF;
            sme.setMessage(state, channel, index + KEY_OFFSET, velocity);
            midiEngine.send(sme, 0L);
        } catch (InvalidMidiDataException ex) {
            Exceptions.printStackTrace(ex);
        }
        repaint();
    }
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) ShortMessage(javax.sound.midi.ShortMessage)

Example 5 with ShortMessage

use of javax.sound.midi.ShortMessage in project blue by kunstmusik.

the class VirtualKeyboardPanel method allNotesOff.

public void allNotesOff() {
    ShortMessage sme = new ShortMessage();
    for (int i = 0; i < 88; i++) {
        if (keyStates[i].getAndSet(false)) {
            try {
                sme.setMessage(ShortMessage.NOTE_OFF, channel, i + KEY_OFFSET, velocity);
                midiEngine.send(sme, 0L);
            } catch (InvalidMidiDataException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    repaint();
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) ShortMessage(javax.sound.midi.ShortMessage)

Aggregations

ShortMessage (javax.sound.midi.ShortMessage)13 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)4 MidiEvent (javax.sound.midi.MidiEvent)4 MetaMessage (javax.sound.midi.MetaMessage)3 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)2 SoftSynthesizer (com.sun.media.sound.SoftSynthesizer)2 MidiMessage (javax.sound.midi.MidiMessage)2 Receiver (javax.sound.midi.Receiver)2 SysexMessage (javax.sound.midi.SysexMessage)2 NoteList (blue.soundObject.NoteList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Random (java.util.Random)1 ControllerEventListener (javax.sound.midi.ControllerEventListener)1 MetaEventListener (javax.sound.midi.MetaEventListener)1 Track (javax.sound.midi.Track)1