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