Skip to content

Commit

Permalink
Merge pull request #2 from park671/feature/opensl
Browse files Browse the repository at this point in the history
opensl功能稳定了,替换掉audio tracker
  • Loading branch information
park671 authored May 18, 2023
2 parents c4669b4 + 52e6fc6 commit f771c09
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#include "../opt/mem_opt.h"
#include "mm_sound.h"
#include "opensl.h"
#include "../global.h"
#include <android/log.h>
#include <unistd.h>

#define SAMPLE_RATE 16000
constexpr const uint64_t kFramesToBuffer = 10240;
constexpr const uint64_t kFramesToBuffer = 1024;

#define CACHE_SIZE 3

Expand All @@ -46,6 +47,7 @@ struct Nsf2WavOptions {
};

pthread_mutex_t soundMutex;
pthread_mutex_t cacheIdxMutex;
xgm::NSFPlayer player;
xgm::NSF nsf;

Expand All @@ -67,7 +69,7 @@ void mixSoundBuffer(short *buffer1, short *buffer2, int len) {
}
}

[[noreturn]] void *nsfPlayerThread(void *) {
void *nsfPlayerThread(void *) {
xgm::NSFPlayerConfig config;
Nsf2WavOptions options(nsf);
nsf.SetDefaults(options.length_ms, options.fade_ms, nsf.default_loopnum);
Expand Down Expand Up @@ -107,6 +109,7 @@ void mixSoundBuffer(short *buffer1, short *buffer2, int len) {
}
fc = kFramesToBuffer;
pthread_mutex_lock(&soundMutex);
pthread_mutex_lock(&cacheIdxMutex);
player.Render((short *)cachedData[(cacheIdx) % CACHE_SIZE], fc);
if(mixCache) {
mixSoundBuffer((short *)cachedData[(cacheIdx) % CACHE_SIZE],
Expand All @@ -119,25 +122,28 @@ void mixSoundBuffer(short *buffer1, short *buffer2, int len) {
}
cacheIdx++;
pthread_mutex_unlock(&soundMutex);
pthread_mutex_unlock(&cacheIdxMutex);
}
return nullptr;
}

char isInited = 0;

extern "C" void initSL() {
//曾经这里有一堆opensl的屎山代码,怎么调都有bug(破音,降调,加速...
//后来我弃暗投明选择了传回java用audio tracker
//以后有缘我再尝试opensl吧
startSLEngine();
if(isInited) {
return;
}
startSLEngine();
isInited = 1;
pthread_t id;
//创建函数线程,并且指定函数线程要执行的函数
pthread_create(&id, nullptr, nsfPlayerThread, nullptr);
}

extern "C" void releaseSL() {
stopSLEngine();
}

extern "C" int getAudioCount() {
return nsf.GetSongNum();
}
Expand Down Expand Up @@ -170,5 +176,17 @@ extern "C" short *getAudioBuffer() {
if(readIdx >= cacheIdx) {
return nullptr;
}
if(readIdx > 100000) {
pthread_mutex_lock(&cacheIdxMutex);
logd("native_sound", "reset cacheIdx");
if (readIdx < cacheIdx) {
cacheIdx %= CACHE_SIZE;
readIdx %= CACHE_SIZE;
if(readIdx >= cacheIdx) {
cacheIdx += CACHE_SIZE;
}
}
pthread_mutex_unlock(&cacheIdxMutex);
}
return (short *)cachedData[(readIdx++) % CACHE_SIZE];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define SUPERCUBE_NATIVE_SLES_H

extern "C" void initSL();
extern "C" void releaseSL();
extern "C" short *getAudioBuffer();
extern "C" void changeAudio(int audioIdx);
extern "C" void renderCache(int preRender);
Expand Down
35 changes: 23 additions & 12 deletions app/src/main/cpp/audio/opensl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//

#include "opensl.h"
#include "native_sles.h"
#include "native_sound.h"
#include "../opt/mem_opt.h"
#include "../global.h"

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
Expand Down Expand Up @@ -36,14 +37,24 @@ static pthread_mutex_t audioEngineLock = PTHREAD_MUTEX_INITIALIZER;
static const SLEnvironmentalReverbSettings reverbSettings =
SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

volatile bool isRunning = false;

void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) {
if(!isRunning) {
pthread_mutex_unlock(&audioEngineLock);
return;
}
assert(bq == bqPlayerBufferQueue);
assert(NULL == context);
SLresult result;
// usleep(1000*64);
void * buffer = getAudioBuffer();
while(buffer == nullptr) {
usleep(1000*100);
buffer = getAudioBuffer();
}
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue,
getAudioBuffer(),
10240);
buffer,
1024 * 2);
assert(result == SL_RESULT_SUCCESS);
}

