Skip to content

Commit 845c6ea

Browse files
committed
SNES: Reverted most changes from a67f5eb (adds back audio double buffering)
1 parent 8562bbb commit 845c6ea

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

snes9x/main/main_snes.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static const char *SNES_BUTTONS[] = {
9696
static rg_app_t *app;
9797
static rg_surface_t *updates[2];
9898
static rg_surface_t *currentUpdate;
99+
static rg_audio_sample_t *audioBuffers[2];
100+
static rg_audio_sample_t *currentAudioBuffer;
99101

100102
#ifdef USE_AUDIO_TASK
101103
static rg_task_t *audio_task_handle;
@@ -306,30 +308,26 @@ void JustifierButtons(uint32_t *justifiers)
306308
(void)justifiers;
307309
}
308310

311+
static inline void mix_samples(int32_t count)
312+
{
313+
currentAudioBuffer = audioBuffers[currentAudioBuffer == audioBuffers[0]];
314+
if (lowpass_filter)
315+
S9xMixSamplesLowPass((int16_t *)currentAudioBuffer, count, AUDIO_LOW_PASS_RANGE);
316+
else
317+
S9xMixSamples((int16_t *)currentAudioBuffer, count);
318+
}
319+
309320
#ifdef USE_AUDIO_TASK
310321
static void audio_task(void *arg)
311322
{
312-
rg_audio_sample_t *audioBuffer = rg_alloc(AUDIO_BUFFER_LENGTH * 4, MEM_FAST);
313323
rg_task_msg_t msg;
314-
bool zeroed = false;
315324
while (rg_task_receive(&msg, -1))
316325
{
317326
if (msg.type == RG_TASK_MSG_STOP)
318327
break;
319-
if (msg.type != 0) // Sound enabled, mix
320-
{
321-
if (lowpass_filter)
322-
S9xMixSamplesLowPass((int16_t *)audioBuffer, msg.dataInt << 1, AUDIO_LOW_PASS_RANGE);
323-
else
324-
S9xMixSamples((int16_t *)audioBuffer, msg.dataInt << 1);
325-
zeroed = false;
326-
}
327-
else if (!zeroed) // Sound disabled, zero buffer only if needed
328-
{
329-
memset(audioBuffer, 0, AUDIO_BUFFER_LENGTH * 4);
330-
zeroed = true;
331-
}
332-
rg_audio_submit(audioBuffer, msg.dataInt);
328+
if (msg.type != 0)
329+
mix_samples(msg.dataInt << 1);
330+
rg_audio_submit(currentAudioBuffer, msg.dataInt);
333331
}
334332
}
335333
#endif
@@ -374,15 +372,22 @@ void app_main(void)
374372
#endif
375373
currentUpdate = updates[0];
376374

377-
if (!updates[0] || !updates[1])
375+
#ifdef AUDIO_DOUBLE_BUFFERING
376+
audioBuffers[0] = (rg_audio_sample_t *)calloc(AUDIO_BUFFER_LENGTH, 4);
377+
audioBuffers[1] = (rg_audio_sample_t *)calloc(AUDIO_BUFFER_LENGTH, 4);
378+
#else
379+
audioBuffers[0] = (rg_audio_sample_t *)calloc(AUDIO_BUFFER_LENGTH, 4);
380+
audioBuffers[1] = audioBuffers[0];
381+
#endif
382+
currentAudioBuffer = audioBuffers[0];
383+
384+
if (!updates[0] || !updates[1] || !audioBuffers[0] || !audioBuffers[1])
378385
RG_PANIC("Failed to allocate buffers!");
379386

380387
#ifdef USE_AUDIO_TASK
381388
// Set up multicore audio
382389
audio_task_handle = rg_task_create("snes_audio", &audio_task, NULL, 2048, RG_TASK_PRIORITY_6, 1);
383390
RG_ASSERT(audio_task_handle, "Failed to create audio task!");
384-
#else
385-
rg_audio_sample_t *audioBuffer = rg_alloc(AUDIO_BUFFER_LENGTH * 4, MEM_FAST);
386391
#endif
387392

388393
Settings.CyclesPercentage = 100;
@@ -433,8 +438,7 @@ void app_main(void)
433438
rg_system_set_tick_rate(Memory.ROMFramesPerSecond);
434439
app->frameskip = 3;
435440

436-
const float samplesPerFrame = (float)app->sampleRate / app->tickRate;
437-
float samplesRemaining = 0.f;
441+
const int samplesPerFrame = (int)roundf((float)app->sampleRate / app->tickRate);
438442
bool menuCancelled = false;
439443
bool menuPressed = false;
440444
int skipFrames = 0;
@@ -461,6 +465,8 @@ void app_main(void)
461465
else if (joystick & RG_KEY_OPTION)
462466
{
463467
rg_gui_options_menu();
468+
memset(audioBuffers[0], 0, AUDIO_BUFFER_LENGTH * 4);
469+
memset(audioBuffers[1], 0, AUDIO_BUFFER_LENGTH * 4);
464470
continue;
465471
}
466472

@@ -483,25 +489,17 @@ void app_main(void)
483489
currentUpdate = updates[currentUpdate == updates[0]];
484490
}
485491

486-
samplesRemaining += samplesPerFrame;
487-
int samples = (int)samplesRemaining;
488-
samplesRemaining -= samples;
489-
490492
#ifdef USE_AUDIO_TASK
491493
rg_system_tick(rg_system_timer() - startTime);
492-
rg_task_msg_t msg = {.type = (int)sound_enabled, .dataInt = samples};
494+
rg_task_msg_t msg = {.type = (int)sound_enabled, .dataInt = samplesPerFrame};
493495
if (sound_enabled || app->frameTime - (rg_system_timer() - startTime) > 2000)
494496
rg_task_send(audio_task_handle, &msg, -1);
495497
#else
496-
if (sound_enabled && lowpass_filter)
497-
S9xMixSamplesLowPass((int16_t *)audioBuffer, samples << 1, AUDIO_LOW_PASS_RANGE);
498-
else if (sound_enabled)
499-
S9xMixSamples((int16_t *)audioBuffer, samples << 1);
500-
else
501-
memset(audioBuffer, 0, AUDIO_BUFFER_LENGTH * sizeof(rg_audio_sample_t));
498+
if (sound_enabled)
499+
mix_samples(samplesPerFrame << 1);
502500
rg_system_tick(rg_system_timer() - startTime);
503501
if (sound_enabled || app->frameTime - (rg_system_timer() - startTime) > 2000)
504-
rg_audio_submit(audioBuffer, samples);
502+
rg_audio_submit(currentAudioBuffer, samplesPerFrame);
505503
#endif
506504

507505
if (skipFrames == 0)

0 commit comments

Comments
 (0)