JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fixed Makefile so we don't have to ship font_guts and font_guts.pov in the tarball
[vor.git] / sound.c
1 #include <SDL.h>
2 #include <SDL_mixer.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "args.h"
8 #include "common.h"
9 #include "config.h"
10 #include "sound.h"
11
12
13 static Mix_Music *music[NUM_TUNES];
14 static int music_volume[NUM_TUNES] = {255};
15 static Mix_Chunk *wav[NUM_SOUNDS];
16
17 int audio_rate;
18 Uint16 audio_format;
19 int audio_channels;
20
21 char *add_data_path(char *);
22 char *wav_file[] = {
23         "bang.wav"
24 };
25
26 char *tune_file[] = {
27         "mph.xm"
28 };
29
30 // Return 1 if the sound is ready to roll, and 0 if not.
31 int
32 init_sound() {
33         int i;
34         char *s;
35
36         // Initialise output with SDL_mixer
37         if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
38         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
39         return 0;
40         }
41
42         // Preload all the tunes into memory
43         for (i=0; i<NUM_TUNES; i++) {
44                 s = add_data_path(tune_file[i]);
45                 if(s) {
46                         music[i] = Mix_LoadMUS(s);
47                         if(!music[i]) printf("Failed to load %s.\n", s);
48                         free(s);
49                 }
50         }
51
52         // Preload all the wav files into memory
53         for (i=0; i<NUM_SOUNDS; i++) {
54                 s = add_data_path(wav_file[i]);
55                 if(s) {
56                         wav[i] = Mix_LoadWAV(s);
57                         free(s);
58                 }
59         }
60
61         return 1;
62 }
63
64 void
65 play_sound(int i)  {
66         if(!opt_sound) return;
67         Mix_PlayChannel(-1, wav[i], 0);
68 }
69
70 int playing = NUM_TUNES + 1;
71
72
73 void
74 play_tune(int i) {
75         if(!opt_sound) {
76                 return;
77         }
78         if (playing == i) {
79                 return;
80         }
81         if (playing < NUM_TUNES) {
82                 Mix_FadeOutMusic(2500);
83         }
84         // There are songs yet to be written...
85         if(i < NUM_TUNES) {
86                 Mix_FadeInMusic(music[i], -1, 2000);
87                 Mix_VolumeMusic(music_volume[i]);
88         }
89
90         playing = i;
91 }
92
93
94 int tune_paused=0;
95
96 void
97 pause_tune() {
98         if(!opt_sound) {
99                 return;
100         }
101         if(playing < NUM_TUNES && !tune_paused) {
102                 Mix_PauseMusic();
103                 tune_paused = 1;
104         }
105 }
106
107 void
108 resume_tune() {
109         if(!opt_sound) {
110                 return;
111         }
112         if(playing < NUM_TUNES && tune_paused) {
113                 Mix_ResumeMusic();
114                 tune_paused = 0;
115         }
116 }
117
118 /*
119  *
120  * The init_sound() routine is called first.
121  * The play_sound() routine is called with the index number of the sound we wish to play.
122  * The play_tune() routine is called with the index number of the tune we wish to play.
123  *
124  */