Expand Down Expand Up @@ -95,7 +106,8 @@ static short *slientBuf;

void createBufferQueueAudioPlayer(int sampleRate) {
SLresult result;
slientBuf = (short *) malloc(sizeof(short) * 10240);
slientBuf = (short *) malloc(sizeof(short) * 1024);
__memset_aarch64(slientBuf, 0, sizeof(short) * 1024);
if (sampleRate >= 0) {
bqPlayerSampleRate = sampleRate * 1000;
}
Expand Down Expand Up @@ -163,11 +175,6 @@ void createBufferQueueAudioPlayer(int sampleRate) {
(void) result;
}

// get the mute/solo interface
// result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_MUTESOLO, &bqPlayerMuteSolo);
// assert(SL_RESULT_SUCCESS == result);
// (void)result;

// get the volume interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
assert(SL_RESULT_SUCCESS == result);
Expand All @@ -180,14 +187,13 @@ void createBufferQueueAudioPlayer(int sampleRate) {
}

void *openslThread(void *) {
usleep(1000 * 1000);
createEngine();
isRunning = true;
int sampleRate = SAMPLE_RATE;
createBufferQueueAudioPlayer(sampleRate);
if (pthread_mutex_trylock(&audioEngineLock)) {
return nullptr;
}
__memset_aarch64(slientBuf, 0, sizeof(short) * 1024);
SLresult result;
(*bqPlayerVolume)->EnableStereoPosition(bqPlayerVolume, SL_BOOLEAN_TRUE);
(*bqPlayerVolume)->SetStereoPosition(bqPlayerVolume, 1);
Expand All @@ -200,7 +206,12 @@ void *openslThread(void *) {
}

void startSLEngine() {
logd("opensl", "startSLEngine()");
pthread_t id;
//创建函数线程,并且指定函数线程要执行的函数
pthread_create(&id, nullptr, openslThread, nullptr);
}

void stopSLEngine() {
isRunning = false;
}
1 change: 1 addition & 0 deletions app/src/main/cpp/audio/opensl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
#define METALMAX_OPENSL_H

void startSLEngine();
void stopSLEngine();

#endif //METALMAX_OPENSL_H
18 changes: 7 additions & 11 deletions app/src/main/cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <android/bitmap.h>
#include <android/log.h>
#include <android/native_window_jni.h>
#include "graphic/native_gl.h"
#include "audio/native_sles.h"
#include "graphic/native_graphic.h"
#include "audio/native_sound.h"
#include "charset/charsets.h"
#include "graphic/palette_data.h"
#include "opt/mem_opt.h"
Expand Down Expand Up @@ -45,33 +45,29 @@ jboolean nativeCharToJavaBmp(JNIEnv *env, jclass clazz, jobject bitmap, jstring
return true;
}

void getAudioBufferJNI(JNIEnv *env, jclass clazz, jshortArray array) {
short *buffer = getAudioBuffer();
if(buffer != nullptr) {
env->SetShortArrayRegion(array, 0, 1024, buffer);
}
}

void slInit(JNIEnv *env, jclass clazz) {
initSL();
}

void slRelease(JNIEnv *env, jclass clazz) {
releaseSL();
}

void initNativeWindow(JNIEnv *env, jclass clazz, jobject surface) {
ANativeWindow* mANativeWindow = ANativeWindow_fromSurface(env, surface);
initGraphic(mANativeWindow);
}

void releaseNativeWindow(JNIEnv *env, jclass clazz) {
releaseGraphic();
releaseLogicThread();
}

static JNINativeMethod methods[] = {
{"commonTest", "()V", (void *) &commonTest},
{"onKeyEvent", "(I)V", (void *) &onKeyEvent},
{"onFuncKeyEvent", "(I)V", (void *) &onFuncKeyEvent},
{"getAudioBuffer", "([S)V", (void *) &getAudioBufferJNI},
{"slInit", "()V", (void *) &slInit},
{"slRelease", "()V", (void *) &slRelease},
{"initNativeWindow", "(Landroid/view/Surface;)V", (void *) &initNativeWindow},
{"releaseNativeWindow", "()V", (void *) &releaseNativeWindow},
{"getCharImg", "(Landroid/graphics/Bitmap;Ljava/lang/String;)Z", (void *) &nativeCharToJavaBmp},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//
// Created by youngpark on 2023/4/23.
// EGL & FrameBuffer & OpenGL
//

#include "native_gl.h"
#include "native_graphic.h"
#include "../opt/matrix.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -215,27 +216,6 @@ void initTextures() {
checkGlError("textures");
}

/**
* debug color code
*/
//unsigned char colorBlockBuffer[256][256];
//
//void initRenderBuffer() {
// unsigned char color = 0;
// for (int j = 0; j < 16; j++) {
// for (int i = 0; i < 16; i++) {
// int startX = i*16;
// int startY = j*16;
// for(int x = startX; x<startX+16;x++) {
// for(int y = startY;y<startY+16;y++) {
// colorBlockBuffer[x][y] = color;
// }
// }
// color++;
// }
// }
//}

void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
program = loadProgram(VERTEX_SHADER, FRAGMENT_SHADER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by youngpark on 2023/4/23.
//

#ifndef METALMAX_NATIVE_GL_H
#define METALMAX_NATIVE_GL_H
#ifndef METALMAX_NATIVE_GRAPHIC_H
#define METALMAX_NATIVE_GRAPHIC_H

#include <android/native_window.h>
#include "GLES2/gl2.h"
Expand All @@ -14,4 +14,4 @@ void updateScreenBuffer(unsigned char * buffer);
void initGraphic(ANativeWindow* window);
void releaseGraphic();

#endif //METALMAX_NATIVE_GL_H
#endif //METALMAX_NATIVE_GRAPHIC_H
2 changes: 1 addition & 1 deletion app/src/main/cpp/logic/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "logic.h"
#include "../maps/map.h"
#include "../charset/charsets.h"
#include "../graphic/native_gl.h"
#include "../graphic/native_graphic.h"
#include "render_map.h"
#include "render.h"

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/logic/render_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../monster/monster.h"
#include "../opt/mem_opt.h"
#include "../charset/charsets.h"
#include "../audio/native_sles.h"
#include "../audio/native_sound.h"

void renderBackground(byte *screenBuffer) {
__memset_aarch64(screenBuffer, 3, 256 * 256);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/logic/render_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../monster/monster.h"
#include "../opt/mem_opt.h"
#include "render_battle.h"
#include "../audio/native_sles.h"
#include "../audio/native_sound.h"
#include "../maps/map.h"

const char chinese_demo[4] = {0, 1, 2, 3};
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/logic/render_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "render_map.h"
#include "../maps/map.h"
#include "render_debug.h"
#include "../audio/native_sles.h"
#include "../audio/native_sound.h"
#include "render_battle.h"
#include "../charset/charsets.h"

Expand Down
Loading

0 comments on commit f771c09

Please sign in to comment.