829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.IOException;
829N/A
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.SourceDataLine;
829N/A
829N/A/**
829N/A * This is a processor object that writes into SourceDataLine
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftAudioPusher implements Runnable {
829N/A
829N/A private volatile boolean active = false;
829N/A private SourceDataLine sourceDataLine = null;
829N/A private Thread audiothread;
6321N/A private final AudioInputStream ais;
6321N/A private final byte[] buffer;
829N/A
829N/A public SoftAudioPusher(SourceDataLine sourceDataLine, AudioInputStream ais,
829N/A int workbuffersizer) {
829N/A this.ais = ais;
829N/A this.buffer = new byte[workbuffersizer];
829N/A this.sourceDataLine = sourceDataLine;
829N/A }
829N/A
829N/A public synchronized void start() {
829N/A if (active)
829N/A return;
829N/A active = true;
829N/A audiothread = new Thread(this);
1170N/A audiothread.setDaemon(true);
829N/A audiothread.setPriority(Thread.MAX_PRIORITY);
829N/A audiothread.start();
829N/A }
829N/A
829N/A public synchronized void stop() {
829N/A if (!active)
829N/A return;
829N/A active = false;
829N/A try {
829N/A audiothread.join();
829N/A } catch (InterruptedException e) {
829N/A //e.printStackTrace();
829N/A }
829N/A }
829N/A
829N/A public void run() {
829N/A byte[] buffer = SoftAudioPusher.this.buffer;
829N/A AudioInputStream ais = SoftAudioPusher.this.ais;
829N/A SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;
829N/A
829N/A try {
829N/A while (active) {
829N/A // Read from audio source
829N/A int count = ais.read(buffer);
829N/A if(count < 0) break;
829N/A // Write byte buffer to source output
829N/A sourceDataLine.write(buffer, 0, count);
829N/A }
829N/A } catch (IOException e) {
829N/A active = false;
829N/A //e.printStackTrace();
829N/A }
829N/A
829N/A }
829N/A}