jdk/src/share/classes/javax/sound/midi/MidiSystem.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 19207 3448b0cb4077
child 22584 eed64ee05369
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 15263
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sound.midi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.midi.spi.MidiFileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.sound.midi.spi.MidiFileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.midi.spi.SoundbankReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.midi.spi.MidiDeviceProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.media.sound.JDK13Services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.media.sound.ReferenceCountingDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.media.sound.AutoConnectSequencer;
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
    50
import com.sun.media.sound.MidiDeviceReceiverEnvelope;
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
    51
import com.sun.media.sound.MidiDeviceTransmitterEnvelope;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The <code>MidiSystem</code> class provides access to the installed MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * system resources, including devices such as synthesizers, sequencers, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * MIDI input and output ports.  A typical simple MIDI application might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * begin by invoking one or more <code>MidiSystem</code> methods to learn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * what devices are installed and to obtain the ones needed in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The class also has methods for reading files, streams, and  URLs that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * contain standard MIDI file data or soundbanks.  You can query the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>MidiSystem</code> for the format of a specified MIDI file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * You cannot instantiate a <code>MidiSystem</code>; all the methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * static.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>Properties can be used to specify default MIDI devices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Both system properties and a properties file are considered.
15263
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 9232
diff changeset
    71
 * The <code>sound.properties</code> properties file is read from
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 9232
diff changeset
    72
 * an implementation-specific location (typically it is the <code>lib</code>
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 9232
diff changeset
    73
 * directory in the Java installation directory).
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 9232
diff changeset
    74
 * If a property exists both as a system property and in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * properties file, the system property takes precedence. If none is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * specified, a suitable default is chosen among the available devices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * The syntax of the properties file is specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * {@link java.util.Properties#load(InputStream) Properties.load}. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * following table lists the available property keys and which methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * consider them:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <table border=0>
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 15263
diff changeset
    83
 *  <caption>MIDI System Property Keys</caption>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   <th>Property Key</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   <th>Interface</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   <th>Affected Method</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   <td><code>javax.sound.midi.Receiver</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <td>{@link Receiver}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <td>{@link #getReceiver}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   <td><code>javax.sound.midi.Sequencer</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   <td>{@link Sequencer}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   <td>{@link #getSequencer}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <td><code>javax.sound.midi.Synthesizer</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   <td>{@link Synthesizer}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   <td>{@link #getSynthesizer}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *  <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   <td><code>javax.sound.midi.Transmitter</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   <td>{@link Transmitter}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   <td>{@link #getTransmitter}</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *  </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * The property value consists of the provider class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * and the device name, separated by the hash mark (&quot;#&quot;).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * The provider class name is the fully-qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * name of a concrete {@link javax.sound.midi.spi.MidiDeviceProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * MIDI device provider} class. The device name is matched against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * the <code>String</code> returned by the <code>getName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * method of <code>MidiDevice.Info</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Either the class name, or the device name may be omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * If only the class name is specified, the trailing hash mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * is optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p>If the provider class is specified, and it can be
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19207
diff changeset
   123
 * successfully retrieved from the installed providers,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <code>MidiDevice.Info</code> objects is retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * from the provider. Otherwise, or when these devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * do not provide a subsequent match, the list is retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * from {@link #getMidiDeviceInfo} to contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * all available <code>MidiDevice.Info</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <p>If a device name is specified, the resulting list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <code>MidiDevice.Info</code> objects is searched:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * the first one with a matching name, and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <code>MidiDevice</code> implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * respective interface, will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * If no matching <code>MidiDevice.Info</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * is found, or the device name is not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * the first suitable device from the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * list will be returned. For Sequencer and Synthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * a device is suitable if it implements the respective
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * interface; whereas for Receiver and Transmitter, a device is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * suitable if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * implements neither Sequencer nor Synthesizer and provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * at least one Receiver or Transmitter, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * For example, the property <code>javax.sound.midi.Receiver</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * with a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <code>&quot;com.sun.media.sound.MidiProvider#SunMIDI1&quot;</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * will have the following consequences when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>getReceiver</code> is called:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * if the class <code>com.sun.media.sound.MidiProvider</code> exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * in the list of installed MIDI device providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * the first <code>Receiver</code> device with name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <code>&quot;SunMIDI1&quot;</code> will be returned. If it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * be found, the first <code>Receiver</code> from that provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * will be returned, regardless of name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * If there is none, the first <code>Receiver</code> with name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <code>&quot;SunMIDI1&quot;</code> in the list of all devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * (as returned by <code>getMidiDeviceInfo</code>) will be returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * or, if not found, the first <code>Receiver</code> that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * be found in the list of all devices is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * If that fails, too, a <code>MidiUnavailableException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
public class MidiSystem {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Private no-args constructor for ensuring against instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private MidiSystem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Obtains an array of information objects representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the set of all MIDI devices available on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * A returned information object can then be used to obtain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * corresponding device object, by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * {@link #getMidiDevice(MidiDevice.Info) getMidiDevice}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return an array of <code>MidiDevice.Info</code> objects, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * for each installed MIDI device.  If no such devices are installed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * an array of length 0 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static MidiDevice.Info[] getMidiDeviceInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        List allInfos = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        List providers = getMidiDeviceProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            MidiDevice.Info[] tmpinfo = provider.getDeviceInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int j = 0; j < tmpinfo.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                allInfos.add( tmpinfo[j] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        MidiDevice.Info[] infosArray = (MidiDevice.Info[]) allInfos.toArray(new MidiDevice.Info[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return infosArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Obtains the requested MIDI device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param info a device information object representing the desired device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the requested device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws MidiUnavailableException if the requested device is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * due to resource restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws IllegalArgumentException if the info object does not represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * a MIDI device installed on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see #getMidiDeviceInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public static MidiDevice getMidiDevice(MidiDevice.Info info) throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        List providers = getMidiDeviceProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (provider.isDeviceSupported(info)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                MidiDevice device = provider.getDevice(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throw new IllegalArgumentException("Requested device not installed: " + info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Obtains a MIDI receiver from an external MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * or other default device.
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   233
     * The returned receiver always implements
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   234
     * the {@code MidiDeviceReceiver} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <code>javax.sound.midi.Receiver</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * is defined or it is defined in the file &quot;sound.properties&quot;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * it is used to identify the device that provides the default receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * For details, refer to the {@link MidiSystem class description}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * If a suitable MIDI port is not available, the Receiver is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * retrieved from an installed synthesizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
9214
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   245
     * <p>If a native receiver provided by the default device does not implement
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   246
     * the {@code MidiDeviceReceiver} interface, it will be wrapped in a
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   247
     * wrapper class that implements the {@code MidiDeviceReceiver} interface.
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   248
     * The corresponding {@code Receiver} method calls will be forwarded
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   249
     * to the native receiver.
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   250
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>If this method returns successfully, the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * javax.sound.midi.MidiDevice MidiDevice} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <code>Receiver</code> belongs to is opened implicitly, if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * not already open. It is possible to close an implicitly opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * device by calling {@link javax.sound.midi.Receiver#close close}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * on the returned <code>Receiver</code>. All open <code>Receiver</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * instances have to be closed in order to release system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * hold by the <code>MidiDevice</code>. For a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * detailed description of open/close behaviour see the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * description of {@link javax.sound.midi.MidiDevice MidiDevice}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return the default MIDI receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws MidiUnavailableException if the default receiver is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         available due to resource restrictions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         or no device providing receivers is installed in the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public static Receiver getReceiver() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        MidiDevice device = getDefaultDeviceWrapper(Receiver.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Receiver receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (device instanceof ReferenceCountingDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            receiver = ((ReferenceCountingDevice) device).getReceiverReferenceCounting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            receiver = device.getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   277
        if (!(receiver instanceof MidiDeviceReceiver)) {
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   278
            receiver = new MidiDeviceReceiverEnvelope(device, receiver);
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   279
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Obtains a MIDI transmitter from an external MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * or other default source.
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   287
     * The returned transmitter always implements
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   288
     * the {@code MidiDeviceTransmitter} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <code>javax.sound.midi.Transmitter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * is defined or it is defined in the file &quot;sound.properties&quot;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * it is used to identify the device that provides the default transmitter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * For details, refer to the {@link MidiSystem class description}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
9214
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   296
     * <p>If a native transmitter provided by the default device does not implement
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   297
     * the {@code MidiDeviceTransmitter} interface, it will be wrapped in a
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   298
     * wrapper class that implements the {@code MidiDeviceTransmitter} interface.
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   299
     * The corresponding {@code Transmitter} method calls will be forwarded
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   300
     * to the native transmitter.
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   301
     *
237acaa608d5 7009127: [Spec clarification request] Wrapping the devices retrieved from MidiDeviceProvider
amenkov
parents: 8528
diff changeset
   302
     * <p>If this method returns successfully, the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * javax.sound.midi.MidiDevice MidiDevice} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <code>Transmitter</code> belongs to is opened implicitly, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * is not already open. It is possible to close an implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * opened device by calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * javax.sound.midi.Transmitter#close close} on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <code>Transmitter</code>. All open <code>Transmitter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * instances have to be closed in order to release system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * hold by the <code>MidiDevice</code>. For a detailed description
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * of open/close behaviour see the class description of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * javax.sound.midi.MidiDevice MidiDevice}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return the default MIDI transmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws MidiUnavailableException if the default transmitter is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *         available due to resource restrictions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         or no device providing transmitters is installed in the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public static Transmitter getTransmitter() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        MidiDevice device = getDefaultDeviceWrapper(Transmitter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Transmitter transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (device instanceof ReferenceCountingDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            transmitter = ((ReferenceCountingDevice) device).getTransmitterReferenceCounting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            transmitter = device.getTransmitter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
7791
7630450ac41e 7006997: A typo in MidiSystem.getTransmitter() implementation code
amenkov
parents: 6508
diff changeset
   328
        if (!(transmitter instanceof MidiDeviceTransmitter)) {
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   329
            transmitter = new MidiDeviceTransmitterEnvelope(device, transmitter);
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   330
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Obtains the default synthesizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>javax.sound.midi.Synthesizer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * is defined or it is defined in the file &quot;sound.properties&quot;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * it is used to identify the default synthesizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * For details, refer to the {@link MidiSystem class description}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return the default synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws MidiUnavailableException if the synthesizer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         available due to resource restrictions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *         or no synthesizer is installed in the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public static Synthesizer getSynthesizer() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return (Synthesizer) getDefaultDeviceWrapper(Synthesizer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Obtains the default <code>Sequencer</code>, connected to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * a default device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * The returned <code>Sequencer</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * connected to the default <code>Synthesizer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * as returned by {@link #getSynthesizer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * If there is no <code>Synthesizer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * available, or the default <code>Synthesizer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * cannot be opened, the <code>sequencer</code> is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * to the default <code>Receiver</code>, as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * by {@link #getReceiver}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * The connection is made by retrieving a <code>Transmitter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * instance from the <code>Sequencer</code> and setting its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <code>Receiver</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Closing and re-opening the sequencer will restore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * connection to the default device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>This method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <code>getSequencer(true)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <p>If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>javax.sound.midi.Sequencer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * is defined or it is defined in the file &quot;sound.properties&quot;,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * it is used to identify the default sequencer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * For details, refer to the {@link MidiSystem class description}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return the default sequencer, connected to a default Receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @throws MidiUnavailableException if the sequencer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *         available due to resource restrictions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *         or there is no <code>Receiver</code> available by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *         installed <code>MidiDevice</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *         or no sequencer is installed in the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see #getSequencer(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see #getSynthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @see #getReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public static Sequencer getSequencer() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return getSequencer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Obtains the default <code>Sequencer</code>, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * connected to a default device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p>If <code>connected</code> is true, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>Sequencer</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * connected to the default <code>Synthesizer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * as returned by {@link #getSynthesizer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * If there is no <code>Synthesizer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * available, or the default <code>Synthesizer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * cannot be opened, the <code>sequencer</code> is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * to the default <code>Receiver</code>, as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * by {@link #getReceiver}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * The connection is made by retrieving a <code>Transmitter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * instance from the <code>Sequencer</code> and setting its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <code>Receiver</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Closing and re-opening the sequencer will restore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * connection to the default device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>If <code>connected</code> is false, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <code>Sequencer</code> instance is not connected, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * has no open <code>Transmitters</code>. In order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * play the sequencer on a MIDI device, or a <code>Synthesizer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * it is necessary to get a <code>Transmitter</code> and set its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <code>Receiver</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <code>javax.sound.midi.Sequencer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * is defined or it is defined in the file "sound.properties",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * it is used to identify the default sequencer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * For details, refer to the {@link MidiSystem class description}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 15263
diff changeset
   429
     * @param connected whether or not the returned {@code Sequencer}
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 15263
diff changeset
   430
     * is connected to the default {@code Synthesizer}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return the default sequencer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @throws MidiUnavailableException if the sequencer is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *         available due to resource restrictions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         or no sequencer is installed in the system,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *         or if <code>connected</code> is true, and there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         no <code>Receiver</code> available by any installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         <code>MidiDevice</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @see #getSynthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see #getReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public static Sequencer getSequencer(boolean connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        Sequencer seq = (Sequencer) getDefaultDeviceWrapper(Sequencer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // IMPORTANT: this code needs to be synch'ed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            //            all AutoConnectSequencer instances,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            //            (e.g. RealTimeSequencer) because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            //            same algorithm for synth retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            //            needs to be used!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Receiver rec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            MidiUnavailableException mue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // first try to connect to the default synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                Synthesizer synth = getSynthesizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                if (synth instanceof ReferenceCountingDevice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    rec = ((ReferenceCountingDevice) synth).getReceiverReferenceCounting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    synth.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        rec = synth.getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        // make sure that the synth is properly closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        if (rec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            synth.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } catch (MidiUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                // something went wrong with synth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (e instanceof MidiUnavailableException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    mue = (MidiUnavailableException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (rec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                // then try to connect to the default Receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    rec = MidiSystem.getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // something went wrong. Nothing to do then!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    if (e instanceof MidiUnavailableException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        mue = (MidiUnavailableException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (rec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                seq.getTransmitter().setReceiver(rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (seq instanceof AutoConnectSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    ((AutoConnectSequencer) seq).setAutoConnect(rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                if (mue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    throw mue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                throw new MidiUnavailableException("no receiver available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Constructs a MIDI sound bank by reading it from the specified stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * The stream must point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * a valid MIDI soundbank file.  In general, MIDI soundbank providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * support the stream, and, if not, reset the stream's read pointer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * its original position.  If the input stream does not support this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * this method may fail with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param stream the source of the sound bank data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @throws InvalidMidiDataException if the stream does not point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * valid MIDI soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public static Soundbank getSoundbank(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            s = sp.getSoundbank(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Constructs a <code>Soundbank</code> by reading it from the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * The URL must point to a valid MIDI soundbank file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param url the source of the sound bank data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public static Soundbank getSoundbank(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            s = sp.getSoundbank(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Constructs a <code>Soundbank</code> by reading it from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * The <code>File</code> must point to a valid MIDI soundbank file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param file the source of the sound bank data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws InvalidMidiDataException if the <code>File</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * point to valid MIDI soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public static Soundbank getSoundbank(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            s = sp.getSoundbank(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Obtains the MIDI file format of the data in the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * The stream must point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * This method and/or the code it invokes may need to read some data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * the stream to determine whether its data format is supported.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * implementation may therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * need to mark the stream, read enough data to determine whether it is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * a supported format, and reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * position.  If the input stream does not permit this set of operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * this method may fail with an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param stream the input stream from which file format information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * should be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return an <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @throws InvalidMidiDataException if the stream does not point to valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @throws IOException if an I/O exception occurs while accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @see #getMidiFileFormat(URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @see #getMidiFileFormat(File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public static MidiFileFormat getMidiFileFormat(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                format = reader.getMidiFileFormat( stream ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            throw new InvalidMidiDataException("input stream is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Obtains the MIDI file format of the data in the specified URL.  The URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * must point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param url the URL from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return a <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @throws IOException if an I/O exception occurs while accessing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @see #getMidiFileFormat(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @see #getMidiFileFormat(File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public static MidiFileFormat getMidiFileFormat(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                format = reader.getMidiFileFormat( url ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            throw new InvalidMidiDataException("url is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Obtains the MIDI file format of the specified <code>File</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <code>File</code> must point to valid MIDI file data for a file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * recognized by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param file the <code>File</code> from which file format information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * should be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @return a <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @throws InvalidMidiDataException if the <code>File</code> does not point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *  to valid MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @throws IOException if an I/O exception occurs while accessing the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @see #getMidiFileFormat(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @see #getMidiFileFormat(URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public static MidiFileFormat getMidiFileFormat(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                format = reader.getMidiFileFormat( file ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            throw new InvalidMidiDataException("file is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Obtains a MIDI sequence from the specified input stream.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * This method and/or the code it invokes may need to read some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * from the stream to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * its data format is supported.  The implementation may therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * need to mark the stream, read enough data to determine whether it is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * a supported format, and reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * position.  If the input stream does not permit this set of operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * this method may fail with an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param stream the input stream from which the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * should be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * contained in the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @throws InvalidMidiDataException if the stream does not point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * valid MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @throws IOException if an I/O exception occurs while accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public static Sequence getSequence(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                sequence = reader.getSequence( stream ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            throw new InvalidMidiDataException("could not get sequence from input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Obtains a MIDI sequence from the specified URL.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @param url the URL from which the <code>Sequence</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * pointed to by the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @throws IOException if an I/O exception occurs while accessing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    public static Sequence getSequence(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                sequence = reader.getSequence( url ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            throw new InvalidMidiDataException("could not get sequence from URL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Obtains a MIDI sequence from the specified <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * The <code>File</code> must point to valid MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * for a file type recognized by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @param file the <code>File</code> from which the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * should be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * pointed to by the File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @throws InvalidMidiDataException if the File does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    public static Sequence getSequence(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                sequence = reader.getSequence( file ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            throw new InvalidMidiDataException("could not get sequence from file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * Obtains the set of MIDI file types for which file writing support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * provided by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @return array of unique file types.  If no file types are supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * an array of length 0 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public static int[] getMidiFileTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        Set allTypes = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // gather from all the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            int[] types = writer.getMidiFileTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            for (int j = 0; j < types.length; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                allTypes.add(new Integer(types[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        int resultTypes[] = new int[allTypes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        Iterator iterator = allTypes.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            Integer integer = (Integer) iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            resultTypes[index++] = integer.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        return resultTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Indicates whether file writing support for the specified MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * is provided by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @param fileType the file type for which write capabilities are queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @return <code>true</code> if the file type is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    public static boolean isFileTypeSupported(int fileType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if( writer.isFileTypeSupported(fileType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * Obtains the set of MIDI file types that the system can write from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * sequence specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param sequence the sequence for which MIDI file type support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * is queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @return the set of unique supported file types.  If no file types are supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    public static int[] getMidiFileTypes(Sequence sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        Set allTypes = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // gather from all the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            int[] types = writer.getMidiFileTypes(sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            for (int j = 0; j < types.length; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                allTypes.add(new Integer(types[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        int resultTypes[] = new int[allTypes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        Iterator iterator = allTypes.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            Integer integer = (Integer) iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            resultTypes[index++] = integer.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        return resultTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Indicates whether a MIDI file of the file type specified can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * from the sequence indicated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @param fileType the file type for which write capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * are queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param sequence the sequence for which file writing support is queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @return <code>true</code> if the file type is supported for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * sequence, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public static boolean isFileTypeSupported(int fileType, Sequence sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if( writer.isFileTypeSupported(fileType,sequence)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * Writes a stream of bytes representing a file of the MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * indicated to the output stream provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @param in sequence containing MIDI data to be written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @param fileType the file type of the file to be written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @param out stream to which the file data should be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @return the number of bytes written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @throws IllegalArgumentException if the file format is not supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @see #isFileTypeSupported(int, Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @see     #getMidiFileTypes(Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    public static int write(Sequence in, int fileType, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        //$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        int bytesWritten = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if( writer.isFileTypeSupported( fileType, in ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                bytesWritten = writer.write(in, fileType, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (bytesWritten == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            throw new IllegalArgumentException("MIDI file type is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * Writes a stream of bytes representing a file of the MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * indicated to the external file provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @param in sequence containing MIDI data to be written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @param type the file type of the file to be written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @param out external file to which the file data should be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @return the number of bytes written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @throws IllegalArgumentException if the file type is not supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @see #isFileTypeSupported(int, Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @see     #getMidiFileTypes(Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    public static int write(Sequence in, int type, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        //$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        int bytesWritten = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if( writer.isFileTypeSupported( type, in ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                bytesWritten = writer.write(in, type, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (bytesWritten == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            throw new IllegalArgumentException("MIDI file type is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private static List getMidiDeviceProviders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        return getProviders(MidiDeviceProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    private static List getSoundbankReaders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return getProviders(SoundbankReader.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    private static List getMidiFileWriters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        return getProviders(MidiFileWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    private static List getMidiFileReaders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return getProviders(MidiFileReader.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /** Attempts to locate and return a default MidiDevice of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * This method wraps {@link #getDefaultDevice}. It catches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * <code>IllegalArgumentException</code> thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * <code>getDefaultDevice</code> and instead throws a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <code>MidiUnavailableException</code>, with the catched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * exception chained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @throws  MidiUnavalableException on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    private static MidiDevice getDefaultDeviceWrapper(Class deviceClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        throws MidiUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            return getDefaultDevice(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            MidiUnavailableException mae = new MidiUnavailableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            mae.initCause(iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            throw mae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /** Attempts to locate and return a default MidiDevice of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @throws  IllegalArgumentException on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    private static MidiDevice getDefaultDevice(Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        List providers = getMidiDeviceProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        String providerClassName = JDK13Services.getDefaultProviderClassName(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        String instanceName = JDK13Services.getDefaultInstanceName(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        if (providerClassName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            MidiDeviceProvider defaultProvider = getNamedProvider(providerClassName, providers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            if (defaultProvider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    device = getNamedDevice(instanceName, defaultProvider, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                        return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                device = getFirstDevice(defaultProvider, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        /* Provider class not specified or cannot be found, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
           provider class specified, and no appropriate device available or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
           provider class and instance specified and instance cannot be found or is not appropriate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            device = getNamedDevice(instanceName, providers, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        /* No default are specified, or if something is specified, everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
           failed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        device = getFirstDevice(providers, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        throw new IllegalArgumentException("Requested device not installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    /** Return a MidiDeviceProcider of a given class from the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        @param providerClassName The class name of the provider to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        @param provider The list of MidiDeviceProviders that is searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        @return A MidiDeviceProvider of the requested class, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private static MidiDeviceProvider getNamedProvider(String providerClassName, List providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            if (provider.getClass().getName().equals(providerClassName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    /** Return a MidiDevice with a given name from a given MidiDeviceProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        @return A MidiDevice matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                                             MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        device = getNamedDevice(deviceName, provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                 false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            device = getNamedDevice(deviceName, provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                     true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    /** Return a MidiDevice with a given name from a given MidiDeviceProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
      @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
      @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
      @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
      Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
      @return A MidiDevice matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                             MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        MidiDevice.Info[] infos = provider.getDeviceInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        for (int i = 0; i < infos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (infos[i].getName().equals(deviceName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                MidiDevice device = provider.getDevice(infos[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                if (isAppropriateDevice(device, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                                        allowSynthesizer, allowSequencer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /** Return a MidiDevice with a given name from a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        @param providers The List of MidiDeviceProviders to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        @return A Mixer matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                             List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        device = getNamedDevice(deviceName, providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                                 false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            device = getNamedDevice(deviceName, providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                                     true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    /** Return a MidiDevice with a given name from a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        @param providers The List of MidiDeviceProviders to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        @return A Mixer matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                                             List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            MidiDevice device = getNamedDevice(deviceName, provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                               deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                                               allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                               allowSequencer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /** From a given MidiDeviceProvider, return the first appropriate device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        @return A MidiDevice is considered appropriate, or null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        appropriate device is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    private static MidiDevice getFirstDevice(MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                                    true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    /** From a given MidiDeviceProvider, return the first appropriate device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        @return A MidiDevice is considered appropriate, or null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        appropriate device is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    private static MidiDevice getFirstDevice(MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        MidiDevice.Info[] infos = provider.getDeviceInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        for (int j = 0; j < infos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            MidiDevice device = provider.getDevice(infos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            if (isAppropriateDevice(device, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                                    allowSynthesizer, allowSequencer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /** From a List of MidiDeviceProviders, return the first appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        MidiDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        @param providers The List of MidiDeviceProviders to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        @return A MidiDevice that is considered appropriate, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    private static MidiDevice getFirstDevice(List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        device = getFirstDevice(providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            device = getFirstDevice(providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                                    true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /** From a List of MidiDeviceProviders, return the first appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        MidiDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        @param providers The List of MidiDeviceProviders to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        @return A MidiDevice that is considered appropriate, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    private static MidiDevice getFirstDevice(List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            MidiDevice device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                               allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                               allowSequencer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /** Checks if a MidiDevice is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        If deviceClass is Synthesizer or Sequencer, a device implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        the respective interface is considered appropriate. If deviceClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        is Receiver or Transmitter, a device is considered appropriate if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        it implements neither Synthesizer nor Transmitter, and if it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        provide at least one Receiver or Transmitter, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        @param device the MidiDevice to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        @param allowSynthesizer if true, Synthesizers are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        appropriate. Otherwise only pure MidiDevices are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        appropriate (unless allowSequencer is true). This flag only has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        effect for deviceClass Receiver and Transmitter. For other device
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        classes (Sequencer and Synthesizer), this flag has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        @param allowSequencer if true, Sequencers are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        appropriate. Otherwise only pure MidiDevices are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        appropriate (unless allowSynthesizer is true). This flag only has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        effect for deviceClass Receiver and Transmitter. For other device
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        classes (Sequencer and Synthesizer), this flag has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        @return true if the device is considered appropriate according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        rules given above, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    private static boolean isAppropriateDevice(MidiDevice device,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                                               Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                                               boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                                               boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        if (deviceClass.isInstance(device)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            // This clause is for deviceClass being either Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            // or Sequencer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            // Now the case that deviceClass is Transmitter or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            // Receiver. If neither allowSynthesizer nor allowSequencer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            // true, we require device instances to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            // neither Synthesizer nor Sequencer, since we only want
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            // devices representing MIDI ports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            // Otherwise, the respective type is accepted, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            if ( (! (device instanceof Sequencer) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                  ! (device instanceof Synthesizer) ) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                 ((device instanceof Sequencer) && allowSequencer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                 ((device instanceof Synthesizer) && allowSynthesizer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                // And of cource, the device has to be able to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                // Receivers or Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                if ((deviceClass == Receiver.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                     device.getMaxReceivers() != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    (deviceClass == Transmitter.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                     device.getMaxTransmitters() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * Obtains the set of services currently installed on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * using sun.misc.Service, the SPI mechanism in 1.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * @return a List of instances of providers for the requested service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * If no providers are available, a List of length 0 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    private static List getProviders(Class providerClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        return JDK13Services.getProviders(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
}