jdk/src/java.desktop/share/classes/com/sun/media/sound/AbstractMidiDevice.java
author serb
Wed, 18 May 2016 20:40:17 +0300
changeset 38981 9521cb19e297
parent 28059 e576535359cc
child 40444 afabcfc2f3ef
permissions -rw-r--r--
8156169: Some sound tests rarely hangs because of incorrect synchronization Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38981
9521cb19e297 8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents: 28059
diff changeset
     2
 * Copyright (c) 1999, 2016, 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
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
    59
    private final Object traRecLock = new Object();
2
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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
    63
    private final MidiDevice.Info info;
2
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
38981
9521cb19e297 8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents: 28059
diff changeset
    68
    private volatile boolean open;
2
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
     */
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
    73
    private List<Object> openKeepingObjects;
2
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
     */
38981
9521cb19e297 8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents: 28059
diff changeset
    78
    protected volatile long id;
2
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
    /*
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    90
     * The initial mode and only supported mode default to OMNI_ON_POLY.
2
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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   105
    public final MidiDevice.Info getDeviceInfo() {
2
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
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18215
diff changeset
   109
    /** Open the device from an application program.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Setting the open reference count to -1 here prevents Transmitters and Receivers that
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   111
     * opened the device implicitly from closing it. The only way to close the device after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * this call is a call to close().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   114
    public final void open() throws MidiUnavailableException {
2
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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   162
    public final void close() {
2
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
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   184
    public final void closeInternal(Object object) {
2
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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   200
    public final void doClose() {
2
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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   212
    public final boolean isOpen() {
2
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
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   287
    @SuppressWarnings("unchecked") // Cast of result of clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public final List<Receiver> getReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        List<Receiver> recs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (receiverList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                recs = Collections.unmodifiableList(new ArrayList<Receiver>(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                recs = Collections.unmodifiableList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    ((List<Receiver>) (receiverList.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return recs;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * This implementation uses createTransmitter, which may throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * If a transmitter is returned in createTransmitter, it is added to the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * TransmitterList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public final Transmitter getTransmitter() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Transmitter transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            transmitter = createTransmitter(); // may throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            getTransmitterList().add(transmitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   317
    @SuppressWarnings("unchecked") // Cast of result of clone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public final List<Transmitter> getTransmitters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        List<Transmitter> tras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (transmitterList == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                || transmitterList.transmitters.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                tras = Collections.unmodifiableList(new ArrayList<Transmitter>(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                tras = Collections.unmodifiableList((List<Transmitter>) (transmitterList.transmitters.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return tras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   334
    final long getId() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // REFERENCE COUNTING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /** Retrieve a Receiver and open the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        This method is called by MidiSystem.getReceiver().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   344
    public final Receiver getReceiverReferenceCounting()
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   345
            throws MidiUnavailableException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        /* Keep this order of commands! If getReceiver() throws an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
           openInternal() should not be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Receiver receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            receiver = getReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            AbstractMidiDevice.this.openInternal(receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return receiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /** Retrieve a Transmitter and open the device implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        This method is called by MidiSystem.getTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   361
    public final Transmitter getTransmitterReferenceCounting()
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   362
            throws MidiUnavailableException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        /* Keep this order of commands! If getTransmitter() throws an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
           openInternal() should not be called!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Transmitter transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            transmitter = getTransmitter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            AbstractMidiDevice.this.openInternal(transmitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return transmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /** Return the list of objects that have opened the device implicitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   377
    private synchronized List<Object> getOpenKeepingObjects() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (openKeepingObjects == null) {
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 22584
diff changeset
   379
            openKeepingObjects = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return openKeepingObjects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    // RECEIVER HANDLING METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /** Return the internal list of Receivers, possibly creating it first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private List<Receiver> getReceiverList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (receiverList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                receiverList = new ArrayList<Receiver>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return receiverList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /** Returns if this device supports Receivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Subclasses that use Receivers should override this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return true. They also should override createReceiver().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        @return true, if the device supports Receivers, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected boolean hasReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /** Create a Receiver object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        throwing an exception here means that Receivers aren't enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        Subclasses that use Receivers should override this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        one that returns objects implementing Receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        Classes overriding this method should also override hasReceivers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    protected Receiver createReceiver() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        throw new MidiUnavailableException("MIDI IN receiver not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // TRANSMITTER HANDLING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /** Return the internal list of Transmitters, possibly creating it first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   429
    final TransmitterList getTransmitterList() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        synchronized (traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (transmitterList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                transmitterList = new TransmitterList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return transmitterList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /** Returns if this device supports Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        Subclasses that use Transmitters should override this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return true. They also should override createTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        @return true, if the device supports Transmitters, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    protected boolean hasTransmitters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /** Create a Transmitter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        throwing an exception here means that Transmitters aren't enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Subclasses that use Transmitters should override this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        one that returns objects implementing Transmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        Classes overriding this method should also override hasTransmitters()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    protected Transmitter createTransmitter() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        throw new MidiUnavailableException("MIDI OUT transmitter not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    // ABSTRACT METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    protected abstract void implOpen() throws MidiUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * close this device if discarded by the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   469
    protected final void finalize() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // INNER CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /** Base class for Receivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        Subclasses that use Receivers must use this base class, since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        contains magic necessary to manage implicit closing the device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        This is necessary for Receivers retrieved via MidiSystem.getReceiver()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        (which opens the device implicitely).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   481
    abstract class AbstractReceiver implements MidiDeviceReceiver {
38981
9521cb19e297 8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents: 28059
diff changeset
   482
        private volatile boolean open = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        /** Deliver a MidiMessage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            This method contains magic related to the closed state of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            Receiver. Therefore, subclasses should not override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            Instead, they should implement implSend().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        */
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   490
        @Override
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   491
        public final synchronized void send(final MidiMessage message,
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   492
                                            final long timeStamp) {
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   493
            if (!open) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                throw new IllegalStateException("Receiver is not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   496
            implSend(message, timeStamp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   499
        abstract void implSend(MidiMessage message, long timeStamp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /** Close the Receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * Here, the call to the magic method closeInternal() takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * Therefore, subclasses that override this method must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * 'super.close()'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         */
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   506
        @Override
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   507
        public final void close() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            synchronized (AbstractMidiDevice.this.traRecLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                AbstractMidiDevice.this.getReceiverList().remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            AbstractMidiDevice.this.closeInternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   515
        @Override
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   516
        public final MidiDevice getMidiDevice() {
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   517
            return AbstractMidiDevice.this;
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   518
        }
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   519
16876
17f574546dc8 8004261: Improve input validation
serb
parents: 7668
diff changeset
   520
        final boolean isOpen() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            return open;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        //$$fb is that a good idea?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        //protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        //    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    } // class AbstractReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Transmitter base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * This class especially makes sure the device is closed if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * has been opened implicitly by a call to MidiSystem.getTransmitter().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * The logic of doing so is actually in closeInternal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Also, it has some optimizations regarding sending to the Receivers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * for known Receivers, and managing itself in the TransmitterList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   541
    class BasicTransmitter implements MidiDeviceTransmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        private Receiver receiver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        TransmitterList tlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        protected BasicTransmitter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        private void setTransmitterList(TransmitterList tlist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            this.tlist = tlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   553
        public final void setReceiver(Receiver receiver) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (tlist != null && this.receiver != receiver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                if (Printer.debug) Printer.debug("Transmitter "+toString()+": set receiver "+receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                tlist.receiverChanged(this, this.receiver, receiver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                this.receiver = 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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   561
        public final Receiver getReceiver() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return receiver;
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
        /** Close the Transmitter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * Here, the call to the magic method closeInternal() takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * Therefore, subclasses that override this method must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         * 'super.close()'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   571
        public final void close() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            AbstractMidiDevice.this.closeInternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (tlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                tlist.receiverChanged(this, this.receiver, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                tlist.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                tlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   580
        public final MidiDevice getMidiDevice() {
6508
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   581
            return AbstractMidiDevice.this;
6c00b4789626 4933700: RFE: Add way to get device from Receiver and Transmitter
amenkov
parents: 5506
diff changeset
   582
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    } // class BasicTransmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * a class to manage a list of transmitters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   590
    final class TransmitterList {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 16876
diff changeset
   592
        private final ArrayList<Transmitter> transmitters = new ArrayList<Transmitter>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        private MidiOutDevice.MidiOutReceiver midiOutReceiver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // how many transmitters must be present for optimized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        private int optimizedReceiverCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        private void add(Transmitter t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                transmitters.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (t instanceof BasicTransmitter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                ((BasicTransmitter) t).setTransmitterList(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            if (Printer.debug) Printer.debug("--added transmitter "+t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        private void remove(Transmitter t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                int index = transmitters.indexOf(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    transmitters.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    if (Printer.debug) Printer.debug("--removed transmitter "+t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        private void receiverChanged(BasicTransmitter t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                     Receiver oldR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                     Receiver newR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                // some optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                if (midiOutReceiver == oldR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    midiOutReceiver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                if (newR != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    if ((newR instanceof MidiOutDevice.MidiOutReceiver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        && (midiOutReceiver == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        midiOutReceiver = ((MidiOutDevice.MidiOutReceiver) newR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                optimizedReceiverCount =
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents: 2
diff changeset
   635
                      ((midiOutReceiver!=null)?1:0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // more potential for optimization here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        /** closes all transmitters and empties the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            synchronized (transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                for(int i = 0; i < transmitters.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    transmitters.get(i).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                transmitters.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (Printer.trace) Printer.trace("TransmitterList.close() succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
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
        * Send this message to all receivers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        * status = packedMessage & 0xFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        * data1 = (packedMessage & 0xFF00) >> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        * data1 = (packedMessage & 0xFF0000) >> 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        void sendMessage(int packedMessage, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    if (optimizedReceiverCount == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        if (midiOutReceiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                            if (TRACE_TRANSMITTER) Printer.println("Sending packed message to MidiOutReceiver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                            midiOutReceiver.sendPackedMidiMessage(packedMessage, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        if (TRACE_TRANSMITTER) Printer.println("Sending packed message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        for (int i = 0; i < size; i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   672
                            Receiver receiver = transmitters.get(i).getReceiver();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                if (optimizedReceiverCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                                    if (receiver instanceof MidiOutDevice.MidiOutReceiver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                                        ((MidiOutDevice.MidiOutReceiver) receiver).sendPackedMidiMessage(packedMessage, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                        receiver.send(new FastShortMessage(packedMessage), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                    receiver.send(new FastShortMessage(packedMessage), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                // this happens when invalid data comes over the wire. Ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        void sendMessage(byte[] data, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    if (TRACE_TRANSMITTER) Printer.println("Sending long message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    for (int i = 0; i < size; i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   698
                        Receiver receiver = transmitters.get(i).getReceiver();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            //$$fb 2002-04-02: SysexMessages are mutable, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            // an application could change the contents of this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            // or try to use the object later. So we can't get around object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            // But the array need not be unique for each FastSysexMessage object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            // because it cannot be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            receiver.send(new FastSysexMessage(data), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                // this happens when invalid data comes over the wire. Ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        * Send this message to all transmitters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        void sendMessage(MidiMessage message, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (message instanceof FastShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                sendMessage(((FastShortMessage) message).getPackedMsg(), timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            synchronized(transmitters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                int size = transmitters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (optimizedReceiverCount == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    if (midiOutReceiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to MidiOutReceiver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        midiOutReceiver.send(message, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    if (TRACE_TRANSMITTER) Printer.println("Sending MIDI message to "+size+" transmitter's receivers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    for (int i = 0; i < size; i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   734
                        Receiver receiver = transmitters.get(i).getReceiver();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        if (receiver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            //$$fb 2002-04-02: ShortMessages are mutable, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                            // an application could change the contents of this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                            // or try to use the object later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            // We violate this spec here, to avoid costly (and gc-intensive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            // object creation for potentially hundred of messages per second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                            // The spec should be changed to allow Immutable MidiMessages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            // (i.e. throws InvalidStateException or so in setMessage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                            receiver.send(message, timeStamp);
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    } // TransmitterList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
}