15
15
#include <unistd.h>
16
16
17
17
#include <SDL.h>
18
+ #if RV32_HAS (SDL_MIXER )
18
19
#include <SDL_mixer.h>
20
+ #endif
19
21
20
22
#include "riscv.h"
21
23
#include "riscv_private.h"
@@ -94,11 +96,13 @@ typedef struct sound {
94
96
/* SDL-mixer-related and music-related variables */
95
97
static pthread_t music_thread ;
96
98
static uint8_t * music_midi_data ;
99
+ #if RV32_HAS (SDL_MIXER )
97
100
static Mix_Music * mid ;
98
101
99
102
/* SDL-mixer-related and sfx-related variables */
100
103
static pthread_t sfx_thread ;
101
104
static Mix_Chunk * sfx_chunk ;
105
+ #endif
102
106
static uint8_t * sfx_samples ;
103
107
static uint32_t nr_sfx_samples ;
104
108
static int chan ;
@@ -718,6 +722,7 @@ uint8_t *mus2midi(uint8_t *data, int *length)
718
722
return midi_data ;
719
723
}
720
724
725
+ #if RV32_HAS (SDL_MIXER )
721
726
static void * sfx_handler (void * arg )
722
727
{
723
728
sound_t * sfx = (sound_t * ) arg ;
@@ -836,8 +841,10 @@ static void play_sfx(riscv_t *rv)
836
841
.size = sfx_data_size ,
837
842
.volume = volume ,
838
843
};
844
+ #if RV32_HAS (SDL_MIXER )
839
845
pthread_create (& sfx_thread , NULL , sfx_handler , & sfx );
840
846
sfx_thread_init = true;
847
+ #endif
841
848
/* FIXME: In web browser runtime, web workers in thread pool do not reap
842
849
* after sfx_handler return, thus we have to join them. sfx_handler does not
843
850
* contain infinite loop,so do not worry to be stalled by it */
@@ -893,8 +900,10 @@ static void play_music(riscv_t *rv)
893
900
.looping = looping ,
894
901
.volume = volume ,
895
902
};
903
+ #if RV32_HAS (SDL_MIXER )
896
904
pthread_create (& music_thread , NULL , music_handler , & music );
897
905
music_thread_init = true;
906
+ #endif
898
907
/* FIXME: In web browser runtime, web workers in thread pool do not reap
899
908
* after music_handler return, thus we have to join them. music_handler does
900
909
* 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)
916
925
/* multiplied by 8 because volume's max is 15 */
917
926
Mix_VolumeMusic (volume * 8 );
918
927
}
928
+ #endif /* RV32_HAS(SDL_MIXER) */
919
929
920
930
static void init_audio (void )
921
931
{
@@ -933,6 +943,7 @@ static void init_audio(void)
933
943
exit (EXIT_FAILURE );
934
944
}
935
945
946
+ #if RV32_HAS (SDL_MIXER )
936
947
/* Initialize SDL2 Mixer */
937
948
if (Mix_Init (MIX_INIT_MID ) != MIX_INIT_MID ) {
938
949
rv_log_fatal ("Mix_Init failed: %s" , Mix_GetError ());
@@ -943,6 +954,7 @@ static void init_audio(void)
943
954
Mix_Quit ();
944
955
exit (EXIT_FAILURE );
945
956
}
957
+ #endif
946
958
audio_init = true;
947
959
}
948
960
@@ -956,6 +968,7 @@ static void shutdown_audio()
956
968
* on a valid pthread_t identifier.
957
969
*/
958
970
971
+ #if RV32_HAS (SDL_MIXER )
959
972
if (music_thread_init ) {
960
973
stop_music ();
961
974
pthread_join (music_thread , NULL );
@@ -974,6 +987,7 @@ static void shutdown_audio()
974
987
975
988
Mix_CloseAudio ();
976
989
Mix_Quit ();
990
+ #endif
977
991
978
992
audio_init = sfx_thread_init = music_thread_init = false;
979
993
}
@@ -1020,16 +1034,24 @@ void syscall_control_audio(riscv_t *rv)
1020
1034
1021
1035
switch (request ) {
1022
1036
case PLAY_MUSIC :
1037
+ #if RV32_HAS (SDL_MIXER )
1023
1038
play_music (rv );
1039
+ #endif
1024
1040
break ;
1025
1041
case PLAY_SFX :
1042
+ #if RV32_HAS (SDL_MIXER )
1026
1043
play_sfx (rv );
1044
+ #endif
1027
1045
break ;
1028
1046
case SET_MUSIC_VOLUME :
1047
+ #if RV32_HAS (SDL_MIXER )
1029
1048
set_music_volume (rv );
1049
+ #endif
1030
1050
break ;
1031
1051
case STOP_MUSIC :
1052
+ #if RV32_HAS (SDL_MIXER )
1032
1053
stop_music ();
1054
+ #endif
1033
1055
break ;
1034
1056
default :
1035
1057
rv_log_error ("Unknown sound control request: %d" , request );
0 commit comments