Commit b2104a90 authored by Paul Hunkin's avatar Paul Hunkin

Working on the sound system

parent 9c7acc66
ANDROID_NDK=/home/paul/Projects/gsoc/sdk/android-ndk-r4
......@@ -26,6 +26,7 @@ static long _getTime(void){
}
JNIEnv* mEnv = NULL;
JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
JavaVM* mVM = NULL;
//Main activity
......@@ -35,6 +36,7 @@ jclass mActivityInstance;
jmethodID midCreateGLContext;
jmethodID midFlipBuffers;
jmethodID midEnableFeature;
jmethodID midUpdateAudio;
extern "C" int SDL_main();
extern "C" int Android_OnKeyDown(int keycode);
......@@ -54,6 +56,7 @@ static const int FEATURE_ACCEL = 2;
//Accelerometer data storage
float fLastAccelerometer[3];
/*******************************************************************************
Functions called by JNI
*******************************************************************************/
......@@ -77,8 +80,10 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){
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 @@ extern "C" void Android_EnableFeature(int featureid, bool enabled){
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");
}
......@@ -428,7 +428,7 @@ void testAudio(){
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 @@ int SDL_main( int argc, char **argv )
resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
//testAudio();
testAudio();
/* wait for events */
......
......@@ -29,14 +29,39 @@
#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;
}
......@@ -46,12 +71,10 @@ AndroidAUD_PlayDevice(_THIS)
__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 @@ AndroidAUD_PlayDevice(_THIS)
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 @@ static void
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment