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