Permalink
Browse files

audio: Soundtouch can now be used within the SLES audio plugin

  • Loading branch information...
fzurita committed Feb 21, 2016
1 parent 0df3f79 commit 821673d05b9b09e39ee1c6dc32460e9a1a22ab8b
View
@@ -6,6 +6,7 @@ SDL_INCLUDES := $(JNI_LOCAL_PATH)/SDL2/include/
PNG_INCLUDES := $(JNI_LOCAL_PATH)/png/include/
SAMPLERATE_INCLUDES := $(JNI_LOCAL_PATH)/libsamplerate/
FREETYPE_INCLUDES := $(JNI_LOCAL_PATH)/freetype/include/
+SOUNDTOUCH_INCLUDES := $(JNI_LOCAL_PATH)/soundtouch/include/
COMMON_CFLAGS := \
-O3 \
@@ -24,20 +24,23 @@ include $(CLEAR_VARS)
LOCAL_MODULE := mupen64plus-audio-sles
LOCAL_STATIC_LIBRARIES := samplerate
+LOCAL_SHARED_LIBRARIES := soundtouch
LOCAL_C_INCLUDES := \
$(M64P_API_INCLUDES) \
$(SAMPLERATE_INCLUDES) \
+ $(SOUNDTOUCH_INCLUDES) \
LOCAL_SRC_FILES := \
- main.c \
- osal_dynamiclib_unix.c \
- threadqueue.c \
+ main.cpp \
+ osal_dynamiclib_unix.cpp \
+ threadqueue.cpp \
LOCAL_CFLAGS := \
$(COMMON_CFLAGS) \
-DUSE_SRC \
+ -fpermissive
-LOCAL_LDLIBS := -lOpenSLES -L$(SYSROOT)/usr/lib -llog
+LOCAL_LDLIBS := -lOpenSLES -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
@@ -32,6 +32,7 @@
#include <pthread.h>
#include <errno.h>
#include <math.h>
+#include <SoundTouch.h>
#ifdef USE_SRC
#include <samplerate.h>
@@ -145,6 +146,9 @@ static struct threadqueue audioConsumerQueue;
static volatile int shutdown = 1;
static volatile int matchGameToAudio = 1;
+using namespace soundtouch;
+static SoundTouch soundTouch;
+
/* Samplerate*/
#ifdef USE_SRC
static float *_src = NULL;
@@ -352,6 +356,13 @@ static int CreateSecondaryBuffers(void)
return status;
}
+void OnInitFailure(void)
+{
+ DebugMessage(M64MSG_ERROR, "Couldn't open OpenSLES audio");
+ CloseAudio();
+ critical_failure = 1;
+}
+
static void InitializeAudio(int freq)
{
SLuint32 sample_rate;
@@ -398,32 +409,68 @@ static void InitializeAudio(int freq)
CloseAudio();
/* Create primary buffer */
- if(!CreatePrimaryBuffer()) goto failure;
+ if(!CreatePrimaryBuffer())
+ {
+ OnInitFailure();
+ return;
+ }
/* Create secondary buffers */
- if(!CreateSecondaryBuffers()) goto failure;
+ if(!CreateSecondaryBuffers())
+ {
+ OnInitFailure();
+ return;
+ }
/* Create thread Locks to ensure synchronization between callback and processing code */
- if (pthread_mutex_init(&(lock.mutex), (pthread_mutexattr_t*) NULL) != 0) goto failure;
- if (pthread_cond_init(&(lock.cond), (pthread_condattr_t*) NULL) != 0) goto failure;
+ if (pthread_mutex_init(&(lock.mutex), (pthread_mutexattr_t*) NULL) != 0)
+ {
+ OnInitFailure();
+ return;
+ }
+ if (pthread_cond_init(&(lock.cond), (pthread_condattr_t*) NULL) != 0)
+ {
+ OnInitFailure();
+ return;
+ }
lock.value = lock.limit = SecondaryBufferNbr;
/* Engine object */
SLresult result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* Output mix object */
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, SecondaryBufferNbr};
@@ -441,39 +488,57 @@ static void InitializeAudio(int freq)
const SLInterfaceID ids1[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
const SLboolean req1[] = {SL_BOOLEAN_TRUE};
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &(playerObject), &audioSrc, &audioSnk, 1, ids1, req1);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* Realize the player */
result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* Get the play interface */
result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &(playerPlay));
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* Get the buffer queue interface */
result = (*playerObject)->GetInterface(playerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &(bufferQueue));
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* register callback on the buffer queue */
result = (*bufferQueue)->RegisterCallback(bufferQueue, queueCallback, &lock);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
/* set the player's state to playing */
result = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
- if(result != SL_RESULT_SUCCESS) goto failure;
+ if(result != SL_RESULT_SUCCESS)
+ {
+ OnInitFailure();
+ return;
+ }
thread_queue_init(&audioConsumerQueue);
shutdown = 0;
pthread_create( &audioConsumerThread, NULL, audioConsumer, NULL);
return;
-
-failure:
- DebugMessage(M64MSG_ERROR, "Couldn't open OpenSLES audio");
- CloseAudio();
- critical_failure = 1;
- return;
}
static int resample(unsigned char *input, int input_avail, int oldsamplerate, unsigned char *output, int output_needed, int newsamplerate)
@@ -517,13 +582,13 @@ static int resample(unsigned char *input, int input_avail, int oldsamplerate, un
{
if(_src) free(_src);
_src_len = input_avail*2;
- _src = malloc(_src_len);
+ _src = (float*)malloc(_src_len);
}
if (_dest_len < output_needed*2 && output_needed > 0)
{
if(_dest) free(_dest);
_dest_len = output_needed*2;
- _dest = malloc(_dest_len);
+ _dest = (float*)malloc(_dest_len);
}
memset(_src,0,_src_len);
memset(_dest,0,_dest_len);
@@ -842,8 +907,8 @@ EXPORT void CALL AiLenChanged(void)
unsigned int LenReg = *AudioInfo.AI_LEN_REG;
unsigned char * p = AudioInfo.RDRAM + (*AudioInfo.AI_DRAM_ADDR_REG & 0xFFFFFF);
- queueData* theQueueData = malloc(sizeof(queueData));
- theQueueData->data = malloc(LenReg);
+ queueData* theQueueData = (queueData*)malloc(sizeof(queueData));
+ theQueueData->data = (unsigned char*)malloc(LenReg);
theQueueData->lenght = LenReg;
theQueueData->speedFactor = speed_factor;
@@ -860,6 +925,9 @@ double TimeDiff(struct timespec* currTime, struct timespec* prevTime)
void* audioConsumer(void* param)
{
+ soundTouch.setSampleRate(GameFreq);
+ soundTouch.setChannels(2);
+
int prevQueueSize = thread_queue_length(&audioConsumerQueue);
int currQueueSize = prevQueueSize;
int maxQueueSize = 7;
@@ -903,7 +971,7 @@ void* audioConsumer(void* param)
if( result != ETIMEDOUT )
{
prevQueueData = currQueueData;
- currQueueData = msg.data;
+ currQueueData = (queueData*)msg.data;
if(prevQueueData)
{
@@ -23,7 +23,7 @@ static inline struct msglist *get_msglist(struct threadqueue *queue)
queue->msgpool = tmp->next;
queue->msgpool_length--;
} else {
- tmp = malloc(sizeof *tmp);
+ tmp = (msglist*)malloc(sizeof *tmp);
}
return tmp;
@@ -39,11 +39,11 @@ LOCAL_LDLIBS += -llog
# Custom Flags:
# -fvisibility=hidden : don't export all symbols
-LOCAL_CFLAGS += -fvisibility=hidden -I $(LOCAL_PATH)/../../../include -fdata-sections -ffunction-sections -fexceptions
+LOCAL_CFLAGS += -I $(LOCAL_PATH)/../../../include -fdata-sections -ffunction-sections -fexceptions
# OpenMP mode : enable these flags to enable using OpenMP for parallel computation
-#LOCAL_CFLAGS += -fopenmp
-#LOCAL_LDFLAGS += -fopenmp
+LOCAL_CFLAGS += -fopenmp
+LOCAL_LDFLAGS += -fopenmp
# Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs

0 comments on commit 821673d

Please sign in to comment.