Skip to content

Commit

Permalink
Fix sound issues in the GB Camera ROM
Browse files Browse the repository at this point in the history
- The ROM takes lots of captures during boot, which slows down emulation.
  This commit adds a cooldown counter.
- Audio was delayed when the game actually started because the boot code
  was sending too many samples to the SDL AudioStream buffer. This commit
  adds code to clear this buffer when it gets too big.
  • Loading branch information
AntonioND committed Dec 30, 2020
1 parent 9f18731 commit 21e435b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
14 changes: 13 additions & 1 deletion source/gb_core/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ int GB_CameraInit(void)
return Webcam_Init();
}

static int webcam_frame_delay = 0;

void GB_CameraWebcamCapture(void)
{
Webcam_GetFrame();
if (webcam_frame_delay == 0)
{
Webcam_GetFrame();
webcam_frame_delay = 5;
}
}

void GB_CameraWebcamDelayDecrease(void)
{
if (webcam_frame_delay > 0)
webcam_frame_delay--;
}

//----------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions source/gb_core/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void GB_CameraWriteRegister(int address, int value);
// Returns 1 if mapper is the GB Camera
int GB_MapperIsGBCamera(void);

void GB_CameraWebcamDelayDecrease(void);

//----------------------------------------------------------------

void GB_CameraClockCounterReset(void);
Expand Down
13 changes: 11 additions & 2 deletions source/gui/win_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,11 @@ void Win_MainLoopHandle(void)
{
size_t size = GBA_SoundGetSamplesFrame(samples, sizeof(samples));

if (Sound_IsBufferOverThreshold())
if (Sound_IsBufferTooBig())
{
Sound_ClearBuffer();
}
else if (Sound_IsBufferOverThreshold())
{
if (size > 8) // This should always be true
size -= 8;
Expand Down Expand Up @@ -1527,6 +1531,7 @@ void Win_MainLoopHandle(void)
{
Input_Update_GB();
GB_RunForOneFrame();
GB_CameraWebcamDelayDecrease();
}

if (_win_main_has_to_frameskip() == 0)
Expand All @@ -1541,7 +1546,11 @@ void Win_MainLoopHandle(void)
{
size_t size = GB_SoundGetSamplesFrame(samples, sizeof(samples));

if (Sound_IsBufferOverThreshold())
if (Sound_IsBufferTooBig())
{
Sound_ClearBuffer();
}
else if (Sound_IsBufferOverThreshold())
{
if (size > 8) // This should always be true
size -= 8;
Expand Down
13 changes: 13 additions & 0 deletions source/sound_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ static void Sound_Callback(unused__ void *userdata, Uint8 *buffer, int len)
}
}

void Sound_ClearBuffer(void)
{
SDL_AudioStreamClear(stream);
}

static void Sound_End(void)
{
SDL_FreeAudioStream(stream);
Expand Down Expand Up @@ -122,6 +127,14 @@ int Sound_IsBufferOverThreshold(void)
return 0;
}

int Sound_IsBufferTooBig(void)
{
if (SDL_AudioStreamAvailable(stream) > SDL_BUFFER_SAMPLES_THRESHOLD * 2)
return 1;

return 0;
}

void Sound_SendSamples(int16_t *buffer, int len)
{
int rc = SDL_AudioStreamPut(stream, buffer, len);
Expand Down
4 changes: 2 additions & 2 deletions source/sound_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#define GBA_SAMPLERATE (32 * 1024)
#define SDL_SAMPLERATE (44100)

typedef void(Sound_CallbackPointer)(void *, long);

void Sound_Init(void);

void Sound_Enable(void);
void Sound_Disable(void);

void Sound_ClearBuffer(void);
int Sound_IsBufferOverThreshold(void);
int Sound_IsBufferTooBig(void);
void Sound_SendSamples(int16_t *buffer, int len);

void Sound_SetVolume(int vol);
Expand Down

0 comments on commit 21e435b

Please sign in to comment.