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

The following examples show how to use javax.sound.midi.MidiSystem#getMidiDeviceInfo() . 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: Midi2WavRenderer.java    From computoser with GNU Affero General Public License v3.0 7 votes vote down vote up
public static AudioSynthesizer findAudioSynthesizer() throws MidiUnavailableException, IOException, InvalidMidiDataException {
    // First check if default synthesizer is AudioSynthesizer.
    Synthesizer synth = MidiSystem.getSynthesizer();
    if (synth instanceof AudioSynthesizer) {
        return (AudioSynthesizer) synth;
    }

    // If default synhtesizer is not AudioSynthesizer, check others.
    Info[] infos = MidiSystem.getMidiDeviceInfo();
    for (int i = 0; i < infos.length; i++) {
        MidiDevice dev = MidiSystem.getMidiDevice(infos[i]);
        if (dev instanceof AudioSynthesizer) {
            return (AudioSynthesizer) dev;
        }
    }

    // No AudioSynthesizer was found, return null.
    return null;
}
 
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: RTMidiIn.java    From jmg with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Initialise the input source
 */
  	private boolean init() {
  		if (trans == null) {
     		try {
        		if (MidiSystem.getReceiver() == null) {
           			System.err.println("MidiSystem Receiver Unavailable");
             			 return false;
         		}
		MidiDevice.Info[] mdi=MidiSystem.getMidiDeviceInfo();
		for(int i=0;i<mdi.length;i++){
			System.out.println(mdi[i]);
		}
		trans = MidiSystem.getTransmitter();
		trans.setReceiver(this);
       	}catch (MidiUnavailableException e) {
        		System.err.println("Midi System Unavailable:" + e);
          		return false;
        	}
  	}
    		return true;
}
 
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: 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 6
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 7
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 8
Source File: UnsupportedInfo.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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
                }
            }
        }
    }
}
 
Example 9
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 10
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 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: 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 13
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 14
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 15
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 16
Source File: JmMidiPlayer.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
public JmMidiPlayer()  throws MidiUnavailableException {

        //System.out.println("Getting Sequencer");        
        sequencer   = MidiSystem.getSequencer();
        //System.out.println("Getting Synthesizer");        
        synthesizer = JmMidiPlayer.getSynthesizer();
        //System.out.println("Getting Transmitter");        
        transmitter = sequencer.getTransmitter();
        //System.out.println("Getting Receiver");        
        receiver    = synthesizer.getReceiver(); 
        //System.out.println("Connecting Receiver");        
        transmitter.setReceiver(receiver);      
        sequencer.open();
        if (sequencer.isOpen()) {
            //System.out.println("Sequencer is Open" );
        }
        else {
            //System.out.println("Sequencer is Not Open" );
        }                                    
        os = new ByteArrayOutputStream();
        //System.out.println("End of Midi Construction");        
        
        MidiDevice.Info synthInfo = synthesizer.getDeviceInfo();
        //System.out.println(
         //       "Synthesizer =" +  synthInfo.toString() 
        //);    
        
        MidiDevice.Info seqInfo   = sequencer.getDeviceInfo();
        //System.out.println( 
               // "Sequencer =" + seqInfo.toString() 
       // );    
        
        MidiDevice.Info[]   devsInfo 
            = MidiSystem.getMidiDeviceInfo();
        MidiDevice.Info     devInfo;
        MidiDevice          dev;
        for( int i=0; i <  devsInfo.length; ++i) {
            devInfo = devsInfo[i];            
            System.out.print(devInfo.toString());  
            try {
                dev = MidiSystem.getMidiDevice(devInfo);
                //System.out.println( " Available");  
            }
            catch(MidiUnavailableException e) {
                System.out.println( " Unavailable");  
            }                                    
        }            
        if (sequencer.isOpen()) {
            //System.out.println("Sequencer is Still Open" );
        }
        else {
           // System.out.println("Sequencer is Not Open" );
        }                                    
    }
 
