Permalink
Browse files

audio: audio now slows down for games that run slow

  • Loading branch information...
fzurita committed Feb 21, 2016
1 parent e09802d commit b0516d29834e22005929a0d13bd4da9d2e42cf1c
Showing with 55 additions and 10 deletions.
  1. +55 −10 jni/mupen64plus-audio-sles/main.c
@@ -856,12 +856,25 @@ void* audioConsumer(void* param)
int prevQueueSize = thread_queue_length(&audioConsumerQueue);
int currQueueSize = prevQueueSize;
int maxQueueSize = 3;
- int currentSpeedFactorAdjustment = 0;
static const int fullSpeed = 100;
+
+ //Game is being sped up, speed up audio
int matchGameToAudioOffset = 0;
+
+ //Device is running too quickly, these are used to slow down emulation
+ int currentSpeedFactorAdjustment = 0;
int desiredGameSpeed = 100;
+
+ //Sound queue ran dry, device is running slow
int ranDry = 0;
+ //adjustment used when a device running too slow
+ int slowAdjustment = 0;
+
+ //how quickly sound speed is adjusted to match a device
+ const int adjustmentPeriod = 10;
+ int currentPeriod = 0;
+
while(!shutdown)
{
struct timespec waitTime;
@@ -879,29 +892,56 @@ void* audioConsumer(void* param)
currQueueSize = thread_queue_length(&audioConsumerQueue);
- //If we are trying to make audio catch up with video
+ //If we want to make the game run slower for us
if(matchGameToAudio)
{
//Game is running too fast, slow it down a little
if(currQueueSize > maxQueueSize)
{
- ++matchGameToAudioOffset;
- desiredGameSpeed = fullSpeed - matchGameToAudioOffset;
- CoreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &desiredGameSpeed);
+ currentPeriod++;
+
+ //Don't adjust it too quickly, it sounds bad
+ if(currentPeriod >= adjustmentPeriod)
+ {
+ //Try making the audio go faster first before slowing the game down
+ if(slowAdjustment > 0)
+ {
+ slowAdjustment--;
+ }
+ //Audio can't go any faster, start slowing the game
+ else
+ {
+ ++matchGameToAudioOffset;
+ desiredGameSpeed = fullSpeed - matchGameToAudioOffset;
+ CoreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &desiredGameSpeed);
+ }
+
+ currentPeriod = 0;
+ }
}
- // Oh no!, game is going to slow now, speed it up
+ // Oh no! game is going too slow now, speed it up
else if(ranDry && matchGameToAudioOffset > 0)
{
--matchGameToAudioOffset;
+ desiredGameSpeed = fullSpeed - matchGameToAudioOffset;
+ CoreDoCommand(M64CMD_CORE_STATE_SET, M64CORE_SPEED_FACTOR, &desiredGameSpeed);
}
- else
+ //Device can't keep up with the game, slow audio down
+ else if(ranDry)
{
- matchGameToAudioOffset = 0;
+ currentPeriod++;
+
+ //Don't adjust it too quickly, it sounds bad
+ if(currentPeriod >= adjustmentPeriod)
+ {
+ slowAdjustment++;
+ currentPeriod = 0;
+ }
}
- processAudio(theQueueData->data, theQueueData->lenght, fullSpeed);
+ processAudio(theQueueData->data, theQueueData->lenght, fullSpeed - slowAdjustment);
}
- //If we want to make the game run slower for us
+ //If we are trying to make audio catch up with video
else
{
if(currQueueSize > maxQueueSize)
@@ -922,6 +962,11 @@ void* audioConsumer(void* param)
free(theQueueData->data);
free(msg.data);
+
+ //Useful logging
+ //DebugMessage(M64MSG_ERROR, "audio adj = %d, length = %d, speed = %d, dry=%d, slow=%d",
+ // currentSpeedFactorAdjustment, thread_queue_length(&audioConsumerQueue),
+ // desiredGameSpeed, ranDry, slowAdjustment);
}
}

0 comments on commit b0516d2

Please sign in to comment.