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 sun.misc.ManagedLocalsThread;
  28 
  29 import java.io.IOException;
  30 
  31 import javax.sound.sampled.AudioInputStream;
  32 import javax.sound.sampled.SourceDataLine;
  33 
  34 /**
  35  * This is a processor object that writes into SourceDataLine
  36  *
  37  * @author Karl Helgason
  38  */
  39 public final class SoftAudioPusher implements Runnable {
  40 
  41     private volatile boolean active = false;
  42     private SourceDataLine sourceDataLine = null;
  43     private Thread audiothread;
  44     private final AudioInputStream ais;
  45     private final byte[] buffer;
  46 
  47     public SoftAudioPusher(SourceDataLine sourceDataLine, AudioInputStream ais,
  48             int workbuffersizer) {
  49         this.ais = ais;
  50         this.buffer = new byte[workbuffersizer];
  51         this.sourceDataLine = sourceDataLine;
  52     }
  53 
  54     public synchronized void start() {
  55         if (active)
  56             return;
  57         active = true;
  58         audiothread = new ManagedLocalsThread(this);
  59         audiothread.setDaemon(true);
  60         audiothread.setPriority(Thread.MAX_PRIORITY);
  61         audiothread.start();
  62     }
  63 
  64     public synchronized void stop() {
  65         if (!active)
  66             return;
  67         active = false;
  68         try {
  69             audiothread.join();
  70         } catch (InterruptedException e) {
  71             //e.printStackTrace();
  72         }
  73     }
  74 
  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     }
  94 }