Java Code Examples for javax.sound.midi.MidiSystem#getMidiDevice()

The following examples show how to use javax.sound.midi.MidiSystem#getMidiDevice() . 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: PassportMidiInterface.java    From jace with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void resume() {
    if (isRunning() && midiOut != null) {
        return;
    }
    try {
        MidiDevice selectedDevice = MidiSystem.getSynthesizer();
        MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
        if (devices.length == 0) {
            System.out.println("No MIDI devices found");
        } else {
            for (MidiDevice.Info dev : devices) {
                if (MidiSystem.getMidiDevice(dev).getMaxReceivers() == 0) {
                    continue;
                }
                System.out.println("MIDI Device found: " + dev);
                if ((preferredMidiDevice.getValue() == null && dev.getName().contains("Java Sound") && dev instanceof Synthesizer) ||
                        preferredMidiDevice.getValue().equalsIgnoreCase(dev.getName())
                    ) {
                    selectedDevice = MidiSystem.getMidiDevice(dev);
                    break;
                }
            }
        }
        if (selectedDevice != null) {
            System.out.println("Selected MIDI device: " + selectedDevice.getDeviceInfo().getName());
            selectedDevice.open();
            midiOut = selectedDevice.getReceiver();
            super.resume();
        }
    } catch (MidiUnavailableException ex) {
        System.out.println("Could not open MIDI synthesizer");
        Logger.getLogger(PassportMidiInterface.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 2
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 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: 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 5
Source File: MidiOutGetMicrosecondPositionBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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!");
        }
    }
}
 
Example 6
Source File: ReceiverTransmitterAvailable.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
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 8
Source File: MidiDeviceGetReceivers.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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!");
    }
}
 
Example 9
Source File: Reopen.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: MidiDeviceTools.java    From jsyn with Apache License 2.0 5 votes vote down vote up
/** Find a MIDI transmitter that contains text in the name. */
public static MidiDevice findKeyboard(String text) {
    MidiDevice keyboard = null;
    // Ask the MidiSystem what is available.
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    // Print info about each device.
    for (MidiDevice.Info info : infos) {
        try {
            MidiDevice device = MidiSystem.getMidiDevice(info);
            // Hardware devices are not Synthesizers or Sequencers.
            if (!(device instanceof Synthesizer) && !(device instanceof Sequencer)) {
                // Is this a transmitter?
                // Might be -1 if unlimited.
                if (device.getMaxTransmitters() != 0) {
                    if ((text == null)
                            || (info.getDescription().toLowerCase()
                                    .contains(text.toLowerCase()))) {
                        keyboard = device;
                        System.out.println("Chose: " + info.getDescription());
                        break;
                    }
                }
            }
        } catch (MidiUnavailableException e) {
            e.printStackTrace();
        }
    }
    return keyboard;
}
 
Example 11
Source File: SequencerState.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: Looping.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: MidiHandler.java    From haxademic with MIT License 5 votes vote down vote up
protected void initDevices(){
	MidiDevice device;
	MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
	
	allTransmitters = new ArrayList<Transmitter>();
	
	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 create a new receiver using our own MidiInputReceiver
			for(int j = 0; j < transmitters.size(); j++ ) {
				Receiver receiver = new MidiInputReceiver(device.getDeviceInfo().toString());
				transmitters.get(j).setReceiver(receiver);
			}
			
			// open each device
			device.open();
			
			// store transmitter for each device
			allTransmitters.add(device.getTransmitter());
			
			// if code gets this far without throwing an exception print a success message
			System.out.println(device.getDeviceInfo()+" was opened");

		} catch (MidiUnavailableException e) {}
	}
}
 
Example 14
Source File: MidiPortProviderImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public List<MidiOutputPort> listPorts() throws MidiPlayerException{
	try {
		List<MidiOutputPort> ports = new ArrayList<MidiOutputPort>();
		MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
		for(int i = 0; i < infos.length; i++){
			try {
				Iterator<MidiOutputPort> it = ports.iterator();
				boolean exists = false;
				while(it.hasNext()){
					if( ((MidiOutputPort)it.next()).getKey().equals(infos[i].getName()) ){
						exists = true;
						break;
					}
				}
				if(!exists){
					MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
					if(device.getMaxReceivers() == 0 || device instanceof Sequencer){
						continue;
					}
					if(device instanceof Synthesizer){
						ports.add(new MidiPortSynthesizer(this.context, (Synthesizer)device));
					}
					else{
						ports.add(new MidiPortOut(device));
					}
				}
			} catch (MidiUnavailableException e) {
				throw new MidiPlayerException(TuxGuitar.getProperty("jsa.error.midi.unavailable"),e);
			}
		}
		return ports;
	}catch (Throwable t) {
		throw new MidiPlayerException(TuxGuitar.getProperty("jsa.error.unknown"),t);
	}
}
 
