private static void doAll() throws Exception { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i=0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if ((! (device instanceof Sequencer)) && (! (device instanceof Synthesizer)) && (device.getMaxReceivers() > 0 || device.getMaxReceivers() == -1)) { System.out.println("--------------"); System.out.println("Testing MIDI device: " + infos[i]); testDevice(device); } if (infos.length==0) { System.out.println("No MIDI devices available!"); } } }
public final MidiDevice getDevice(MidiDevice.Info info) { if (info instanceof Info) { readDeviceInfos(); MidiDevice[] devices = getDeviceCache(); Info[] infos = getInfoCache(); Info thisInfo = (Info) info; int index = thisInfo.getIndex(); if (index >= 0 && index < devices.length && infos[index] == info) { if (devices[index] == null) { devices[index] = createDevice(thisInfo); } if (devices[index] != null) { return devices[index]; } } } throw new IllegalArgumentException("MidiDevice " + info.toString() + " not supported by this provider."); }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ public static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } if (!result) { System.err.println("Soundcard does not exist or sound drivers not installed!"); System.err.println("This test requires sound drivers for execution."); } return result; }
public static void main(final String[] args) { final MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) { for (final MidiDevice.Info info : infos) { if (mdp.isDeviceSupported(info)) { if (mdp.getDevice(info) == null) { throw new RuntimeException("MidiDevice is null"); } } else { try { mdp.getDevice(info); throw new RuntimeException( "IllegalArgumentException expected"); } catch (final IllegalArgumentException ignored) { // expected } } } } }
static boolean test(Sequencer sequencer) throws MidiUnavailableException { log(""); log("opening sequencer..."); sequencer.open(); // opens connected synthesizer implicitly MidiDevice synth = getConnectedDevice(sequencer); log(" connected device: " + getDeviceStr(synth)); log("closing sequencer..."); sequencer.close(); // closes the synth implicitly log(" synth is " + getDeviceStr(synth)); MidiDevice synth2 = getConnectedDevice(sequencer); log(" currently connected device: " + getDeviceStr(synth2)); if (synth != null && synth.isOpen()) { log("FAIL."); return false; } log("OK."); return true; }
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; }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ private static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = !(device instanceof Sequencer) && !(device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } return result; }
@Override public final MidiDevice getDevice(final MidiDevice.Info info) { Objects.requireNonNull(info); if (info instanceof Info) { readDeviceInfos(); MidiDevice[] devices = getDeviceCache(); Info[] infos = getInfoCache(); Info thisInfo = (Info) info; int index = thisInfo.getIndex(); if (index >= 0 && index < devices.length && infos[index] == info) { if (devices[index] == null) { devices[index] = createDevice(thisInfo); } if (devices[index] != null) { return devices[index]; } } } throw MidiUtils.unsupportedDevice(info); }
private static void doAllTests() { boolean problemOccured = false; boolean succeeded = true; MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = null; try { device = MidiSystem.getMidiDevice(infos[i]); succeeded &= doTest(device); } catch (MidiUnavailableException e) { out("exception occured; cannot test"); problemOccured = true; } } if (infos.length == 0) { out("Soundcard does not exist or sound drivers not installed!"); out("This test requires sound drivers for execution."); } isTestExecuted = !problemOccured; isTestPassed = succeeded; }
public static void main(String[] args) throws Exception { boolean allOk = true; MidiDevice.Info[] infos; out("\nTesting MidiDevices retrieved via MidiSystem"); infos = MidiSystem.getMidiDeviceInfo(); allOk &= testDevices(infos, null); out("\nTesting MidiDevices retrieved from MidiDeviceProviders"); List providers = JDK13Services.getProviders(MidiDeviceProvider.class); for (int i = 0; i < providers.size(); i++) { MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i); infos = provider.getDeviceInfo(); allOk &= testDevices(infos, provider.getClass().getName()); } if (!allOk) { throw new Exception("Test failed"); } else { out("Test passed"); } }
private static boolean testDevice(MidiDevice device, Class type, String providerClassName, boolean testWrong, boolean expectedResult) { boolean allOk = true; String instanceName = device.getDeviceInfo().getName(); // no error allOk &= testDevice(device, type, providerClassName, instanceName, expectedResult); if (testWrong) { // erroneous provider class name, correct instance name allOk &= testDevice(device, type, ERROR_PROVIDER_CLASS_NAME, instanceName, expectedResult); // correct provider class name, erroneous instance name // we presume that provider provides only one class of requested type allOk &= testDevice(device, type, providerClassName, ERROR_INSTANCE_NAME, expectedResult); } return allOk; }
@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(); } } } } }
/** * Sets and updates the input MIDI device in the plugin. If there has already been connected an another input device then replaces it. * * @param deviceName the input MIDI device name. */ public void connectInputDevice(String deviceName) { MidiDevice device = getDeviceByName(deviceName); if (device == null) { throw new SpongeException("MIDI device named '" + deviceName + "' not found"); } setInputDevice(device); updateInputDevice(); }
private void checkOpen(MidiDevice device, boolean desiredState) { if (device.isOpen() != desiredState) { out("device should be " + getStateString(desiredState) + ", but isn't!"); failed = true; } }
/** * Sets the input device. If there has already been an another input device set then it will be closed. * * @param inputDevice the input device. */ public void setInputDevice(MidiDevice inputDevice) { if (inputDevice != this.inputDevice) { MidiUtils.close(this.inputDevice); } this.inputDevice = inputDevice; }
/** * Returns all available MIDI devices. * * @return all available MIDI devices. */ public static List<MidiDevice> getDevices() { return Arrays.stream(MidiSystem.getMidiDeviceInfo()).map(info -> { try { return MidiSystem.getMidiDevice(info); } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } }).collect(Collectors.toList()); }
/** * Returns the default input MIDI device. * * @return the default input MIDI device or {@code null} if not found. */ public static MidiDevice getDefaultInputDevice() { try { Transmitter transmitter = MidiSystem.getTransmitter(); if (transmitter != null && transmitter instanceof MidiDeviceTransmitter) { return ((MidiDeviceTransmitter) transmitter).getMidiDevice(); } return null; } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } }
public static void main(String[] args) throws Exception { SoftProvider provider = new SoftProvider(); Info[] infos = provider.getDeviceInfo(); assertTrue(infos.length > 0); for (int i = 0; i < infos.length; i++) { assertTrue(infos[i] != null); MidiDevice d = provider.getDevice(infos[i]); assertTrue(d instanceof SoftSynthesizer); } }
public static void main(String[] args) { try { log("getting sequencer..."); Sequencer sequencer = MidiSystem.getSequencer(); log(" - got " + getDeviceStr(sequencer)); // obtain connected device (usually synthesizer) MidiDevice synth = getConnectedDevice(sequencer); if (synth == null) { log("could not get connected device, returning"); return; } log("connected device: " + getDeviceStr(synth)); int success = 0; for (int i=0; i<TEST_COUNT; i++) { if (test(sequencer)) { success++; } } if (success != TEST_COUNT) { throw new RuntimeException("test FAILS"); } } catch (MidiUnavailableException ex) { // this is not a failure log("Could not get Sequencer"); } log("test PASSED."); }
/** * Opens the MIDI device. * * @param device the MIDI device. */ public static void open(MidiDevice device) { if (device != null && !device.isOpen()) { try { device.open(); } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } } }
private static void useDevice(MidiDevice device) throws MidiUnavailableException { Integer i = USAGE.get(device); if (i == null) { device.open(); long time = System.nanoTime(); TIME.put(device, time); USAGE.put(device, 1); } else USAGE.put(device, i + 1); }
public MidiDevice getDevice(MidiDevice.Info info) { if ((info != null) && (!info.equals(RealTimeSequencer.info))) { return null; } try { return new RealTimeSequencer(); } catch (MidiUnavailableException e) { return null; } }
private static boolean doTest(int numIterations, boolean input) throws Exception { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); MidiDevice outDevice = null; MidiDevice inDevice = null; for (int i = 0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if (! (device instanceof Sequencer) && ! (device instanceof Synthesizer)) { if (device.getMaxReceivers() != 0) { outDevice = device; } if (device.getMaxTransmitters() != 0) { inDevice = device; } } } MidiDevice testDevice = null; if (input) { testDevice = inDevice; } else { testDevice = outDevice; } if (testDevice == null) { out("Cannot test: device not available."); return true; } out("Using Device: " + testDevice); for (int i = 0; i < numIterations; i++) { out("@@@ ITERATION: " + i); testDevice.open(); // This sleep ensures that the thread of MidiInDevice is started. sleep(50); testDevice.close(); } return true; }
/** * Execute the test on all available Sequencers. * * @return true if the test passed for all Sequencers, false otherwise */ private static boolean testAll() throws Exception { boolean result = true; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); if (device instanceof Sequencer) { result &= testSequencer((Sequencer) device); } } return result; }
private static void doAllTests() { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = null; try { device = MidiSystem.getMidiDevice(infos[i]); doTest(device); } catch (MidiUnavailableException e) { out("Exception occured when retrieving device "+infos[i]+": "+e); } } if (infos.length == 0) { out("No MIDI devices exist or sound drivers not installed!"); } }
public final MidiDevice.Info[] getDeviceInfo() { readDeviceInfos(); Info[] infos = getInfoCache(); MidiDevice.Info[] localArray = new MidiDevice.Info[infos.length]; System.arraycopy(infos, 0, localArray, 0, infos.length); return localArray; }
/** * Indicates whether the device provider supports the device represented by * the specified device info object. * * @param info an info object that describes the device for which support * is queried * @return {@code true} if the specified device is supported, otherwise * {@code false} */ public boolean isDeviceSupported(MidiDevice.Info info) { MidiDevice.Info infos[] = getDeviceInfo(); for(int i=0; i<infos.length; i++) { if( info.equals( infos[i] ) ) { return true; } } return false; }
@Override public MidiDevice getDevice(final MidiDevice.Info info) { Objects.requireNonNull(info); if (RealTimeSequencer.info.equals(info)) { return new RealTimeSequencer(); } throw MidiUtils.unsupportedDevice(info); }
@Override public final MidiDevice.Info[] getDeviceInfo() { readDeviceInfos(); Info[] infos = getInfoCache(); MidiDevice.Info[] localArray = new MidiDevice.Info[infos.length]; System.arraycopy(infos, 0, localArray, 0, infos.length); return localArray; }
@Override MidiDevice createDevice(AbstractMidiDeviceProvider.Info info) { if (enabled && (info instanceof MidiInDeviceInfo)) { return new MidiInDevice(info); } return null; }
/** * Constructs an AbstractMidiDevice with the specified info object. * @param info the description of the device */ /* * The initial mode and only supported mode default to OMNI_ON_POLY. */ protected AbstractMidiDevice(MidiDevice.Info info) { if(Printer.trace) Printer.trace(">> AbstractMidiDevice CONSTRUCTOR"); this.info = info; openRefCount = 0; if(Printer.trace) Printer.trace("<< AbstractMidiDevice CONSTRUCTOR completed"); }
@Override MidiDevice createDevice(AbstractMidiDeviceProvider.Info info) { if (enabled && (info instanceof MidiOutDeviceInfo)) { return new MidiOutDevice(info); } return null; }
@Override public MidiDevice getDevice(final MidiDevice.Info info) { Objects.requireNonNull(info); if (SoftSynthesizer.info.equals(info)) { return new SoftSynthesizer(); } throw MidiUtils.unsupportedDevice(info); }
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."); } }
private static void testDevice(MidiDevice device) throws Exception { boolean timestampsAvailable = false; boolean timestampPrecisionOk = false; try { // expected behaviour if not opened? device.open(); /* First, we're testing if timestamps are provided at all. Returning -1 (unsupported), while allowed by the API specification, is not sufficient to pass this test. */ long timestamp = device.getMicrosecondPosition(); timestampsAvailable = (timestamp != -1); /* Then, we're testing the precision. Note that the system time is measured in milliseconds, while the device time is measured in microseconds. */ long systemTime1 = System.currentTimeMillis(); long deviceTime1 = device.getMicrosecondPosition(); // rest for 5 seconds Thread.sleep(5000); long systemTime2 = System.currentTimeMillis(); long deviceTime2 = device.getMicrosecondPosition(); // now both period measurements are calculated in milliseconds. long systemDuration = systemTime2 - systemTime1; long deviceDuration = (deviceTime2 - deviceTime1) / 1000; long delta = Math.abs(systemDuration - deviceDuration); // a deviation of 0.5 seconds (= 500 ms) is allowed. timestampPrecisionOk = (delta <= 500); } catch (Throwable t) { System.out.println(" - Caught exception. Not failed."); System.out.println(" - " + t.toString()); return; } finally { device.close(); } if (! timestampsAvailable) { throw new Exception("timestamps are not supported"); } if (! timestampPrecisionOk) { throw new Exception("device timer not precise enough"); } successfulTests++; }
private InputOption(MidiDevice.Info info, MidiDevice device) { this.info = info; this.device = device; }
public ReceiverTestHelper(MidiDevice device) { super(device); }