changeset 147:e0d34e48bc29

2008-09-26 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java (write): Removed commented out chceck for isStarted. Initialize availableSize to 0. Reduced calls to stream.getAvailableSize. currentFramePosition is now updated on every iteration of the loop. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java Fixed aSupportedFormat to have a proper frameRate. (testFindLineWithFormat): Fixed frame rate paramter in the audio format the test is looking for by using aSupportedFormat.
author Omair Majid <omajid@redhat.com>
date Fri, 26 Sep 2008 12:54:37 -0400
parents 313b9ddd2f04
children f29cbcfbc354
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java
diffstat 2 files changed, 13 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Fri Sep 26 11:51:32 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Fri Sep 26 12:54:37 2008 -0400
@@ -135,11 +135,6 @@
 			throw new IllegalStateException("must call open() before write()");
 		}
 
-		/*
-		 * if (!isStarted) { throw new IllegalStateException("must call start()
-		 * before write()"); }
-		 */
-
 		int frameSize = currentFormat.getFrameSize();
 		if (length % frameSize != 0) {
 			throw new IllegalArgumentException(
@@ -155,7 +150,7 @@
 
 		int position = offset;
 		int remainingLength = length;
-		int availableSize;
+		int availableSize = 0;
 
 		int sizeWritten = 0;
 
@@ -164,16 +159,17 @@
 		while (remainingLength != 0) {
 
 			synchronized (eventLoop.threadLock) {
-				availableSize = stream.getWritableSize();
 
 				do {
+					availableSize = stream.getWritableSize();
+
 					if (availableSize < 0) {
 						return sizeWritten;
 					}
 
 					if (availableSize == 0) {
 						try {
-							eventLoop.threadLock.wait(100);
+							eventLoop.threadLock.wait();
 						} catch (InterruptedException e) {
 							// ignore for now
 							interrupted = true;
@@ -181,28 +177,29 @@
 
 					}
 
-					availableSize = stream.getWritableSize();
-					// System.out.println(availableSize);
-
 				} while (availableSize == 0);
 
 				if (availableSize > remainingLength) {
 					availableSize = remainingLength;
 				}
-				/* write a little bit of the buffer */
 
+				// only write entire frames, so round down avialableSize to a
+				// multiple of frameSize
+				availableSize = (availableSize / frameSize) * frameSize;
+
+				/* write a little bit of the buffer */
 				stream.write(data, position, availableSize);
-				// System.out.println("written " + availableSize);
 
 				sizeWritten += availableSize;
 				position += availableSize;
 				remainingLength -= availableSize;
+
+				currentFramePosition += availableSize / frameSize;
 			}
 		}
 
 		// all the data should have been played by now
 		assert (sizeWritten == length);
-		currentFramePosition += (sizeWritten / getFormat().getFrameSize());
 		/*
 		 * FIXME when the stream is flushed() etc, instead of returning length
 		 * this should unblock and return the the size of data written so far
--- a/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java	Fri Sep 26 11:51:32 2008 -0400
+++ b/unittests/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLineTest.java	Fri Sep 26 12:54:37 2008 -0400
@@ -74,7 +74,7 @@
 	int closed = 0;
 
 	AudioFormat aSupportedFormat = new AudioFormat(
-			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true);
+			AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 44100f, true);
 
 	class ThreadWriter extends Thread {
 		SourceDataLine line;
@@ -481,12 +481,9 @@
 	public void testFindLineWithFormat() throws LineUnavailableException {
 		System.out
 				.println("This test tries to find a line with a valid format");
-		AudioFormat wantedFormat = new AudioFormat(
-				AudioFormat.Encoding.PCM_UNSIGNED, 44100f, 8, 1, 1, 10, true);
-		System.out.println(wantedFormat);
 
 		sourceDataLine = (SourceDataLine) mixer.getLine(new DataLine.Info(
-				SourceDataLine.class, wantedFormat));
+				SourceDataLine.class, aSupportedFormat));
 		sourceDataLine.open();
 		System.out.println(sourceDataLine.getFormat());
 		sourceDataLine.close();