Class: javax.sound.midi.MidiSystem
- public class MidiSystem
MidiSystem class provides access to the installed MIDI
system resources, including devices such as synthesizers, sequencers, and
MIDI input and output ports. A typical simple MIDI application might
begin by invoking one or more MidiSystem methods to learn
what devices are installed and to obtain the ones needed in that
application.
The class also has methods for reading files, streams, and URLs that
contain standard MIDI file data or soundbanks. You can query the
MidiSystem for the format of a specified MIDI file.
You cannot instantiate a MidiSystem; all the methods are
static.
Properties can be used to specify default MIDI devices. Both system properties and a properties file are considered. The properties file is "lib/sound.properties" in the JRE directory. If a property exists both as a system property and in the properties file, the system property takes precedence. If none is specified, a suitable default is chosen among the available devices. The syntax of the properties file is specified in Properties.load. The following table lists the available property keys and which methods consider them:
| Property Key | Interface | Affected Method |
|---|---|---|
javax.sound.midi.Receiver | javax.sound.midi.Receiver | javax.sound.midi.MidiSystem.getReceiver() |
javax.sound.midi.Sequencer | javax.sound.midi.Sequencer | javax.sound.midi.MidiSystem.getSequencer() |
javax.sound.midi.Synthesizer | javax.sound.midi.Synthesizer | javax.sound.midi.MidiSystem.getSynthesizer() |
javax.sound.midi.Transmitter | javax.sound.midi.Transmitter | javax.sound.midi.MidiSystem.getTransmitter() |
String returned by the getName
method of MidiDevice.Info.
Either the class name, or the device name may be omitted.
If only the class name is specified, the trailing hash mark
is optional.
If the provider class is specified, and it can be
successully retrieved from the installed providers,
the list of
MidiDevice.Info objects is retrieved
from the provider. Otherwise, or when these devices
do not provide a subsequent match, the list is retrieved
from javax.sound.midi.MidiSystem.getMidiDeviceInfo() to contain
all available MidiDevice.Info objects.
If a device name is specified, the resulting list of
MidiDevice.Info objects is searched:
the first one with a matching name, and whose
MidiDevice implements the
respective interface, will be returned.
If no matching MidiDevice.Info object
is found, or the device name is not specified,
the first suitable device from the resulting
list will be returned. For Sequencer and Synthesizer,
a device is suitable if it implements the respective
interface; whereas for Receiver and Transmitter, a device is
suitable if it
implements neither Sequencer nor Synthesizer and provides
at least one Receiver or Transmitter, respectively.
For example, the property javax.sound.midi.Receiver
with a value
"com.sun.media.sound.MidiProvider#SunMIDI1"
will have the following consequences when
getReceiver is called:
if the class com.sun.media.sound.MidiProvider exists
in the list of installed MIDI device providers,
the first Receiver device with name
"SunMIDI1" will be returned. If it cannot
be found, the first Receiver from that provider
will be returned, regardless of name.
If there is none, the first Receiver with name
"SunMIDI1" in the list of all devices
(as returned by getMidiDeviceInfo) will be returned,
or, if not found, the first Receiver that can
be found in the list of all devices is returned.
If that fails, too, a MidiUnavailableException
is thrown.
Methods
-
getMidiDevicetop
Obtains the requested MIDI device. -
getMidiDeviceInfotop
public static MidiDevice.Info[] getMidiDeviceInfo()Obtains an array of information objects representing the set of all MIDI devices available on the system. A returned information object can then be used to obtain the corresponding device object, by invoking getMidiDevice. -
getMidiFileFormattop
public static MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOExceptionObtains the MIDI file format of the specifiedFile. TheFilemust point to valid MIDI file data for a file type recognized by the system.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while determining the file format.
-
getMidiFileFormattop
public static MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOExceptionObtains the MIDI file format of the data in the specified input stream. The stream must point to valid MIDI file data for a file type recognized by the system.This method and/or the code it invokes may need to read some data from the stream to determine whether its data format is supported. The implementation may therefore need to mark the stream, read enough data to determine whether it is in a supported format, and reset the stream's read pointer to its original position. If the input stream does not permit this set of operations, this method may fail with an
IOException.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while determining the file format.
-
getMidiFileFormattop
public static MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOExceptionObtains the MIDI file format of the data in the specified URL. The URL must point to valid MIDI file data for a file type recognized by the system.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while determining the file format.
-
getMidiFileTypestop
public static int[] getMidiFileTypes()Obtains the set of MIDI file types for which file writing support is provided by the system. -
getMidiFileTypestop
public static int[] getMidiFileTypes(Sequence sequence)Obtains the set of MIDI file types that the system can write from the sequence specified. -
getReceivertop
public static Receiver getReceiver() throws MidiUnavailableExceptionObtains a MIDI receiver from an external MIDI port or other default device.If the system property
javax.sound.midi.Receiveris defined or it is defined in the file "sound.properties", it is used to identify the device that provides the default receiver. For details, refer to the class description. If a suitable MIDI port is not available, the Receiver is retrieved from an installed synthesizer.If this method returns successfully, the MidiDevice the
Receiverbelongs to is opened implicitly, if it is not already open. It is possible to close an implicitly opened device by calling close on the returnedReceiver. All openReceiverinstances have to be closed in order to release system resources hold by theMidiDevice. For a detailed description of open/close behaviour see the class description of MidiDevice. -
getSequencetop
Obtains a MIDI sequence from the specifiedFile. TheFilemust point to valid MIDI file data for a file type recognized by the system.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while constructing the
Sequenceobject from the file data. -
getSequencetop
Obtains a MIDI sequence from the specified input stream. The stream must point to valid MIDI file data for a file type recognized by the system.This method and/or the code it invokes may need to read some data from the stream to determine whether its data format is supported. The implementation may therefore need to mark the stream, read enough data to determine whether it is in a supported format, and reset the stream's read pointer to its original position. If the input stream does not permit this set of operations, this method may fail with an
IOException.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while constructing the
Sequenceobject from the file data. -
getSequencetop
Obtains a MIDI sequence from the specified URL. The URL must point to valid MIDI file data for a file type recognized by the system.This operation can only succeed for files of a type which can be parsed by an installed file reader. It may fail with an InvalidMidiDataException even for valid files if no compatible file reader is installed. It will also fail with an InvalidMidiDataException if a compatible file reader is installed, but encounters errors while constructing the
Sequenceobject from the file data. -
getSequencertop
public static Sequencer getSequencer() throws MidiUnavailableExceptionObtains the defaultSequencer, connected to a default device. The returnedSequencerinstance is connected to the defaultSynthesizer, as returned by javax.sound.midi.MidiSystem.getSynthesizer(). If there is noSynthesizeravailable, or the defaultSynthesizercannot be opened, thesequenceris connected to the defaultReceiver, as returned by javax.sound.midi.MidiSystem.getReceiver(). The connection is made by retrieving aTransmitterinstance from theSequencerand setting itsReceiver. Closing and re-opening the sequencer will restore the connection to the default device.This method is equivalent to calling
getSequencer(true).If the system property
javax.sound.midi.Sequenceris defined or it is defined in the file "sound.properties", it is used to identify the default sequencer. For details, refer to the class description. -
getSequencertop
public static Sequencer getSequencer(boolean connected) throws MidiUnavailableExceptionObtains the defaultSequencer, optionally connected to a default device.If
connectedis true, the returnedSequencerinstance is connected to the defaultSynthesizer, as returned by javax.sound.midi.MidiSystem.getSynthesizer(). If there is noSynthesizeravailable, or the defaultSynthesizercannot be opened, thesequenceris connected to the defaultReceiver, as returned by javax.sound.midi.MidiSystem.getReceiver(). The connection is made by retrieving aTransmitterinstance from theSequencerand setting itsReceiver. Closing and re-opening the sequencer will restore the connection to the default device.If
connectedis false, the returnedSequencerinstance is not connected, it has no openTransmitters. In order to play the sequencer on a MIDI device, or aSynthesizer, it is necessary to get aTransmitterand set itsReceiver.If the system property
javax.sound.midi.Sequenceris defined or it is defined in the file "sound.properties", it is used to identify the default sequencer. For details, refer to the class description. -
getSoundbanktop
Constructs aSoundbankby reading it from the specifiedFile. TheFilemust point to a valid MIDI soundbank file. -
getSoundbanktop
public static Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOExceptionConstructs a MIDI sound bank by reading it from the specified stream. The stream must point to a valid MIDI soundbank file. In general, MIDI soundbank providers may need to read some data from the stream before determining whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support this, this method may fail with an IOException. -
getSoundbanktop
Constructs aSoundbankby reading it from the specified URL. The URL must point to a valid MIDI soundbank file. -
getSynthesizertop
public static Synthesizer getSynthesizer() throws MidiUnavailableExceptionObtains the default synthesizer.If the system property
javax.sound.midi.Synthesizeris defined or it is defined in the file "sound.properties", it is used to identify the default synthesizer. For details, refer to the class description. -
getTransmittertop
public static Transmitter getTransmitter() throws MidiUnavailableExceptionObtains a MIDI transmitter from an external MIDI port or other default source.If the system property
javax.sound.midi.Transmitteris defined or it is defined in the file "sound.properties", it is used to identify the device that provides the default transmitter. For details, refer to the class description. If this method returns successfully, the MidiDevice theTransmitterbelongs to is opened implicitly, if it is not already open. It is possible to close an implicitly opened device by calling close on the returnedTransmitter. All openTransmitterinstances have to be closed in order to release system resources hold by theMidiDevice. For a detailed description of open/close behaviour see the class description of MidiDevice. -
isFileTypeSupportedtop
public static boolean isFileTypeSupported(int fileType)Indicates whether file writing support for the specified MIDI file type is provided by the system. -
isFileTypeSupportedtop
public static boolean isFileTypeSupported(int fileType, Sequence sequence)Indicates whether a MIDI file of the file type specified can be written from the sequence indicated. -
writetop
Writes a stream of bytes representing a file of the MIDI file type indicated to the external file provided. -
writetop
Writes a stream of bytes representing a file of the MIDI file type indicated to the output stream provided.
