|
5 | 5 |
|
6 | 6 | // #define FRAME_DOUBLE_BUFFERING |
7 | 7 | // #define AUDIO_DOUBLE_BUFFERING |
8 | | -// #define USE_AUDIO_TASK |
| 8 | +#define USE_AUDIO_TASK |
9 | 9 |
|
10 | 10 | typedef struct |
11 | 11 | { |
@@ -77,10 +77,7 @@ static rg_audio_sample_t *audioBuffers[2]; |
77 | 77 | static rg_audio_sample_t *currentAudioBuffer; |
78 | 78 |
|
79 | 79 | #ifdef USE_AUDIO_TASK |
80 | | -static rg_mutex_t *audio_mutex; |
81 | 80 | static rg_task_t *audio_task_handle; |
82 | | -static volatile bool audio_processing = false; |
83 | | -static volatile bool audio_shutdown = false; |
84 | 81 | #endif |
85 | 82 |
|
86 | 83 | static bool apu_enabled = true; |
@@ -302,24 +299,14 @@ static inline void mix_samples(int32_t count) |
302 | 299 | #ifdef USE_AUDIO_TASK |
303 | 300 | static void audio_task(void *arg) |
304 | 301 | { |
305 | | - while (!audio_shutdown) |
| 302 | + rg_task_msg_t msg; |
| 303 | + while (rg_task_receive(&msg)) |
306 | 304 | { |
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); |
320 | 309 | } |
321 | | - rg_mutex_free(audio_mutex); |
322 | | - audio_mutex = NULL; |
323 | 310 | } |
324 | 311 | #endif |
325 | 312 |
|
@@ -373,9 +360,8 @@ void snes_main(void) |
373 | 360 |
|
374 | 361 | #ifdef USE_AUDIO_TASK |
375 | 362 | // Set up multicore audio |
376 | | - audio_mutex = rg_mutex_create(); |
377 | 363 | 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!"); |
379 | 365 | #endif |
380 | 366 |
|
381 | 367 | Settings.CyclesPercentage = 100; |
@@ -465,10 +451,10 @@ void snes_main(void) |
465 | 451 | S9xMainLoop(); |
466 | 452 |
|
467 | 453 | #ifdef USE_AUDIO_TASK |
468 | | - if (rg_mutex_take(audio_mutex, 5)) |
| 454 | + if (apu_enabled) |
469 | 455 | { |
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); |
472 | 458 | } |
473 | 459 | #endif |
474 | 460 |
|
@@ -509,8 +495,4 @@ void snes_main(void) |
509 | 495 | skipFrames--; |
510 | 496 | } |
511 | 497 | } |
512 | | - |
513 | | -#ifdef USE_AUDIO_TASK |
514 | | - audio_shutdown = true; |
515 | | -#endif |
516 | 498 | } |
0 commit comments