Working on the sound system
authorPaul Hunkin <paul@bieh.net>
Sat, 14 Aug 2010 12:35:21 +1200
changeset 4724 d86332c0fb9b
parent 4723 74da47b2f5b7
child 4725 4eb9d3c7fdd2
Working on the sound system
android/config.cfg
android/testproject/jni/app-android.cpp
android/testproject/jni/lesson05.c
src/audio/android/SDL_androidaudio.c
src/audio/android/SDL_androidaudio.o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/android/config.cfg	Sat Aug 14 12:35:21 2010 +1200
@@ -0,0 +1,1 @@
+ANDROID_NDK=/home/paul/Projects/gsoc/sdk/android-ndk-r4
--- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:58:18 2010 +0200
+++ b/android/testproject/jni/app-android.cpp	Sat Aug 14 12:35:21 2010 +1200
@@ -26,6 +26,7 @@
 }
 
 JNIEnv* mEnv = NULL;
+JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
 JavaVM* mVM = NULL;
 
 //Main activity
@@ -35,6 +36,7 @@
 jmethodID midCreateGLContext;
 jmethodID midFlipBuffers;
 jmethodID midEnableFeature;
+jmethodID midUpdateAudio;
 
 extern "C" int SDL_main();
 extern "C" int Android_OnKeyDown(int keycode);
@@ -54,6 +56,7 @@
 //Accelerometer data storage
 float fLastAccelerometer[3];
 
+
 /*******************************************************************************
                  Functions called by JNI
 *******************************************************************************/	
@@ -77,8 +80,10 @@
     midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
     midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
     midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
+    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
 
-    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
+    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
+        !midUpdateAudio){
         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
     }else{
         __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
@@ -200,3 +205,32 @@
                                 featureid, (int)enabled); 
 }
 
+extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
+
+    //Annoyingly we can't just call into Java from any thread. Because the audio
+    //callback is dispatched from the SDL audio thread (that wasn't made from
+    //java, we have to do some magic here to let the JVM know about the thread.
+    //Because everything it touches on the Java side is static anyway, it's 
+    //not a big deal, just annoying.
+    if(!mAudioThreadEnv){
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
+
+        mJVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
+
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
+    }
+    
+    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
+
+    //blah. We probably should rework this so we avoid the copy. 
+    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
+    
+    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
+
+    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
+                                            midUpdateAudio, arr );
+
+    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
+    
+}
+
--- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:58:18 2010 +0200
+++ b/android/testproject/jni/lesson05.c	Sat Aug 14 12:35:21 2010 +1200
@@ -428,7 +428,7 @@
     
     while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
          //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
-        //SDL_Delay(100);
+        SDL_Delay(100);
     }
 
      __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
@@ -511,7 +511,7 @@
     resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
 
 
-    //testAudio();
+    testAudio();
 
 
     /* wait for events */ 
--- a/src/audio/android/SDL_androidaudio.c	Tue Jul 27 21:58:18 2010 +0200
+++ b/src/audio/android/SDL_androidaudio.c	Sat Aug 14 12:35:21 2010 +1200
@@ -29,13 +29,38 @@
 #include "../SDL_audio_c.h"
 #include "SDL_androidaudio.h"
 
+extern void Android_UpdateAudioBuffer(unsigned char *buf, int len);
+
 #include <android/log.h>
 
 static int
 AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
 {
+    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
+    int valid_datatype = 0;
+    
     //TODO: Sample rates etc
     __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Open\n");
+
+    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
+    if (!this->hidden) {
+        SDL_OutOfMemory();
+        return 0;
+    }
+    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
+
+    while ((!valid_datatype) && (test_format)) {
+        this->spec.format = test_format;
+        switch (test_format) {
+        case AUDIO_S8:
+            /*case AUDIO_S16LSB: */
+            valid_datatype = 1;
+            break;
+        default:
+            test_format = SDL_NextAudioFormat();
+            break;
+        }
+    }
     
     return 1;
 }
@@ -45,13 +70,11 @@
 {
     __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Play\n");
     
-    
 
     //playGenericSound(this->hidden->mixbuf, this->hidden->mixlen);
     
 #if 0
-//    sound->data = this->hidden->mixbuf;/* pointer to raw audio data */
-//    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
+
 //    sound->rate = 22050; /* sample rate = 22050Hz */
 //    sound->vol = 127;    /* volume [0..127] for [min..max] */
 //    sound->pan = 64;     /* balance [0..127] for [left..right] */
@@ -64,6 +87,15 @@
 static Uint8 *
 AndroidAUD_GetDeviceBuf(_THIS)
 {
+     //__android_log_print(ANDROID_LOG_INFO, "SDL", "****** get device buf\n");
+
+     
+    //    sound->data = this->hidden->mixbuf;/* pointer to raw audio data */
+//    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
+
+
+    Android_UpdateAudioBuffer(this->hidden->mixbuf, this->hidden->mixlen);
+    
     return this->hidden->mixbuf;        /* is this right? */
 }
 
@@ -71,12 +103,14 @@
 AndroidAUD_WaitDevice(_THIS)
 {
     /* stub */
+     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** wait device buf\n");
 }
 
 static void
 AndroidAUD_CloseDevice(_THIS)
 {
     /* stub */
+     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** close device buf\n");
 }
 
 static int
Binary file src/audio/android/SDL_androidaudio.o has changed