Working on the sound system
authorPaul Hunkin <paul@bieh.net>
Sat, 14 Aug 2010 12:35:21 +1200
changeset 4724d86332c0fb9b
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
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/android/config.cfg	Sat Aug 14 12:35:21 2010 +1200
     1.3 @@ -0,0 +1,1 @@
     1.4 +ANDROID_NDK=/home/paul/Projects/gsoc/sdk/android-ndk-r4
     2.1 --- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:58:18 2010 +0200
     2.2 +++ b/android/testproject/jni/app-android.cpp	Sat Aug 14 12:35:21 2010 +1200
     2.3 @@ -26,6 +26,7 @@
     2.4  }
     2.5  
     2.6  JNIEnv* mEnv = NULL;
     2.7 +JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
     2.8  JavaVM* mVM = NULL;
     2.9  
    2.10  //Main activity
    2.11 @@ -35,6 +36,7 @@
    2.12  jmethodID midCreateGLContext;
    2.13  jmethodID midFlipBuffers;
    2.14  jmethodID midEnableFeature;
    2.15 +jmethodID midUpdateAudio;
    2.16  
    2.17  extern "C" int SDL_main();
    2.18  extern "C" int Android_OnKeyDown(int keycode);
    2.19 @@ -54,6 +56,7 @@
    2.20  //Accelerometer data storage
    2.21  float fLastAccelerometer[3];
    2.22  
    2.23 +
    2.24  /*******************************************************************************
    2.25                   Functions called by JNI
    2.26  *******************************************************************************/	
    2.27 @@ -77,8 +80,10 @@
    2.28      midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    2.29      midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    2.30      midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    2.31 +    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    2.32  
    2.33 -    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
    2.34 +    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
    2.35 +        !midUpdateAudio){
    2.36          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    2.37      }else{
    2.38          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    2.39 @@ -200,3 +205,32 @@
    2.40                                  featureid, (int)enabled); 
    2.41  }
    2.42  
    2.43 +extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
    2.44 +
    2.45 +    //Annoyingly we can't just call into Java from any thread. Because the audio
    2.46 +    //callback is dispatched from the SDL audio thread (that wasn't made from
    2.47 +    //java, we have to do some magic here to let the JVM know about the thread.
    2.48 +    //Because everything it touches on the Java side is static anyway, it's 
    2.49 +    //not a big deal, just annoying.
    2.50 +    if(!mAudioThreadEnv){
    2.51 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
    2.52 +
    2.53 +        mJVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
    2.54 +
    2.55 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
    2.56 +    }
    2.57 +    
    2.58 +    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
    2.59 +
    2.60 +    //blah. We probably should rework this so we avoid the copy. 
    2.61 +    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
    2.62 +    
    2.63 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
    2.64 +
    2.65 +    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
    2.66 +                                            midUpdateAudio, arr );
    2.67 +
    2.68 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
    2.69 +    
    2.70 +}
    2.71 +
     3.1 --- a/android/testproject/jni/lesson05.c	Tue Jul 27 21:58:18 2010 +0200
     3.2 +++ b/android/testproject/jni/lesson05.c	Sat Aug 14 12:35:21 2010 +1200
     3.3 @@ -428,7 +428,7 @@
     3.4      
     3.5      while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
     3.6           //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
     3.7 -        //SDL_Delay(100);
     3.8 +        SDL_Delay(100);
     3.9      }
    3.10  
    3.11       __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
    3.12 @@ -511,7 +511,7 @@
    3.13      resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
    3.14  
    3.15  
    3.16 -    //testAudio();
    3.17 +    testAudio();
    3.18  
    3.19  
    3.20      /* wait for events */ 
     4.1 --- a/src/audio/android/SDL_androidaudio.c	Tue Jul 27 21:58:18 2010 +0200
     4.2 +++ b/src/audio/android/SDL_androidaudio.c	Sat Aug 14 12:35:21 2010 +1200
     4.3 @@ -29,13 +29,38 @@
     4.4  #include "../SDL_audio_c.h"
     4.5  #include "SDL_androidaudio.h"
     4.6  
     4.7 +extern void Android_UpdateAudioBuffer(unsigned char *buf, int len);
     4.8 +
     4.9  #include <android/log.h>
    4.10  
    4.11  static int
    4.12  AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
    4.13  {
    4.14 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
    4.15 +    int valid_datatype = 0;
    4.16 +    
    4.17      //TODO: Sample rates etc
    4.18      __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Open\n");
    4.19 +
    4.20 +    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
    4.21 +    if (!this->hidden) {
    4.22 +        SDL_OutOfMemory();
    4.23 +        return 0;
    4.24 +    }
    4.25 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    4.26 +
    4.27 +    while ((!valid_datatype) && (test_format)) {
    4.28 +        this->spec.format = test_format;
    4.29 +        switch (test_format) {
    4.30 +        case AUDIO_S8:
    4.31 +            /*case AUDIO_S16LSB: */
    4.32 +            valid_datatype = 1;
    4.33 +            break;
    4.34 +        default:
    4.35 +            test_format = SDL_NextAudioFormat();
    4.36 +            break;
    4.37 +        }
    4.38 +    }
    4.39      
    4.40      return 1;
    4.41  }
    4.42 @@ -45,13 +70,11 @@
    4.43  {
    4.44      __android_log_print(ANDROID_LOG_INFO, "SDL", "AndroidAudio Play\n");
    4.45      
    4.46 -    
    4.47  
    4.48      //playGenericSound(this->hidden->mixbuf, this->hidden->mixlen);
    4.49      
    4.50  #if 0
    4.51 -//    sound->data = this->hidden->mixbuf;/* pointer to raw audio data */
    4.52 -//    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
    4.53 +
    4.54  //    sound->rate = 22050; /* sample rate = 22050Hz */
    4.55  //    sound->vol = 127;    /* volume [0..127] for [min..max] */
    4.56  //    sound->pan = 64;     /* balance [0..127] for [left..right] */
    4.57 @@ -64,6 +87,15 @@
    4.58  static Uint8 *
    4.59  AndroidAUD_GetDeviceBuf(_THIS)
    4.60  {
    4.61 +     //__android_log_print(ANDROID_LOG_INFO, "SDL", "****** get device buf\n");
    4.62 +
    4.63 +     
    4.64 +    //    sound->data = this->hidden->mixbuf;/* pointer to raw audio data */
    4.65 +//    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
    4.66 +
    4.67 +
    4.68 +    Android_UpdateAudioBuffer(this->hidden->mixbuf, this->hidden->mixlen);
    4.69 +    
    4.70      return this->hidden->mixbuf;        /* is this right? */
    4.71  }
    4.72  
    4.73 @@ -71,12 +103,14 @@
    4.74  AndroidAUD_WaitDevice(_THIS)
    4.75  {
    4.76      /* stub */
    4.77 +     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** wait device buf\n");
    4.78  }
    4.79  
    4.80  static void
    4.81  AndroidAUD_CloseDevice(_THIS)
    4.82  {
    4.83      /* stub */
    4.84 +     __android_log_print(ANDROID_LOG_INFO, "SDL", "****** close device buf\n");
    4.85  }
    4.86  
    4.87  static int
     5.1 Binary file src/audio/android/SDL_androidaudio.o has changed