/** * Capture midi input events, dispatching them to given Receiver. * The MidiDevice returned is the device providing the input, and * should be closed when input events are no longer needed. * Note that this method returns the first MidiDevice which * has at least one transmitter. * * @param receiver the Receiver to which midi input events should be dispatched * @return the MidiDevice providing the input events * @throws MidiUnavailableException if midi input can't be found */ public static MidiDevice getMidiInput(Receiver receiver) throws MidiUnavailableException { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (MidiDevice.Info info : infos) { MidiDevice device; device = MidiSystem.getMidiDevice(info); if (DEBUG) { System.out.println("Found: " + device); } int maxTransmitters = device.getMaxTransmitters(); if (DEBUG) { System.out.println(" Max transmitters: " + maxTransmitters); } if (maxTransmitters == -1 || maxTransmitters > 0) { Transmitter transmitter = device.getTransmitter(); transmitter.setReceiver(receiver); device.open(); return device; } } throw new MidiUnavailableException("Could not find any midi input sources"); }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
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 } }
static MidiDevice getConnectedDevice(Sequencer sequencer) { List<Transmitter> trans = sequencer.getTransmitters(); log(" sequencer has " + trans.size() + " opened transmitters:"); for (Transmitter tr: trans) { Receiver r = tr.getReceiver(); log(" " + getClassStr(tr) + " connected to " + getClassStr(r)); if (r instanceof MidiDeviceReceiver) { MidiDeviceReceiver recv = (MidiDeviceReceiver)r; MidiDevice dev = recv.getMidiDevice(); log(" - receiver of " + getClassStr(dev)); return dev; } else { log(" - does NOT implement MidiDeviceReceiver"); } } return null; }
private void receiverChanged(BasicTransmitter t, Receiver oldR, Receiver newR) { synchronized(transmitters) { // some optimization if (midiOutReceiver == oldR) { midiOutReceiver = null; } if (newR != null) { if ((newR instanceof MidiOutDevice.MidiOutReceiver) && (midiOutReceiver == null)) { midiOutReceiver = ((MidiOutDevice.MidiOutReceiver) newR); } } optimizedReceiverCount = ((midiOutReceiver!=null)?1:0); } // more potential for optimization here }
void sendMessage(byte[] data, long timeStamp) { try { synchronized(transmitters) { int size = transmitters.size(); if (TRACE_TRANSMITTER) Printer.println("Sending long 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: SysexMessages are mutable, so // an application could change the contents of this object, // or try to use the object later. So we can't get around object creation // But the array need not be unique for each FastSysexMessage object, // because it cannot be modified. receiver.send(new FastSysexMessage(data), timeStamp); } } } } catch (InvalidMidiDataException e) { // this happens when invalid data comes over the wire. Ignore it. return; } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class<?> typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
/** * 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; }
@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 setReceiver(Receiver receiver) { EventGroup currentGroup = null; lock.lock(); try { if (this.receiver != null) { if (this.future.cancel(false)) { currentGroup = triggerTask.eventGroup; } } else { nextGroupTime = System.nanoTime(); } this.receiver = receiver; scheduleIfRequired(receiver, currentGroup); } finally { lock.unlock(); } }
public int test() throws MidiUnavailableException, IOException, InterruptedException { int result = -1; MidiDevice keyboard = MidiDeviceTools.findKeyboard(); Receiver receiver = new CustomReceiver(); // Just use default synthesizer. if (keyboard != null) { // If you forget to open them you will hear no sound. keyboard.open(); // Put the receiver in the transmitter. // This gives fairly low latency playing. keyboard.getTransmitter().setReceiver(receiver); System.out.println("Play MIDI keyboard: " + keyboard.getDeviceInfo().getDescription()); result = 0; Thread.sleep(4000); System.out.println("Close the keyboard. It may not work after this according to the docs!"); keyboard.close(); } else { System.out.println("Could not find a keyboard."); } return result; }
/** * * @return The reciever associated with this lane */ public Receiver getReceiver() { MidiDevice dev = ftw.getMidiDevice(); if (dev == null) { return null; } try { // Jens: // return getTrack().getMidiDevice().getReceiver(); Receiver r = ftw.getMidiDevice().getReceiver(); if (r != null) { return new MonitorReceiver(midiMessageListeners, r); } else { return null; } } catch (MidiUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
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) {} }
/** * Send note-off to all pending notes * */ public final void releasePendingNoteOffs() { for(Receiver recv : pendingNoteOffs.keySet()) { for (int note : pendingNoteOffs.get(recv)) { ShortMessage shm = new ShortMessage(); try { shm.setMessage(ShortMessage.NOTE_ON, (note >> 8) & 0xf, note & 0xff, 0); recv.send(shm, -1); } catch (InvalidMidiDataException e) { e.printStackTrace(); } } } pendingNoteOffs.clear(); }
void init() { MidiDevice dev=dm.getDefaultMidiDevice(); Receiver recv=null; if (dev != null ) try { recv= dev.getReceiver(); } catch (MidiUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } configOut.setReceiver(recv); }
public int setupMidiKeyboard() throws MidiUnavailableException, IOException, InterruptedException { messageParser = new MyParser(); int result = 2; MidiDevice keyboard = MidiDeviceTools.findKeyboard(); Receiver receiver = new CustomReceiver(); // Just use default synthesizer. if (keyboard != null) { // If you forget to open them you will hear no sound. keyboard.open(); // Put the receiver in the transmitter. // This gives fairly low latency playing. keyboard.getTransmitter().setReceiver(receiver); System.out.println("Play MIDI keyboard: " + keyboard.getDeviceInfo().getDescription()); result = 0; } else { System.out.println("Could not find a keyboard."); } return result; }
@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(); } } } } }
/** * Closes the MIDI receiver. * * @param receiver the MIDI receiver. */ public static void close(Receiver receiver) { try { if (receiver != null) { receiver.close(); } } catch (Exception e) { logger.warn("close", e); } }
void removeReceiver(Receiver recv) { boolean perform_close = false; synchronized (control_mutex) { if (recvslist.remove(recv)) { if (implicitOpen && recvslist.isEmpty()) perform_close = true; } } if (perform_close) close(); }
public Receiver getReceiver() throws MidiUnavailableException { synchronized (control_mutex) { SoftReceiver receiver = new SoftReceiver(this); receiver.open = open; recvslist.add(receiver); return receiver; } }
public List<Receiver> getReceivers() { synchronized (control_mutex) { ArrayList<Receiver> recvs = new ArrayList<Receiver>(); recvs.addAll(recvslist); return recvs; } }
public Receiver getReceiverReferenceCounting() throws MidiUnavailableException { if (!isOpen()) { open(); synchronized (control_mutex) { implicitOpen = true; } } return getReceiver(); }