1 /*
   2  * Copyright (c) 2007, 2015, 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 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 
  45     public SoftAudioPusher(SourceDataLine sourceDataLine, AudioInputStream ais,
  46             int workbuffersizer) {
  47         this.ais = ais;
  48         this.buffer = new byte[workbuffersizer];
  49         this.sourceDataLine = sourceDataLine;
  50     }
  51 
  52     public synchronized void start() {
  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 }