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