Top Description Constructors Methods
javax.sound.midi

public Class MidiSystem

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .InputStream, .OutputStream, java.net.URL, java.util.ArrayList, .Collections, .HashSet, .Iterator, .List, .Objects, .Properties, .Set, javax.sound.midi.spi.MidiDeviceProvider, .MidiFileReader, .MidiFileWriter, .SoundbankReader, com.sun.media.sound.AutoConnectSequencer, .JDK13Services, .MidiDeviceReceiverEnvelope, .MidiDeviceTransmitterEnvelope, .ReferenceCountingDevice

The 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 "sound.properties" properties file is read from an implementation-specific location (typically it is the conf directory in the Java installation directory). The optional "javax.sound.config.file" system property can be used to specify the properties file that will be read as the initial configuration. 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:

MIDI System Property Keys
Property Key Interface Affected Method
javax.sound.midi.Receiver Receiver getReceiver
javax.sound.midi.Sequencer Sequencer getSequencer
javax.sound.midi.Synthesizer Synthesizer getSynthesizer
javax.sound.midi.Transmitter Transmitter getTransmitter
The property value consists of the provider class name and the device name, separated by the hash mark ("#"). The provider class name is the fully-qualified name of a concrete MIDI device provider class. The device name is matched against the 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 successfully 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 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.

Authors
Kara Kytle, Florian Bomers, Matthias Pfisterer

Constructor Summary

AccessConstructor and Description
private
MidiSystem()

Private no-args constructor for ensuring against instantiation.

Method Summary

Modifier and TypeMethod and Description
private static MidiDevice

Returns:

