src/share/classes/sun/audio/AudioDevice.java

Print this page




  41  * the Java Sound API.
  42  *
  43  * This class emulates systems with multiple audio channels, mixing
  44  * multiple streams for the workstation's single-channel device.
  45  *
  46  * @see AudioData
  47  * @see AudioDataStream
  48  * @see AudioStream
  49  * @see AudioStreamSequence
  50  * @see ContinuousAudioDataStream
  51  * @author David Rivas
  52  * @author Kara Kytle
  53  * @author Jan Borgersen
  54  * @author Florian Bomers
  55  */
  56 
  57 public final class AudioDevice {
  58 
  59     private boolean DEBUG = false  /*true*/ ;
  60 
  61     /** Hashtable of audio clips / input streams. */
  62     private Hashtable clipStreams;
  63 
  64     private Vector infos;
  65 
  66     /** Are we currently playing audio? */
  67     private boolean playing = false;
  68 
  69     /** Handle to the JS audio mixer. */
  70     private Mixer mixer = null;
  71 
  72 
  73 
  74     /**
  75      * The default audio player. This audio player is initialized
  76      * automatically.
  77      */
  78     public static final AudioDevice device = new AudioDevice();
  79 
  80     /**
  81      * Create an AudioDevice instance.
  82      */
  83     private AudioDevice() {
  84 
  85         clipStreams = new Hashtable();
  86         infos = new Vector();
  87     }
  88 
  89 
  90     private synchronized void startSampled( AudioInputStream as,
  91                                             InputStream in ) throws UnsupportedAudioFileException,
  92                                   LineUnavailableException {
  93 
  94         Info info = null;
  95         DataPusher datapusher = null;
  96         DataLine.Info lineinfo = null;
  97         SourceDataLine sourcedataline = null;
  98 
  99         // if ALAW or ULAW, we must convert....
 100         as = Toolkit.getPCMConvertedAudioInputStream(as);
 101 
 102         if( as==null ) {
 103             // could not convert
 104             return;
 105         }
 106 


 144 
 145     }
 146 
 147 
 148 
 149     /**
 150      *  Open an audio channel.
 151      */
 152     public synchronized void openChannel(InputStream in) {
 153 
 154 
 155         if(DEBUG) {
 156             System.out.println("AudioDevice: openChannel");
 157             System.out.println("input stream =" + in);
 158         }
 159 
 160         Info info = null;
 161 
 162         // is this already playing?  if so, then just return
 163         for(int i=0; i<infos.size(); i++) {
 164             info = (AudioDevice.Info)infos.elementAt(i);
 165             if( info.in == in ) {
 166 
 167                 return;
 168             }
 169         }
 170 
 171 
 172         AudioInputStream as = null;
 173 
 174         if( in instanceof AudioStream ) {
 175 
 176             if ( ((AudioStream)in).midiformat != null ) {
 177 
 178                 // it's a midi file
 179                 try {
 180                     startMidi( ((AudioStream)in).stream, in );
 181                 } catch (Exception e) {
 182                     return;
 183                 }
 184 


 273         // don't forget adjust for a new stream.
 274         notify();
 275     }
 276 
 277 
 278     /**
 279      *  Close an audio channel.
 280      */
 281     public synchronized void closeChannel(InputStream in) {
 282 
 283         if(DEBUG) {
 284             System.out.println("AudioDevice.closeChannel");
 285         }
 286 
 287         if (in == null) return;         // can't go anywhere here!
 288 
 289         Info info;
 290 
 291         for(int i=0; i<infos.size(); i++) {
 292 
 293             info = (AudioDevice.Info)infos.elementAt(i);
 294 
 295             if( info.in == in ) {
 296 
 297                 if( info.sequencer != null ) {
 298 
 299                     info.sequencer.stop();
 300                     //info.sequencer.close();
 301                     infos.removeElement( info );
 302 
 303                 } else if( info.datapusher != null ) {
 304 
 305                     info.datapusher.stop();
 306                     infos.removeElement( info );
 307                 }
 308             }
 309         }
 310         notify();
 311     }
 312 
 313 


 338     public void play() {
 339 
 340         // $$jb: 06.24.99:  Holdover from old architechture ...
 341         // we now open/close the devices as needed on a per-stream
 342         // basis using the JavaSound API.
 343 
 344         if (DEBUG) {
 345             System.out.println("exiting play()");
 346         }
 347     }
 348 
 349     /**
 350      * Close streams
 351      */
 352     public synchronized void closeStreams() {
 353 
 354         Info info;
 355 
 356         for(int i=0; i<infos.size(); i++) {
 357 
 358             info = (AudioDevice.Info)infos.elementAt(i);
 359 
 360             if( info.sequencer != null ) {
 361 
 362                 info.sequencer.stop();
 363                 info.sequencer.close();
 364                 infos.removeElement( info );
 365 
 366             } else if( info.datapusher != null ) {
 367 
 368                 info.datapusher.stop();
 369                 infos.removeElement( info );
 370             }
 371         }
 372 
 373 
 374         if (DEBUG) {
 375             System.err.println("Audio Device: Streams all closed.");
 376         }
 377         // Empty the hash table.
 378         clipStreams = new Hashtable();
 379         infos = new Vector();
 380     }
 381 
 382     /**
 383      * Number of channels currently open.
 384      */
 385     public int openChannels() {
 386         return infos.size();
 387     }
 388 
 389     /**
 390      * Make the debug info print out.
 391      */
 392     void setVerbose(boolean v) {
 393         DEBUG = v;
 394     }
 395 
 396 
 397 
 398 
 399 




  41  * the Java Sound API.
  42  *
  43  * This class emulates systems with multiple audio channels, mixing
  44  * multiple streams for the workstation's single-channel device.
  45  *
  46  * @see AudioData
  47  * @see AudioDataStream
  48  * @see AudioStream
  49  * @see AudioStreamSequence
  50  * @see ContinuousAudioDataStream
  51  * @author David Rivas
  52  * @author Kara Kytle
  53  * @author Jan Borgersen
  54  * @author Florian Bomers
  55  */
  56 
  57 public final class AudioDevice {
  58 
  59     private boolean DEBUG = false  /*true*/ ;
  60 
  61     private Vector<Info> infos;



  62 
  63     /** Are we currently playing audio? */
  64     private boolean playing = false;
  65 
  66     /** Handle to the JS audio mixer. */
  67     private Mixer mixer = null;
  68 
  69 
  70 
  71     /**
  72      * The default audio player. This audio player is initialized
  73      * automatically.
  74      */
  75     public static final AudioDevice device = new AudioDevice();
  76 
  77     /**
  78      * Create an AudioDevice instance.
  79      */
  80     private AudioDevice() {
  81         infos = new Vector<>();


  82     }
  83 
  84 
  85     private synchronized void startSampled( AudioInputStream as,
  86                                             InputStream in ) throws UnsupportedAudioFileException,
  87                                   LineUnavailableException {
  88 
  89         Info info = null;
  90         DataPusher datapusher = null;
  91         DataLine.Info lineinfo = null;
  92         SourceDataLine sourcedataline = null;
  93 
  94         // if ALAW or ULAW, we must convert....
  95         as = Toolkit.getPCMConvertedAudioInputStream(as);
  96 
  97         if( as==null ) {
  98             // could not convert
  99             return;
 100         }
 101 


 139 
 140     }
 141 
 142 
 143 
 144     /**
 145      *  Open an audio channel.
 146      */
 147     public synchronized void openChannel(InputStream in) {
 148 
 149 
 150         if(DEBUG) {
 151             System.out.println("AudioDevice: openChannel");
 152             System.out.println("input stream =" + in);
 153         }
 154 
 155         Info info = null;
 156 
 157         // is this already playing?  if so, then just return
 158         for(int i=0; i<infos.size(); i++) {
 159             info = infos.elementAt(i);
 160             if( info.in == in ) {
 161 
 162                 return;
 163             }
 164         }
 165 
 166 
 167         AudioInputStream as = null;
 168 
 169         if( in instanceof AudioStream ) {
 170 
 171             if ( ((AudioStream)in).midiformat != null ) {
 172 
 173                 // it's a midi file
 174                 try {
 175                     startMidi( ((AudioStream)in).stream, in );
 176                 } catch (Exception e) {
 177                     return;
 178                 }
 179 


 268         // don't forget adjust for a new stream.
 269         notify();
 270     }
 271 
 272 
 273     /**
 274      *  Close an audio channel.
 275      */
 276     public synchronized void closeChannel(InputStream in) {
 277 
 278         if(DEBUG) {
 279             System.out.println("AudioDevice.closeChannel");
 280         }
 281 
 282         if (in == null) return;         // can't go anywhere here!
 283 
 284         Info info;
 285 
 286         for(int i=0; i<infos.size(); i++) {
 287 
 288             info = infos.elementAt(i);
 289 
 290             if( info.in == in ) {
 291 
 292                 if( info.sequencer != null ) {
 293 
 294                     info.sequencer.stop();
 295                     //info.sequencer.close();
 296                     infos.removeElement( info );
 297 
 298                 } else if( info.datapusher != null ) {
 299 
 300                     info.datapusher.stop();
 301                     infos.removeElement( info );
 302                 }
 303             }
 304         }
 305         notify();
 306     }
 307 
 308 


 333     public void play() {
 334 
 335         // $$jb: 06.24.99:  Holdover from old architechture ...
 336         // we now open/close the devices as needed on a per-stream
 337         // basis using the JavaSound API.
 338 
 339         if (DEBUG) {
 340             System.out.println("exiting play()");
 341         }
 342     }
 343 
 344     /**
 345      * Close streams
 346      */
 347     public synchronized void closeStreams() {
 348 
 349         Info info;
 350 
 351         for(int i=0; i<infos.size(); i++) {
 352 
 353             info = infos.elementAt(i);
 354 
 355             if( info.sequencer != null ) {
 356 
 357                 info.sequencer.stop();
 358                 info.sequencer.close();
 359                 infos.removeElement( info );
 360 
 361             } else if( info.datapusher != null ) {
 362 
 363                 info.datapusher.stop();
 364                 infos.removeElement( info );
 365             }
 366         }
 367 
 368 
 369         if (DEBUG) {
 370             System.err.println("Audio Device: Streams all closed.");
 371         }
 372         // Empty the hash table.
 373         infos = new Vector<>();

 374     }
 375 
 376     /**
 377      * Number of channels currently open.
 378      */
 379     public int openChannels() {
 380         return infos.size();
 381     }
 382 
 383     /**
 384      * Make the debug info print out.
 385      */
 386     void setVerbose(boolean v) {
 387         DEBUG = v;
 388     }
 389 
 390 
 391 
 392 
 393