Mercurial > hg > pulseaudio
view src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java @ 124:7ad349ee575d
2008-09-15 Omair Majid <omajid@redhat.com>
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java
(connectLine): May throw LineUnavailableException now.
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioDataLine.java
(open): Now rethrows exceptions from connectLine.
(connectLine): Throws LineUnavailableException.
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java
(connectLine): Likewise.
* src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java
(connectLine): Likewise.
* src/java/org/classpath/icedtea/pulseaudio/Stream.java
(connectForPlayback): Throws an exception if pa_stream_connect_playback
fails.
(connectForRecording): Throws an exception if pa_stream_connect_record
fails.
* src/native/org_classpath_icedtea_pulseaudio_Stream.c
(Java_org_classpath_icedtea_pulseaudio_Stream_native_1pa_1stream_1connect_1playback):
Dont assert out and die if call to pa_stream_connect_playback fails.
(Java_org_classpath_icedtea_pulseaudio_Stream_native_1pa_1stream_1connect_1record):
Dont assert out and die if the call to pa_stream_connect_record fails.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioMixerRawTest.java
(setUp): Close the mixer if it was open before.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineRawTest.java
(testStartNotificationOnCork): Added a note explaining that this test is
broken.
* unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java
(testDrainTwice): New function. This test drains a mixer twice.
(testFlushTwice): New function. This test flushes a mixer twice.
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Mon, 15 Sep 2008 16:52:31 -0400 |
parents | 6fa7b2b70780 |
children | da120992e52b |
line wrap: on
line source
/* PulseAudioClip.java Copyright (C) 2008 Red Hat, Inc. This file is part of IcedTea. IcedTea is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. IcedTea is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with IcedTea; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package org.classpath.icedtea.pulseaudio; import java.io.IOException; import java.util.concurrent.Semaphore; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import org.classpath.icedtea.pulseaudio.Stream.WriteListener; public class PulseAudioClip extends PulseAudioDataLine implements Clip, PulseAudioPlaybackLine { private byte[] data = null; private boolean muted; private float volume; // these are frame indices. so counted from 0 private int currentFrame = 0; private int frameCount = 0; private int startFrame = 0; private int endFrame = 0; private int framesSinceOpen = 0; public static final String DEFAULT_CLIP_NAME = "Clip"; private Object clipLock = new Object(); private boolean clipThreadStarted; private int loopsLeft; private Semaphore clipSemaphore = new Semaphore(1); private class ClipThread extends Thread { @Override public void run() { clipThreadStarted = true; while (loopsLeft >= 0) { writeFrames(currentFrame, endFrame + 1); if (Thread.interrupted()) { // Thread.currentThread().interrupt(); clipThreadStarted = false; return; } try { // if loop(0) has been called from the mainThread, // wait until loopsLeft has been set clipSemaphore.acquire(); if (loopsLeft == 0) { System.out.println("Reading to the end of the file"); writeFrames(endFrame, getFrameLength()); return; } else { synchronized (clipLock) { currentFrame = startFrame; loopsLeft--; } } clipSemaphore.release(); } catch (InterruptedException e) { return; } } } } private ClipThread clipThread; private void writeFrames(int startingFrame, int lastFrame) { WriteListener writeListener = new WriteListener() { @Override public void update() { eventLoop.threadLock.notifyAll(); } }; stream.addWriteListener(writeListener); int remainingFrames = lastFrame - startingFrame - 1; while (remainingFrames > 0) { synchronized (eventLoop.threadLock) { int availableSize; do { availableSize = stream.getWritableSize(); if (availableSize < 0) { Thread.currentThread().interrupt(); stream.removeWriteListener(writeListener); return; } if (availableSize == 0) { try { eventLoop.threadLock.wait(); } catch (InterruptedException e) { // clean up and return Thread.currentThread().interrupt(); stream.removeWriteListener(writeListener); return; } } } while (availableSize == 0); int framesToWrite = Math.min(remainingFrames, availableSize / getFormat().getFrameSize()); stream.write(data, currentFrame * getFormat().getFrameSize(), framesToWrite * getFormat().getFrameSize()); remainingFrames -= framesToWrite; currentFrame += framesToWrite; if (Thread.interrupted()) { Thread.currentThread().interrupt(); break; } // System.out.println("remaining frames" + remainingFrames); } } stream.removeWriteListener(writeListener); } public PulseAudioClip(EventLoop eventLoop, AudioFormat[] formats, AudioFormat defaultFormat) { supportedFormats = formats; this.eventLoop = eventLoop; this.defaultFormat = defaultFormat; this.currentFormat = defaultFormat; this.volume = PulseAudioVolumeControl.MAX_VOLUME; clipThread = new ClipThread(); } protected void connectLine(int bufferSize) throws LineUnavailableException { StreamBufferAttributes bufferAttributes = new StreamBufferAttributes( bufferSize, bufferSize / 2, bufferSize / 2, bufferSize / 2, 0); stream.connectForPlayback(Stream.DEFAULT_DEVICE, bufferAttributes); } @Override public int available() { return 0; // a clip always returns 0 } @Override public void close() { try { clipThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } PulseAudioMixer mixer = PulseAudioMixer.getInstance(); mixer.removeSourceLine(this); super.close(); } @Override public void drain() { Operation operation; synchronized (eventLoop.threadLock) { operation = stream.drain(); } operation.waitForCompletion(); operation.releaseReference(); } @Override public void flush() { Operation operation; synchronized (eventLoop.threadLock) { operation = stream.flush(); operation.waitForCompletion(); } operation.releaseReference(); } @Override public int getFrameLength() { return frameCount; } @Override public int getFramePosition() { return (int) framesSinceOpen; } @Override public long getLongFramePosition() { synchronized (clipLock) { return framesSinceOpen; } } @Override public long getMicrosecondLength() { if (!isOpen) { return AudioSystem.NOT_SPECIFIED; } synchronized (clipLock) { return frameCount / currentFormat.getFrameSize(); } } @Override public long getMicrosecondPosition() { synchronized (clipLock) { return framesSinceOpen / currentFormat.getFrameSize(); } } @Override public void loop(int count) { System.out.println("Loop " + count + " called"); if (clipThreadStarted && count != 0) { // Do nothing; behavior not specified by the Java API return; } super.start(); synchronized (clipLock) { if (currentFrame > endFrame) { loopsLeft = 0; } else { loopsLeft = count; } } if (!clipThread.isAlive()) { clipThread = new ClipThread(); clipThread.start(); } } @Override public void open() throws LineUnavailableException { throw new IllegalArgumentException("open() on a Clip is not allowed"); } @Override public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException { super.open(format); this.data = new byte[bufferSize]; System.arraycopy(data, offset, this.data, 0, bufferSize); frameCount = bufferSize / format.getFrameSize(); PulseAudioVolumeControl volumeControl = new PulseAudioVolumeControl( this, eventLoop); PulseAudioMuteControl muteControl = new PulseAudioMuteControl(this, volumeControl); controls.add(volumeControl); controls.add(muteControl); PulseAudioMixer mixer = PulseAudioMixer.getInstance(); mixer.addSourceLine(this); isOpen = true; } public byte[] native_setVolume(float value) { return stream.native_setVolume(value); } public boolean isMuted() { return muted; } public void setMuted(boolean value) { muted = value; } public float getVolume() { return this.volume; } public void setVolume(float value) { this.volume = value; } @Override public void open(AudioInputStream stream) throws LineUnavailableException, IOException { byte[] buffer = new byte[(int) (stream.getFrameLength() * stream .getFormat().getFrameSize())]; stream.read(buffer, 0, buffer.length); open(stream.getFormat(), buffer, 0, buffer.length); } @Override public void setFramePosition(int frames) { if (frames > frameCount) { throw new IllegalArgumentException("incorreft frame value"); } synchronized (clipLock) { currentFrame = frames; } } @Override public void setLoopPoints(int start, int end) { if (end == -1) { end = frameCount; } if (end < start) { throw new IllegalArgumentException( "ending point must be greater than or equal to the starting point"); } synchronized (clipLock) { startFrame = start; endFrame = end; } } @Override public void setMicrosecondPosition(long microseconds) { float frameIndex = microseconds * currentFormat.getFrameRate(); synchronized (clipLock) { currentFrame = (int) frameIndex; } } @Override public void start() { if (isStarted) { throw new IllegalStateException("already started"); } super.start(); if (!clipThread.isAlive()) { synchronized (clipLock) { loopsLeft = 0; } clipThread = new ClipThread(); clipThread.start(); } } public void stop() { if (!isStarted) { throw new IllegalStateException("not started, so cant stop"); } if (clipThread.isAlive()) { clipThread.interrupt(); } try { clipThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (clipLock) { currentFrame = 0; loopsLeft = 0; } super.stop(); } }