Search in sources :

Example 6 with ShortMessage

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

the class ScoPadReceiver method send.

/* MIDI Receiver Methods */
@Override
public void send(MidiMessage message, long timeStamp) {
    if (message instanceof ShortMessage) {
        ShortMessage shortMsg = (ShortMessage) message;
        int noteNum, velocity;
        // long duration;
        MidiNote n;
        switch(shortMsg.getCommand()) {
            case ShortMessage.NOTE_ON:
                noteNum = shortMsg.getData1();
                velocity = shortMsg.getData2();
                n = notes[noteNum];
                if (velocity > 0) {
                    n.start = System.currentTimeMillis();
                    n.velocity = velocity;
                } else {
                    // duration = System.currentTimeMillis() - n.start;
                    // if(start > 0) {
                    // 
                    // String noteStr = "i1 " + ((n.start - start)/ 1000.0f)
                    // + " " + (duration / 1000.f) + " " + noteNum + " " +
                    // n.velocity;
                    // outputTextArea.setText(outputTextArea.getText() +
                    // noteStr + "\n");
                    // 
                    // }
                    n.start = -1;
                    n.velocity = -1;
                }
                break;
            case ShortMessage.NOTE_OFF:
                noteNum = shortMsg.getData1();
                velocity = shortMsg.getData2();
                n = notes[noteNum];
                // duration = System.currentTimeMillis() - n.start;
                // if(start > 0) {
                // String noteStr = "i1 " + ((n.start - start) / 1000.0f) +
                // " " + (duration / 1000.f) + " " + noteNum + " " +
                // n.velocity;
                // outputTextArea.setText(outputTextArea.getText() + noteStr
                // + "\n");
                // 
                // }
                n.start = -1;
                n.velocity = -1;
                break;
        }
    }
}
Also used : ShortMessage(javax.sound.midi.ShortMessage)

Example 7 with ShortMessage

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

the class MidiInputEngine method send.

@Override
public void send(MidiMessage message, long timeStamp) {
    if (message instanceof ShortMessage) {
        ShortMessage shortMsg = (ShortMessage) message;
        int channel = shortMsg.getChannel();
        int noteNum = shortMsg.getData1();
        int velocity = shortMsg.getData2();
        if (processor == null || arrangement == null || channel >= arrangement.size()) {
            return;
        }
        String id = arrangement.get(channel).arrangementId;
        String score = "i";
        switch(shortMsg.getCommand()) {
            case ShortMessage.NOTE_ON:
                if (velocity > 0) {
                    score = processor.getNoteOn(id, noteNum, noteNum, velocity);
                } else {
                    score = processor.getNoteOff(id, noteNum);
                }
                break;
            case ShortMessage.NOTE_OFF:
                // noteNum = shortMsg.getData1();
                // velocity = shortMsg.getData2();
                score = processor.getNoteOff(id, noteNum);
                break;
        }
        System.err.println(score);
        if (toolbar == null) {
            toolbar = BlueLiveToolBar.getInstance();
        }
        toolbar.sendEvents(score);
    }
}
Also used : ShortMessage(javax.sound.midi.ShortMessage)

Example 8 with ShortMessage

use of javax.sound.midi.ShortMessage in project Zong by Xenoage.

the class JseMidiSequenceWriter method writeShortMessage.

@Override
public void writeShortMessage(int track, int channel, long tick, int command, int data1, int data2) {
    try {
        ShortMessage message = new ShortMessage();
        message.setMessage(command, channel, data1, data2);
        tracks[track].add(new MidiEvent(message, tick));
    } catch (InvalidMidiDataException ex) {
    // ignore - TODO
    }
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) ShortMessage(javax.sound.midi.ShortMessage) MidiEvent(javax.sound.midi.MidiEvent)

Example 9 with ShortMessage

use of javax.sound.midi.ShortMessage in project sponge by softelnet.

the class MidiSpongeEventReceiver method send.

