jdk/src/share/classes/com/sun/media/sound/AbstractMidiDevice.java
author amenkov
Mon, 28 Feb 2011 18:36:33 +0300
changeset 8525 08f98f5a11df
parent 7668 d4a77089c587
child 16876 17f574546dc8
permissions -rw-r--r--
7013521: AudioSystem.write for AIFF files closes source audio stream Reviewed-by: dav
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: 1846
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: 1846
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: 1846
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
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 com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sound.midi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Abstract AbstractMidiDevice class representing functionality shared by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * MidiInDevice and MidiOutDevice objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
abstract class AbstractMidiDevice implements MidiDevice, ReferenceCountingDevice {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // STATIC VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final boolean TRACE_TRANSMITTER = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // INSTANCE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ArrayList<Receiver> receiverList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private TransmitterList transmitterList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // lock to protect receiverList and transmitterList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // from simultaneous creation and destruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // reduces possibility of deadlock, compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // synchronizing to the class instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Object traRecLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // DEVICE ATTRIBUTES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private MidiDevice.Info info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // DEVICE STATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected /*private*/ boolean open          = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int openRefCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** List of Receivers and Transmitters that opened the device implicitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private List openKeepingObjects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * This is the device handle returned from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected long id                   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // CONSTRUCTOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Constructs an AbstractMidiDevice with the specified info object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param info the description of the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The initial mode and and only supported mode default to OMNI_ON_POLY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected AbstractMidiDevice(MidiDevice.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if(Printer.trace) Printer.trace(">> AbstractMidiDevice CONSTRUCTOR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.info = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        openRefCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if(Printer.trace) Printer.trace("<< AbstractMidiDevice CONSTRUCTOR completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // MIDI DEVICE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public MidiDevice.Info getDeviceInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /** Open the device from an aplication program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Setting the open reference count to -1 here prevents Transmitters and Receivers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * opened the the device implicitly from closing it. The only way to close the device after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * this call is a call to close().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void open() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: open()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            openRefCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            doOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: open() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** Open the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This method is intended to be used by AbstractReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * and BasicTransmitter. Actually, it is called by getReceiverReferenceCounting() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * getTransmitterReferenceCounting(). These, in turn, are called by MidiSytem on calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * getReceiver() and getTransmitter(). The former methods should pass the Receiver or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Transmitter just created as the object parameter to this method. Storing references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * these objects is necessary to be able to decide later (when it comes to closing) if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * R/T's are ones that opened the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @object The Receiver or Transmitter instance that triggered this implicit open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private void openInternal(Object object) throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: openInternal()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (openRefCount != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                openRefCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                getOpenKeepingObjects().add(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // double calls to doOpens() will be catched by the open flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            doOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: openInternal() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private void doOpen() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: doOpen()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (! isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                implOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                open = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: doOpen() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: close()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            doClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            openRefCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: close() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /** Close the device for an object that implicitely opened it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * This method is intended to be used by Transmitter.close() and Receiver.close().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Those methods should pass this for the object parameter. Since Transmitters or Receivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * do not know if their device has been opened implicitely because of them, they call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * method in any case. This method now is able to seperate Receivers/Transmitters that opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * the device implicitely from those that didn't by looking up the R/T in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * openKeepingObjects list. Only if the R/T is contained there, the reference count is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * reduced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param object The object that might have been opening the device implicitely (for now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * this may be a Transmitter or receiver).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void closeInternal(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: closeInternal()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (getOpenKeepingObjects().remove(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (openRefCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    openRefCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    if (openRefCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        doClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: closeInternal() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void doClose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (Printer.trace) Printer.trace("> AbstractMidiDevice: doClose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (Printer.trace) Printer.trace("< AbstractMidiDevice: doClose() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return open;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected void implClose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (receiverList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // close all receivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                for(int i = 0; i < receiverList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    receiverList.get(i).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                receiverList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (transmitterList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // close all transmitters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                transmitterList.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * This implementation always returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Devices that actually provide this should over-ride
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public long getMicrosecondPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /** Return the maximum number of Receivers supported by this device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Depending on the return value of hasReceivers(), this method returns either 0 or -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Subclasses should rather override hasReceivers() than override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public final int getMaxReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (hasReceivers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /** Return the maximum number of Transmitters supported by this device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Depending on the return value of hasTransmitters(), this method returns either 0 or -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Subclasses should override hasTransmitters().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public final int getMaxTransmitters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (hasTransmitters()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /** Retrieve a Receiver for this device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        This method returns the value returned by createReceiver(), if it doesn't throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        an exception. Subclasses should rather override createReceiver() than override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        If createReceiver returns a Receiver, it is added to the internal list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        of Receivers (see getReceiversList)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public final Receiver getReceiver() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Receiver receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            receiver = createReceiver(); // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            getReceiverList().add(receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public final List<Receiver> getReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        List<Receiver> recs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (receiverList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                recs = Collections.unmodifiableList(new ArrayList<Receiver>(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                recs = Collections.unmodifiableList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    ((List<Receiver>) (receiverList.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return recs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * This implementation uses createTransmitter, which may throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * If a transmitter is returned in createTransmitter, it is added to the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * TransmitterList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public final Transmitter getTransmitter() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Transmitter transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            transmitter = createTransmitter(); // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            getTransmitterList().add(transmitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public final List<Transmitter> getTransmitters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        List<Transmitter> tras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (transmitterList == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                || transmitterList.transmitters.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                tras = Collections.unmodifiableList(new ArrayList<Transmitter>(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                tras = Collections.unmodifiableList((List<Transmitter>) (transmitterList.transmitters.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return tras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    long getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // REFERENCE COUNTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /** Retrieve a Receiver and open the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        This method is called by MidiSystem.getReceiver().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public Receiver getReceiverReferenceCounting() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /* Keep this order of commands! If getReceiver() throws an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
           openInternal() should not be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        Receiver receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            receiver = getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            AbstractMidiDevice.this.openInternal(receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /** Retrieve a Transmitter and open the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        This method is called by MidiSystem.getTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public Transmitter getTransmitterReferenceCounting() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        /* Keep this order of commands! If getTransmitter() throws an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
           openInternal() should not be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        Transmitter transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            transmitter = getTransmitter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            AbstractMidiDevice.this.openInternal(transmitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /** Return the list of objects that have opened the device implicitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private synchronized List getOpenKeepingObjects() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (openKeepingObjects == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            openKeepingObjects = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return openKeepingObjects;
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
    // RECEIVER HANDLING METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /** Return the internal list of Receivers, possibly creating it first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private List<Receiver> getReceiverList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (receiverList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                receiverList = new ArrayList<Receiver>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return receiverList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /** Returns if this device supports Receivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        Subclasses that use Receivers should override this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return true. They also should override createReceiver().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        @return true, if the device supports Receivers, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected boolean hasReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /** Create a Receiver object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        throwing an exception here means that Receivers aren't enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        Subclasses that use Receivers should override this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        one that returns objects implementing Receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Classes overriding this method should also override hasReceivers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    protected Receiver createReceiver() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throw new MidiUnavailableException("MIDI IN receiver not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    // TRANSMITTER HANDLING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /** Return the internal list of Transmitters, possibly creating it first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    protected TransmitterList getTransmitterList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (transmitterList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                transmitterList = new TransmitterList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return transmitterList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /** Returns if this device supports Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        Subclasses that use Transmitters should override this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return true. They also should override createTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        @return true, if the device supports Transmitters, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected boolean hasTransmitters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /** Create a Transmitter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        throwing an exception here means that Transmitters aren't enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        Subclasses that use Transmitters should override this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        one that returns objects implementing Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        Classes overriding this method should also override hasTransmitters()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    protected Transmitter createTransmitter() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throw new MidiUnavailableException("MIDI OUT transmitter not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    // ABSTRACT METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    protected abstract void implOpen() throws MidiUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * close this device if discarded by the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // INNER CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /** Base class for Receivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Subclasses that use Receivers must use this base class, since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        contains magic necessary to manage implicit closing the device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        This is necessary for Receivers retrieved via MidiSystem.getReceiver()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        (which opens the device implicitely).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   477
    protected abstract class AbstractReceiver implements MidiDeviceReceiver {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        private boolean open = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        /** Deliver a MidiMessage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            This method contains magic related to the closed state of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            Receiver. Therefore, subclasses should not override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            Instead, they should implement implSend().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public synchronized void send(MidiMessage message, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (open) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                implSend(message, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                throw new IllegalStateException("Receiver is not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
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
        protected abstract void implSend(MidiMessage message, long timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        /** Close the Receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * Here, the call to the magic method closeInternal() takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * Therefore, subclasses that override this method must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * 'super.close()'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            synchronized (AbstractMidiDevice.this.traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                AbstractMidiDevice.this.getReceiverList().remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            AbstractMidiDevice.this.closeInternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   511
        public MidiDevice getMidiDevice() {
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   512
            return AbstractMidiDevice.this;
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   513
        }
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   514
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        protected boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return open;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        //$$fb is that a good idea?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        //protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        //    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    } // class AbstractReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Transmitter base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * This class especially makes sure the device is closed if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * has been opened implicitly by a call to MidiSystem.getTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * The logic of doing so is actually in closeInternal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Also, it has some optimizations regarding sending to the Receivers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * for known Receivers, and managing itself in the TransmitterList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   536
    protected class BasicTransmitter implements MidiDeviceTransmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        private Receiver receiver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        TransmitterList tlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        protected BasicTransmitter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        private void setTransmitterList(TransmitterList tlist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            this.tlist = tlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        public void setReceiver(Receiver receiver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (tlist != null && this.receiver != receiver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (Printer.debug) Printer.debug("Transmitter "+toString()+": set receiver "+receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                tlist.receiverChanged(this, this.receiver, receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                this.receiver = receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        public Receiver getReceiver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        /** Close the Transmitter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * Here, the call to the magic method closeInternal() takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * Therefore, subclasses that override this method must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * 'super.close()'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            AbstractMidiDevice.this.closeInternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (tlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                tlist.receiverChanged(this, this.receiver, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                tlist.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                tlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   575
        public MidiDevice getMidiDevice() {
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   576
            return AbstractMidiDevice.this;
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   577
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    } // class BasicTransmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * a class to manage a list of transmitters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    class TransmitterList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        private ArrayList<Transmitter> transmitters = new ArrayList<Transmitter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        private MidiOutDevice.MidiOutReceiver midiOutReceiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // how many transmitters must be present for optimized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        private int optimizedReceiverCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        private void add(Transmitter t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                transmitters.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (t instanceof BasicTransmitter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                ((BasicTransmitter) t).setTransmitterList(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (Printer.debug) Printer.debug("--added transmitter "+t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        private void remove(Transmitter t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                int index = transmitters.indexOf(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    transmitters.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    if (Printer.debug) Printer.debug("--removed transmitter "+t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        private void receiverChanged(BasicTransmitter t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                     Receiver oldR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                     Receiver newR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                // some optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                if (midiOutReceiver == oldR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    midiOutReceiver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (newR != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    if ((newR instanceof MidiOutDevice.MidiOutReceiver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        && (midiOutReceiver == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        midiOutReceiver = ((MidiOutDevice.MidiOutReceiver) newR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                optimizedReceiverCount =
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents: 2
diff changeset
   630
                      ((midiOutReceiver!=null)?1:0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // more potential for optimization here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        /** closes all transmitters and empties the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            synchronized (transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                for(int i = 0; i < transmitters.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    transmitters.get(i).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                transmitters.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (Printer.trace) Printer.trace("TransmitterList.close() succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        * Send this message to all receivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        * status = packedMessage & 0xFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        * data1 = (packedMessage & 0xFF00) >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        * data1 = (packedMessage & 0xFF0000) >> 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        void sendMessage(int packedMessage, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    if (optimizedReceiverCount == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        if (midiOutReceiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            if (TRACE_TRANSMITTER) Printer.println("Sending packed message to MidiOutReceiver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            midiOutReceiver.sendPackedMidiMessage(packedMessage, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        if (TRACE_TRANSMITTER) Printer.println("Sending packed message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                            Receiver receiver = ((Transmitter)transmitters.get(i)).getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                                if (optimizedReceiverCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                    if (receiver instanceof MidiOutDevice.MidiOutReceiver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                        ((MidiOutDevice.MidiOutReceiver) receiver).sendPackedMidiMessage(packedMessage, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                        receiver.send(new FastShortMessage(packedMessage), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                    receiver.send(new FastShortMessage(packedMessage), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                // this happens when invalid data comes over the wire. Ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        void sendMessage(byte[] data, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    if (TRACE_TRANSMITTER) Printer.println("Sending long message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        Receiver receiver = ((Transmitter)transmitters.get(i)).getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            //$$fb 2002-04-02: SysexMessages are mutable, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                            // an application could change the contents of this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            // or try to use the object later. So we can't get around object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            // But the array need not be unique for each FastSysexMessage object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                            // because it cannot be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            receiver.send(new FastSysexMessage(data), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                // this happens when invalid data comes over the wire. Ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        * Send this message to all transmitters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        void sendMessage(MidiMessage message, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (message instanceof FastShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                sendMessage(((FastShortMessage) message).getPackedMsg(), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (optimizedReceiverCount == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    if (midiOutReceiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to MidiOutReceiver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        midiOutReceiver.send(message, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        Receiver receiver = ((Transmitter)transmitters.get(i)).getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                            //$$fb 2002-04-02: ShortMessages are mutable, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                            // an application could change the contents of this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                            // or try to use the object later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            // We violate this spec here, to avoid costly (and gc-intensive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                            // object creation for potentially hundred of messages per second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            // The spec should be changed to allow Immutable MidiMessages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                            // (i.e. throws InvalidStateException or so in setMessage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                            receiver.send(message, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    } // TransmitterList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
}