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