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
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.
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:
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
class. The device name is matched against
the
.
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 #getMidiDeviceInfo to contain
all available MidiDevice.Info
objects.
Method from javax.sound.midi.MidiSystem Detail: |
public static MidiDevice getMidiDevice(Info info) throws MidiUnavailableException {
List providers = getMidiDeviceProviders();
for(int i = 0; i < providers.size(); i++) {
MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
if (provider.isDeviceSupported(info)) {
MidiDevice device = provider.getDevice(info);
return device;
}
}
throw new IllegalArgumentException("Requested device not installed: " + info);
}
Obtains the requested MIDI device. |
public static Info[] getMidiDeviceInfo() {
List allInfos = new ArrayList();
List providers = getMidiDeviceProviders();
for(int i = 0; i < providers.size(); i++) {
MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
MidiDevice.Info[] tmpinfo = provider.getDeviceInfo();
for (int j = 0; j < tmpinfo.length; j++) {
allInfos.add( tmpinfo[j] );
}
}
MidiDevice.Info[] infosArray = (MidiDevice.Info[]) allInfos.toArray(new MidiDevice.Info[0]);
return infosArray;
}
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 . |
public static MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
MidiFileFormat format = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
format = reader.getMidiFileFormat( stream ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( format==null ) {
throw new InvalidMidiDataException("input stream is not a supported file type");
} else {
return format;
}
}
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. |
public static MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
MidiFileFormat format = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
format = reader.getMidiFileFormat( url ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( format==null ) {
throw new InvalidMidiDataException("url is not a supported file type");
} else {
return format;
}
}
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. |
public static MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
MidiFileFormat format = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
format = reader.getMidiFileFormat( file ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( format==null ) {
throw new InvalidMidiDataException("file is not a supported file type");
} else {
return format;
}
}
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. |
public static int[] getMidiFileTypes() {
List providers = getMidiFileWriters();
Set allTypes = new HashSet();
// gather from all the providers
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
int[] types = writer.getMidiFileTypes();
for (int j = 0; j < types.length; j++ ) {
allTypes.add(new Integer(types[j]));
}
}
int resultTypes[] = new int[allTypes.size()];
int index = 0;
Iterator iterator = allTypes.iterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
resultTypes[index++] = integer.intValue();
}
return resultTypes;
}
Obtains the set of MIDI file types for which file writing support is
provided by the system. |
public static int[] getMidiFileTypes(Sequence sequence) {
List providers = getMidiFileWriters();
Set allTypes = new HashSet();
// gather from all the providers
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
int[] types = writer.getMidiFileTypes(sequence);
for (int j = 0; j < types.length; j++ ) {
allTypes.add(new Integer(types[j]));
}
}
int resultTypes[] = new int[allTypes.size()];
int index = 0;
Iterator iterator = allTypes.iterator();
while (iterator.hasNext()) {
Integer integer = (Integer) iterator.next();
resultTypes[index++] = integer.intValue();
}
return resultTypes;
}
Obtains the set of MIDI file types that the system can write from the
sequence specified. |
public static Receiver getReceiver() throws MidiUnavailableException {
// may throw MidiUnavailableException
MidiDevice device = getDefaultDeviceWrapper(Receiver.class);
Receiver receiver;
if (device instanceof ReferenceCountingDevice) {
receiver = ((ReferenceCountingDevice) device).getReceiverReferenceCounting();
} else {
receiver = device.getReceiver();
}
if (!(receiver instanceof MidiDeviceReceiver)) {
receiver = new MidiDeviceReceiverEnvelope(device, receiver);
}
return receiver;
}
Obtains a MIDI receiver from an external MIDI port
or other default device.
The returned receiver always implements
the {@code 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 {@code MidiDeviceReceiver} interface, it will be wrapped in a
wrapper class that implements the {@code MidiDeviceReceiver} interface.
The corresponding {@code 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 . |
public static Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
Sequence sequence = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
sequence = reader.getSequence( stream ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( sequence==null ) {
throw new InvalidMidiDataException("could not get sequence from input stream");
} else {
return sequence;
}
}
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. |
public static Sequence getSequence(URL url) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
Sequence sequence = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
sequence = reader.getSequence( url ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( sequence==null ) {
throw new InvalidMidiDataException("could not get sequence from URL");
} else {
return sequence;
}
}
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. |
public static Sequence getSequence(File file) throws InvalidMidiDataException, IOException {
List providers = getMidiFileReaders();
Sequence sequence = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = (MidiFileReader) providers.get(i);
try {
sequence = reader.getSequence( file ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( sequence==null ) {
throw new InvalidMidiDataException("could not get sequence from file");
} else {
return sequence;
}
}
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. |
public static Sequencer getSequencer() throws MidiUnavailableException {
return getSequencer(true);
}
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 . |
public static Sequencer getSequencer(boolean connected) throws MidiUnavailableException {
Sequencer seq = (Sequencer) getDefaultDeviceWrapper(Sequencer.class);
if (connected) {
// IMPORTANT: this code needs to be synch'ed with
// all AutoConnectSequencer instances,
// (e.g. RealTimeSequencer) because the
// same algorithm for synth retrieval
// needs to be used!
Receiver rec = null;
MidiUnavailableException mue = null;
// first try to connect to the default synthesizer
try {
Synthesizer synth = getSynthesizer();
if (synth instanceof ReferenceCountingDevice) {
rec = ((ReferenceCountingDevice) synth).getReceiverReferenceCounting();
} else {
synth.open();
try {
rec = synth.getReceiver();
} finally {
// make sure that the synth is properly closed
if (rec == null) {
synth.close();
}
}
}
} catch (MidiUnavailableException e) {
// something went wrong with synth
if (e instanceof MidiUnavailableException) {
mue = (MidiUnavailableException) e;
}
}
if (rec == null) {
// then try to connect to the default Receiver
try {
rec = MidiSystem.getReceiver();
} catch (Exception e) {
// something went wrong. Nothing to do then!
if (e instanceof MidiUnavailableException) {
mue = (MidiUnavailableException) e;
}
}
}
if (rec != null) {
seq.getTransmitter().setReceiver(rec);
if (seq instanceof AutoConnectSequencer) {
((AutoConnectSequencer) seq).setAutoConnect(rec);
}
} else {
if (mue != null) {
throw mue;
}
throw new MidiUnavailableException("no receiver available");
}
}
return seq;
}
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 . |
public static Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException {
SoundbankReader sp = null;
Soundbank s = null;
List providers = getSoundbankReaders();
for(int i = 0; i < providers.size(); i++) {
sp = (SoundbankReader)providers.get(i);
s = sp.getSoundbank(stream);
if( s!= null) {
return s;
}
}
throw new InvalidMidiDataException("cannot get soundbank from stream");
}
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. |
public static Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException {
SoundbankReader sp = null;
Soundbank s = null;
List providers = getSoundbankReaders();
for(int i = 0; i < providers.size(); i++) {
sp = (SoundbankReader)providers.get(i);
s = sp.getSoundbank(url);
if( s!= null) {
return s;
}
}
throw new InvalidMidiDataException("cannot get soundbank from stream");
}
Constructs a Soundbank by reading it from the specified URL.
The URL must point to a valid MIDI soundbank file. |
public static Soundbank getSoundbank(File file) throws InvalidMidiDataException, IOException {
SoundbankReader sp = null;
Soundbank s = null;
List providers = getSoundbankReaders();
for(int i = 0; i < providers.size(); i++) {
sp = (SoundbankReader)providers.get(i);
s = sp.getSoundbank(file);
if( s!= null) {
return s;
}
}
throw new InvalidMidiDataException("cannot get soundbank from stream");
}
Constructs a Soundbank by reading it from the specified
File .
The File must point to a valid MIDI soundbank file. |
public static Synthesizer getSynthesizer() throws MidiUnavailableException {
// may throw MidiUnavailableException
return (Synthesizer) getDefaultDeviceWrapper(Synthesizer.class);
}
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 . |
public static Transmitter getTransmitter() throws MidiUnavailableException {
// may throw MidiUnavailableException
MidiDevice device = getDefaultDeviceWrapper(Transmitter.class);
Transmitter transmitter;
if (device instanceof ReferenceCountingDevice) {
transmitter = ((ReferenceCountingDevice) device).getTransmitterReferenceCounting();
} else {
transmitter = device.getTransmitter();
}
if (!(transmitter instanceof MidiDeviceTransmitter)) {
transmitter = new MidiDeviceTransmitterEnvelope(device, transmitter);
}
return transmitter;
}
Obtains a MIDI transmitter from an external MIDI port
or other default source.
The returned transmitter always implements
the {@code 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 {@code MidiDeviceTransmitter} interface, it will be wrapped in a
wrapper class that implements the {@code MidiDeviceTransmitter} interface.
The corresponding {@code 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 . |
public static boolean isFileTypeSupported(int fileType) {
List providers = getMidiFileWriters();
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
if( writer.isFileTypeSupported(fileType)) {
return true;
}
}
return false;
}
Indicates whether file writing support for the specified MIDI file type
is provided by the system. |
public static boolean isFileTypeSupported(int fileType,
Sequence sequence) {
List providers = getMidiFileWriters();
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
if( writer.isFileTypeSupported(fileType,sequence)) {
return true;
}
}
return false;
}
Indicates whether a MIDI file of the file type specified can be written
from the sequence indicated. |
public static int write(Sequence in,
int fileType,
OutputStream out) throws IOException {
List providers = getMidiFileWriters();
//$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
int bytesWritten = -2;
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
if( writer.isFileTypeSupported( fileType, in ) ) {
bytesWritten = writer.write(in, fileType, out);
break;
}
}
if (bytesWritten == -2) {
throw new IllegalArgumentException("MIDI file type is not supported");
}
return bytesWritten;
}
Writes a stream of bytes representing a file of the MIDI file type
indicated to the output stream provided. |
public static int write(Sequence in,
int type,
File out) throws IOException {
List providers = getMidiFileWriters();
//$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
int bytesWritten = -2;
for (int i = 0; i < providers.size(); i++ ) {
MidiFileWriter writer = (MidiFileWriter) providers.get(i);
if( writer.isFileTypeSupported( type, in ) ) {
bytesWritten = writer.write(in, type, out);
break;
}
}
if (bytesWritten == -2) {
throw new IllegalArgumentException("MIDI file type is not supported");
}
return bytesWritten;
}
Writes a stream of bytes representing a file of the MIDI file type
indicated to the external file provided. |