changeset 33:3e7111680ba6

added write() to SourceDataLine committer: Ioana Ivan <iivan@redhat.com>
author Ioana Ivan <iivan@redhat.com>
date Fri, 01 Aug 2008 11:27:40 -0400
parents b9836224602b
children a22db30b1290 7dbea4cf0170
files src/org/openjdk/sound/PulseAudioMixer.java src/org/openjdk/sound/PulseAudioSourceDataLine.java src/org_openjdk_sound_PulseAudioSourceDataLine.c src/org_openjdk_sound_PulseAudioSourceDataLine.h
diffstat 4 files changed, 58 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/openjdk/sound/PulseAudioMixer.java	Fri Aug 01 10:59:19 2008 -0400
+++ b/src/org/openjdk/sound/PulseAudioMixer.java	Fri Aug 01 11:27:40 2008 -0400
@@ -353,6 +353,16 @@
 		AudioFormat	audioFormat = audioInputStream.getFormat();
 		line.open(audioFormat);
 		line.start();
+		byte[]	abData = new byte[1000];
+		int bytesRead = 0;
+
+		while ( bytesRead >= 0) {
+			bytesRead = audioInputStream.read(abData, 0, abData.length);
+			if (bytesRead > 0) {
+					
+		    		line.write(abData, 0, bytesRead);
+		    	}
+	    }
 		selectedMixer.close();
 		
 
--- a/src/org/openjdk/sound/PulseAudioSourceDataLine.java	Fri Aug 01 10:59:19 2008 -0400
+++ b/src/org/openjdk/sound/PulseAudioSourceDataLine.java	Fri Aug 01 11:27:40 2008 -0400
@@ -16,6 +16,7 @@
 import javax.sound.sampled.Control.Type;
 
 
+
 public class PulseAudioSourceDataLine implements SourceDataLine {
 
 	
@@ -40,7 +41,7 @@
 	private native void native_open(int contextPointer, String name, String encoding, float rate, int size,
 			int channels, boolean bigEndian, int bufferSize);
 
-	private native void native_write(byte[] data, int offset, int length);
+	private native void native_write(byte[] data, int length);
 
 	private native int native_get_writable_size();
 
@@ -101,13 +102,36 @@
 	}
 
 	public int write(byte[] data, int off, int len) {
-		// FIXME
-		
-		
-		synchronized (eventLoop.threadLock) {
-			native_write(data, off, len);
+		byte[] buffer;
+		int position = off;
+		int remainingLength = len;
+		int availableSize;
+		// 
+		while (remainingLength != 0) {
+
+			synchronized (eventLoop.threadLock) {
+				availableSize = available();
+				if (availableSize < 0) {
+					//throw new StreamFailed();
+				}
+				if (availableSize > remainingLength) {
+					availableSize = remainingLength;
+				}
+				buffer = new byte[availableSize];
+				System.arraycopy(data, position, buffer, 0, availableSize);
+				/* write a little bit of the buffer */
+				native_write(buffer, buffer.length);
+
+				position += availableSize;
+				remainingLength -= availableSize;
+
+			}
 		}
 
+		// all the data should have been played by now
+
+	
+
 		/*
 		 * FIXME when the stream is flushed() etc, instead of returning length
 		 * this should unblock and return the the size of data written so far
@@ -183,7 +207,11 @@
 		return isOpen;
 	}
 
-	public native int available();
+	public int available() {
+		synchronized(eventLoop.threadLock) {
+			return native_get_writable_size();
+		}
+	};
 
 	public void close() {
 		synchronized (eventLoop.threadLock) {
--- a/src/org_openjdk_sound_PulseAudioSourceDataLine.c	Fri Aug 01 10:59:19 2008 -0400
+++ b/src/org_openjdk_sound_PulseAudioSourceDataLine.c	Fri Aug 01 11:27:40 2008 -0400
@@ -142,13 +142,13 @@
  * Signature: ([BII)V
  */
 JNIEXPORT void JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_native_1write
-(JNIEnv* env, jobject obj, jbyteArray arr, jint int1, jint int2) {
+(JNIEnv* env, jobject obj, jbyteArray data, jint data_length) {
 
-//	pa_stream* stream = (pa_stream*) streamPointer;
-//	jbyte* data_buffer = (*env)->GetByteArrayElements(env, data, NULL);
-//
-//	pa_stream_write(stream, data_buffer, data_length, NULL, 0, PA_SEEK_RELATIVE);
-//	(*env)->ReleaseByteArrayElements(env, data, data_buffer, 0);
+	pa_stream *stream = getJavaIntField(env, obj, "streamPointer");
+	jbyte* data_buffer = (*env)->GetByteArrayElements(env, data, NULL);
+
+	pa_stream_write(stream, data_buffer, data_length, NULL, 0, PA_SEEK_RELATIVE);
+	(*env)->ReleaseByteArrayElements(env, data, data_buffer, 0);
 	
 }
 
@@ -160,12 +160,12 @@
 JNIEXPORT jint JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_native_1get_1writable_1size
 (JNIEnv* env, jobject obj) {
 	
-//	jfieldID fid = (*env)->GetIntField
-//	
-//	pa_stream* stream = (pa_stream*)streamPointer;
-//	return pa_stream_writable_size(stream);
+	pa_stream *stream = getJavaIntField(env, obj, "streamPointer");
+  	int available = pa_stream_writable_size(stream);
+    
+ }
 	
-}
+
 
 /*
  * Class:     org_openjdk_sound_PulseAudioSourceDataLine
@@ -233,13 +233,5 @@
 
 }
 
-/*
- * Class:     org_openjdk_sound_PulseAudioSourceDataLine
- * Method:    available
- * Signature: ()I
- */
-JNIEXPORT jint JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_available
-(JNIEnv* env, jobject obj) {
 
-}
 
--- a/src/org_openjdk_sound_PulseAudioSourceDataLine.h	Fri Aug 01 10:59:19 2008 -0400
+++ b/src/org_openjdk_sound_PulseAudioSourceDataLine.h	Fri Aug 01 11:27:40 2008 -0400
@@ -20,10 +20,10 @@
 /*
  * Class:     org_openjdk_sound_PulseAudioSourceDataLine
  * Method:    native_write
- * Signature: ([BII)V
+ * Signature: ([BI)V
  */
 JNIEXPORT void JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_native_1write
-  (JNIEnv *, jobject, jbyteArray, jint, jint);
+  (JNIEnv *, jobject, jbyteArray, jint);
 
 /*
  * Class:     org_openjdk_sound_PulseAudioSourceDataLine
@@ -81,14 +81,6 @@
 JNIEXPORT void JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_native_1close
   (JNIEnv *, jobject);
 
-/*
- * Class:     org_openjdk_sound_PulseAudioSourceDataLine
- * Method:    available
- * Signature: ()I
- */
-JNIEXPORT jint JNICALL Java_org_openjdk_sound_PulseAudioSourceDataLine_available
-  (JNIEnv *, jobject);
-
 #ifdef __cplusplus
 }
 #endif