JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
0b8a046c557c828473f091c8412e8f034b354f60
[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 #define CONDERROR(a) if ((a)) {fprintf(stderr,"Error: %s\n",SDL_GetError());exit(1);}
8 #define NULLERROR(a) CONDERROR((a)==NULL)
9
10 #define TUNE_TITLE_PAGE         0
11 #define TUNE_GAMEPLAY           1
12 #define TUNE_HIGH_SCORE_ENTRY   2
13 #define NUM_TUNES               3
14
15 #define SOUND_BANG              0
16 #define NUM_SOUNDS              4
17
18 static Mix_Music *music[NUM_TUNES];
19 static int music_volume[NUM_TUNES] = {128,128,128};
20 static Mix_Chunk *wav[NUM_SOUNDS];
21
22 int audio_rate;
23 Uint16 audio_format;
24 int audio_channels;
25
26 char *load_file(char *);
27 char *wav_file[] = {
28     "sounds/booom.wav",
29         "sounds/cboom.wav",
30         "sounds/boom.wav",
31         "sounds/bzboom.wav"
32 };
33
34 char *tune_file[] = {/*{{{*/
35     "music/magic.mod",
36     "music/getzznew.mod",
37     "music/4est_fulla3s.mod"
38 };/*}}}*/
39
40 int init_sound() {/*{{{*/
41     // Return 1 if the sound is ready to roll, and 0 if not.
42
43     int i;
44 #ifdef DEBUG
45     printf ("Initialise sound\n");
46 #endif
47
48     // Initialise output with SDL_mixer
49     if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
50         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
51         return 0;
52     }
53
54 #ifdef DEBUG
55     // What kind of sound did we get?  Ah who cares. As long as it can play
56     // some basic bangs and simple music.
57     Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
58     printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
59             (audio_format&0xFF),
60             (audio_channels > 1) ? "stereo" : "mono");
61 #endif
62
63     // Preload all the tunes into memory
64     for (i=0; i<NUM_TUNES; i++) {
65         if (!(music[i] = Mix_LoadMUS(load_file(tune_file[i])))) {
66             printf ("Failed to load %s\n",load_file(tune_file[i]));
67         }
68     }
69
70     // Preload all the wav files into memory
71     for (i=0; i<NUM_SOUNDS; i++) {
72         wav[i] = Mix_LoadWAV(load_file(wav_file[i]));
73     }
74
75     return 1;
76 }/*}}}*/
77
78 void play_sound(int i)  {/*{{{*/
79 #ifdef DEBUG
80     printf ("play sound %d on first free channel\n",i);
81 #endif
82     Mix_PlayChannel(-1, wav[i], 0);
83 }/*}}}*/
84
85 int playing=-1;
86
87
88 #undef DEBUG
89
90 void play_tune(int i) {/*{{{*/
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  */