001package bradleyross.coremidi4j.samples;
002import javax.sound.midi.MidiDevice;
003import javax.sound.midi.MidiSystem;
004import javax.sound.midi.MidiUnavailableException;
005// import javax.sound.midi.Receiver;
006// import javax.sound.midi.Transmitter;
007import java.util.ArrayList;
008import uk.co.xfactorylibrarians.coremidi4j.CoreMidiDeviceProvider;
009// import uk.co.xfactorylibrarians.coremidi4j.CoreMidiNotification;
010/**
011 * Generates a list of MIDI devices.
012 * 
013 * @author Bradley Ross
014 *
015 */
016public class ListItems {
017        /**
018         *    List of devices returned by CoreMidi4J.
019         */
020        protected MidiDevice.Info[] deviceList;
021        /**
022         * List of MidiDevices that can send information.
023         */
024        protected ArrayList<MidiDevice.Info> sources = new ArrayList<MidiDevice.Info>();
025        /**
026         * List of MidiDevices that can receive information.
027         */
028        protected ArrayList<MidiDevice.Info> dests = new ArrayList<MidiDevice.Info>(); 
029        /**
030         *  Lists devices.
031         */
032        public void run() {
033                deviceList = CoreMidiDeviceProvider.getMidiDeviceInfo(); 
034                for (MidiDevice.Info item : deviceList) {
035                        MidiDevice device;
036                        System.out.println("****");
037                        System.out.println(item.getName() + " : " + item.getVendor());
038                        System.out.println(item.getDescription());
039                        System.out.println("Version: " + item.getVersion());
040                        System.out.println("Hash code: " + Integer.toString(item.hashCode()));
041                        try {
042                                device = MidiSystem.getMidiDevice(item);
043                                int maxRcvr = device.getMaxReceivers();
044                                if (maxRcvr != 0) {
045                                        dests.add(item);
046                                        if (maxRcvr < 0) {
047                                                System.out.println("Unlimited Receivers");
048                                        } else {
049                                                System.out.println("Max. Receivers: " + maxRcvr);
050                                        }
051                                }
052                                int maxXmtr = device.getMaxTransmitters();
053                                if (maxXmtr != 0 ) {
054                                        sources.add(item);
055                                        if (maxXmtr < 0) {
056                                                System.out.println("Unlimited Transmitters");
057                                        } else {
058                                                System.out.println("Max. Transmitters: " + maxXmtr);
059                                        }
060                                }
061                        } catch (MidiUnavailableException e) {
062                                System.out.println("Error obtaining list of MIDI devices");
063                                e.printStackTrace();
064                        }
065                        System.out.println("****");
066                        System.out.println("Sources:");
067                        for (MidiDevice.Info source : sources) {
068                                System.out.println("    " + source.getName() + " : " + source.getVendor() + " : " +
069                                                Integer.toString(source.hashCode()));
070                        }
071                        System.out.println("****");
072                        System.out.println("Sinks:");
073                        for (MidiDevice.Info sink : dests) {
074                                System.out.println("    " + sink.getName() + " : " + sink.getVendor() + " : " +
075                                                Integer.toString(sink.hashCode()));
076                        }
077                }
078        }
079        /**
080         * Test driver.
081         * 
082         * @param args Not used in this example.
083         */
084        public static void main(String[] args) {
085                try {
086                        Class.forName
087                        ("uk.co.xfactorylibrarians.coremidi4j.CoreMidiDeviceProvider");
088                } catch (Exception e) {
089                        e.printStackTrace();
090                        System.out.println("Unable to open driver - aborting");
091                        return;
092                }
093                ListItems instance = new ListItems();
094                instance.run();
095        }
096}