J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.media.sound; |
| 27 | |
| 28 | import java.util.Vector; |
| 29 | |
| 30 | import javax.sound.sampled.Mixer; |
| 31 | import javax.sound.sampled.spi.MixerProvider; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Port provider. |
| 36 | * |
| 37 | * @author Florian Bomers |
| 38 | */ |
| 39 | public class PortMixerProvider extends MixerProvider { |
| 40 | |
| 41 | // STATIC VARIABLES |
| 42 | |
| 43 | /** |
| 44 | * Set of info objects for all port input devices on the system. |
| 45 | */ |
| 46 | private static PortMixerInfo[] infos; |
| 47 | |
| 48 | /** |
| 49 | * Set of all port input devices on the system. |
| 50 | */ |
| 51 | private static PortMixer[] devices; |
| 52 | |
| 53 | |
| 54 | // STATIC |
| 55 | |
| 56 | static { |
| 57 | // initialize |
| 58 | Platform.initialize(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // CONSTRUCTOR |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Required public no-arg constructor. |
| 67 | */ |
| 68 | public PortMixerProvider() { |
| 69 | //if (Printer.trace) Printer.trace("PortMixerProvider: constructor"); |
| 70 | if (Platform.isPortsEnabled()) { |
| 71 | init(); |
| 72 | } else { |
| 73 | infos = new PortMixerInfo[0]; |
| 74 | devices = new PortMixer[0]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private static synchronized void init() { |
| 79 | // get the number of input devices |
| 80 | int numDevices = nGetNumDevices(); |
| 81 | |
| 82 | if (infos == null || infos.length != numDevices) { |
| 83 | if (Printer.trace) Printer.trace("PortMixerProvider: init()"); |
| 84 | // initialize the arrays |
| 85 | infos = new PortMixerInfo[numDevices]; |
| 86 | devices = new PortMixer[numDevices]; |
| 87 | |
| 88 | // fill in the info objects now. |
| 89 | // we'll fill in the device objects as they're requested. |
| 90 | for (int i = 0; i < infos.length; i++) { |
| 91 | infos[i] = nNewPortMixerInfo(i); |
| 92 | } |
| 93 | if (Printer.trace) Printer.trace("PortMixerProvider: init(): found numDevices: " + numDevices); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public Mixer.Info[] getMixerInfo() { |
| 98 | Mixer.Info[] localArray = new Mixer.Info[infos.length]; |
| 99 | System.arraycopy(infos, 0, localArray, 0, infos.length); |
| 100 | return localArray; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public Mixer getMixer(Mixer.Info info) { |
| 105 | for (int i = 0; i < infos.length; i++) { |
| 106 | if (infos[i].equals(info)) { |
| 107 | return getDevice(infos[i]); |
| 108 | } |
| 109 | } |
| 110 | throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider."); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | private Mixer getDevice(PortMixerInfo info) { |
| 115 | int index = info.getIndex(); |
| 116 | if (devices[index] == null) { |
| 117 | devices[index] = new PortMixer(info); |
| 118 | } |
| 119 | return devices[index]; |
| 120 | } |
| 121 | |
| 122 | // INNER CLASSES |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Info class for PortMixers. Adds an index value for |
| 127 | * making native references to a particular device. |
| 128 | * This constructor is called from native. |
| 129 | */ |
| 130 | static class PortMixerInfo extends Mixer.Info { |
| 131 | private int index; |
| 132 | |
| 133 | private PortMixerInfo(int index, String name, String vendor, String description, String version) { |
| 134 | super("Port " + name, vendor, description, version); |
| 135 | this.index = index; |
| 136 | } |
| 137 | |
| 138 | int getIndex() { |
| 139 | return index; |
| 140 | } |
| 141 | |
| 142 | } // class PortMixerInfo |
| 143 | |
| 144 | // NATIVE METHODS |
| 145 | private static native int nGetNumDevices(); |
| 146 | private static native PortMixerInfo nNewPortMixerInfo(int mixerIndex); |
| 147 | } |