Permalink
Browse files

audio: Audio stays in sync with video by increasing pitch to catch up.

  • Loading branch information...
fzurita committed Feb 20, 2016
1 parent 7fca2fe commit f2d90cf918ce896775fe5739aa04a938d5d5a461
Showing with 30 additions and 4 deletions.
  1. +30 −4 jni/mupen64plus-audio-sles/main.c
@@ -132,9 +132,10 @@ typedef struct queueData_
{
unsigned char* data;
unsigned int lenght;
unsigned int speedFactor;
} queueData;
void processAudio(const unsigned char* buffer, int length);
void processAudio(const unsigned char* buffer, unsigned int length, unsigned int speedFactor);
static void* audioConsumer(void*);
static pthread_t audioConsumerThread;
static struct threadqueue audioConsumerQueue;
@@ -838,6 +839,7 @@ EXPORT void CALL AiLenChanged(void)
queueData* theQueueData = malloc(sizeof(queueData));
theQueueData->data = malloc(LenReg);
theQueueData->lenght = LenReg;
theQueueData->speedFactor = speed_factor;
memcpy(theQueueData->data, p, LenReg);
@@ -846,6 +848,10 @@ EXPORT void CALL AiLenChanged(void)
void* audioConsumer(void* param)
{
int prevQueueSize = thread_queue_length(&audioConsumerQueue);
int currQueueSize = prevQueueSize;
int maxQueueSize = 3;
int currentSpeedFactorAdjustment = 0;
while(!shutdown)
{
@@ -859,16 +865,34 @@ void* audioConsumer(void* param)
if( result != ETIMEDOUT )
{
queueData* theQueueData = msg.data;
processAudio(theQueueData->data, theQueueData->lenght);
currQueueSize = thread_queue_length(&audioConsumerQueue);
if(currQueueSize > maxQueueSize)
{
int difference = currQueueSize - prevQueueSize;
if(difference >= 0)
{
currentSpeedFactorAdjustment += 1;
}
}
else
{
currentSpeedFactorAdjustment = 0;
}
processAudio(theQueueData->data, theQueueData->lenght, theQueueData->speedFactor + currentSpeedFactorAdjustment);
free(theQueueData->data);
free(msg.data);
//DebugMessage(M64MSG_ERROR, "Current adjustment = %d, Queue length = %d", currentSpeedFactorAdjustment, thread_queue_length(&audioConsumerQueue));
}
}
return 0;
}
void processAudio(const unsigned char* buffer, int length)
void processAudio(const unsigned char* buffer, unsigned int length, unsigned int speedFactor)
{
if (primaryBufferPos + length < primaryBufferBytes)
{
@@ -903,9 +927,11 @@ void processAudio(const unsigned char* buffer, int length)
DebugMessage(M64MSG_WARNING, "AiLenChanged(): Audio primary buffer overflow.");
int newsamplerate = OutputFreq * 100 / speed_factor;
int newsamplerate = OutputFreq * 100 / speedFactor;
int oldsamplerate = GameFreq;
//DebugMessage(M64MSG_ERROR, "sample rate = %d", newsamplerate);
while (primaryBufferPos >= ((secondaryBufferBytes * oldsamplerate) / newsamplerate))
{
pthread_mutex_lock(&(lock.mutex));

0 comments on commit f2d90cf

Please sign in to comment.