5353#define WIIU_DEVICE_MIRRORED 2
5454#define WIIU_MAX_DEVICES 3
5555
56+ static int mirroredHandle ;
57+ static int tvHandle ;
58+ static int drcHandle ;
59+
5660static void _WIIUAUDIO_framecallback ();
5761static SDL_AudioDevice * wiiuDevices [WIIU_MAX_DEVICES ];
5862static int deviceType ;
@@ -85,16 +89,6 @@ static int _WIIUAUDIO_OpenDeviceFunction(_THIS) {
8589
8690 SDL_zerop (this -> hidden );
8791
88- /* Take a quick aside to init the wiiu audio */
89- if (!AXIsInit ()) {
90- /* Init the AX audio engine */
91- AXInitParams initparams = {
92- .renderer = AX_INIT_RENDERER_48KHZ ,
93- .pipeline = AX_INIT_PIPELINE_SINGLE ,
94- };
95- AXInitWithParams (& initparams );
96- } else printf ("DEBUG: AX already up?\n" );
97-
9892 if (this -> spec .channels < 1 ) this -> spec .channels = 1 ;
9993 if (this -> spec .channels > WIIU_MAX_VALID_CHANNELS )
10094 this -> spec .channels = WIIU_MAX_VALID_CHANNELS ;
@@ -166,7 +160,6 @@ static int _WIIUAUDIO_OpenDeviceFunction(_THIS) {
166160 return SDL_SetError ("Couldn't allocate deinterleave buffer" );
167161 }
168162
169-
170163 for (int i = 0 ; i < this -> spec .channels ; i ++ ) {
171164 /* Get a voice, top priority */
172165 this -> hidden -> voice [i ] = AXAcquireVoice (31 , NULL , NULL );
@@ -263,11 +256,6 @@ static int _WIIUAUDIO_OpenDeviceFunction(_THIS) {
263256 }
264257
265258 wiiuDevices [deviceCount ] = this ;
266-
267- if (deviceCount < 1 ) {
268- AXRegisterAppFrameCallback (_WIIUAUDIO_framecallback );
269- }
270-
271259 deviceCount ++ ;
272260
273261 return 0 ;
@@ -282,10 +270,6 @@ static void _WIIUAUDIO_ThreadCleanup(OSThread *thread, void *stack) {
282270}
283271
284272static void WIIUAUDIO_DetectDevices (void ) {
285- void * drcHandle ;
286- void * tvHandle ;
287- void * mirrorHandle ;
288-
289273 /* This gets reset later anyways */
290274 SDL_AudioSpec spec ;
291275
@@ -295,7 +279,7 @@ static void WIIUAUDIO_DetectDevices(void) {
295279
296280 SDL_CalculateAudioSpec (& spec );
297281
298- SDL_AddAudioDevice (SDL_FALSE , SDL_AUDIO_DEVICE_WIIU_MIRRORED , & spec , & mirrorHandle );
282+ SDL_AddAudioDevice (SDL_FALSE , SDL_AUDIO_DEVICE_WIIU_MIRRORED , & spec , & mirroredHandle );
299283 SDL_AddAudioDevice (SDL_FALSE , SDL_AUDIO_DEVICE_WIIU_TV , & spec , & tvHandle );
300284 SDL_AddAudioDevice (SDL_FALSE , SDL_AUDIO_DEVICE_WIIU_GAMEPAD , & spec , & drcHandle );
301285}
@@ -309,13 +293,11 @@ static int WIIUAUDIO_OpenDevice(_THIS, const char *devname) {
309293
310294 deviceType = WIIU_DEVICE_MIRRORED ;
311295
312- if (devname != NULL ) {
313- if (SDL_strcmp (devname , SDL_AUDIO_DEVICE_WIIU_TV ) == 0 ) {
314- deviceType = WIIU_DEVICE_TV ;
315- }
316- else if (SDL_strcmp (devname , SDL_AUDIO_DEVICE_WIIU_GAMEPAD ) == 0 ) {
317- deviceType = WIIU_DEVICE_GAMEPAD ;
318- }
296+ if (this -> handle == & tvHandle ) {
297+ deviceType = WIIU_DEVICE_TV ;
298+ }
299+ else if (this -> handle == & drcHandle ) {
300+ deviceType = WIIU_DEVICE_GAMEPAD ;
319301 }
320302
321303 /* AX functions need to run from the same core.
@@ -499,16 +481,13 @@ static Uint8* WIIUAUDIO_GetDeviceBuf(_THIS) {
499481}
500482
501483static void WIIUAUDIO_CloseDevice (_THIS ) {
502- if ((AXIsInit ()) && (deviceCount < 1 )) {
503- AXDeregisterAppFrameCallback (_WIIUAUDIO_framecallback );
504- for (int i = 0 ; i < SIZEOF_ARR (this -> hidden -> voice ); i ++ ) {
505- if (this -> hidden -> voice [i ]) {
506- AXFreeVoice (this -> hidden -> voice [i ]);
507- this -> hidden -> voice [i ] = NULL ;
508- }
484+ for (int i = 0 ; i < SIZEOF_ARR (this -> hidden -> voice ); i ++ ) {
485+ if (this -> hidden -> voice [i ]) {
486+ AXFreeVoice (this -> hidden -> voice [i ]);
487+ this -> hidden -> voice [i ] = NULL ;
509488 }
510- AXQuit ();
511489 }
490+
512491 if (this -> hidden -> mixbufs [0 ]) free (this -> hidden -> mixbufs [0 ]);
513492 if (this -> hidden -> deintvbuf ) SDL_free (this -> hidden -> deintvbuf );
514493 SDL_free (this -> hidden );
@@ -524,14 +503,34 @@ static void WIIUAUDIO_ThreadInit(_THIS) {
524503 OSSetThreadPriority (currentThread , priority );
525504}
526505
506+ static void WIIUAUDIO_Deinitialize (void ) {
507+ if (AXIsInit ()) {
508+ AXDeregisterAppFrameCallback (_WIIUAUDIO_framecallback );
509+ AXQuit ();
510+ }
511+ }
512+
527513static SDL_bool WIIUAUDIO_Init (SDL_AudioDriverImpl * impl ) {
514+ /* Take a quick aside to init the wiiu audio */
515+ if (!AXIsInit ()) {
516+ /* Init the AX audio engine */
517+ AXInitParams initparams = {
518+ .renderer = AX_INIT_RENDERER_48KHZ ,
519+ .pipeline = AX_INIT_PIPELINE_SINGLE ,
520+ };
521+ AXInitWithParams (& initparams );
522+ } else printf ("DEBUG: AX already up?\n" );
523+
524+ AXRegisterAppFrameCallback (_WIIUAUDIO_framecallback );
525+
528526 impl -> DetectDevices = WIIUAUDIO_DetectDevices ;
529527 impl -> OpenDevice = WIIUAUDIO_OpenDevice ;
530528 impl -> PlayDevice = WIIUAUDIO_PlayDevice ;
531529 impl -> WaitDevice = WIIUAUDIO_WaitDevice ;
532530 impl -> GetDeviceBuf = WIIUAUDIO_GetDeviceBuf ;
533531 impl -> CloseDevice = WIIUAUDIO_CloseDevice ;
534532 impl -> ThreadInit = WIIUAUDIO_ThreadInit ;
533+ impl -> Deinitialize = WIIUAUDIO_Deinitialize ;
535534
536535 impl -> OnlyHasDefaultOutputDevice = SDL_FALSE ;
537536
0 commit comments