static int ANDROIDAUD_OpenAudio(_THIS, const char *devname, int iscapture) { SDL_AudioSpec *audioFormat = &this->spec; int bytesPerSample; JNIEnv * jniEnv = NULL; this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if ( this->hidden == NULL ) { SDL_OutOfMemory(); return(-1); } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); if( ! (this->spec.format == AUDIO_S8 || this->spec.format == AUDIO_S16) ) { __android_log_print(ANDROID_LOG_ERROR, "libSDL", "Application requested unsupported audio format - only S8 and S16 are supported"); return (-1); // TODO: enable format conversion? Don't know how to do that in SDL } bytesPerSample = (audioFormat->format & 0xFF) / 8; audioFormat->format = ( bytesPerSample == 2 ) ? AUDIO_S16 : AUDIO_S8; (*jniVM)->AttachCurrentThread(jniVM, &jniEnv, NULL); if( !jniEnv ) { __android_log_print(ANDROID_LOG_ERROR, "libSDL", "ANDROIDAUD_OpenAudio: Java VM AttachCurrentThread() failed"); return (-1); // TODO: enable format conversion? Don't know how to do that in SDL } audioBufferSize = (*jniEnv)->CallIntMethod( jniEnv, JavaAudioThread, JavaInitAudio, (jint)audioFormat->freq, (jint)audioFormat->channels, (jint)(( bytesPerSample == 2 ) ? 1 : 0), (jint)audioFormat->size); if( audioBufferSize == 0 ) { __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio(): failed to get audio buffer from JNI"); ANDROIDAUD_CloseAudio(this); return(-1); } /* We cannot call DetachCurrentThread() from main thread or we'll crash */ /* (*jniVM)->DetachCurrentThread(jniVM); */ audioFormat->samples = audioBufferSize / bytesPerSample / audioFormat->channels; audioFormat->size = audioBufferSize; SDL_CalculateAudioSpec(&this->spec); return(1); }
static int ANDROIDAUD_OpenAudio (_THIS, const char *devname, int iscapture) { SDL_AudioSpec *audioFormat = &this->spec; #else static int ANDROIDAUD_OpenAudio (_THIS, SDL_AudioSpec *spec) { SDL_AudioSpec *audioFormat = spec; #endif int bytesPerSample; JNIEnv * jniEnv = NULL; this->hidden = NULL; if( ! (audioFormat->format == AUDIO_S8 || audioFormat->format == AUDIO_S16) ) { __android_log_print(ANDROID_LOG_ERROR, "libSDL", "Application requested unsupported audio format - only S8 and S16 are supported"); return (-1); // TODO: enable format conversion? Don't know how to do that in SDL } bytesPerSample = (audioFormat->format & 0xFF) / 8; audioFormat->format = ( bytesPerSample == 2 ) ? AUDIO_S16 : AUDIO_S8; __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio(): app requested audio bytespersample %d freq %d channels %d samples %d", bytesPerSample, audioFormat->freq, (int)audioFormat->channels, (int)audioFormat->samples); if(audioFormat->samples <= 0) audioFormat->samples = 128; // Some sane value if( audioFormat->samples > 32768 ) // Why anyone need so huge audio buffer? { audioFormat->samples = 32768; __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio(): limiting samples size to ", (int)audioFormat->samples); } SDL_CalculateAudioSpec(audioFormat); (*jniVM)->AttachCurrentThread(jniVM, &jniEnv, NULL); if( !jniEnv ) { __android_log_print(ANDROID_LOG_ERROR, "libSDL", "ANDROIDAUD_OpenAudio: Java VM AttachCurrentThread() failed"); return (-1); // TODO: enable format conversion? Don't know how to do that in SDL } // The returned audioBufferSize may be huge, up to 100 Kb for 44100 because user may have selected large audio buffer to get rid of choppy sound audioBufferSize = (*jniEnv)->CallIntMethod( jniEnv, JavaAudioThread, JavaInitAudio, (jint)audioFormat->freq, (jint)audioFormat->channels, (jint)(( bytesPerSample == 2 ) ? 1 : 0), (jint)(audioFormat->size) ); if( audioBufferSize == 0 ) { __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio(): failed to get audio buffer from JNI"); ANDROIDAUD_CloseAudio(this); return(-1); } /* We cannot call DetachCurrentThread() from main thread or we'll crash */ /* (*jniVM)->DetachCurrentThread(jniVM); */ audioFormat->samples = audioBufferSize / bytesPerSample / audioFormat->channels; audioFormat->size = audioBufferSize; __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio(): app opened audio bytespersample %d freq %d channels %d bufsize %d", bytesPerSample, audioFormat->freq, (int)audioFormat->channels, audioBufferSize); SDL_CalculateAudioSpec(audioFormat); #if SDL_VERSION_ATLEAST(1,3,0) return(1); #else return(0); #endif }