default MidiDevice of the specified type.
getDefaultDevice
(Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

Attempts to locate and return a default MidiDevice of the specified type.

private static MidiDevice

Returns:

default MidiDevice of the specified type
getDefaultDeviceWrapper
(Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

Attempts to locate and return a default MidiDevice of the specified type.

private static MidiDevice

Returns:

A MidiDevice is considered appropriate, or null if no appropriate device is found
getFirstDevice
(MidiDeviceProvider
The MidiDeviceProvider to check for MidiDevices
provider
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

From a given MidiDeviceProvider, return the first appropriate device.

private static MidiDevice

Returns:

A MidiDevice is considered appropriate, or null if no appropriate device is found
getFirstDevice
(MidiDeviceProvider
The MidiDeviceProvider to check for MidiDevices
provider
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
,
boolean
if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSynthesizer
,
boolean
if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSequencer
)

From a given MidiDeviceProvider, return the first appropriate device.

private static MidiDevice

Returns:

A MidiDevice that is considered appropriate, or null if none is found
getFirstDevice
(List<MidiDeviceProvider>
The List of MidiDeviceProviders to search
providers
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

From a List of MidiDeviceProviders, return the first appropriate MidiDevice.

private static MidiDevice

Returns:

A MidiDevice that is considered appropriate, or null if none is found
getFirstDevice
(List<MidiDeviceProvider>
The List of MidiDeviceProviders to search
providers
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
,
boolean
if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSynthesizer
,
boolean
if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSequencer
)

From a List of MidiDeviceProviders, return the first appropriate MidiDevice.

public static MidiDevice

Returns:

the requested device
getMidiDevice
(final MidiDevice.Info
a device information object representing the desired device
info
)

Obtains the requested MIDI device.

public static MidiDevice.Info[]

Returns:

an array of MidiDevice.Info objects, one for each installed MIDI device. If no such devices are installed, an array of length 0 is returned.
getMidiDeviceInfo
()

Obtains an array of information objects representing the set of all MIDI devices available on the system.

private static List<MidiDeviceProvider>

Returns:

the list of MidiDeviceProviders installed on the system
getMidiDeviceProviders
()

Obtains the list of MidiDeviceProviders installed on the system.

public static MidiFileFormat

Returns:

an MidiFileFormat object describing the MIDI file format
getMidiFileFormat
(final InputStream
the input stream from which file format information should be extracted
stream
)

Obtains the MIDI file format of the data in the specified input stream.

public static MidiFileFormat

Returns:

a MidiFileFormat object describing the MIDI file format
getMidiFileFormat
(final URL
the URL from which file format information should be extracted
url
)

Obtains the MIDI file format of the data in the specified URL.

public static MidiFileFormat

Returns:

a MidiFileFormat object describing the MIDI file format
getMidiFileFormat
(final File
the File from which file format information should be extracted
file
)

Obtains the MIDI file format of the specified File.

private static List<MidiFileReader>

Returns:

the list of MidiFileReaders installed on the system
getMidiFileReaders
()

Obtains the list of MidiFileReaders installed on the system.

public static int[]

Returns:

array of unique file types. If no file types are supported, an array of length 0 is returned.
getMidiFileTypes
()

Obtains the set of MIDI file types for which file writing support is provided by the system.

public static int[]

Returns:

the set of unique supported file types. If no file types are supported, returns an array of length 0.
getMidiFileTypes
(final Sequence
the sequence for which MIDI file type support is queried
sequence
)

Obtains the set of MIDI file types that the system can write from the sequence specified.

private static List<MidiFileWriter>

Returns:

the list of MidiFileWriters installed on the system
getMidiFileWriters
()

Obtains the list of MidiFileWriters installed on the system.

private static MidiDevice

Returns:

A MidiDevice matching the requirements, or null if none is found
getNamedDevice
(String
The name of the MidiDevice to be returned
deviceName
,
MidiDeviceProvider
The MidiDeviceProvider to check for MidiDevices
provider
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

Return a MidiDevice with a given name from a given MidiDeviceProvider.

private static MidiDevice

Returns:

A MidiDevice matching the requirements, or null if none is found
getNamedDevice
(String
The name of the MidiDevice to be returned
deviceName
,
MidiDeviceProvider
The MidiDeviceProvider to check for MidiDevices
provider
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
,
boolean
if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSynthesizer
,
boolean
if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSequencer
)

Return a MidiDevice with a given name from a given MidiDeviceProvider.

private static MidiDevice

Returns:

A Mixer matching the requirements, or null if none is found
getNamedDevice
(String
The name of the MidiDevice to be returned
deviceName
,
List<MidiDeviceProvider>
The List of MidiDeviceProviders to check for MidiDevices
providers
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
)

Return a MidiDevice with a given name from a list of MidiDeviceProviders.

private static MidiDevice

Returns:

A Mixer matching the requirements, or null if none is found
getNamedDevice
(String
The name of the MidiDevice to be returned
deviceName
,
List<MidiDeviceProvider>
The List of MidiDeviceProviders to check for MidiDevices
providers
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
,
boolean
if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSynthesizer
,
boolean
if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSequencer
)

Return a MidiDevice with a given name from a list of MidiDeviceProviders.

private static MidiDeviceProvider

Returns:

A MidiDeviceProvider of the requested class, or null if none is found
getNamedProvider
(String
The class name of the provider to be returned
providerClassName
,
List<MidiDeviceProvider>
The list of MidiDeviceProviders that is searched
providers
)

Return a MidiDeviceProvider of a given class from the list of MidiDeviceProviders.

private static List<?>

Returns:

a List of instances of providers for the requested service. If no providers are available, a List of length 0 will be returned.
getProviders
(Class<?>
The type of providers requested. This should be one of AudioFileReader.class, AudioFileWriter.class, FormatConversionProvider.class, MixerProvider.class, MidiDeviceProvider.class, MidiFileReader.class, MidiFileWriter.class or SoundbankReader.class.
providerClass
)

Obtains the set of services currently installed on the system using the SPI mechanism in 1.3.

public static Receiver

Returns:

the default MIDI receiver
getReceiver
()

Obtains a MIDI receiver from an external MIDI port or other default device.

public static Sequence

Returns:

a Sequence object based on the MIDI file data contained in the input stream
getSequence
(final InputStream
the input stream from which the Sequence should be constructed
stream
)

Obtains a MIDI sequence from the specified input stream.

public static Sequence

Returns:

a Sequence object based on the MIDI file data pointed to by the URL
getSequence
(final URL
the URL from which the Sequence should be constructed
url
)

Obtains a MIDI sequence from the specified URL.

public static Sequence

Returns:

a Sequence object based on the MIDI file data pointed to by the File
getSequence
(final File
the File from which the Sequence should be constructed
file
)

Obtains a MIDI sequence from the specified File.

public static Sequencer

Returns:

the default sequencer, connected to a default Receiver
getSequencer
()

Obtains the default Sequencer, connected to a default device.

public static Sequencer

Returns:

the default sequencer
getSequencer
(boolean
whether or not the returned Sequencer is connected to the default Synthesizer
connected
)

Obtains the default Sequencer, optionally connected to a default device.

public static Soundbank

Returns:

the sound bank
getSoundbank
(final InputStream
the source of the sound bank data
stream
)

Constructs a MIDI sound bank by reading it from the specified stream.

public static Soundbank

Returns:

the sound bank
getSoundbank
(final URL
the source of the sound bank data
url
)

Constructs a Soundbank by reading it from the specified URL.

public static Soundbank

Returns:

the sound bank
getSoundbank
(final File
the source of the sound bank data
file
)

Constructs a Soundbank by reading it from the specified File.

private static List<SoundbankReader>

Returns:

the list of SoundbankReaders installed on the system
getSoundbankReaders
()

Obtains the list of SoundbankReaders installed on the system.

public static Synthesizer

Returns:

the default synthesizer
getSynthesizer
()

Obtains the default synthesizer.

public static Transmitter

Returns:

the default MIDI transmitter
getTransmitter
()

Obtains a MIDI transmitter from an external MIDI port or other default source.

private static boolean

Returns:

true if the device is considered appropriate according to the rules given above, false otherwise
isAppropriateDevice
(MidiDevice
the MidiDevice to test
device
,
Class<?>
The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class
deviceClass
,
boolean
if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSynthesizer
,
boolean
if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.
allowSequencer
)

Checks if a MidiDevice is appropriate.

public static boolean

Returns:

true if the file type is supported, otherwise false
isFileTypeSupported
(int
the file type for which write capabilities are queried
fileType
)

Indicates whether file writing support for the specified MIDI file type is provided by the system.

public static boolean

Returns:

true if the file type is supported for this sequence, otherwise false
isFileTypeSupported
(final int
the file type for which write capabilities are queried
fileType
,
final Sequence
the sequence for which file writing support is queried
sequence
)

Indicates whether a MIDI file of the file type specified can be written from the sequence indicated.

public static int

Returns:

the number of bytes written to the output stream
write
(final Sequence
sequence containing MIDI data to be written to the file
in
,
final int
the file type of the file to be written to the output stream
fileType
,
final OutputStream
stream to which the file data should be written
out
)

Writes a stream of bytes representing a file of the MIDI file type indicated to the output stream provided.

public static int

Returns:

the number of bytes written to the file
write
(final Sequence
sequence containing MIDI data to be written to the file
in
,
final int
the file type of the file to be written to the output stream
type
,
final File
external file to which the file data should be written
out
)

Writes a stream of bytes representing a file of the MIDI file type indicated to the external file provided.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MidiSystemback to summary
private MidiSystem()

Private no-args constructor for ensuring against instantiation.

Method Detail

getDefaultDeviceback to summary
private static MidiDevice getDefaultDevice(Class<?> deviceClass)

Attempts to locate and return a default MidiDevice of the specified type.

Parameters
deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

default MidiDevice of the specified type.

Exceptions
IllegalArgumentException:
on failure
getDefaultDeviceWrapperback to summary
private static MidiDevice getDefaultDeviceWrapper(Class<?> deviceClass) throws MidiUnavailableException

Attempts to locate and return a default MidiDevice of the specified type. This method wraps getDefaultDevice. It catches the IllegalArgumentException thrown by getDefaultDevice and instead throws a MidiUnavailableException, with the caught exception chained.

Parameters
deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

default MidiDevice of the specified type

Exceptions
MidiUnavailableException:
on failure
getFirstDeviceback to summary
private static MidiDevice getFirstDevice(MidiDeviceProvider provider, Class<?> deviceClass)

From a given MidiDeviceProvider, return the first appropriate device.

Parameters
provider:MidiDeviceProvider

The MidiDeviceProvider to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

A MidiDevice is considered appropriate, or null if no appropriate device is found

getFirstDeviceback to summary
private static MidiDevice getFirstDevice(MidiDeviceProvider provider, Class<?> deviceClass, boolean allowSynthesizer, boolean allowSequencer)

From a given MidiDeviceProvider, return the first appropriate device.

Parameters
provider:MidiDeviceProvider

The MidiDeviceProvider to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

allowSynthesizer:boolean

if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

allowSequencer:boolean

if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

Returns:MidiDevice

A MidiDevice is considered appropriate, or null if no appropriate device is found

getFirstDeviceback to summary
private static MidiDevice getFirstDevice(List<MidiDeviceProvider> providers, Class<?> deviceClass)

From a List of MidiDeviceProviders, return the first appropriate MidiDevice.

Parameters
providers:List<MidiDeviceProvider>

The List of MidiDeviceProviders to search

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

A MidiDevice that is considered appropriate, or null if none is found

getFirstDeviceback to summary
private static MidiDevice getFirstDevice(List<MidiDeviceProvider> providers, Class<?> deviceClass, boolean allowSynthesizer, boolean allowSequencer)

From a List of MidiDeviceProviders, return the first appropriate MidiDevice.

Parameters
providers:List<MidiDeviceProvider>

The List of MidiDeviceProviders to search

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

allowSynthesizer:boolean

if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

allowSequencer:boolean

if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

Returns:MidiDevice

A MidiDevice that is considered appropriate, or null if none is found

getMidiDeviceback to summary
public static MidiDevice getMidiDevice(final MidiDevice.Info info) throws MidiUnavailableException

Obtains the requested MIDI device.

Parameters
info:MidiDevice.Info

a device information object representing the desired device

Returns:MidiDevice

the requested device

Exceptions
MidiUnavailableException:
if the requested device is not available due to resource restrictions
IllegalArgumentException:
if the info object does not represent a MIDI device installed on the system
NullPointerException:
if info is null
See Also
getMidiDeviceInfo
getMidiDeviceInfoback to summary
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.

Returns:MidiDevice.Info[]

an array of MidiDevice.Info objects, one for each installed MIDI device. If no such devices are installed, an array of length 0 is returned.

getMidiDeviceProvidersback to summary
private static List<MidiDeviceProvider> getMidiDeviceProviders()

Obtains the list of MidiDeviceProviders installed on the system.

Returns:List<MidiDeviceProvider>

the list of MidiDeviceProviders installed on the system

Annotations
@SuppressWarnings:unchecked
getMidiFileFormatback to summary
public static MidiFileFormat getMidiFileFormat(final InputStream stream) throws InvalidMidiDataException, IOException

Obtains 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.

Parameters
stream:InputStream

the input stream from which file format information should be extracted

Returns:MidiFileFormat

an MidiFileFormat object describing the MIDI file format

Exceptions
InvalidMidiDataException:
if the stream does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs while accessing the stream
NullPointerException:
if stream is null
See Also
getMidiFileFormat(URL), getMidiFileFormat(File), InputStream#markSupported, InputStream#mark
getMidiFileFormatback to summary
public static MidiFileFormat getMidiFileFormat(final URL url) throws InvalidMidiDataException, IOException

Obtains 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.

Parameters
url:URL

the URL from which file format information should be extracted

Returns:MidiFileFormat

a MidiFileFormat object describing the MIDI file format

Exceptions
InvalidMidiDataException:
if the URL does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs while accessing the URL
NullPointerException:
if url is null
See Also
getMidiFileFormat(InputStream), getMidiFileFormat(File)
getMidiFileFormatback to summary
public static MidiFileFormat getMidiFileFormat(final File file) throws InvalidMidiDataException, IOException

Obtains the MIDI file format of the specified File. The File 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.

Parameters
file:File

the File from which file format information should be extracted

Returns:MidiFileFormat

a MidiFileFormat object describing the MIDI file format

Exceptions
InvalidMidiDataException:
if the File does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs while accessing the file
NullPointerException:
if file is null
See Also
getMidiFileFormat(InputStream), getMidiFileFormat(URL)
getMidiFileReadersback to summary
private static List<MidiFileReader> getMidiFileReaders()

Obtains the list of MidiFileReaders installed on the system.

Returns:List<MidiFileReader>

the list of MidiFileReaders installed on the system

Annotations
@SuppressWarnings:unchecked
getMidiFileTypesback to summary
public static int[] getMidiFileTypes()

Obtains the set of MIDI file types for which file writing support is provided by the system.

Returns:int[]

array of unique file types. If no file types are supported, an array of length 0 is returned.

getMidiFileTypesback to summary
public static int[] getMidiFileTypes(final Sequence sequence)

Obtains the set of MIDI file types that the system can write from the sequence specified.

Parameters
sequence:Sequence

the sequence for which MIDI file type support is queried

Returns:int[]

the set of unique supported file types. If no file types are supported, returns an array of length 0.

Exceptions
NullPointerException:
if sequence is null
getMidiFileWritersback to summary
private static List<MidiFileWriter> getMidiFileWriters()

Obtains the list of MidiFileWriters installed on the system.

Returns:List<MidiFileWriter>

the list of MidiFileWriters installed on the system

Annotations
@SuppressWarnings:unchecked
getNamedDeviceback to summary
private static MidiDevice getNamedDevice(String deviceName, MidiDeviceProvider provider, Class<?> deviceClass)

Return a MidiDevice with a given name from a given MidiDeviceProvider.

Parameters
deviceName:String

The name of the MidiDevice to be returned

provider:MidiDeviceProvider

The MidiDeviceProvider to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

A MidiDevice matching the requirements, or null if none is found

getNamedDeviceback to summary
private static MidiDevice getNamedDevice(String deviceName, MidiDeviceProvider provider, Class<?> deviceClass, boolean allowSynthesizer, boolean allowSequencer)

Return a MidiDevice with a given name from a given MidiDeviceProvider.

Parameters
deviceName:String

The name of the MidiDevice to be returned

provider:MidiDeviceProvider

The MidiDeviceProvider to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

allowSynthesizer:boolean

if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

allowSequencer:boolean

if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

Returns:MidiDevice

A MidiDevice matching the requirements, or null if none is found

getNamedDeviceback to summary
private static MidiDevice getNamedDevice(String deviceName, List<MidiDeviceProvider> providers, Class<?> deviceClass)

Return a MidiDevice with a given name from a list of MidiDeviceProviders.

Parameters
deviceName:String

The name of the MidiDevice to be returned

providers:List<MidiDeviceProvider>

The List of MidiDeviceProviders to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

Returns:MidiDevice

A Mixer matching the requirements, or null if none is found

getNamedDeviceback to summary
private static MidiDevice getNamedDevice(String deviceName, List<MidiDeviceProvider> providers, Class<?> deviceClass, boolean allowSynthesizer, boolean allowSequencer)

Return a MidiDevice with a given name from a list of MidiDeviceProviders.

Parameters
deviceName:String

The name of the MidiDevice to be returned

providers:List<MidiDeviceProvider>

The List of MidiDeviceProviders to check for MidiDevices

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

allowSynthesizer:boolean

if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

allowSequencer:boolean

if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

Returns:MidiDevice

A Mixer matching the requirements, or null if none is found

getNamedProviderback to summary
private static MidiDeviceProvider getNamedProvider(String providerClassName, List<MidiDeviceProvider> providers)

Return a MidiDeviceProvider of a given class from the list of MidiDeviceProviders.

Parameters
providerClassName:String

The class name of the provider to be returned

providers:List<MidiDeviceProvider>

The list of MidiDeviceProviders that is searched

Returns:MidiDeviceProvider

A MidiDeviceProvider of the requested class, or null if none is found

getProvidersback to summary
private static List<?> getProviders(Class<?> providerClass)

Obtains the set of services currently installed on the system using the SPI mechanism in 1.3.

Parameters
providerClass:Class<?>

The type of providers requested. This should be one of AudioFileReader.class, AudioFileWriter.class, FormatConversionProvider.class, MixerProvider.class, MidiDeviceProvider.class, MidiFileReader.class, MidiFileWriter.class or SoundbankReader.class.

Returns:List<?>

a List of instances of providers for the requested service. If no providers are available, a List of length 0 will be returned.

getReceiverback to summary
public static Receiver getReceiver() throws MidiUnavailableException

Obtains a MIDI receiver from an external MIDI port or other default device. The returned receiver always implements the MidiDeviceReceiver interface.

If the system property javax.sound.midi.Receiver is 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 a native receiver provided by the default device does not implement the MidiDeviceReceiver interface, it will be wrapped in a wrapper class that implements the MidiDeviceReceiver interface. The corresponding Receiver method calls will be forwarded to the native receiver.

If this method returns successfully, the MidiDevice the Receiver belongs to is opened implicitly, if it is not already open. It is possible to close an implicitly opened device by calling close on the returned Receiver. All open Receiver instances have to be closed in order to release system resources hold by the MidiDevice. For a detailed description of open/close behaviour see the class description of MidiDevice.

Returns:Receiver

the default MIDI receiver

Exceptions
MidiUnavailableException:
if the default receiver is not available due to resource restrictions, or no device providing receivers is installed in the system
getSequenceback to summary
public static Sequence getSequence(final InputStream stream) throws InvalidMidiDataException, IOException

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 Sequence object from the file data.

Parameters
stream:InputStream

the input stream from which the Sequence should be constructed

Returns:Sequence

a Sequence object based on the MIDI file data contained in the input stream

Exceptions
InvalidMidiDataException:
if the stream does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs while accessing the stream
NullPointerException:
if stream is null
See Also
InputStream#markSupported, InputStream#mark
getSequenceback to summary
public static Sequence getSequence(final URL url) throws InvalidMidiDataException, IOException

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 Sequence object from the file data.

Parameters
url:URL

the URL from which the Sequence should be constructed

Returns:Sequence

a Sequence object based on the MIDI file data pointed to by the URL

Exceptions
InvalidMidiDataException:
if the URL does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs while accessing the URL
NullPointerException:
if url is null
getSequenceback to summary
public static Sequence getSequence(final File file) throws InvalidMidiDataException, IOException

Obtains a MIDI sequence from the specified File. The File 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 Sequence object from the file data.

Parameters
file:File

the File from which the Sequence should be constructed

Returns:Sequence

a Sequence object based on the MIDI file data pointed to by the File

Exceptions
InvalidMidiDataException:
if the File does not point to valid MIDI file data recognized by the system
IOException:
if an I/O exception occurs
NullPointerException:
if file is null
getSequencerback to summary
public static Sequencer getSequencer() throws MidiUnavailableException

Obtains the default Sequencer, connected to a default device. The returned Sequencer instance is connected to the default Synthesizer, as returned by getSynthesizer. If there is no Synthesizer available, or the default Synthesizer cannot be opened, the sequencer is connected to the default Receiver, as returned by getReceiver. The connection is made by retrieving a Transmitter instance from the Sequencer and setting its Receiver. 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.Sequencer is 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.

Returns:Sequencer

the default sequencer, connected to a default Receiver

Exceptions
MidiUnavailableException:
if the sequencer is not available due to resource restrictions, or there is no Receiver available by any installed MidiDevice, or no sequencer is installed in the system
See Also
getSequencer(boolean), getSynthesizer, getReceiver
getSequencerback to summary
public static Sequencer getSequencer(boolean connected) throws MidiUnavailableException

Obtains the default Sequencer, optionally connected to a default device.

If connected is true, the returned Sequencer instance is connected to the default Synthesizer, as returned by getSynthesizer. If there is no Synthesizer available, or the default Synthesizer cannot be opened, the sequencer is connected to the default Receiver, as returned by getReceiver. The connection is made by retrieving a Transmitter instance from the Sequencer and setting its Receiver. Closing and re-opening the sequencer will restore the connection to the default device.

If connected is false, the returned Sequencer instance is not connected, it has no open Transmitters. In order to play the sequencer on a MIDI device, or a Synthesizer, it is necessary to get a Transmitter and set its Receiver.

If the system property javax.sound.midi.Sequencer is 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.

Parameters
connected:boolean

whether or not the returned Sequencer is connected to the default Synthesizer

Returns:Sequencer

the default sequencer

Exceptions
MidiUnavailableException:
if the sequencer is not available due to resource restrictions, or no sequencer is installed in the system, or if connected is true, and there is no Receiver available by any installed MidiDevice
Since
1.5
See Also
getSynthesizer, getReceiver
getSoundbankback to summary
public static Soundbank getSoundbank(final InputStream stream) throws InvalidMidiDataException, IOException

Constructs 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.

Parameters
stream:InputStream

the source of the sound bank data

Returns:Soundbank

the sound bank

Exceptions
InvalidMidiDataException:
if the stream does not point to valid MIDI soundbank data recognized by the system
IOException:
if an I/O error occurred when loading the soundbank
NullPointerException:
if stream is null
See Also
InputStream#markSupported, InputStream#mark
getSoundbankback to summary
public static Soundbank getSoundbank(final URL url) throws InvalidMidiDataException, IOException

Constructs a Soundbank by reading it from the specified URL. The URL must point to a valid MIDI soundbank file.

Parameters
url:URL

the source of the sound bank data

Returns:Soundbank

the sound bank

Exceptions
InvalidMidiDataException:
if the URL does not point to valid MIDI soundbank data recognized by the system
IOException:
if an I/O error occurred when loading the soundbank
NullPointerException:
if url is null
getSoundbankback to summary
public static Soundbank getSoundbank(final File file) throws InvalidMidiDataException, IOException

Constructs a Soundbank by reading it from the specified File. The File must point to a valid MIDI soundbank file.

Parameters
file:File

the source of the sound bank data

Returns:Soundbank

the sound bank

Exceptions
InvalidMidiDataException:
if the File does not point to valid MIDI soundbank data recognized by the system
IOException:
if an I/O error occurred when loading the soundbank
NullPointerException:
if file is null
getSoundbankReadersback to summary
private static List<SoundbankReader> getSoundbankReaders()

Obtains the list of SoundbankReaders installed on the system.

Returns:List<SoundbankReader>

the list of SoundbankReaders installed on the system

Annotations
@SuppressWarnings:unchecked
getSynthesizerback to summary
public static Synthesizer getSynthesizer() throws MidiUnavailableException

Obtains the default synthesizer.

If the system property javax.sound.midi.Synthesizer is 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.

Returns:Synthesizer

the default synthesizer

Exceptions
MidiUnavailableException:
if the synthesizer is not available due to resource restrictions, or no synthesizer is installed in the system
getTransmitterback to summary
public static Transmitter getTransmitter() throws MidiUnavailableException

Obtains a MIDI transmitter from an external MIDI port or other default source. The returned transmitter always implements the MidiDeviceTransmitter interface.

If the system property javax.sound.midi.Transmitter is 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 a native transmitter provided by the default device does not implement the MidiDeviceTransmitter interface, it will be wrapped in a wrapper class that implements the MidiDeviceTransmitter interface. The corresponding Transmitter method calls will be forwarded to the native transmitter.

If this method returns successfully, the MidiDevice the Transmitter belongs to is opened implicitly, if it is not already open. It is possible to close an implicitly opened device by calling close on the returned Transmitter. All open Transmitter instances have to be closed in order to release system resources hold by the MidiDevice. For a detailed description of open/close behaviour see the class description of MidiDevice.

Returns:Transmitter

the default MIDI transmitter

Exceptions
MidiUnavailableException:
if the default transmitter is not available due to resource restrictions, or no device providing transmitters is installed in the system
isAppropriateDeviceback to summary
private static boolean isAppropriateDevice(MidiDevice device, Class<?> deviceClass, boolean allowSynthesizer, boolean allowSequencer)

Checks if a MidiDevice is appropriate. If deviceClass is Synthesizer or Sequencer, a device implementing the respective interface is considered appropriate. If deviceClass is Receiver or Transmitter, a device is considered appropriate if it implements neither Synthesizer nor Transmitter, and if it can provide at least one Receiver or Transmitter, respectively.

Parameters
device:MidiDevice

the MidiDevice to test

deviceClass:Class<?>

The requested device type, one of Synthesizer.class, Sequencer.class, Receiver.class or Transmitter.class

allowSynthesizer:boolean

if true, Synthesizers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSequencer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

allowSequencer:boolean

if true, Sequencers are considered appropriate. Otherwise only pure MidiDevices are considered appropriate (unless allowSynthesizer is true). This flag only has an effect for deviceClass Receiver and Transmitter. For other device classes (Sequencer and Synthesizer), this flag has no effect.

Returns:boolean

true if the device is considered appropriate according to the rules given above, false otherwise

isFileTypeSupportedback to summary
public static boolean isFileTypeSupported(int fileType)

Indicates whether file writing support for the specified MIDI file type is provided by the system.

Parameters
fileType:int

the file type for which write capabilities are queried

Returns:boolean

true if the file type is supported, otherwise false

isFileTypeSupportedback to summary
public static boolean isFileTypeSupported(final int fileType, final Sequence sequence)

Indicates whether a MIDI file of the file type specified can be written from the sequence indicated.

Parameters
fileType:int

the file type for which write capabilities are queried

sequence:Sequence

the sequence for which file writing support is queried

Returns:boolean

true if the file type is supported for this sequence, otherwise false

Exceptions
NullPointerException:
if sequence is null
writeback to summary
public static int write(final Sequence in, final int fileType, final OutputStream out) throws IOException

Writes a stream of bytes representing a file of the MIDI file type indicated to the output stream provided.

Parameters
in:Sequence

sequence containing MIDI data to be written to the file

fileType:int

the file type of the file to be written to the output stream

out:OutputStream

stream to which the file data should be written

Returns:int

the number of bytes written to the output stream

Exceptions
IOException:
if an I/O exception occurs
IllegalArgumentException:
if the file format is not supported by the system
NullPointerException:
if in or out are null
See Also
isFileTypeSupported(int, Sequence), getMidiFileTypes(Sequence)
writeback to summary
public static int write(final Sequence in, final int type, final File out) throws IOException

Writes a stream of bytes representing a file of the MIDI file type indicated to the external file provided.

Parameters
in:Sequence

sequence containing MIDI data to be written to the file

type:int

the file type of the file to be written to the output stream

out:File

external file to which the file data should be written

Returns:int

the number of bytes written to the file

Exceptions
IOException:
if an I/O exception occurs
IllegalArgumentException:
if the file type is not supported by the system
NullPointerException:
if in or out are null
See Also
isFileTypeSupported(int, Sequence), getMidiFileTypes(Sequence)