Example 17
Source File: MidiIO.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    out("4356787: MIDI device I/O is not working (windows)");

    if (System.getProperty("os.name").startsWith("Windows")) {
        boolean forInput=true;
        boolean forOutput=true;
        int outOnlyCount=0;
        int inOnlyCount=0;
        out("  available MIDI devices:");
        MidiDevice.Info[] aInfos = MidiSystem.getMidiDeviceInfo();
        for (int i = 0; i < aInfos.length; i++) {
            try {
                MidiDevice      device = MidiSystem.getMidiDevice(aInfos[i]);
                boolean         bAllowsInput = (device.getMaxTransmitters() != 0);
                boolean         bAllowsOutput = (device.getMaxReceivers() != 0);
                if (bAllowsInput && !bAllowsOutput) {
                    inOnlyCount++;
                }
                if (!bAllowsInput && bAllowsOutput) {
                    outOnlyCount++;
                }
                if ((bAllowsInput && forInput) || (bAllowsOutput && forOutput)) {
                    out(""+i+"  "
                            +(bAllowsInput?"IN ":"   ")
                            +(bAllowsOutput?"OUT ":"    ")
                            +aInfos[i].getName()+", "
                            +aInfos[i].getVendor()+", "
                            +aInfos[i].getVersion()+", "
                            +aInfos[i].getDescription());
                }
            }
            catch (MidiUnavailableException e) {
                // device is obviously not available...
            }
        }
        if (aInfos.length == 0) {
            out("No devices available. Test should be run on systems with MIDI drivers installed.");
        } else {
            if (outOnlyCount>1) {
                if (inOnlyCount==0) {
                    //throw new Exception("No input devices! test fails.");
                    out("System provides out devices, but no input devices. This means either");
                    out("a bug in Java Sound, or the drivers are not set up correctly.");
                }
                out("Test passed.");
            } else {
                out("no MIDI I/O installed. Test should be run on systems with MIDI drivers installed.");
            }
        }
    } else {
        out("  -- not on Windows. Test doesn't apply.");
    }
}
 
Example 18
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 19
Source File: PortsItemEventHandler.java    From EWItool with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void handle( ActionEvent arg0 ) {
  
  Dialog<ButtonType> dialog = new Dialog<>();
  dialog.setTitle( "EWItool - Select MIDI Ports" );
  dialog.getDialogPane().getButtonTypes().addAll( ButtonType.CANCEL, ButtonType.OK );
  GridPane gp = new GridPane();
  
  gp.add( new Label( "MIDI In Ports" ), 0, 0 );
  gp.add( new Label( "MIDI Out Ports" ), 1, 0 );
  
  ListView<String> inView, outView;
  List<String> inPortList = new ArrayList<>(),
               outPortList = new ArrayList<>();
  ObservableList<String> inPorts = FXCollections.observableArrayList( inPortList ), 
                         outPorts = FXCollections.observableArrayList( outPortList );
  inView = new ListView<>( inPorts );
  outView = new ListView<>( outPorts );
     
  String lastInDevice = userPrefs.getMidiInPort();
  String lastOutDevice = userPrefs.getMidiOutPort();
  int ipIx = -1, opIx = -1;
  
  MidiDevice device;
  MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  for ( MidiDevice.Info info : infos ) {
    try {
      device = MidiSystem.getMidiDevice( info );
      if (!( device instanceof Sequencer ) && !( device instanceof Synthesizer )) {
        if (device.getMaxReceivers() != 0) {
          opIx++;
          outPorts.add( info.getName() );
          if (info.getName().equals( lastOutDevice )) {
            outView.getSelectionModel().clearAndSelect( opIx );
          }
          Debugger.log( "DEBUG - Found OUT Port: " + info.getName() + " - " + info.getDescription() );
        } else if (device.getMaxTransmitters() != 0) {
          ipIx++;
          inPorts.add( info.getName() );
          if (info.getName().equals( lastInDevice )) {
            inView.getSelectionModel().clearAndSelect( ipIx );
          }
          Debugger.log( "DEBUG - Found IN Port: " + info.getName() + " - " + info.getDescription() );
        }
      }
    } catch (MidiUnavailableException ex) {
      ex.printStackTrace();
    }
  }
 
  gp.add( inView, 0, 1 );
  gp.add( outView, 1, 1 );
  dialog.getDialogPane().setContent( gp );
  
  Optional<ButtonType> rc = dialog.showAndWait();
  
  if (rc.get() == ButtonType.OK) {
    if (outView.getSelectionModel().getSelectedIndex() != -1) {
      userPrefs.setMidiOutPort( outView.getSelectionModel().getSelectedItem() );
    }
    if (inView.getSelectionModel().getSelectedIndex() != -1) {
      userPrefs.setMidiInPort( inView.getSelectionModel().getSelectedItem() );
    } 
  }
}
 
Example 20
Source File: MidiCommunication.java    From jmg with GNU General Public License v2.0 2 votes vote down vote up
/**
* A constructor that automatic assigns MIDI input and output to the ports
 * you define. Use this as a convenient way of setting up MIDI IO if your 
 * application is running on a hardware setup that never (or rarely) changes.
 * Warning: Attempting to assigning unavailible devicePortID's will cause 
 * a NullPointerException.
 * @param inputDeviceID The MIDI port number to use for input to Java.
 * @param outputDeviceID The MIDI port number to use for output from Java.
 */
public MidiCommunication(int inputDeviceID, int outputDeviceID) {
    final MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
    setMidiInputSelection(inputDeviceID, info);
    setMidiOutputSelection(outputDeviceID, info);
}