JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Some code cleanup.
[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 init_sound() {/*{{{*/
39     // Return 1 if the sound is ready to roll, and 0 if not.
40
41     int i;
42 #ifdef DEBUG
43     printf ("Initialise sound\n");
44 #endif
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 #ifdef 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 #endif
60
61     // Preload all the tunes into memory
62     for (i=0; i<NUM_TUNES; i++) {
63         if (!(music[i] = Mix_LoadMUS(load_file(tune_file[i])))) {
64             printf ("Failed to load %s\n",load_file(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(load_file(wav_file[i]));
71     }
72
73     return 1;
74 }/*}}}*/
75
76 void play_sound(int i)  {/*{{{*/
77         if(!sound_flag) return;
78 #ifdef DEBUG
79     printf ("play sound %d on first free channel\n",i);
80 #endif
81     Mix_PlayChannel(-1, wav[i], 0);
82 }/*}}}*/
83
84 int playing=-1;
85
86
87 #undef DEBUG
88
89 void play_tune(int i) {/*{{{*/
90         if(!sound_flag || !music_flag) return;
91     if (playing==i)
92         return;
93     if (playing) {
94         Mix_FadeOutMusic(1500);
95 #ifdef DEBUG
96         printf("Stop playing %d\n",playing);
97 #endif
98     }
99 #ifdef DEBUG
100     printf ("Play music %d\n",i);
101     printf ("volume %d\n",music_volume[i]);
102 #endif
103     Mix_FadeInMusic(music[i],-1,2000);
104     Mix_VolumeMusic(music_volume[i]);
105
106     playing = i;
107 }/*}}}*/
108
109 /*
110  *
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.
114  *
115  */