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