@Override
public void send(MidiMessage message, long timeStamp) {
    try {
        if (sound) {
            midiPlugin.sound(message);
        }
        MidiMessageEvent<?> event;
        if (message instanceof ShortMessage) {
            event = MidiUtils.createShortMessageEvent(midiPlugin, (ShortMessage) message, timeStamp);
        } else if (message instanceof SysexMessage) {
            event = new MidiSysexMessageEvent(midiPlugin.getMidiSysexMessageEventName(), midiPlugin.getEngine().getDefaultParameters().getEventClonePolicy(), (SysexMessage) message, timeStamp);
        } else if (message instanceof MetaMessage) {
            event = new MidiMetaMessageEvent(midiPlugin.getMidiMetaMessageEventName(), midiPlugin.getEngine().getDefaultParameters().getEventClonePolicy(), (MetaMessage) message, timeStamp);
        } else {
            throw new SpongeException("Unknown MIDI message type: " + message.getClass());
        }
        midiPlugin.getEngine().getOperations().event(event).send();
    } catch (Exception e) {
        logger.error("send", e);
    }
}
Also used : ShortMessage(javax.sound.midi.ShortMessage) SpongeException(org.openksavi.sponge.SpongeException) SysexMessage(javax.sound.midi.SysexMessage) MetaMessage(javax.sound.midi.MetaMessage) MidiMetaMessageEvent(org.openksavi.sponge.midi.event.MidiMetaMessageEvent) SpongeException(org.openksavi.sponge.SpongeException) MidiSysexMessageEvent(org.openksavi.sponge.midi.event.MidiSysexMessageEvent)

Example 10 with ShortMessage

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

the class TestPreciseTimestampRendering method test.

public static void test(Soundbank soundbank) throws Exception {
    // Create instance of synthesizer using the testing soundbank above
    AudioSynthesizer synth = new SoftSynthesizer();
    AudioInputStream stream = synth.openStream(format, null);
    synth.unloadAllInstruments(synth.getDefaultSoundbank());
    synth.loadAllInstruments(soundbank);
    Receiver recv = synth.getReceiver();
    // Set volume to max and turn reverb off
    ShortMessage reverb_off = new ShortMessage();
    reverb_off.setMessage(ShortMessage.CONTROL_CHANGE, 91, 0);
    recv.send(reverb_off, -1);
    ShortMessage full_volume = new ShortMessage();
    full_volume.setMessage(ShortMessage.CONTROL_CHANGE, 7, 127);
    recv.send(full_volume, -1);
    Random random = new Random(3485934583945l);
    // Create random timestamps
    long[] test_timestamps = new long[30];
    for (int i = 1; i < test_timestamps.length; i++) {
        test_timestamps[i] = i * 44100 + (int) (random.nextDouble() * 22050.0);
    }
    // Send midi note on message to synthesizer
    for (int i = 0; i < test_timestamps.length; i++) {
        ShortMessage midi_on = new ShortMessage();
        midi_on.setMessage(ShortMessage.NOTE_ON, 69, 127);
        recv.send(midi_on, (long) ((test_timestamps[i] / 44100.0) * 1000000.0));
    }
    // Measure timing from rendered audio
    float[] fbuffer = new float[100];
    byte[] buffer = new byte[fbuffer.length * format.getFrameSize()];
    long firsts = -1;
    int counter = 0;
    long s = 0;
    long max_jitter = 0;
    outerloop: for (int k = 0; k < 10000000; k++) {
        stream.read(buffer);
        AudioFloatConverter.getConverter(format).toFloatArray(buffer, fbuffer);
        for (int i = 0; i < fbuffer.length; i++) {
            if (fbuffer[i] != 0) {
                if (firsts == -1)
                    firsts = s;
                long measure_time = (s - firsts);
                long predicted_time = test_timestamps[counter];
                long jitter = Math.abs(measure_time - predicted_time);
                if (jitter > 10)
                    max_jitter = jitter;
                counter++;
                if (counter == test_timestamps.length)
                    break outerloop;
            }
            s++;
        }
    }
    synth.close();
    if (counter == 0)
        throw new Exception("Nothing was measured!");
    if (max_jitter != 0) {
        throw new Exception("Jitter has occurred! " + "(max jitter = " + max_jitter + ")");
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Random(java.util.Random) ShortMessage(javax.sound.midi.ShortMessage) Receiver(javax.sound.midi.Receiver) SoftSynthesizer(com.sun.media.sound.SoftSynthesizer) AudioSynthesizer(com.sun.media.sound.AudioSynthesizer)

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