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