@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); } }
public void send(MidiMessage message, long timeStamp) { synchronized (control_mutex) { if (!open) throw new IllegalStateException("Receiver is not open"); } if (timeStamp != -1) { synchronized (control_mutex) { mainmixer.activity(); while (midimessages.get(timeStamp) != null) timeStamp++; if (message instanceof ShortMessage && (((ShortMessage)message).getChannel() > 0xF)) { midimessages.put(timeStamp, message.clone()); } else { midimessages.put(timeStamp, message.getMessage()); } } } else { mainmixer.processMessage(message); } }
@Override public void send(MidiMessage message, long timeStamp) { synchronized (control_mutex) { if (!open) throw new IllegalStateException("Receiver is not open"); } if (timeStamp != -1) { synchronized (control_mutex) { mainmixer.activity(); while (midimessages.get(timeStamp) != null) timeStamp++; if (message instanceof ShortMessage && (((ShortMessage)message).getChannel() > 0xF)) { midimessages.put(timeStamp, message.clone()); } else { midimessages.put(timeStamp, message.getMessage()); } } } else { mainmixer.processMessage(message); } }
/** parses this message for a META tempo message and returns * the tempo in MPQ, or -1 if this isn't a tempo message */ public static int getTempoMPQ(MidiMessage midiMsg) { // first check if it is a META message at all if (midiMsg.getLength() != 6 || midiMsg.getStatus() != MetaMessage.META) { return -1; } byte[] msg = midiMsg.getMessage(); if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) { return -1; } int tempo = (msg[5] & 0xFF) | ((msg[4] & 0xFF) << 8) | ((msg[3] & 0xFF) << 16); return tempo; }
/** * Send midi player events. */ void sendControllerEvents(MidiMessage message) { int size = controllerEventListeners.size(); if (size == 0) return; //if (Printer.debug) Printer.debug("sending a controller event"); if (! (message instanceof ShortMessage)) { if (Printer.debug) Printer.debug("sendControllerEvents: message is NOT instanceof ShortMessage!"); return; } ShortMessage msg = (ShortMessage) message; int controller = msg.getData1(); List<Object> sendToListeners = new ArrayList<>(); for (int i = 0; i < size; i++) { ControllerListElement cve = controllerEventListeners.get(i); for(int j = 0; j < cve.controllers.length; j++) { if (cve.controllers[j] == controller) { sendToListeners.add(cve.listener); break; } } } getEventDispatcher().sendAudioEvents(message, sendToListeners); }
private void init() throws MidiUnavailableException { devIn = MidiSystem.getMidiDevice(inInfo); devIn.open(); devOut = MidiSystem.getMidiDevice(outInfo); devOut.open(); midiIn = new MidiInput(); devIn.getTransmitter().setReceiver(midiIn); midiOut = (MidiDeviceReceiver)devOut.getReceiver(); //MIDI Message Sender Thread: midiTx = new Thread(() -> { while(true) { int size; while((size=cue.size()) > 0) { MidiMessage msg; synchronized(this) { msg = cue.get(0); cue.remove(0); } midiOut.send(msg, -1); if(size > 10000) { //Keep array under 10,000... synchronized(this) { size=cue.size(); while(size > 50) { cue.remove(size-1); size--; }} debug("Buffer had to be force-cleared!"); } if(Thread.interrupted()) return; } if(Thread.interrupted()) return; }}); midiTx.setPriority(1); midiTx.start(); programMode(); setAll(-1); //Reset LaunchPad Lights. }
private void parseMessage(MidiMessage message) throws MidiPlayerException{ byte[] data = message.getMessage(); if( data.length > 0 ){ //NOTE ON if(((data[0] & 0xFF) & 0xF0) == ShortMessage.NOTE_ON){ parseNoteOn(data, findChannel(message), findVoice(message), findBendMode(message)); } //NOTE OFF else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.NOTE_OFF){ parseNoteOff(data, findChannel(message), findVoice(message), findBendMode(message)); } //PITCH BEND else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.PITCH_BEND){ parsePitchBend(data, findChannel(message), findVoice(message), findBendMode(message)); } //PROGRAM CHANGE else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.PROGRAM_CHANGE){ parseProgramChange(data, findChannel(message)); } //CONTROL CHANGE else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.CONTROL_CHANGE){ parseControlChange(data, findChannel(message)); } } }
public void send(MidiMessage inMessage, long inTimeStamp) { if(inMessage instanceof ShortMessage) { ShortMessage mm = (ShortMessage)inMessage; switch(mm.getCommand()) { case ShortMessage.NOTE_ON: case ShortMessage.NOTE_OFF: MiProvider.instance().noteReceived(mm, inTimeStamp); break; case ShortMessage.CONTROL_CHANGE: //System.err.println("Control change"); break; } } }
public void send(MidiMessage message, long timeStamp) { synchronized (control_mutex) { if (!open) throw new IllegalStateException("Receiver is not open"); } if (timeStamp != -1) { synchronized (control_mutex) { while (midimessages.get(timeStamp) != null) timeStamp++; if (message instanceof ShortMessage && (((ShortMessage)message).getChannel() > 0xF)) { midimessages.put(timeStamp, message.clone()); } else { midimessages.put(timeStamp, message.getMessage()); } } } else { mainmixer.processMessage(message); } }
@Override public void render() throws RenderCommandException { sleepUntil(getFrame().playOutTime); for(MidiMessage msg : getFrame().messages) { if(msg instanceof ShortMessage) { final ShortMessage sm = (ShortMessage)msg; final MidiChannel ch = synth.getChannels()[sm.getChannel()]; switch(sm.getCommand()) { case ShortMessage.NOTE_ON: ch.noteOn(sm.getData1(), sm.getData2()); continue; case ShortMessage.NOTE_OFF: ch.noteOff(sm.getData1(), sm.getData2()); continue; case ShortMessage.PROGRAM_CHANGE: ch.programChange(sm.getData1()); continue; case ShortMessage.CONTROL_CHANGE: ch.controlChange(sm.getData1(), sm.getData2()); continue; } } throw new RenderCommandException("Unknown MIDI Command:" + MidiToString.toString(msg)); } }
@Override public void handle(MidiMessage msg) { if(msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage)msg; switch(smsg.getCommand()) { case ShortMessage.CONTROL_CHANGE: int cc = smsg.getData1(); if(cc2run[cc] != null) cc2run[cc].handle(msg); break; case ShortMessage.NOTE_ON: int key = smsg.getData1(); if(key2run[key] != null) key2run[key].handle(msg); break; case ShortMessage.PITCH_BEND: pitchBend.set(smsg); synchronized (processMidi) {processMidi.notifyAll();} break; } } System.out.println(MidiToString.toString(msg)); }
@Override protected void captureMidiMessages(final Receiver receiver) throws MidiUnavailableException { // Modify all MidiMessages so that they specify channel 10 (encoded as 9, // which I suppose means that 0 indicates midi channel 1) Receiver setChannelReceiver = new Receiver() { @Override public void send(MidiMessage message, long timeStamp) { if (message instanceof ShortMessage) { ShortMessage sm = (ShortMessage) message; byte[] data = sm.getMessage(); byte orig = data[0]; data[0] = (byte) ((orig | 9) & 0xff); message = Midi.createShortMessage(data); } receiver.send(message, timeStamp); } @Override public void close() { receiver.close(); } }; super.captureMidiMessages(setChannelReceiver); }
@Override public void send(MidiMessage message, long timeStamp) { super.send(message, timeStamp); if (!midiLearnService.isMidiLearning() && interpretedMessage != null && !interpretedSignature.equals(MidiUtils.UNKNOWN_MESSAGE) && !isExecuting) { log.debug(name + " checking MIDI trigger for message: " + interpretedSignature); guiAutomationsService .activateAutomationsByMidiMessage(interpretedMessage); isExecuting = false; } }
public static MidiMessage createMTCSetup(int channel, MTC.Time time, MTC.FrameRate rate, int type, int event, byte[] add) throws InvalidMidiDataException { int addlen = add == null ? 0 : 2 * add.length; byte[] data = new byte[13+addlen]; createUniversalHeader(data, ID_UNIVERSAL_NON_REALTIME, channel, 0x04, type); data[5] = (byte) ((time.hours & 0x0f) | (rate.getIndex() << 4)); data[6] = (byte) (time.minutes & 0x3f); // mn data[7] = (byte) (time.seconds & 0x3f); // sc data[8] = (byte) (time.frames & 0x1f); // fr data[9] = (byte) (time.fractionalFrames & 0x7f); // ff data[10] = (byte) (event & 0x7f); // sl data[11] = (byte) ((event >> 7) & 0x7f); // sm // nibblize additional data if ( addlen > 0 ) { for ( int i = 0, j = 12; i < addlen; i++, j += 2 ) { data[j] = (byte)(add[i] & 0x0f); // ls nibble data[j+1] = (byte)((add[i] >> 4) & 0x0f); // ms nibble } } data[data.length-1] = (byte) END_OF_EXCLUSIVE; return createSysex(data, 13); }
@Override public void send(MidiMessage message, long timeStamp) { if (receiver == null) return; if (hasAbcPart && message instanceof ShortMessage) { ShortMessage m = (ShortMessage) message; int c = m.getChannel(); int cmd = m.getCommand(); int noteId = m.getData1(); int speed = m.getData2(); boolean noteOnMsg = (cmd == ShortMessage.NOTE_ON) && speed > 0; boolean noteOffMsg = (cmd == ShortMessage.NOTE_OFF) || (cmd == ShortMessage.NOTE_ON && speed == 0); if (!isNoteActive(noteId) && !(isNoteOn(c, noteId) && noteOffMsg)) return; if (noteOnMsg) setNoteOn(c, noteId, true); else if (noteOffMsg) setNoteOn(c, noteId, false); } receiver.send(message, timeStamp); }
public void midiMessage(MidiMessage message) { if (!sequencer.isRecording()) { return; } long tick = sequencer.getRealTimeTickPosition(); if (message instanceof ShortMessage) { try { ShortMessage shm = (ShortMessage) message; Event event = stack.poke(); event.mess = (ShortMessage) message; event.stamp = tick; } catch (Exception ex) { Logger.getLogger(RecordingManager.class.getName()).log(Level.SEVERE, null, ex); } } }
public void send(MidiMessage mess, long arg1) { ShortMessage smsg = (ShortMessage) mess; System.out.println("ch cmd data1 data2: " + smsg.getChannel() + " " + smsg.getCommand() + " " + smsg.getData1() + " " + smsg.getData2()); double t = valueizer.getValue((ShortMessage) mess); System.out.println(" Send message to " + cntrl + " " + t); if (cntrl instanceof ControlLaw) { ControlLaw law = ((LawControl) cntrl).getLaw(); float val = (float) (law.getMaximum() * t + law.getMinimum() * (1 - t)); ((LawControl) cntrl).setValue(val); } else if (cntrl instanceof BooleanControl) { System.out.println(" BOOLEAN" ); ((BooleanControl) cntrl).setValue(t>0); } }
public void send(MidiMessage mess, long timeStamp) { if (mess.getStatus() >= ShortMessage.MIDI_TIME_CODE) { // System.out.println(mess + " " + mess.getStatus() ); return; } // make time stamp -1 (otherwise bad timing) if (mess instanceof ShortMessage) { ShortMessage shm = (ShortMessage) mess; int cmd = shm.getCommand(); if (cmd != 240) { int chn = shm.getChannel(); int dat1 = shm.getData1(); int dat2 = shm.getData2(); System.out.println(" cmd:" + cmd + " chn:" + chn + " data:" + dat1 + " " + dat2); } } chained.send(mess, -1); }
/** * Render a bar of notes as MIDI to the specified Track from the * specified start tick with the specified ticks per bar. * @param notes the notes to render * @param track the MIDI Track to render to * @param startTick the tick at the start of the bar * @param ticksPerBar the number of ticks per bar */ public void renderBar(int[] notes, Track track, long startTick, int ppqn) throws InvalidMidiDataException { final int channel = getInstrument().getChannel(); MidiMessage msg; for ( int i = 0; i < notes.length; i++) { int note = notes[i]; float timeOn = swing(Note.getTime(note)); int pitch = Note.getPitch(note); int level = Note.getLevel(note); long onTick = ticks2MidiTicks(timeOn, ppqn); msg = ChannelMsg.createChannel( ChannelMsg.NOTE_ON, channel, pitch, level); track.add(new MidiEvent(msg, startTick + onTick)); // note off msg = ChannelMsg.createChannel( ChannelMsg.NOTE_OFF, channel, pitch, 0); float timeOff = swing(Note.getTime(note)+Note.getDuration(note)); long offTick = ticks2MidiTicks(timeOff, ppqn); track.add(new MidiEvent(msg, startTick + offTick)); } }
public static MidiMessage createControl( int providerId, int moduleId, int instanceIndex, int controlId, int value) throws InvalidMidiDataException { if ( instanceIndex > 7 ) throw new InvalidMidiDataException( "Instance index "+instanceIndex+" MUST be < 8"); byte[] data = new byte[LENGTH]; data[0] = (byte)SYSTEM_EXCLUSIVE; data[1] = ID_NON_COMMERCIAL; data[2] = (byte)(providerId & 0x7f); data[3] = (byte)(moduleId & 0x7f); data[4] = (byte)(instanceIndex & 0x07); // only use 3 bits, 4 spare data[5] = (byte)(controlId & 0x7f); data[6] = (byte)(value & 0x7f); data[7] = (byte)((value >> 7) & 0x7f); // could borrow 2 bits here MidiMessage msg = createSysex(data, LENGTH); assert isControl(msg); return msg; }
@Override public void update() throws Exception { this.transmitterLoop++; if (this.transmitterLoop % 600 == 0) { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if (NMOConfiguration.INSTANCE.integrations.midiTransmitter.transmitters.contains(device.getDeviceInfo().getName())) { if (!device.isOpen() && device.getMaxTransmitters() != 0) { final String name = device.getDeviceInfo().getName() + "/" + device.getDeviceInfo().getDescription() + "/" + device.getDeviceInfo().getVendor(); log.info("Connected MIDI device: " + name); device.getTransmitter().setReceiver(new Receiver() { @Override public void send(MidiMessage message, long timeStamp) { MainDialog.resetActivityTimer(MIDI_TRANSMITTER); } @Override public void close() { log.info("Disconnected MIDI device: " + name); } }); device.open(); } } } } }
public void send(MidiMessage msg, long timeStamp) { if(first == 0){ first = System.currentTimeMillis(); } System.out.println(((System.currentTimeMillis() - first) / 1000.0) + " midi received"); }
public void processMessage(MidiMessage message) { if (message instanceof ShortMessage) { ShortMessage sms = (ShortMessage)message; processMessage(sms.getChannel(), sms.getCommand(), sms.getData1(), sms.getData2()); return; } processMessage(message.getMessage()); }
/** * Loads and adds the effects to the given midi track * * @param track The track to write to * @param channel The channel to write to */ @Override public void writeToTrack(Track track, int channel) { long lastEnd = 0; for (String name : effects.keySet()) { Sequence sequence = FileUtils.LoadMidiFile("effects/" + name + ".mid"); if (sequence != null) { int start =// Position effect in track (int) (QUARTER * tempo.averageBpm / 60.0 * //beats per second 15 * //because 15 seconds effects.get(name) //i-th word in text ); if (start < lastEnd) start = (int) lastEnd; if (sequence.getMicrosecondLength() / 1000000.0 + TicksInSecs(start, tempo.resolution) > 15.0) { start -= SecsInTicks(TicksInSecs(start, tempo.resolution) + sequence.getMicrosecondLength() / 1000000.0 - 15, tempo.resolution); } float scale = tempo.resolution / (float) sequence.getResolution(); // Make the tempo fit for (Track t : sequence.getTracks()) { for (int i = 0; i < t.size(); i++) { MidiEvent event = t.get(i); byte[] data = event.getMessage().getMessage();//(command & 0xF0) | (channel & 0x0F) data[0] += 2; // Keep channel 1 and 2 free long tick = (long) (event.getTick() * scale) + start; MidiEvent ev = new MidiEvent(new MidiMessage(data) { @Override public Object clone() { return null; } }, tick); track.add(ev); if (tick > lastEnd) lastEnd = tick; } } } } }
/** Deliver a MidiMessage. This method contains magic related to the closed state of a Receiver. Therefore, subclasses should not override this method. Instead, they should implement implSend(). */ @Override public final synchronized void send(final MidiMessage message, final long timeStamp) { if (!open) { throw new IllegalStateException("Receiver is not open"); } implSend(message, timeStamp); }
/** * Send this message to all transmitters. */ void sendMessage(MidiMessage message, long timeStamp) { if (message instanceof FastShortMessage) { sendMessage(((FastShortMessage) message).getPackedMsg(), timeStamp); return; } synchronized(transmitters) { int size = transmitters.size(); if (optimizedReceiverCount == size) { if (midiOutReceiver != null) { if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to MidiOutReceiver"); midiOutReceiver.send(message, timeStamp); } } else { if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to "+size+" transmitter's receivers"); for (int i = 0; i < size; i++) { Receiver receiver = transmitters.get(i).getReceiver(); if (receiver != null) { //$$fb 2002-04-02: ShortMessages are mutable, so // an application could change the contents of this object, // or try to use the object later. // We violate this spec here, to avoid costly (and gc-intensive) // object creation for potentially hundred of messages per second. // The spec should be changed to allow Immutable MidiMessages // (i.e. throws InvalidStateException or so in setMessage) receiver.send(message, timeStamp); } } } } }
/** return true if the passed message is Meta End Of Track */ public static boolean isMetaEndOfTrack(MidiMessage midiMsg) { // first check if it is a META message at all if (midiMsg.getLength() != 3 || midiMsg.getStatus() != MetaMessage.META) { return false; } // now get message and check for end of track byte[] msg = midiMsg.getMessage(); return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0); }
/** return if the given message is a meta tempo message */ public static boolean isMetaTempo(MidiMessage midiMsg) { // first check if it is a META message at all if (midiMsg.getLength() != 6 || midiMsg.getStatus() != MetaMessage.META) { return false; } // now get message and check for tempo byte[] msg = midiMsg.getMessage(); // meta type must be 0x51, and data length must be 3 return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3); }
/** * Send midi player events. * must not be synchronized on "this" */ void sendMetaEvents(MidiMessage message) { if (metaEventListeners.size() == 0) return; //if (Printer.debug) Printer.debug("sending a meta event"); getEventDispatcher().sendAudioEvents(message, metaEventListeners); }
public static void main(String[] args) throws Exception { MidiDevice/*Synthesizer*/ synth = null; try { synth = MidiSystem.getSynthesizer(); //synth = MidiSystem.getMidiDevice(infos[0]); System.out.println("Synthesizer: " + synth.getDeviceInfo()); synth.open(); MidiMessage msg = new GenericMidiMessage(0x90, 0x3C, 0x40); //ShortMessage msg = new ShortMessage(); //msg.setMessage(0x90, 0x3C, 0x40); synth.getReceiver().send(msg, 0); Thread.sleep(2000); } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { if (synth != null && synth.isOpen()) synth.close(); } System.out.print("Did you heard a note? (enter 'y' or 'n') "); int result = System.in.read(); System.in.skip(1000); if (result == 'y' || result == 'Y') { System.out.println("Test passed sucessfully."); } else { System.out.println("Test FAILED."); throw new RuntimeException("Test failed."); } }
public static void printEvent(MidiEvent event) { MidiMessage message = event.getMessage(); long tick = event.getTick(); byte[] data = message.getMessage(); StringBuffer sb = new StringBuffer((data.length * 3) - 1); for (int i = 0; i < data.length; i++) { sb.append(toHexByteString(data[i])); if (i < data.length - 1) sb.append(' '); } System.out.printf("%5d: %s%n", tick, sb); }
private static boolean messagesEqual(MidiMessage m1, MidiMessage m2) { if (m1 == null || m2 == null) { return false; } if (m1.getLength() != m2.getLength()) { return false; } byte[] array1 = m1.getMessage(); byte[] array2 = m2.getMessage(); return messagesEqual(array1, array2); }