Whole document tree Chapter 9Accessing MIDI System ResourcesThe JavaTM Sound API offers a flexible model for MIDI system configuration, just as it does for configuration of the sampled-audio system. An implementation of the Java Sound API can itself provide different sorts of MIDI devices, and additional ones can be supplied by service providers and installed by users. You can write your program in such a way that it makes few assumptions about which specific MIDI devices are installed on the computer. Instead, the program can take advantage of the MIDI system's defaults, or it can allow the user to select from whatever devices happen to be available. This section shows how your program can learn what MIDI resources have been installed, and how to get access to the desired ones. After you've accessed and opened the devices, you can connect them to each other, as discussed in the next chapter, "Transmitting and Receiving MIDI Messages." The MidiSystem Class
The role of the
You can query the
The
An application program can obtain the following resources from the
MidiSystem class's file-handling facilities are discussed in Chapter 11, "Playing, Recording, and Editing MIDI Sequences," and Chapter 12, "Synthesizing Sound." To understand how the MIDI system itself gets access to all these resources, see Part III of this guide, "Service Provider Interfaces."
Obtaining Default DevicesA typical MIDI application program that uses the Java Sound API begins by obtaining the devices it needs, which can consist of one or more sequencers, synthesizers, input ports, or output ports.
There is a default synthesizer device, a default sequencer device, a default transmitting device, and a default receiving device. The latter two devices normally represent the MIDI input and output ports, respectively, if there are any available on the system. (It's easy to get confused about the directionality here. Think of the ports' transmission or reception in relation to the software, not in relation to any external physical devices connected to the physical ports. A MIDI input port transmits data from an external device to a Java Sound API
A simple application program might just use the default instead of exploring all the installed devices. The static Sequencer getSequencer() static Synthesizer getSynthesizer() static Receiver getReceiver() static Transmitter getTransmitter()
The first two of these methods obtain the system's default sequencing and synthesis resources, which either represent physical devices or are implemented wholly in software. The Learning What Devices Are Installed
Instead of using the default devices, a more thorough approach is to select the desired devices from the full set of devices that are installed on the system. An application program can select the desired devices programmatically, or it can display a list of available devices and let the user select which ones to use. The Here is the method for learning about the installed devices: static MidiDevice.Info[] getMidiDeviceInfo()
As you can see, it returns an array of information objects. Each of these returned However, to use the strings programmatically to select a device (as opposed to displaying the strings to the user), you need to know in advance what they might be. The company that provides each device should include this information in its documentation. An application program that requires or prefers a particular device can use this information to locate that device. This approach has the drawback of limiting the program to device implementations that it knows about in advance.
Another, more general, approach is to go ahead and iterate over the Obtaining a Desired Device
Once an appropriate device's info object is found, the application program invokes the following static MidiDevice getMidiDevice(MidiDevice.Info info)
You can use this method if you've already found the info object describing the device you need. However, if you can't interpret the info objects returned by // Obtain information about all the installed synthesizers. Vector synthInfos; MidiDevice device; MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { try { device = MidiSystem.getMidiDevice(info); } catch (MidiUnavailableException e) { // Handle or throw exception... } if (device instanceof Synthesizer) { synthInfos.add(info); } } // Now, display strings from synthInfos list in GUI.
As another example, you might choose a device programmatically, without involving the user. Let's suppose you want to obtain the synthesizer that can play the most notes simultaneously. You iterate over all the MidiDevice.Info objects, as above, but after determining that a device is a synthesizer, you query its capabilities by invoking the Opening Devices
The previous section showed how to get an installed device. However, a device might be installed but unavailable. For example, another application program might have exclusive use of it. To actually reserve a device for your program, you need to use the if (!(device.isOpen())) { try { device.open(); } catch (MidiUnavailableException e) { // Handle or throw exception... } } Once you've accessed a device and reserved it by opening it, you'll probably want to connect it to one or more other devices to let MIDI data flow between them. This procedure is described in Chapter 10, "Transmitting and Receiving MIDI Messages."
When done with a device, you release it for other programs' use by invoking the [Top] [Prev] [Next] [Bottom] Copyright © 2000, Sun Microsystems Inc. All rights reserved. |