jdk/src/share/classes/com/sun/media/sound/AbstractMidiDeviceProvider.java
author amenkov
Mon, 28 Feb 2011 18:36:33 +0300
changeset 8525 08f98f5a11df
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.sound.midi.MidiDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.sound.midi.spi.MidiDeviceProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Super class for MIDI input or output device provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public abstract class AbstractMidiDeviceProvider extends MidiDeviceProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static boolean enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Create objects representing all MIDI output devices on the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: static");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Platform.initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        enabled = Platform.isMidiIOEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (Printer.trace) Printer.trace("AbstractMidiDeviceProvider: enabled: " + enabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // $$fb number of MIDI devices may change with time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // also for memory's sake, do not initialize the arrays here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    synchronized void readDeviceInfos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Info[] infos = getInfoCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        MidiDevice[] devices = getDeviceCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (!enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (infos == null || infos.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                setInfoCache(new Info[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (devices == null || devices.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                setDeviceCache(new MidiDevice[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int oldNumDevices = (infos==null)?-1:infos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int newNumDevices = getNumDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (oldNumDevices != newNumDevices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (Printer.trace) Printer.trace(getClass().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                             +": readDeviceInfos: old numDevices: "+oldNumDevices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                             +"  newNumDevices: "+ newNumDevices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // initialize the arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Info[] newInfos = new Info[newNumDevices];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            MidiDevice[] newDevices = new MidiDevice[newNumDevices];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            for (int i = 0; i < newNumDevices; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                Info newInfo = createInfo(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                // in case that we are re-reading devices, try to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // the previous one and reuse it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (infos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    for (int ii = 0; ii < infos.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        Info info = infos[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        if (info != null && info.equalStrings(newInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            // new info matches the still existing info. Use old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            newInfos[i] = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                            info.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            infos[ii] = null; // prevent re-use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                            newDevices[i] = devices[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            devices[ii] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (newInfos[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    newInfos[i] = newInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // the remaining MidiDevice.Info instances in the infos array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // have become obsolete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (infos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                for (int i = 0; i < infos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    if (infos[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        // disable this device info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        infos[i].setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    // what to do with the MidiDevice instances that are left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    // in the devices array ?? Close them ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // commit new list of infos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            setInfoCache(newInfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            setDeviceCache(newDevices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public MidiDevice.Info[] getDeviceInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        readDeviceInfos();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Info[] infos = getInfoCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        MidiDevice.Info[] localArray = new MidiDevice.Info[infos.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.arraycopy(infos, 0, localArray, 0, infos.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return localArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public MidiDevice getDevice(MidiDevice.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (info instanceof Info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            readDeviceInfos();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            MidiDevice[] devices = getDeviceCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Info[] infos = getInfoCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Info thisInfo = (Info) info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int index = thisInfo.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (index >= 0 && index < devices.length && infos[index] == info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (devices[index] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    devices[index] = createDevice(thisInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (devices[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    return devices[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throw new IllegalArgumentException("MidiDevice " + info.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                           + " not supported by this provider.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // INNER CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Info class for MidiDevices.  Adds an index value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * making native references to a particular device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static class Info extends MidiDevice.Info {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Info(String name, String vendor, String description, String version, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            super(name, vendor, description, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        boolean equalStrings(Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return      (info != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                         && getName().equals(info.getName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                         && getVendor().equals(info.getVendor())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                         && getDescription().equals(info.getDescription())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                         && getVersion().equals(info.getVersion()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        void setIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    } // class Info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // ABSTRACT METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    abstract int getNumDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    abstract MidiDevice[] getDeviceCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    abstract void setDeviceCache(MidiDevice[] devices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    abstract Info[] getInfoCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    abstract void setInfoCache(Info[] infos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    abstract Info createInfo(int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    abstract MidiDevice createDevice(Info info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}