Permalink
Browse files

audio: Attempt at fixing the syncing issues

  • Loading branch information...
fzurita committed Feb 27, 2016
1 parent c4241e2 commit 974c8be8355c3a6ebfd8641de820663a3356c708
Showing with 22 additions and 78 deletions.
  1. +22 −78 jni/mupen64plus-audio-sles/main.cpp
@@ -121,7 +121,6 @@ static pthread_t audioConsumerThread;
static struct threadqueue audioConsumerQueue;
static volatile bool shutdown = true;
static volatile bool matchGameToAudio = true;
using namespace soundtouch;
static SoundTouch soundTouch;
@@ -788,17 +787,12 @@ void* audioConsumer(void* param)
int prevQueueSize = thread_queue_length(&audioConsumerQueue);
int currQueueSize = prevQueueSize;
int maxQueueSize = TargetSecondaryBuffers;
int maxQueueSize = TargetSecondaryBuffers + 10;
int minQueueSize = TargetSecondaryBuffers;
int desiredGameSpeed = 100;
static const int fullSpeed = 100;
//Game is being sped up, speed up audio
int matchGameToAudioOffset = 0;
int matchGameToAudioCurrentPeriod = 0;
const int matchGameToAudioAdjustmentPeriod = 30;
//Sound queue ran dry, device is running slow
int ranDry = 0;
@@ -856,75 +850,37 @@ void* audioConsumer(void* param)
feedTimesSet = true;
}
//If the intention is to allow the game run as close to 100% as possible
//while keeping the audio in sync
//TODO: First two conditions are not needed with fixed core speed limiter
if(matchGameToAudio)
//Game is running too fast speed up audio
if(queueLength > maxQueueSize)
{
//Game is running too fast, slow it down a little
if(queueLength > maxQueueSize && slowAdjustment >= 1.0 && isSpeedLimiterEnabled())
{
++matchGameToAudioCurrentPeriod;
if(matchGameToAudioCurrentPeriod >= matchGameToAudioAdjustmentPeriod)
{
matchGameToAudioCurrentPeriod = 0;
++matchGameToAudioOffset;
desiredGameSpeed = fullSpeed - matchGameToAudioOffset;
CoreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &desiredGameSpeed);
}
}
// Oh no! game is going too slow now, speed it up
else if(ranDry && matchGameToAudioOffset > 0)
{
++matchGameToAudioCurrentPeriod;
if(matchGameToAudioCurrentPeriod >= matchGameToAudioAdjustmentPeriod)
{
matchGameToAudioCurrentPeriod = 0;
--matchGameToAudioOffset;
desiredGameSpeed = fullSpeed - matchGameToAudioOffset;
CoreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &desiredGameSpeed);
}
}
//Device can't keep up with the game or we have too much in the queue after slowing it down
else if(ranDry || queueLength > maxQueueSize)
{
float temp = averageGameTime/averageFeedTime;
float temp = averageGameTime/averageFeedTime;
if(temp < 1.0)
{
currAdjustment = temp;
}
}
//Device has caught up
else
if(temp > 1.0)
{
currAdjustment = 1.0;
currAdjustment = temp;
}
}
//If we are trying to make audio catch up with video, usually when fast forwarding
else
//Device can't keep up with the game or we have too much in the queue after slowing it down
else if(ranDry || queueLength > maxQueueSize)
{
if(queueLength > maxQueueSize)
{
float temp = averageGameTime/averageFeedTime;
float temp = averageGameTime/averageFeedTime;
if(temp > 1.0)
{
currAdjustment = temp;
}
}
else
if(temp < 1.0)
{
currAdjustment = 1.0;
currAdjustment = temp;
}
}
//Device has caught up
else
{
currAdjustment = 1.0;
}
//Allow the tempo to differentiate quickly, but restore original speed more slowly
//Allow the tempo to differentiate quickly with no minimum value change, but restore original tempo more slowly
//by making sure that it must change by at least the minimum value
if( (currAdjustment > minSlowValue &&
( (fabs(currAdjustment - 1.0) > fabs(slowAdjustment - 1.0) && fabs(currAdjustment) > minSlowAdjustment) ||
(fabs(currAdjustment - 1.0) < fabs(slowAdjustment - 1.0) && fabs(currAdjustment) > minSlowAdjustment*5) ))
( (fabs(currAdjustment - 1.0) > fabs(slowAdjustment - 1.0) ) ||
(fabs(currAdjustment - 1.0) < fabs(slowAdjustment - 1.0) && fabs(currAdjustment) > minSlowAdjustment) ))
|| currAdjustment == 1.0 )
{
slowAdjustment = currAdjustment;
@@ -937,11 +893,11 @@ void* audioConsumer(void* param)
free(currQueueData);
//Useful logging
if(queueLength == 0)
/*if(queueLength == 0)
{
DebugMessage(M64MSG_ERROR, "target=%d, length = %d, speed = %d, dry=%d, slow_adj=%f, curr_adj=%f, feed_time=%f, game_time=%f",
TargetSecondaryBuffers, queueLength, desiredGameSpeed, ranDry, slowAdjustment, currAdjustment, averageFeedTime, averageGameTime);
}
}*/
}
}
@@ -1060,18 +1016,6 @@ EXPORT void CALL SetSpeedFactor(int percentage)
return;
if (percentage >= 10 && percentage <= 300)
speed_factor = percentage;
if(percentage == 100)
{
matchGameToAudio = 1;
}
//We don't want to snap ourselves out of this mode
if(matchGameToAudio && percentage > 100)
{
matchGameToAudio = 0;
}
}
EXPORT void CALL VolumeMute(void)

0 comments on commit 974c8be

Please sign in to comment.