Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]
    1   /*
    2    * Copyright (c) 2002, 2007, Oracle and/or its affiliates. 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.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * 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    * DirectAudioDevice provider.
   36    *
   37    * @author Florian Bomers
   38    */
   39   public class DirectAudioDeviceProvider 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 DirectAudioDeviceInfo[] infos;
   47   
   48       /**
   49        * Set of all port input devices on the system.
   50        */
   51       private static DirectAudioDevice[] 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 DirectAudioDeviceProvider() {
   69           //if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: constructor");
   70           if (Platform.isDirectAudioEnabled()) {
   71               init();
   72           } else {
   73               infos = new DirectAudioDeviceInfo[0];
   74               devices = new DirectAudioDevice[0];
   75           }
   76       }
   77   
   78       private synchronized static 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("DirectAudioDeviceProvider: init()");
   84               // initialize the arrays
   85               infos = new DirectAudioDeviceInfo[numDevices];
   86               devices = new DirectAudioDevice[numDevices];
   87   
   88               // fill in the info objects now.
   89               for (int i = 0; i < infos.length; i++) {
   90                   infos[i] = nNewDirectAudioDeviceInfo(i);
   91               }
   92               if (Printer.trace) Printer.trace("DirectAudioDeviceProvider: init(): found numDevices: " + numDevices);
   93           }
   94       }
   95   
   96       public Mixer.Info[] getMixerInfo() {
   97           Mixer.Info[] localArray = new Mixer.Info[infos.length];
   98           System.arraycopy(infos, 0, localArray, 0, infos.length);
   99           return localArray;
  100       }
  101   
  102   
  103       public Mixer getMixer(Mixer.Info info) {
  104           // if the default device is asked, we provide the mixer
  105           // with SourceDataLine's
  106           if (info == null) {
  107               for (int i = 0; i < infos.length; i++) {
  108                   Mixer mixer = getDevice(infos[i]);
  109                   if (mixer.getSourceLineInfo().length > 0) {
  110                       return mixer;
  111                   }
  112               }
  113           }
  114           // otherwise get the first mixer that matches
  115           // the requested info object
  116           for (int i = 0; i < infos.length; i++) {
  117               if (infos[i].equals(info)) {
  118                   return getDevice(infos[i]);
  119               }
  120           }
  121   
  122           throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider.");
  123       }
  124   
  125   
  126       private Mixer getDevice(DirectAudioDeviceInfo info) {
  127           int index = info.getIndex();
  128           if (devices[index] == null) {
  129               devices[index] = new DirectAudioDevice(info);
  130           }
  131           return devices[index];
  132       }
  133   
  134       // INNER CLASSES
  135   
  136   
  137       /**
  138        * Info class for DirectAudioDevices.  Adds an index value and a string for
  139        * making native references to a particular device.
  140        * This constructor is called from native.
  141        */
  142       static class DirectAudioDeviceInfo extends Mixer.Info {
  143           private int index;
  144           private int maxSimulLines;
  145   
  146           // For ALSA, the deviceID contains the encoded card index, device index, and sub-device-index
  147           private int deviceID;
  148   
  149           private DirectAudioDeviceInfo(int index, int deviceID, int maxSimulLines,
  150                                         String name, String vendor,
  151                                         String description, String version) {
  152               super(name, vendor, "Direct Audio Device: "+description, version);
  153               this.index = index;
  154               this.maxSimulLines = maxSimulLines;
  155               this.deviceID = deviceID;
  156           }
  157   
  158           int getIndex() {
  159               return index;
  160           }
  161   
  162           int getMaxSimulLines() {
  163               return maxSimulLines;
  164           }
  165   
  166           int getDeviceID() {
  167               return deviceID;
  168           }
  169       } // class DirectAudioDeviceInfo
  170   
  171       // NATIVE METHODS
  172       private static native int nGetNumDevices();
  173       // index: [0..nGetNumDevices()-1]
  174       private static native DirectAudioDeviceInfo nNewDirectAudioDeviceInfo(int deviceIndex);
  175   }

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]