Skip to content

Commit 6a95cb3

Browse files
committed
proof of concept fix of audio induced frameskip bug
1 parent cbc97e5 commit 6a95cb3

File tree

3 files changed

+37
-59
lines changed

3 files changed

+37
-59
lines changed

src/drivers/dingux-sdl/dingoo-sound.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/// \brief Handles sound emulation using the SDL.
2323

2424
#include <stdio.h>
25-
#include <string.h>
25+
#include <string.h>
2626
#include <stdlib.h>
2727

2828
#include "dingoo.h"
@@ -67,14 +67,14 @@ static void fillaudio(void *udata, uint8 *stream, int len) // len == spec.sample
6767

6868
*tmps = sample;
6969
tmps++;
70-
len--;
70+
len--;
7171
}
7272
}
7373

7474
/**
7575
* Initialize the audio subsystem.
7676
*/
77-
int InitSound()
77+
int InitSound()
7878
{
7979
int sound, soundrate, soundbufsize, soundvolume, soundtrianglevolume,
8080
soundsquare1volume, soundsquare2volume, soundnoisevolume,
@@ -94,7 +94,7 @@ int InitSound()
9494
}
9595
char driverName[8];
9696
SDL_AudioDriverName(driverName, 8);
97-
97+
9898
fprintf(stderr, "Loading SDL sound with %s driver...\n", driverName);
9999

100100
// load configuration variables
@@ -179,7 +179,7 @@ uint32 GetBufferedSound(void) {
179179
/**
180180
* Send a sound clip to the audio subsystem.
181181
*/
182-
void WriteSound(int32 *buf, int Count)
182+
void WriteSound(int32 *buf, int Count)
183183
{
184184
//extern int EmulationPaused;
185185
// if(ispressed(DINGOO_L)) {
@@ -196,19 +196,14 @@ void WriteSound(int32 *buf, int Count)
196196
s_Buffer[s_BufferWrite] = *buf;
197197
Count--;
198198
s_BufferWrite = (s_BufferWrite + 1) % s_BufferSize;
199-
199+
200200
s_BufferIn++;
201-
201+
202202
buf++;
203203
}
204204
}
205205
_exit:
206206
SDL_UnlockAudio();
207-
208-
// If we have too much audio, wait a bit before accepting more.
209-
// This keeps the lag in check.
210-
while (GetBufferedSound() > 3 * GetBufferSize())
211-
usleep(1000);
212207
}
213208

