From 2b2248f27fc28a589b3fcf7e96a437e71ea1e637 Mon Sep 17 00:00:00 2001 From: pelya Date: Thu, 1 Jul 2010 18:48:00 +0300 Subject: [PATCH] TOTALLY STUPID BUG how could I lose 3 days debugging it. SDL is working perfeclty, that was the application crashing which could not load music file. --- .../project/jni/application/src/mixer.cpp | 81 +++++++++++++++---- .../sdl/src/audio/android/SDL_androidaudio.c | 9 --- alienblaster/project/src/Audio.java | 9 +-- 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/alienblaster/project/jni/application/src/mixer.cpp b/alienblaster/project/jni/application/src/mixer.cpp index 5852b2b58..30b7673ab 100644 --- a/alienblaster/project/jni/application/src/mixer.cpp +++ b/alienblaster/project/jni/application/src/mixer.cpp @@ -24,7 +24,9 @@ using namespace std; #include #include #include +#ifdef ANDROID #include +#endif Mixer * mixerInstance = NULL; @@ -91,13 +93,39 @@ void Mixer::freeMixer() { int Mixer::loadSample(string fileName, int volume) { if (enabled) { if (fn2snd.find(fileName) == fn2snd.end()) { - // open the file for reading - ifstream inputFile ( fileName.c_str(), ios::in); - if (!inputFile.good()) { - cout << "ERROR: file " << fileName << " does not exist!" << endl; - exit(1); - } - mixChunks.push_back(Mix_LoadWAV(fileName.c_str())); + + string fn = fileName; + string fn1 = fn; + __android_log_print(ANDROID_LOG_INFO, "Alien Blaster", (string( "Loading sound " ) + fn).c_str() ); + + // Check if file exist + FILE * inputFile = fopen( fn1.c_str(), "rb"); + if (!inputFile) { + if( fn1.size() > 4 && fn1.find(".wav") != string::npos ) { + fn1 = fn1.substr( 0, fn1.size() - 4 ) + ".ogg"; + inputFile = fopen( fn1.c_str(), "rb"); + } + if (!inputFile) { + cout << "ERROR: file " << fn1 << " does not exist!" << endl; + #ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() ); + #endif + exit(1); + } + } + fclose(inputFile); + + // TODO: error-handling + Mix_Chunk *newSound = Mix_LoadWAV(fn1.c_str()); + if( !newSound ) { + cout << "ERROR: file " << fn1 << " cannot be loaded!" << endl; + #ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() ); + #endif + exit(1); + } + + mixChunks.push_back(newSound); fn2snd[ fileName ] = mixChunks.size() - 1; if ( 0 <= volume && volume < 128 ) { Mix_VolumeChunk( mixChunks[ mixChunks.size() - 1 ], volume ); @@ -151,14 +179,39 @@ void Mixer::fadeOut(int mSecs) { int Mixer::loadMusic( string fn ) { if (enabled) { if (fn2mus.find(fn) == fn2mus.end()) { - // open the file for reading - ifstream inputFile ( fn.c_str(), ios::in); - if (!inputFile.good()) { - cout << "ERROR: file " << fn << " does not exist!" << endl; - exit(1); - } + + __android_log_print(ANDROID_LOG_INFO, "Alien Blaster", (string( "Loading sound " ) + fn).c_str() ); + + string fn1 = fn; + // Check if file exist + FILE * inputFile = fopen( fn1.c_str(), "rb"); + if (!inputFile) { + if( fn1.size() > 4 && fn1.find(".wav") != string::npos ) { + fn1 = fn1.substr( 0, fn1.size() - 4 ) + ".ogg"; + inputFile = fopen( fn1.c_str(), "rb"); + } + if (!inputFile) { + cout << "ERROR: file " << fn1 << " does not exist!" << endl; + #ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() ); + #endif + exit(1); + } + } + fclose(inputFile); + + // TODO: error-handling + Mix_Music *newSound = Mix_LoadMUS(fn1.c_str()); + if( !newSound ) { + cout << "ERROR: file " << fn1 << " cannot be loaded!" << endl; + #ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "Alien Blaster", (string( "Cannot load sound " ) + fn1).c_str() ); + #endif + exit(1); + } + - musics.push_back(Mix_LoadMUS(fn.c_str())); + musics.push_back(newSound); fn2mus[ fn ] = musics.size() - 1; return musics.size() - 1; } diff --git a/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c b/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c index 96757b78a..993d7475b 100644 --- a/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c +++ b/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c @@ -100,7 +100,6 @@ static Uint8 *ANDROIDAUD_GetAudioBuf(_THIS) static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture) { - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio: enter"); SDL_AudioSpec *audioFormat = &this->spec; int bytesPerSample; JNIEnv * jniEnv = NULL; @@ -148,7 +147,6 @@ static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture) SDL_CalculateAudioSpec(&this->spec); - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio: exit, audioBufferSize %d", audioBufferSize); return(1); } @@ -184,7 +182,6 @@ static jmethodID JavaFillBuffer = NULL; static void ANDROIDAUD_ThreadInit(_THIS) { - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_ThreadInit: enter"); jclass JavaAudioThreadClass = NULL; jmethodID JavaInitThread = NULL; jmethodID JavaGetBuffer = NULL; @@ -212,8 +209,6 @@ static void ANDROIDAUD_ThreadInit(_THIS) __android_log_print(ANDROID_LOG_ERROR, "libSDL", "ANDROIDAUD_OpenAudio(): JNI returns a copy of byte array - no audio will be played"); SDL_memset(audioBuffer, this->spec.silence, this->spec.size); - - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_ThreadInit: exit, audioBuffer %p", audioBuffer); }; static void ANDROIDAUD_ThreadDeinit(_THIS) @@ -223,15 +218,12 @@ static void ANDROIDAUD_ThreadDeinit(_THIS) static void ANDROIDAUD_PlayAudio(_THIS) { - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: enter, audiobuffer %p", audioBuffer); jboolean isCopy = JNI_TRUE; (*jniEnvPlaying)->ReleaseByteArrayElements(jniEnvPlaying, audioBufferJNI, (jbyte *)audioBuffer, 0); audioBuffer = NULL; - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: before JavaFillBuffer"); (*jniEnvPlaying)->CallIntMethod( jniEnvPlaying, JavaAudioThread, JavaFillBuffer ); - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: after JavaFillBuffer"); audioBuffer = (unsigned char *) (*jniEnvPlaying)->GetByteArrayElements(jniEnvPlaying, audioBufferJNI, &isCopy); if( !audioBuffer ) @@ -239,7 +231,6 @@ static void ANDROIDAUD_PlayAudio(_THIS) if( isCopy == JNI_TRUE ) __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio() JNI returns a copy of byte array - that's slow"); - __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio: exit audioBuffer %p", audioBuffer); } #ifndef SDL_JAVA_PACKAGE_PATH diff --git a/alienblaster/project/src/Audio.java b/alienblaster/project/src/Audio.java index 213048c76..f386f4a34 100644 --- a/alienblaster/project/src/Audio.java +++ b/alienblaster/project/src/Audio.java @@ -34,14 +34,7 @@ class AudioThread { public int fillBuffer() { - Log.i("libSDL", "JNI: fillBuffer() enter, mAudioBuffer len " + String.valueOf(mAudioBuffer.length)); - int ret = 0; - try{ - ret = mAudio.write( mAudioBuffer, 0, mAudioBuffer.length ); - } catch( Throwable t ) { - Log.i("libSDL", "JNI: fillBuffer() caught exception!"); - } - Log.i("libSDL", "JNI: fillBuffer() exit, written " + String.valueOf(ret)); + mAudio.write( mAudioBuffer, 0, mAudioBuffer.length ); return 1; }