Make a state object for the Android audio wrapper

This commit is contained in:
Henrik Rydgård committed 2017-08-01 15:28:45 +02:00
1 parent 107e085ec0
commit a453dc8123
3 files changed
+31 -25

No files matched your search

+15 -4
View File
@@ -54,6 +54,8 @@ JNIEnv *jniEnvMain;
JNIEnv *jniEnvGraphics;
JavaVM *javaVM;
static AndroidAudioState *g_audioState;
enum {
ANDROID_VERSION_GINGERBREAD = 9,
ANDROID_VERSION_ICS = 14,
@@ -620,21 +622,30 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_audioInit(JNIEnv *, jclass) {
}
ILOG("NativeApp.audioInit() -- Using OpenSL audio! frames/buffer: %i optimal sr: %i actual sr: %i", optimalFramesPerBuffer, optimalSampleRate, sampleRate);
AndroidAudio_Init(&NativeMix, library_path, framesPerBuffer, sampleRate);
if (!g_audioState) {
g_audioState = AndroidAudio_Init(&NativeMix, library_path, framesPerBuffer, sampleRate);
} else {
ELOG("Audio state already initialized");
}
}
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_audioShutdown(JNIEnv *, jclass) {
AndroidAudio_Shutdown();
if (g_audioState) {
AndroidAudio_Shutdown(g_audioState);
g_audioState = nullptr;
} else {
ELOG("Audio state already shutdown!");
}
}
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_resume(JNIEnv *, jclass) {
ILOG("NativeApp.resume() - resuming audio");
AndroidAudio_Resume();
AndroidAudio_Resume(g_audioState);
}
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_pause(JNIEnv *, jclass) {
ILOG("NativeApp.pause() - pausing audio");
AndroidAudio_Pause();
AndroidAudio_Pause(g_audioState);
}
extern "C" void Java_org_ppsspp_ppsspp_NativeApp_shutdown(JNIEnv *, jclass) {
+10 -17
View File
@@ -2,7 +2,7 @@
#include "android/jni/native_audio.h"
#include "android/jni/native-audio-so.h"
struct AudioState {
struct AndroidAudioState {
void *so;
AndroidAudioCallback callback;
bool playing;
@@ -10,24 +10,16 @@ struct AudioState {
int sample_rate;
};
static AudioState *state = 0;
bool AndroidAudio_Init(AndroidAudioCallback callback, std::string libraryDir, int optimalFramesPerBuffer, int optimalSampleRate) {
if (state != 0) {
ELOG("Audio state already exists");
return false;
}
state = new AudioState();
AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback callback, std::string libraryDir, int optimalFramesPerBuffer, int optimalSampleRate) {
AndroidAudioState *state = new AndroidAudioState();
state->callback = callback;
state->playing = false;
state->frames_per_buffer = optimalFramesPerBuffer ? optimalFramesPerBuffer : 256;
state->sample_rate = optimalSampleRate ? optimalSampleRate : 44100;
return true;
return state;
}
bool AndroidAudio_Resume() {
bool AndroidAudio_Resume(AndroidAudioState *state) {
if (!state) {
ELOG("Audio was shutdown, cannot resume!");
return false;
@@ -42,7 +34,7 @@ bool AndroidAudio_Resume() {
return false;
}
bool AndroidAudio_Pause() {
bool AndroidAudio_Pause(AndroidAudioState *state) {
if (!state) {
ELOG("Audio was shutdown, cannot pause!");
return false;
@@ -57,15 +49,16 @@ bool AndroidAudio_Pause() {
return false;
}
void AndroidAudio_Shutdown() {
bool AndroidAudio_Shutdown(AndroidAudioState *state) {
if (!state) {
ELOG("Audio already shutdown!");
return;
return false;
}
if (state->playing) {
ELOG("Should not shut down when playing! Something is wrong!");
return false;
}
delete state;
state = 0;
ILOG("OpenSLWrap completely unloaded.");
return true;
}
+6 -4
View File
@@ -3,6 +3,8 @@
#include "native-audio-so.h"
#include <string>
struct AndroidAudioState;
// This is the file you should include from your program. It dynamically loads
// the native_audio.so shared object and sets up the function pointers.
@@ -10,7 +12,7 @@
// 2.2, as it will fail miserably.
// It's okay for optimalFramesPerBuffer and optimalSampleRate to be 0. Defaults will be used.
bool AndroidAudio_Init(AndroidAudioCallback cb, std::string libraryDir, int optimalFramesPerBuffer, int optimalSampleRate);
bool AndroidAudio_Pause();
bool AndroidAudio_Resume();
void AndroidAudio_Shutdown();
AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback cb, std::string libraryDir, int optimalFramesPerBuffer, int optimalSampleRate);
bool AndroidAudio_Pause(AndroidAudioState *state);
bool AndroidAudio_Resume(AndroidAudioState *state);
bool AndroidAudio_Shutdown(AndroidAudioState *state);