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