5 #include <SDL/SDL_mixer.h>
7 extern int sound_flag, music_flag;
9 #define CONDERROR(a) if ((a)) {fprintf(stderr,"Error: %s\n",SDL_GetError());exit(1);}
10 #define NULLERROR(a) CONDERROR((a)==NULL)
12 #define TUNE_TITLE_PAGE 0
13 #define TUNE_GAMEPLAY 1
14 #define TUNE_HIGH_SCORE_ENTRY 2
20 static Mix_Music *music[NUM_TUNES];
21 static int music_volume[NUM_TUNES] = {128,128,128};
22 static Mix_Chunk *wav[NUM_SOUNDS];
28 char *load_file(char *);
36 char *tune_file[] = {/*{{{*/
39 "music/4est_fulla3s.mod"
42 int init_sound() {/*{{{*/
43 // Return 1 if the sound is ready to roll, and 0 if not.
47 printf ("Initialise sound\n");
50 // Initialise output with SDL_mixer
51 if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
52 fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
57 // What kind of sound did we get? Ah who cares. As long as it can play
58 // some basic bangs and simple music.
59 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
60 printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
62 (audio_channels > 1) ? "stereo" : "mono");
65 // Preload all the tunes into memory
66 for (i=0; i<NUM_TUNES; i++) {
67 if (!(music[i] = Mix_LoadMUS(load_file(tune_file[i])))) {
68 printf ("Failed to load %s\n",load_file(tune_file[i]));
72 // Preload all the wav files into memory
73 for (i=0; i<NUM_SOUNDS; i++) {
74 wav[i] = Mix_LoadWAV(load_file(wav_file[i]));
80 void play_sound(int i) {/*{{{*/
81 if(!sound_flag) return;
83 printf ("play sound %d on first free channel\n",i);
85 Mix_PlayChannel(-1, wav[i], 0);
93 void play_tune(int i) {/*{{{*/
94 if(!sound_flag || !music_flag) return;
98 Mix_FadeOutMusic(1500);
100 printf("Stop playing %d\n",playing);
104 printf ("Play music %d\n",i);
105 printf ("volume %d\n",music_volume[i]);
107 Mix_FadeInMusic(music[i],-1,2000);
108 Mix_VolumeMusic(music_volume[i]);
115 * The init_sound() routine is called first.
116 * The play_sound() routine is called with the index number of the sound we wish to play.
117 * The play_tune() routine is called with the index number of the tune we wish to play.