Re-do the way the libretro port does audio. Audio is now sent to libretro at 384KHz, which is then resampled to whatever rate the user configured.
#include <signal.h>
#include <stdarg.h>
-#define AUDIO_FREQUENCY 44100
+#define AUDIO_FREQUENCY 384000
+#define FRAME_RATE (0x400000 / 70224.0)
#ifdef _WIN32
#include <direct.h>
@@ -103,15 +104,24 @@ static void GB_update_keys_status(GB_gameboy_t *gb)
static void audio_callback(void *gb)
{
- GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, (float)AUDIO_FREQUENCY / 59.72);
- audio_batch_cb(soundbuf, (float)AUDIO_FREQUENCY / 59.72);
+ size_t length = GB_apu_get_current_buffer_length(gb);
+
+ while (length > sizeof(soundbuf) / 4)
+ {
+ GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, 1024);
+ audio_batch_cb(soundbuf, 1024);
+ length -= 1024;
+ }
+ if (length) {
+ GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, length);
+ audio_batch_cb(soundbuf, length);
+ }
}
static void vblank(GB_gameboy_t *gb)
{
GB_update_keys_status(gb);
- GB_set_pixels_output(gb, frame_buf);
audio_callback(gb);
}
@@ -180,7 +190,7 @@ void retro_get_system_info(struct retro_system_info *info)
void retro_get_system_av_info(struct retro_system_av_info *info)
{
struct retro_game_geometry geom = { VIDEO_WIDTH, VIDEO_HEIGHT,VIDEO_WIDTH, VIDEO_HEIGHT ,160.0 / 144.0 };
- struct retro_system_timing timing = { 59.72, 44100.0 };
+ struct retro_system_timing timing = { FRAME_RATE, AUDIO_FREQUENCY };
info->geometry = geom;
info->timing = timing;
@@ -269,20 +279,10 @@ static void check_variables(void)
void retro_run(void)
{
- static int frames;
- size_t samples;
-
bool updated = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
- samples = GB_apu_get_current_buffer_length(&gb);
- if (!(frames < (samples / 35112)))
- {
GB_run_frame(&gb);
- frames ++;
- }
- else
- frames = 0;
video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t));
}
