@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); } }
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 } }
@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); } }
/** * 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); }
void notesOff(boolean doControllers) { int done = 0; for (int ch=0; ch<16; ch++) { int channelMask = (1<<ch); for (int i=0; i<128; i++) { if ((noteOnCache[i] & channelMask) != 0) { noteOnCache[i] ^= channelMask; // send note on with velocity 0 getTransmitterList().sendMessage((ShortMessage.NOTE_ON | ch) | (i<<8), -1); done++; } } /* all notes off */ getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (123<<8), -1); /* sustain off */ getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (64<<8), -1); if (doControllers) { /* reset all controllers */ getTransmitterList().sendMessage((ShortMessage.CONTROL_CHANGE | ch) | (121<<8), -1); done++; } } if (DEBUG_PUMP) Printer.println(" noteOff: sent "+done+" messages."); }
/** * Execute Receiver.send() and expect that there is no exception. */ private static boolean testReceiverSend() { boolean result = true; Receiver receiver; ShortMessage shMsg = new ShortMessage(); try { receiver = MidiSystem.getReceiver(); shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93); try { receiver.send( shMsg, -1 ); } catch(IllegalStateException ilEx) { ilEx.printStackTrace(System.out); out("IllegalStateException was thrown incorrectly!"); result = false; } receiver.close(); } catch(MidiUnavailableException e) { out("Midi unavailable, cannot test."); } catch(InvalidMidiDataException ine) { out("InvalidMidiDataException, cannot test."); } return result; }
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)); }
/** * Add the given MidiNote into the {@link #sequence}. * * @param note The note to add. * * @throws InvalidMidiDataException If the MidiNote contains invalid Midi data. */ public void addMidiNote(MidiNote note) throws InvalidMidiDataException { int correctVoice = note.getCorrectVoice(); // Pad with enough tracks while (sequence.getTracks().length <= correctVoice) { sequence.createTrack(); } // Get the correct track Track track = sequence.getTracks()[correctVoice]; ShortMessage noteOn = new ShortMessage(); noteOn.setMessage(ShortMessage.NOTE_ON | correctVoice, note.getPitch(), note.getVelocity()); MidiEvent noteOnEvent = new MidiEvent(noteOn, note.getOnsetTick()); ShortMessage noteOff = new ShortMessage(); noteOff.setMessage(ShortMessage.NOTE_OFF | correctVoice, note.getPitch(), 0); MidiEvent noteOffEvent = new MidiEvent(noteOff, note.getOffsetTick()); track.add(noteOnEvent); track.add(noteOffEvent); }
public void send(MidiMessage message, long timeStamp) { if (message instanceof ShortMessage) { ShortMessage sm = ((ShortMessage)message); switch(sm.getCommand()) { case ShortMessage.CONTROL_CHANGE: System.out.println(sm.getChannel()); System.out.println(sm.getData1()); System.out.println(sm.getData2()); MidiControlChangedListener listener = listenersDic.get(sm.getData1()); if(listener != null){ listener.changed(sm.getData1(), sm.getData2()); } break; } } System.out.println("midi received"); }
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); }
public long calcLastNoteTick() { long lastNoteTick = 0; for (Track t : sequence.getTracks()) { for (int j = t.size() - 1; j >= 0; j--) { MidiEvent evt = t.get(j); MidiMessage msg = evt.getMessage(); if (msg instanceof ShortMessage) { ShortMessage m = (ShortMessage) msg; if (m.getCommand() == ShortMessage.NOTE_OFF) { if (evt.getTick() > lastNoteTick) { lastNoteTick = evt.getTick(); } break; } } } } return lastNoteTick; }
/** * Checks whether a status byte represents a real-time message, which can occur even in the middle of another * multi-byte message. * * @param status the status byte which has just been received. * * @return true if the status byte represents a standalone real-time message which should be passed on without * interrupting any other message being gathered. */ private boolean isRealTimeMessage(byte status) { switch ( status ) { case (byte) ShortMessage.TIMING_CLOCK: case (byte) ShortMessage.START: case (byte) ShortMessage.CONTINUE: case (byte) ShortMessage.STOP: case (byte) ShortMessage.ACTIVE_SENSING: case (byte) ShortMessage.SYSTEM_RESET: return true; default: return false; } }
/** * Checks whether a status byte represents a running-status message, which means that multiple messages can be * sent without re-sending the status byte, for example to support multiple note-on messages in a row by simply * sending a stream of data byte pairs after the note-on status byte. * * @param status the status byte which is being processed. * * @return true if we should stay in this status after receiving our full complement of data bytes. */ private boolean isRunningStatusMessage (int status) { switch( status & 0xF0 ) { case ShortMessage.NOTE_OFF: case ShortMessage.NOTE_ON: case ShortMessage.POLY_PRESSURE: case ShortMessage.CONTROL_CHANGE: case ShortMessage.PROGRAM_CHANGE: case ShortMessage.CHANNEL_PRESSURE: case ShortMessage.PITCH_BEND: return true; default: return false; } }
@Override protected void messageReceived(Bead message) { if (Midi.hasMidiMessage(message)) { MidiMessage msg = Midi.getMidiMessage(message); if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; int note = smsg.getData1(); if (smsg.getCommand() == ShortMessage.NOTE_ON) { onNoteOn(smsg, note); } else if (smsg.getCommand() == ShortMessage.NOTE_OFF) { // Only requests to stop playing the current note will be honored if (note == this.note) { onNoteOff(smsg, note); } } } } }
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 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); } } }
/** * ��ijһ�������������MIDI�����¼� * * @param events ����ӵ�MIDI�¼����У�����һ������ * @param duration ����ʱ�� * @param pitch �����������MIDI�����ֵ * @param time �¼���ʱ��� */ public void setNoteEvent(List<MidiEvent> events, int duration, int pitch, int time){ ShortMessage message = new ShortMessage(); ShortMessage messageOff = new ShortMessage(); try{ message.setMessage(ShortMessage.NOTE_ON, 0, pitch, volume); messageOff.setMessage(ShortMessage.NOTE_OFF, 0, pitch, volume); } catch (InvalidMidiDataException e) { e.printStackTrace(); } events.add(new MidiEvent(message, time)); time += duration; events.add(new MidiEvent(messageOff, time)); }
@SuppressWarnings("deprecation") @Override public void commitAddImpl() { // Jens, renamed to be able to handle notification of CommitListeners in MultiEvent, see MultiEvent.commitXxx() try { ShortMessage shm = new ShortMessage(); shm.setMessage(ShortMessage.PITCH_BEND,channel,value & 0x3f,(value >> 7)); midiEvent = new MidiEvent(shm,startTick); getTrack().add(midiEvent); } catch(InvalidMidiDataException e) { e.printStackTrace(); } zombie=false; }
@SuppressWarnings("deprecation") @Override public void commitAddImpl() { // Jens, renamed to be able to handle notification of CommitListeners in MultiEvent, see MultiEvent.commitXxx() try { ShortMessage shm = new ShortMessage(); shm.setMessage(ShortMessage.CONTROL_CHANGE,channel,controlNumber,value); midiEvent = new MidiEvent(shm,startTick); getTrack().add(midiEvent); } catch(InvalidMidiDataException e) { e.printStackTrace(); } zombie=false; }
public final void interceptMessage(MidiMessage msg, Receiver receiver) { try { ShortMessage shm = (ShortMessage)msg; if(shm.getCommand() == ShortMessage.NOTE_ON) { if(shm.getData2()==0) { pendingNoteOffs.get(receiver).remove(shm.getChannel() << 8 | shm.getData1()); } else { if(!pendingNoteOffs.containsKey(receiver)) pendingNoteOffs.put(receiver,new HashSet<Integer>()); pendingNoteOffs.get(receiver).add(shm.getChannel() << 8 | shm.getData1()); } } } catch(Exception e) {} }
public void on() { MidiPart part = ev.getPart(); MidiLane lane = ((MidiLane) (part.getLane())); chan = lane.getMidiChannel(); if (chan < 0 ) return; recv = lane.getReceiver(); if (recv == null) return; pitch = ev.getNote(); ShortMessage shm = new ShortMessage(); try { shm.setMessage(ShortMessage.NOTE_ON, chan, lane.mapNote(pitch), ev.getVelocity()); } catch (InvalidMidiDataException e) { e.printStackTrace(); } recv.send(shm, -1); int millis = (int) ((60000.0*ev.getDuration()/project.getSequence().getResolution())/project.getSequencer().getTempoInBPM()); timer.setInitialDelay(millis); timer.restart(); }
/** * Called when a NOTE_ON message is received, and playing the * specified note should start. * Subclasses may override (but should call this method * as part of their implementation.) * * @param smsg the ShortMessage specifying the NOTE_ON message * @param note the MIDI note number */ protected void onNoteOn(ShortMessage smsg, int note) { // Keep track of current note this.note = note; // Glide to note frequency freq.setValue(Pitch.mtof(note)); // Notify Voices of note starting for (Voice v : voices) { v.noteOn(smsg, note); } // Notify note envelope of note starting noteEnv.noteOn(smsg, note); }
public void off() { setNoteState(OFF); // if (pianoRoll == null) // return; if (recv == null) return; ShortMessage shm = new ShortMessage(); try { shm.setMessage(ShortMessage.NOTE_ON, chan, kNum, 0); } catch (InvalidMidiDataException e) { // TODO Auto-generated catch block e.printStackTrace(); } // try { recv.send(shm, -1); // } catch (MidiUnavailableException e) { // TODO Auto-generated catch block // e.printStackTrace(); // } recv = null; }