5 #include <SDL/SDL_mixer.h>
7 #define CONDERROR(a) if ((a)) {fprintf(stderr,"Error: %s\n",SDL_GetError());exit(1);}
8 #define NULLERROR(a) CONDERROR((a)==NULL)
10 #define TUNE_TITLE_PAGE 0
11 #define TUNE_GAMEPLAY 1
12 #define TUNE_HIGH_SCORE_ENTRY 2
18 static Mix_Music *music[NUM_TUNES];
19 static int music_volume[NUM_TUNES] = {128,128,128};
20 static Mix_Chunk *wav[NUM_SOUNDS];
26 char *load_file(char *);
34 char *tune_file[] = {/*{{{*/
37 "music/4est_fulla3s.mod"
40 int init_sound() {/*{{{*/
41 // Return 1 if the sound is ready to roll, and 0 if not.
45 printf ("Initialise sound\n");
48 // Initialise output with SDL_mixer
49 if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
50 fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
55 // What kind of sound did we get? Ah who cares. As long as it can play
56 // some basic bangs and simple music.
57 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
58 printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
60 (audio_channels > 1) ? "stereo" : "mono");
63 // Preload all the tunes into memory
64 for (i=0; i<NUM_TUNES; i++) {
65 if (!(music[i] = Mix_LoadMUS(load_file(tune_file[i])))) {
66 printf ("Failed to load %s\n",load_file(tune_file[i]));
70 // Preload all the wav files into memory
71 for (i=0; i<NUM_SOUNDS; i++) {
72 wav[i] = Mix_LoadWAV(load_file(wav_file[i]));
78 void play_sound(int i) {/*{{{*/
80 printf ("play sound %d on first free channel\n",i);
82 Mix_PlayChannel(-1, wav[i], 0);
90 void play_tune(int i) {/*{{{*/
94 Mix_FadeOutMusic(1500);
96 printf("Stop playing %d\n",playing);
100 printf ("Play music %d\n",i);
101 printf ("volume %d\n",music_volume[i]);
103 Mix_FadeInMusic(music[i],-1,2000);
104 Mix_VolumeMusic(music_volume[i]);
111 * The init_sound() routine is called first.
112 * The play_sound() routine is called with the index number of the sound we wish to play.
113 * The play_tune() routine is called with the index number of the tune we wish to play.