< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/SoftAudioPusher.java

Print this page




   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 package com.sun.media.sound;
  26 
  27 import java.io.IOException;
  28 
  29 import javax.sound.sampled.AudioInputStream;
  30 import javax.sound.sampled.SourceDataLine;
  31 
  32 /**
  33  * This is a processor object that writes into SourceDataLine
  34  *
  35  * @author Karl Helgason
  36  */
  37 public final class SoftAudioPusher implements Runnable {
  38 
  39     private volatile boolean active = false;
  40     private SourceDataLine sourceDataLine = null;
  41     private Thread audiothread;
  42     private final AudioInputStream ais;
  43     private final byte[] buffer;
  44 


  53         if (active)
  54             return;
  55         active = true;
  56         audiothread = new Thread(null, this, "AudioPusher", 0, false);
  57         audiothread.setDaemon(true);
  58         audiothread.setPriority(Thread.MAX_PRIORITY);
  59         audiothread.start();
  60     }
  61 
  62     public synchronized void stop() {
  63         if (!active)
  64             return;
  65         active = false;
  66         try {
  67             audiothread.join();
  68         } catch (InterruptedException e) {
  69             //e.printStackTrace();
  70         }
  71     }
  72 

  73     public void run() {
  74         byte[] buffer = SoftAudioPusher.this.buffer;
  75         AudioInputStream ais = SoftAudioPusher.this.ais;
  76         SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;
  77 
  78         try {
  79             while (active) {
  80                 // Read from audio source
  81                 int count = ais.read(buffer);
  82                 if(count < 0) break;
  83                 // Write byte buffer to source output
  84                 sourceDataLine.write(buffer, 0, count);
  85             }
  86         } catch (IOException e) {
  87             active = false;
  88             //e.printStackTrace();
  89         }
  90 
  91     }
  92 }


   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.io.IOException;
  29 
  30 import javax.sound.sampled.AudioInputStream;
  31 import javax.sound.sampled.SourceDataLine;
  32 
  33 /**
  34  * This is a processor object that writes into SourceDataLine
  35  *
  36  * @author Karl Helgason
  37  */
  38 public final class SoftAudioPusher implements Runnable {
  39 
  40     private volatile boolean active = false;
  41     private SourceDataLine sourceDataLine = null;
  42     private Thread audiothread;
  43     private final AudioInputStream ais;
  44     private final byte[] buffer;
  45 


  54         if (active)
  55             return;
  56         active = true;
  57         audiothread = new Thread(null, this, "AudioPusher", 0, false);
  58         audiothread.setDaemon(true);
  59         audiothread.setPriority(Thread.MAX_PRIORITY);
  60         audiothread.start();
  61     }
  62 
  63     public synchronized void stop() {
  64         if (!active)
  65             return;
  66         active = false;
  67         try {
  68             audiothread.join();
  69         } catch (InterruptedException e) {
  70             //e.printStackTrace();
  71         }
  72     }
  73 
  74     @Override
  75     public void run() {
  76         byte[] buffer = SoftAudioPusher.this.buffer;
  77         AudioInputStream ais = SoftAudioPusher.this.ais;
  78         SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;
  79 
  80         try {
  81             while (active) {
  82                 // Read from audio source
  83                 int count = ais.read(buffer);
  84                 if(count < 0) break;
  85                 // Write byte buffer to source output
  86                 sourceDataLine.write(buffer, 0, count);
  87             }
  88         } catch (IOException e) {
  89             active = false;
  90             //e.printStackTrace();
  91         }

  92     }
  93 }
< prev index next >