javax.sound.midi.MidiDevice Java Examples

The following examples show how to use javax.sound.midi.MidiDevice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractMidiDeviceProvider.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #2
Source File: SequencerImplicitSynthOpen.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: MidiDeviceTools.java    From jsyn with Apache License 2.0 6 votes vote down vote up
/** Print the available MIDI Devices. */
public static void listDevices() {
    // Ask the MidiSystem what is available.
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    // Print info about each device.
    for (MidiDevice.Info info : infos) {
        System.out.println("MIDI Info: " + info.getDescription() + ", " + info.getName() + ", "
                + info.getVendor() + ", " + info.getVersion());
        // Get the device for more information.
        try {
            MidiDevice device = MidiSystem.getMidiDevice(info);
            System.out.println("   Device: " + ", #recv = " + device.getMaxReceivers()
                    + ", #xmit = " + device.getMaxTransmitters() + ", open = "
                    + device.isOpen() + ", " + device);
        } catch (MidiUnavailableException e) {
            e.printStackTrace();
        }
    }
}
 
Example #4
Source File: ClosedReceiver.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: SequencerImplicitSynthOpen.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: AbstractMidiDeviceProvider.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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.");
}
 
Example #7
Source File: DefaultDevices.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: MidiCommunication.java    From jmg with GNU General Public License v2.0 6 votes vote down vote up
private void fillFrame(Frame f, List dataList, MidiDevice.Info[] info) {
    try {
        f.setSize(340, 200);
        f.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 170, 
                      Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 100);
        String[] data = new String[info.length];
        data[0] = "" + info[0];
        data[1] = "" + info[1];
        for(int i=2; i< info.length; i++) {
            data[i] = MidiSystem.getMidiDevice(info[i]).toString();
        }
        for(int i=0; i< info.length; i++) {
            dataList.add(data[i]);
        }
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.add(dataList);
        f.add(scrollPane);
    } catch (Exception e) {
        System.out.println (e);
        System.exit (0);
    }
    
}
 
Example #9
Source File: InstrumentTester.java    From jsyn with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: MidiOutGetMicrosecondPositionBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #11
Source File: DefaultDevices.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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");
    }
}
 
Example #12
Source File: SequencerImplicitSynthOpen.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #13
Source File: SequencerImplicitSynthOpen.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: UseMidiKeyboard.java    From jsyn with Apache License 2.0 6 votes vote down vote up
public int test() throws MidiUnavailableException, IOException, InterruptedException {
    setupSynth();

    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;
}
 
Example #15
Source File: AbstractMidiDeviceProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #16
Source File: FinnwMusicModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int compare(MidiDevice.Info o1, MidiDevice.Info o2) {
    float score1 = score(o1), score2 = score(o2);
    if (score1 < score2) {
        return 1;
    } else if (score1 > score2) {
        return -1;
    } else {
        return 0;
    }
}
 
Example #17
Source File: RealTimeSequencerProvider.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #18
Source File: AbstractMidiDeviceProvider.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #19
Source File: AbstractMidiDevice.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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");
}
 
Example #20
Source File: MidiSequencerProviderImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public List<MidiSequencer> listSequencers() throws MidiPlayerException {
	try {
		List<MidiSequencer> sequencers = new ArrayList<MidiSequencer>();
		MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
		for(int i = 0; i < infos.length; i++){
			try {
				Iterator<MidiSequencer> it = sequencers.iterator();
				boolean exists = false;
				while(it.hasNext()){
					if( ((MidiSequencer)it.next()).getKey().equals(infos[i].getName()) ){
						exists = true;
						break;
					}
				}
				if(!exists){
					MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
					if(device instanceof Sequencer){
						sequencers.add(new MidiSequencerImpl((Sequencer)device));
					}
				}
			} catch (MidiUnavailableException e) {
				throw new MidiPlayerException(TuxGuitar.getProperty("jsa.error.midi.unavailable"),e);
			}
		}
		return sequencers;
	}catch (Throwable t) {
		throw new MidiPlayerException(TuxGuitar.getProperty("jsa.error.unknown"),t);
	}
}
 
Example #21
Source File: AbstractMidiDeviceProvider.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #22
Source File: MidiDeviceProvider.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: MidiReciever.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public MidiReciever()
    {
        MidiDevice device;
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        for (int i = 0; i < infos.length; i++) {
            try {
            device = MidiSystem.getMidiDevice(infos[i]);
            //does the device have any transmitters?
            //if it does, add it to the device list
            System.out.println(infos[i]);

            //get all transmitters
            List<Transmitter> transmitters = device.getTransmitters();
            //and for each transmitter

            for(int j = 0; j<transmitters.size();j++) {
                //create a new receiver
                transmitters.get(j).setReceiver(
                        //using my own MidiInputReceiver
                        new MidiInputReceiver(device.getDeviceInfo().toString())
                );
            }

            Transmitter trans = device.getTransmitter();
            trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));

            //open each device
            device.open();
            //if code gets this far without throwing an exception
            //print a success message
            System.out.println(device.getDeviceInfo()+" Was Opened");


        } catch (MidiUnavailableException e) {}
    }


}
 
Example #24
Source File: RealTimeSequencerProvider.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #25
Source File: AbstractMidiDeviceProvider.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: AbstractMidiDevice.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
    this.info = info;
    openRefCount = 0;
}
 
Example #27
Source File: RealTimeSequencerProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #28
Source File: SequencerImplicitSynthOpen.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example #29
Source File: RealTimeSequencerProvider.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #30
Source File: AbstractMidiDeviceProvider.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
final synchronized void readDeviceInfos() {
    Info[] infos = getInfoCache();
    MidiDevice[] devices = getDeviceCache();
    if (!enabled) {
        if (infos == null || infos.length != 0) {
            setInfoCache(new Info[0]);
        }
        if (devices == null || devices.length != 0) {
            setDeviceCache(new MidiDevice[0]);
        }
        return;
    }

    int oldNumDevices = (infos==null)?-1:infos.length;
    int newNumDevices = getNumDevices();
    if (oldNumDevices != newNumDevices) {
        if (Printer.trace) Printer.trace(getClass().toString()
                                         +": readDeviceInfos: old numDevices: "+oldNumDevices
                                         +"  newNumDevices: "+ newNumDevices);

        // initialize the arrays
        Info[] newInfos = new Info[newNumDevices];
        MidiDevice[] newDevices = new MidiDevice[newNumDevices];

        for (int i = 0; i < newNumDevices; i++) {
            Info newInfo = createInfo(i);

            // in case that we are re-reading devices, try to find
            // the previous one and reuse it
            if (infos != null) {
                for (int ii = 0; ii < infos.length; ii++) {
                    Info info = infos[ii];
                    if (info != null && info.equalStrings(newInfo)) {
                        // new info matches the still existing info. Use old one
                        newInfos[i] = info;
                        info.setIndex(i);
                        infos[ii] = null; // prevent re-use
                        newDevices[i] = devices[ii];
                        devices[ii] = null;
                        break;
                    }
                }
            }
            if (newInfos[i] == null) {
                newInfos[i] = newInfo;
            }
        }
        // the remaining MidiDevice.Info instances in the infos array
        // have become obsolete.
        if (infos != null) {
            for (int i = 0; i < infos.length; i++) {
                if (infos[i] != null) {
                    // disable this device info
                    infos[i].setIndex(-1);
                }
                // what to do with the MidiDevice instances that are left
                // in the devices array ?? Close them ?
            }
        }
        // commit new list of infos.
        setInfoCache(newInfos);
        setDeviceCache(newDevices);
    }
}