214209
/**
@@ -218,7 +213,7 @@ void SilenceSound(int n)
218213
{
219214
// Not needed, the callback will write silence to buffer anyway
220215
// otherwise it causes noticable lag
221-
SDL_PauseAudio(n);
216+
SDL_PauseAudio(n);
222217
}
223218

224219
/**

src/drivers/dingux-sdl/dingoo-throttle.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ void delay_us(uint64 us_count)
5858
void RefreshThrottleFPS()
5959
{
6060
uint64 fps = FCEUI_GetDesiredFPS(); // Do >> 24 to get in Hz
61-
desired_frametime = 16777216.0l / (fps * g_fpsScale);
61+
//desired_frametime = 16777216.0l / (fps * g_fpsScale);
62+
desired_frametime = 20132659.0l / (fps * g_fpsScale);
6263
desired_frametime_us = (uint64)(desired_frametime * 1000000.0l);
6364

64-
Lasttime=0;
65+
Lasttime=0;
6566
Nexttime=0;
6667
InFrame=0;
6768
printf("desired_frametime: %i\n", desired_frametime_us);
@@ -80,30 +81,30 @@ int SpeedThrottle()
8081
}
8182
uint64 time_left;
8283
uint64 cur_time;
83-
84+
8485
if(!Lasttime) Lasttime = get_ticks_us();
85-
86+
8687
if(!InFrame) {
8788
InFrame = 1;
8889
Nexttime = Lasttime + desired_frametime_us;
8990
}
90-
91+
9192
cur_time = get_ticks_us();
9293

9394
if(cur_time >= Nexttime)
9495
time_left = 0;
9596
else
9697
time_left = Nexttime - cur_time;
97-
98+
9899
if(time_left > 50000) {
99100
time_left = 50000;
100101
/* In order to keep input responsive, don't wait too long at once */
101102
/* 50 ms wait gives us a 20 Hz responsetime which is nice. */
102103
} else InFrame = 0;
103-
104+
104105
//printf("attempting to sleep %Ld ms, frame complete=%i\n", time_left, InFrame);
105106
delay_us(time_left);
106-
107+
107108
if(!InFrame)
108109
{
109110
Lasttime = get_ticks_us();
@@ -119,11 +120,11 @@ void IncreaseEmulationSpeed(void)
119120
{
120121
puts("IncreaseEmulationSpeed");
121122
g_fpsScale *= LOGMUL;
122-
123+
123124
if(g_fpsScale > Fastest) g_fpsScale = Fastest;
124125

125126
RefreshThrottleFPS();
126-
127+
127128
FCEU_DispMessage("emulation speed %.1f%%", g_fpsScale*100.0);
128129
}
129130

@@ -150,7 +151,7 @@ FCEUD_SetEmulationSpeed(int cmd)
150151
{
151152
puts("SetEmulationSpeed");
152153
MaxSpeed = false;
153-
154+
154155
switch(cmd) {
155156
case EMUSPEED_SLOWEST:
156157
g_fpsScale = Slowest;

src/drivers/dingux-sdl/dingoo.cpp

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -325,44 +325,26 @@ void quick_save_and_poweroff()
325325
}
326326

327327
static void DoFun(int fskip) {
328-
uint8 *gfx;
329-
int32 *sound;
330-
int32 ssize;
331-
extern uint8 PAL;
332-
int done = 0, timer = 0, ticks = 0, tick = 0, fps = 0;
333-
unsigned int frame_limit = 60, frametime = 16667;
334-
335-
while (GameInfo) {
336-
/* Frameskip decision based on the audio buffer */
337-
if (!fpsthrottle) {
338-
// Fill up the audio buffer with up to 6 frames dropped.
339-
int FramesSkipped = 0;
340-
while (GameInfo
341-
&& GetBufferedSound() < GetBufferSize() * 3 / 2
342-
&& ++FramesSkipped < 6) {
343-
FCEUI_Emulate(&gfx, &sound, &ssize, 1);
344-
FCEUD_Update(NULL, sound, ssize);
345-
}
328+
uint8 *gfx;
329+
int32 *sound;
330+
int32 ssize;
331+
extern uint8 PAL;
332+
int done = 0, timer = 0, ticks = 0, tick = 0, fps = 0;
333+
unsigned int frame_limit = 60, frametime = 16667;
346334

347-
// Force at least one frame to be displayed.
348-
if (GameInfo) {
349-
FCEUI_Emulate(&gfx, &sound, &ssize, 0);
350-
FCEUD_Update(gfx, sound, ssize);
351-
}
335+
fpsthrottle = false;
352336

353-
// Then render all frames while audio is sufficient.
354-
while (GameInfo
355-
&& GetBufferedSound() > GetBufferSize() * 3 / 2) {
356-
FCEUI_Emulate(&gfx, &sound, &ssize, 0);
357-
FCEUD_Update(gfx, sound, ssize);
358-
}
359-
}
360-
else {
361-
FCEUI_Emulate(&gfx, &sound, &ssize, 0);
362-
FCEUD_Update(gfx, sound, ssize);
363-
}
364-
}
337+
while (GameInfo) {
338+
int ticks = SDL_GetTicks();
339+
340+
FCEUI_Emulate(&gfx, &sound, &ssize, 0);
341+
FCEUD_Update(gfx, sound, ssize);
342+
343+
int msec = SDL_GetTicks() - ticks;
365344

345+
if (msec < 16)
346+
SDL_Delay(16 - msec);
347+
}
366348
}
367349

368350
/**

0 commit comments

Comments
 (0)