Example 15
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 16
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 17
Source File: MidiPortProviderImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public List<MidiOutputPort> listPorts() throws MidiPlayerException{
	try {
		List<MidiOutputPort> ports = new ArrayList<MidiOutputPort>();
		MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
		for(int i = 0; i < infos.length; i++){
			try {
				Iterator<MidiOutputPort> it = ports.iterator();
				boolean exists = false;
				while(it.hasNext()){
					if( ((MidiOutputPort)it.next()).getKey().equals(infos[i].getName()) ){
						exists = true;
						break;
					}
				}
				if(!exists){
					MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
					if(device.getMaxReceivers() == 0 || device instanceof Sequencer){
						continue;
					}
					if(device instanceof Synthesizer){
						ports.add(new MidiPortSynthesizer((Synthesizer)device));
					}
					else{
						ports.add(new MidiPortOut(device));
					}
				}
			} catch (MidiUnavailableException e) {
				throw new MidiPlayerException("jsa.error.midi.unavailable", e);
			}
		}
		return ports;
	}catch (Throwable t) {
		throw new MidiPlayerException("jsa.error.unknown", t);
	}
}
 
Example 18
Source File: MidiCommunication.java    From jmg with GNU General Public License v2.0 5 votes vote down vote up
private void setMidiInputSelection(int inputDeviceID, MidiDevice.Info[] info) {
    try {
        // inut setup
        MidiDevice inputPort = MidiSystem.getMidiDevice (info [inputDeviceID]);
        //System.out.println (inputPort);
        inputPort.open ();
        Transmitter t = inputPort.getTransmitter ();
        t.setReceiver(this);
    } catch (Exception e) {
        // Oops! Should never get here
        System.out.println ("Exception in PlumStone main ()");
        System.out.println (e);
        System.exit (0);
    }
}
 
Example 19
Source File: MiPortProvider.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static List<String> listPortsNames() throws MiException
{
try {
	List<String>		portsNames	= new ArrayList<String>();
	MidiDevice.Info[]	infos		= MidiSystem.getMidiDeviceInfo();
	
	for(int i = 0; i < infos.length; i++)
		{
		try {
			Iterator<String>	it		= portsNames.iterator();
			boolean		exists	= false;
			
			while(it.hasNext())
				{
				if(((String)it.next()).equals(infos[i].getName()))
					{
					exists = true;
					break;
					}
				}

			if(!exists)
				{
				MidiDevice	device = MidiSystem.getMidiDevice(infos[i]);

				if(	device.getMaxTransmitters() == 0 ||
					device instanceof Sequencer)
					continue;

				portsNames.add(infos[i].getName());
				}

			}
		catch (MidiUnavailableException mue)
			{
			throw new MiException(TuxGuitar.getProperty("midiinput.error.midi.unavailable"), mue);
			}
		}
	
	return portsNames;
	}
catch(Throwable t)
	{
	throw new MiException(TuxGuitar.getProperty("midiinput.error.unknown"), t);
	}
}
 
Example 20
Source File: VolumeScalingReceiver.java    From mochadoom with GNU General Public License v3.0 4 votes vote down vote up
/** Guess how suitable a MidiDevice is for music output. */
private float score(MidiDevice.Info info) {
    String lcName = info.getName().toLowerCase(Locale.ENGLISH);
    float result = 0f;
    try {
        MidiDevice dev = MidiSystem.getMidiDevice(info);
        dev.open();
        try {
            if (dev instanceof Sequencer) {
                // The sequencer cannot be the same device as the synthesizer - that would create an infinite loop.
                return Float.NEGATIVE_INFINITY;
            } else if (lcName.contains("mapper")) {
                // "Midi Mapper" is ideal, because the user can select the default output device in the control panel
                result += 100;
            } else {
                if (dev instanceof Synthesizer) {
                    // A synthesizer is usually better than a sequencer or USB MIDI port
                    result += 50;
                    if (lcName.contains("java")) {
                        // "Java Sound Synthesizer" often has a low sample rate or no default soundbank;  Prefer another software synth
                        if (((Synthesizer) dev).getDefaultSoundbank() != null) {
                            result -= 10;
                        } else {
                            // Probably won't be audible
                            result -= 500;
                        }
                    }
                    if (lcName.contains("microsoft")) {
                        // "Microsoft GS Wavetable Synth" is notoriously unpopular, but sometimes it's the only one
                        // with a decent sample rate.
                        result -= 7;
                    }
                }
            }
            return result;
        } finally {
            dev.close();
        }
    } catch (MidiUnavailableException ex) {
        // Cannot use this one
        return Float.NEGATIVE_INFINITY;
    }
}