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