JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
By Jove, I think I've got it!
[vor.git] / sound.c
1 #include "sound.h"
2 #include "config.h"
3
4 extern int sound_flag, music_flag;
5
6 #define CONDERROR(a) if ((a)) {fprintf(stderr,"Error: %s\n",SDL_GetError());exit(1);}
7 #define NULLERROR(a) CONDERROR((a)==NULL)
8
9 #define TUNE_TITLE_PAGE         0
10 #define TUNE_GAMEPLAY           1
11 #define TUNE_HIGH_SCORE_ENTRY   2
12 #define NUM_TUNES               3
13
14 #define SOUND_BANG              0
15 #define NUM_SOUNDS              4
16
17 static Mix_Music *music[NUM_TUNES];
18 static int music_volume[NUM_TUNES] = {128,128,128};
19 static Mix_Chunk *wav[NUM_SOUNDS];
20
21 int audio_rate;
22 Uint16 audio_format;
23 int audio_channels;
24
25 char *add_path(char *);
26 char *wav_file[] = {
27     "sounds/booom.wav",
28         "sounds/cboom.wav",
29         "sounds/boom.wav",
30         "sounds/bzboom.wav"
31 };
32
33 char *tune_file[] = {
34     "music/magic.mod",
35     "music/getzznew.mod",
36     "music/4est_fulla3s.mod"
37 };
38
39 int
40 init_sound() {
41     // Return 1 if the sound is ready to roll, and 0 if not.
42
43     int i;
44     debug(printf ("Initialise sound\n"));
45
46     // Initialise output with SDL_mixer
47     if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
48         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
49         return 0;
50     }
51
52         debug(
53                         // What kind of sound did we get?  Ah who cares. As long as it can play
54                         // some basic bangs and simple music.
55                         Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
56                         printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
57                                 (audio_format&0xFF),
58                                 (audio_channels > 1) ? "stereo" : "mono");
59                         ) 
60
61     // Preload all the tunes into memory
62     for (i=0; i<NUM_TUNES; i++) {
63         if (!(music[i] = Mix_LoadMUS(add_path(tune_file[i])))) {
64             printf ("Failed to load %s\n",add_path(tune_file[i]));
65         }
66     }
67
68     // Preload all the wav files into memory
69     for (i=0; i<NUM_SOUNDS; i++) {
70         wav[i] = Mix_LoadWAV(add_path(wav_file[i]));
71     }
72
73     return 1;
74 }
75
76 void
77 play_sound(int i)  {
78         if(!sound_flag) return;
79     debug(printf ("play sound %d on first free channel\n",i));
80     Mix_PlayChannel(-1, wav[i], 0);
81 }/*}}}*/
82
83 int playing=-1;
84
85
86 void
87 play_tune(int i) {/*{{{*/
88         if(!sound_flag || !music_flag) return;
89     if (playing==i)
90         return;
91     if (playing) {
92         Mix_FadeOutMusic(1500);
93         debug(printf("Stop playing %d\n",playing));
94     }
95         debug(
96                         printf ("Play music %d\n",i);
97                         printf ("volume %d\n",music_volume[i]);
98                         )
99     Mix_FadeInMusic(music[i],-1,2000);
100     Mix_VolumeMusic(music_volume[i]);
101
102     playing = i;
103 }
104
105 /*
106  *
107  * The init_sound() routine is called first.
108  * The play_sound() routine is called with the index number of the sound we wish to play.
109  * The play_tune() routine is called with the index number of the tune we wish to play.
110  *
111  */