Skip to content

Commit 68fc8ce

Browse files
committed
SNES: Re-enable audio task but kept in close sync
The performance gain is definitely less than the free-running audio task, but it's still significant (about 15-20%) and doesn't break games as much...
1 parent 6657c5e commit 68fc8ce

1 file changed

Lines changed: 11 additions & 29 deletions

File tree

retro-core/main/main_snes.c

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// #define FRAME_DOUBLE_BUFFERING
77
// #define AUDIO_DOUBLE_BUFFERING
8-
// #define USE_AUDIO_TASK
8+
#define USE_AUDIO_TASK
99

1010
typedef struct
1111
{
@@ -77,10 +77,7 @@ static rg_audio_sample_t *audioBuffers[2];
7777
static rg_audio_sample_t *currentAudioBuffer;
7878

7979
#ifdef USE_AUDIO_TASK
80-
static rg_mutex_t *audio_mutex;
8180
static rg_task_t *audio_task_handle;
82-
static volatile bool audio_processing = false;
83-
static volatile bool audio_shutdown = false;
8481
#endif
8582

8683
static bool apu_enabled = true;
@@ -302,24 +299,14 @@ static inline void mix_samples(int32_t count)
302299
#ifdef USE_AUDIO_TASK
303300
static void audio_task(void *arg)
304301
{
305-
while (!audio_shutdown)
302+
rg_task_msg_t msg;
303+
while (rg_task_receive(&msg))
306304
{
307-
// Lock the mutex to safely access shared data
308-
if (audio_processing && rg_mutex_take(audio_mutex, 5))
309-
{
310-
// Re-check flag after acquiring lock
311-
if (audio_processing)
312-
{
313-
mix_samples(AUDIO_BUFFER_LENGTH << 1);
314-
rg_audio_submit(currentAudioBuffer, AUDIO_BUFFER_LENGTH);
315-
audio_processing = false; // Signal that we are done
316-
}
317-
rg_mutex_give(audio_mutex);
318-
}
319-
rg_task_delay(1); // Yield to prevent watchdog timeout and busy-waiting
305+
if (msg.type == RG_TASK_MSG_STOP)
306+
break;
307+
mix_samples(AUDIO_BUFFER_LENGTH << 1);
308+
rg_audio_submit(currentAudioBuffer, AUDIO_BUFFER_LENGTH);
320309
}
321-
rg_mutex_free(audio_mutex);
322-
audio_mutex = NULL;
323310
}
324311
#endif
325312

@@ -373,9 +360,8 @@ void snes_main(void)
373360

374361
#ifdef USE_AUDIO_TASK
375362
// Set up multicore audio
376-
audio_mutex = rg_mutex_create();
377363
audio_task_handle = rg_task_create("snes_audio", &audio_task, NULL, 2048, RG_TASK_PRIORITY_6, 1);
378-
RG_ASSERT(audio_mutex && audio_task_handle, "Failed to create audio task!");
364+
RG_ASSERT(audio_task_handle, "Failed to create audio task!");
379365
#endif
380366

381367
Settings.CyclesPercentage = 100;
@@ -465,10 +451,10 @@ void snes_main(void)
465451
S9xMainLoop();
466452

467453
#ifdef USE_AUDIO_TASK
468-
if (rg_mutex_take(audio_mutex, 5))
454+
if (apu_enabled)
469455
{
470-
audio_processing = apu_enabled;
471-
rg_mutex_give(audio_mutex);
456+
rg_task_msg_t msg = {0};
457+
rg_task_send(audio_task_handle, &msg);
472458
}
473459
#endif
474460

@@ -509,8 +495,4 @@ void snes_main(void)
509495
skipFrames--;
510496
}
511497
}
512-
513-
#ifdef USE_AUDIO_TASK
514-
audio_shutdown = true;
515-
#endif
516498
}

0 commit comments

Comments
 (0)