jdk/src/share/classes/javax/sound/midi/MidiSystem.java
author lana
Tue, 04 Jan 2011 23:41:13 -0800
changeset 7793 bcc54a3c2825
parent 7791 7630450ac41e
parent 7668 d4a77089c587
child 8528 f496950c5d98
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6508
diff changeset
     2
 * Copyright (c) 1999, 2010, 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
                    // only use MixerSynth if it could successfully load a soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    if (synth.getClass().toString().contains("com.sun.media.sound.MixerSynth")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        && (synth.getDefaultSoundbank() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        // don't use this receiver if no soundbank available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        rec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        synth.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    synth.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        rec = synth.getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        // make sure that the synth is properly closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        if (rec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            synth.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } catch (MidiUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                // something went wrong with synth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                if (e instanceof MidiUnavailableException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    mue = (MidiUnavailableException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (rec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // then try to connect to the default Receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    rec = MidiSystem.getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    // something went wrong. Nothing to do then!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    if (e instanceof MidiUnavailableException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        mue = (MidiUnavailableException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (rec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                seq.getTransmitter().setReceiver(rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (seq instanceof AutoConnectSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    ((AutoConnectSequencer) seq).setAutoConnect(rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (mue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    throw mue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                throw new MidiUnavailableException("no receiver available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Constructs a MIDI sound bank by reading it from the specified stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * The stream must point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * a valid MIDI soundbank file.  In general, MIDI soundbank providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * support the stream, and, if not, reset the stream's read pointer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * its original position.  If the input stream does not support this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * this method may fail with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param stream the source of the sound bank data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @throws InvalidMidiDataException if the stream does not point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * valid MIDI soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public static Soundbank getSoundbank(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            s = sp.getSoundbank(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Constructs a <code>Soundbank</code> by reading it from the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * The URL must point to a valid MIDI soundbank file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param url the source of the sound bank data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public static Soundbank getSoundbank(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            s = sp.getSoundbank(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Constructs a <code>Soundbank</code> by reading it from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * The <code>File</code> must point to a valid MIDI soundbank file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param file the source of the sound bank data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @return the sound bank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @throws InvalidMidiDataException if the <code>File</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * point to valid MIDI soundbank data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @throws IOException if an I/O error occurred when loading the soundbank
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public static Soundbank getSoundbank(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        SoundbankReader sp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        Soundbank s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        List providers = getSoundbankReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            sp = (SoundbankReader)providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            s = sp.getSoundbank(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if( s!= null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        throw new InvalidMidiDataException("cannot get soundbank from stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Obtains the MIDI file format of the data in the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * The stream must point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * This method and/or the code it invokes may need to read some data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * the stream to determine whether its data format is supported.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * implementation may therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * need to mark the stream, read enough data to determine whether it is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * a supported format, and reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * position.  If the input stream does not permit this set of operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * this method may fail with an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param stream the input stream from which file format information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * should be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return an <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws InvalidMidiDataException if the stream does not point to valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @throws IOException if an I/O exception occurs while accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @see #getMidiFileFormat(URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @see #getMidiFileFormat(File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public static MidiFileFormat getMidiFileFormat(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                format = reader.getMidiFileFormat( stream ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                continue;
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
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            throw new InvalidMidiDataException("input stream is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Obtains the MIDI file format of the data in the specified URL.  The URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * must point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param url the URL from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @return a <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @throws IOException if an I/O exception occurs while accessing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see #getMidiFileFormat(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @see #getMidiFileFormat(File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public static MidiFileFormat getMidiFileFormat(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                format = reader.getMidiFileFormat( url ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                continue;
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
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new InvalidMidiDataException("url is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Obtains the MIDI file format of the specified <code>File</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <code>File</code> must point to valid MIDI file data for a file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * recognized by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * is installed, but encounters errors while determining the file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @param file the <code>File</code> from which file format information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * should be extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @return a <code>MidiFileFormat</code> object describing the MIDI file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @throws InvalidMidiDataException if the <code>File</code> does not point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *  to valid MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @throws IOException if an I/O exception occurs while accessing the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @see #getMidiFileFormat(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @see #getMidiFileFormat(URL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public static MidiFileFormat getMidiFileFormat(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        MidiFileFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                format = reader.getMidiFileFormat( file ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                continue;
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
        if( format==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            throw new InvalidMidiDataException("file is not a supported file type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Obtains a MIDI sequence from the specified input stream.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * This method and/or the code it invokes may need to read some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * from the stream to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * its data format is supported.  The implementation may therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * need to mark the stream, read enough data to determine whether it is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * a supported format, and reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * position.  If the input stream does not permit this set of operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * this method may fail with an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @param stream the input stream from which the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * should be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * contained in the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @throws InvalidMidiDataException if the stream does not point to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * valid MIDI file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @throws IOException if an I/O exception occurs while accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public static Sequence getSequence(InputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                sequence = reader.getSequence( stream ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                continue;
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
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            throw new InvalidMidiDataException("could not get sequence from input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Obtains a MIDI sequence from the specified URL.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * point to valid MIDI file data for a file type recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @param url the URL from which the <code>Sequence</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * pointed to by the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @throws InvalidMidiDataException if the URL does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @throws IOException if an I/O exception occurs while accessing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public static Sequence getSequence(URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                sequence = reader.getSequence( url ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                continue;
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
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            throw new InvalidMidiDataException("could not get sequence from URL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Obtains a MIDI sequence from the specified <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * The <code>File</code> must point to valid MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * for a file type recognized by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * This operation can only succeed for files of a type which can be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * by an installed file reader.  It may fail with an InvalidMidiDataException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * even for valid files if no compatible file reader is installed.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * will also fail with an InvalidMidiDataException if a compatible file reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * is installed, but encounters errors while constructing the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * object from the file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @param file the <code>File</code> from which the <code>Sequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * should be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @return a <code>Sequence</code> object based on the MIDI file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * pointed to by the File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @throws InvalidMidiDataException if the File does not point to valid MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public static Sequence getSequence(File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        throws InvalidMidiDataException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        List providers = getMidiFileReaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            MidiFileReader reader = (MidiFileReader) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                sequence = reader.getSequence( file ); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                continue;
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
        if( sequence==null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            throw new InvalidMidiDataException("could not get sequence from file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            return sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Obtains the set of MIDI file types for which file writing support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * provided by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return array of unique file types.  If no file types are supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * an array of length 0 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public static int[] getMidiFileTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        Set allTypes = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        // gather from all the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            int[] types = writer.getMidiFileTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            for (int j = 0; j < types.length; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                allTypes.add(new Integer(types[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        int resultTypes[] = new int[allTypes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        Iterator iterator = allTypes.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            Integer integer = (Integer) iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            resultTypes[index++] = integer.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return resultTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Indicates whether file writing support for the specified MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * is provided by the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param fileType the file type for which write capabilities are queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @return <code>true</code> if the file type is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    public static boolean isFileTypeSupported(int fileType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if( writer.isFileTypeSupported(fileType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Obtains the set of MIDI file types that the system can write from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * sequence specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @param sequence the sequence for which MIDI file type support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * is queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @return the set of unique supported file types.  If no file types are supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public static int[] getMidiFileTypes(Sequence sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        Set allTypes = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        // gather from all the providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            int[] types = writer.getMidiFileTypes(sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            for (int j = 0; j < types.length; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                allTypes.add(new Integer(types[j]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        int resultTypes[] = new int[allTypes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        Iterator iterator = allTypes.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            Integer integer = (Integer) iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            resultTypes[index++] = integer.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        return resultTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * Indicates whether a MIDI file of the file type specified can be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * from the sequence indicated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @param fileType the file type for which write capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * are queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @param sequence the sequence for which file writing support is queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @return <code>true</code> if the file type is supported for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * sequence, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    public static boolean isFileTypeSupported(int fileType, Sequence sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            if( writer.isFileTypeSupported(fileType,sequence)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Writes a stream of bytes representing a file of the MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * indicated to the output stream provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @param in sequence containing MIDI data to be written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @param fileType the file type of the file to be written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @param out stream to which the file data should be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @return the number of bytes written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @throws IllegalArgumentException if the file format is not supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @see #isFileTypeSupported(int, Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @see     #getMidiFileTypes(Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public static int write(Sequence in, int fileType, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        //$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        int bytesWritten = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if( writer.isFileTypeSupported( fileType, in ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                bytesWritten = writer.write(in, fileType, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (bytesWritten == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            throw new IllegalArgumentException("MIDI file type is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Writes a stream of bytes representing a file of the MIDI file type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * indicated to the external file provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @param in sequence containing MIDI data to be written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @param type the file type of the file to be written to the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @param out external file to which the file data should be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @return the number of bytes written to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @throws IllegalArgumentException if the file type is not supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @see #isFileTypeSupported(int, Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @see     #getMidiFileTypes(Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    public static int write(Sequence in, int type, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        List providers = getMidiFileWriters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        //$$fb 2002-04-17: Fix for 4635287: Standard MidiFileWriter cannot write empty Sequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        int bytesWritten = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        for (int i = 0; i < providers.size(); i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            MidiFileWriter writer = (MidiFileWriter) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if( writer.isFileTypeSupported( type, in ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                bytesWritten = writer.write(in, type, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        if (bytesWritten == -2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            throw new IllegalArgumentException("MIDI file type is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
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
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    private static List getMidiDeviceProviders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        return getProviders(MidiDeviceProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    private static List getSoundbankReaders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        return getProviders(SoundbankReader.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private static List getMidiFileWriters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        return getProviders(MidiFileWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    private static List getMidiFileReaders() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return getProviders(MidiFileReader.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    /** Attempts to locate and return a default MidiDevice of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * This method wraps {@link #getDefaultDevice}. It catches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <code>IllegalArgumentException</code> thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * <code>getDefaultDevice</code> and instead throws a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <code>MidiUnavailableException</code>, with the catched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * exception chained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @throws  MidiUnavalableException on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    private static MidiDevice getDefaultDeviceWrapper(Class deviceClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        throws MidiUnavailableException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return getDefaultDevice(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            MidiUnavailableException mae = new MidiUnavailableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            mae.initCause(iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            throw mae;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    /** Attempts to locate and return a default MidiDevice of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @throws  IllegalArgumentException on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private static MidiDevice getDefaultDevice(Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        List providers = getMidiDeviceProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        String providerClassName = JDK13Services.getDefaultProviderClassName(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        String instanceName = JDK13Services.getDefaultInstanceName(deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (providerClassName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            MidiDeviceProvider defaultProvider = getNamedProvider(providerClassName, providers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            if (defaultProvider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    device = getNamedDevice(instanceName, defaultProvider, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                        return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                device = getFirstDevice(defaultProvider, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        /* Provider class not specified or cannot be found, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
           provider class specified, and no appropriate device available or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
           provider class and instance specified and instance cannot be found or is not appropriate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if (instanceName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            device = getNamedDevice(instanceName, providers, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        /* No default are specified, or if something is specified, everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
           failed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        device = getFirstDevice(providers, deviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        throw new IllegalArgumentException("Requested device not installed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /** Return a MidiDeviceProcider of a given class from the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        @param providerClassName The class name of the provider to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        @param provider The list of MidiDeviceProviders that is searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        @return A MidiDeviceProvider of the requested class, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    private static MidiDeviceProvider getNamedProvider(String providerClassName, List providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            if (provider.getClass().getName().equals(providerClassName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /** Return a MidiDevice with a given name from a given MidiDeviceProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        @return A MidiDevice matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                                             MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        device = getNamedDevice(deviceName, provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                                 false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            device = getNamedDevice(deviceName, provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                     true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /** Return a MidiDevice with a given name from a given MidiDeviceProvider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
      @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
      @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
      @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
      Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
      @return A MidiDevice matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                                             MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        MidiDevice.Info[] infos = provider.getDeviceInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        for (int i = 0; i < infos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (infos[i].getName().equals(deviceName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                MidiDevice device = provider.getDevice(infos[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                if (isAppropriateDevice(device, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                        allowSynthesizer, allowSequencer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    /** Return a MidiDevice with a given name from a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        @param providers The List of MidiDeviceProviders to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        @return A Mixer matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                                             List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        device = getNamedDevice(deviceName, providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                                 false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            device = getNamedDevice(deviceName, providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                                     true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    /** Return a MidiDevice with a given name from a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        MidiDeviceProviders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        @param deviceName The name of the MidiDevice to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        @param providers The List of MidiDeviceProviders to check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        @return A Mixer matching the requirements, or null if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    private static MidiDevice getNamedDevice(String deviceName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                                             List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            MidiDevice device = getNamedDevice(deviceName, provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                                               deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                                               allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                               allowSequencer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    /** From a given MidiDeviceProvider, return the first appropriate device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        @return A MidiDevice is considered appropriate, or null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        appropriate device is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    private static MidiDevice getFirstDevice(MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                                false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                    true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    /** From a given MidiDeviceProvider, return the first appropriate device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        @param provider The MidiDeviceProvider to check for MidiDevices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        @return A MidiDevice is considered appropriate, or null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        appropriate device is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    private static MidiDevice getFirstDevice(MidiDeviceProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        MidiDevice.Info[] infos = provider.getDeviceInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        for (int j = 0; j < infos.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            MidiDevice device = provider.getDevice(infos[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            if (isAppropriateDevice(device, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                                    allowSynthesizer, allowSequencer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    /** From a List of MidiDeviceProviders, return the first appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        MidiDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        @param providers The List of MidiDeviceProviders to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        @return A MidiDevice that is considered appropriate, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    private static MidiDevice getFirstDevice(List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                                             Class deviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        MidiDevice device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // try to get MIDI port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        device = getFirstDevice(providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                                false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        if (deviceClass == Receiver.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            // try to get Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            device = getFirstDevice(providers, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                    true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /** From a List of MidiDeviceProviders, return the first appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        MidiDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        @param providers The List of MidiDeviceProviders to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        @param deviceClass The requested device type, one of Synthesizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        Sequencer.class, Receiver.class or Transmitter.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        @return A MidiDevice that is considered appropriate, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        if none is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    private static MidiDevice getFirstDevice(List providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                                             Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                                             boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                                             boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        for(int i = 0; i < providers.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            MidiDeviceProvider provider = (MidiDeviceProvider) providers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            MidiDevice device = getFirstDevice(provider, deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                               allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                                               allowSequencer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            if (device != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                return device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    /** Checks if a MidiDevice is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        If deviceClass is Synthesizer or Sequencer, a device implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        the respective interface is considered appropriate. If deviceClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        is Receiver or Transmitter, a device is considered appropriate if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        it implements neither Synthesizer nor Transmitter, and if it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        provide at least one Receiver or Transmitter, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        @param device the MidiDevice to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        @param allowSynthesizer if true, Synthesizers are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        appropriate. Otherwise only pure MidiDevices are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        appropriate (unless allowSequencer is true). This flag only has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        effect for deviceClass Receiver and Transmitter. For other device
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        classes (Sequencer and Synthesizer), this flag has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        @param allowSequencer if true, Sequencers are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        appropriate. Otherwise only pure MidiDevices are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        appropriate (unless allowSynthesizer is true). This flag only has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        effect for deviceClass Receiver and Transmitter. For other device
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        classes (Sequencer and Synthesizer), this flag has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        @return true if the device is considered appropriate according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        rules given above, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    private static boolean isAppropriateDevice(MidiDevice device,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                                               Class deviceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                                               boolean allowSynthesizer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                                               boolean allowSequencer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        if (deviceClass.isInstance(device)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            // This clause is for deviceClass being either Synthesizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            // or Sequencer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            // Now the case that deviceClass is Transmitter or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            // Receiver. If neither allowSynthesizer nor allowSequencer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            // true, we require device instances to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // neither Synthesizer nor Sequencer, since we only want
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            // devices representing MIDI ports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            // Otherwise, the respective type is accepted, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            if ( (! (device instanceof Sequencer) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                  ! (device instanceof Synthesizer) ) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                 ((device instanceof Sequencer) && allowSequencer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                 ((device instanceof Synthesizer) && allowSynthesizer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                // And of cource, the device has to be able to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                // Receivers or Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                if ((deviceClass == Receiver.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                     device.getMaxReceivers() != 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                    (deviceClass == Transmitter.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                     device.getMaxTransmitters() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * Obtains the set of services currently installed on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * using sun.misc.Service, the SPI mechanism in 1.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @return a List of instances of providers for the requested service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * If no providers are available, a List of length 0 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    private static List getProviders(Class providerClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        return JDK13Services.getProviders(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
}