Permalink
Browse files

Make a state object for the Android audio wrapper

  • Loading branch information...
hrydgard committed Aug 1, 2017
1 parent 107e085 commit a453dc81236fce2f2ad2fa169bbadc5c020c056c
Showing with 31 additions and 25 deletions.
  1. +15 −4 android/jni/app-android.cpp
  2. +10 −17 android/jni/native_audio.cpp
  3. +6 −4 android/jni/native_audio.h
@@ -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) {
@@ -2,32 +2,24 @@
#include "android/jni/native_audio.h"
#include "android/jni/native-audio-so.h"
struct AudioState {
struct AndroidAudioState {
void *so;
AndroidAudioCallback callback;
bool playing;
int frames_per_buffer;
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;
}
View
@@ -3,14 +3,16 @@
#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.
// Do not call this if you have detected that the android version is below
// 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);

0 comments on commit a453dc8

Please sign in to comment.