1515#include <unistd.h>
1616
1717#include <SDL.h>
18+ #if RV32_HAS (SDL_MIXER )
1819#include <SDL_mixer.h>
20+ #endif
1921
2022#include "riscv.h"
2123#include "riscv_private.h"
@@ -94,11 +96,13 @@ typedef struct sound {
9496/* SDL-mixer-related and music-related variables */
9597static pthread_t music_thread ;
9698static uint8_t * music_midi_data ;
99+ #if RV32_HAS (SDL_MIXER )
97100static Mix_Music * mid ;
98101
99102/* SDL-mixer-related and sfx-related variables */
100103static pthread_t sfx_thread ;
101104static Mix_Chunk * sfx_chunk ;
105+ #endif
102106static uint8_t * sfx_samples ;
103107static uint32_t nr_sfx_samples ;
104108static int chan ;
@@ -718,6 +722,7 @@ uint8_t *mus2midi(uint8_t *data, int *length)
718722 return midi_data ;
719723}
720724
725+ #if RV32_HAS (SDL_MIXER )
721726static void * sfx_handler (void * arg )
722727{
723728 sound_t * sfx = (sound_t * ) arg ;
@@ -836,8 +841,10 @@ static void play_sfx(riscv_t *rv)
836841 .size = sfx_data_size ,
837842 .volume = volume ,
838843 };
844+ #if RV32_HAS (SDL_MIXER )
839845 pthread_create (& sfx_thread , NULL , sfx_handler , & sfx );
840846 sfx_thread_init = true;
847+ #endif
841848 /* FIXME: In web browser runtime, web workers in thread pool do not reap
842849 * after sfx_handler return, thus we have to join them. sfx_handler does not
843850 * contain infinite loop,so do not worry to be stalled by it */
@@ -893,8 +900,10 @@ static void play_music(riscv_t *rv)
893900 .looping = looping ,
894901 .volume = volume ,
895902 };
903+ #if RV32_HAS (SDL_MIXER )
896904 pthread_create (& music_thread , NULL , music_handler , & music );
897905 music_thread_init = true;
906+ #endif
898907 /* FIXME: In web browser runtime, web workers in thread pool do not reap
899908 * after music_handler return, thus we have to join them. music_handler does
900909 * not contain infinite loop,so do not worry to be stalled by it */
@@ -916,6 +925,7 @@ static void set_music_volume(riscv_t *rv)
916925 /* multiplied by 8 because volume's max is 15 */
917926 Mix_VolumeMusic (volume * 8 );
918927}
928+ #endif /* RV32_HAS(SDL_MIXER) */
919929
920930static void init_audio (void )
921931{
@@ -933,6 +943,7 @@ static void init_audio(void)
933943 exit (EXIT_FAILURE );
934944 }
935945
946+ #if RV32_HAS (SDL_MIXER )
936947 /* Initialize SDL2 Mixer */
937948 if (Mix_Init (MIX_INIT_MID ) != MIX_INIT_MID ) {
938949 rv_log_fatal ("Mix_Init failed: %s" , Mix_GetError ());
@@ -943,6 +954,7 @@ static void init_audio(void)
943954 Mix_Quit ();
944955 exit (EXIT_FAILURE );
945956 }
957+ #endif
946958 audio_init = true;
947959}
948960
@@ -956,6 +968,7 @@ static void shutdown_audio()
956968 * on a valid pthread_t identifier.
957969 */
958970
971+ #if RV32_HAS (SDL_MIXER )
959972 if (music_thread_init ) {
960973 stop_music ();
961974 pthread_join (music_thread , NULL );
@@ -974,6 +987,7 @@ static void shutdown_audio()
974987
975988 Mix_CloseAudio ();
976989 Mix_Quit ();
990+ #endif
977991
978992 audio_init = sfx_thread_init = music_thread_init = false;
979993}
@@ -1020,16 +1034,24 @@ void syscall_control_audio(riscv_t *rv)
10201034
10211035 switch (request ) {
10221036 case PLAY_MUSIC :
1037+ #if RV32_HAS (SDL_MIXER )
10231038 play_music (rv );
1039+ #endif
10241040 break ;
10251041 case PLAY_SFX :
1042+ #if RV32_HAS (SDL_MIXER )
10261043 play_sfx (rv );
1044+ #endif
10271045 break ;
10281046 case SET_MUSIC_VOLUME :
1047+ #if RV32_HAS (SDL_MIXER )
10291048 set_music_volume (rv );
1049+ #endif
10301050 break ;
10311051 case STOP_MUSIC :
1052+ #if RV32_HAS (SDL_MIXER )
10321053 stop_music ();
1054+ #endif
10331055 break ;
10341056 default :
10351057 rv_log_error ("Unknown sound control request: %d" , request );
0 commit comments