JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added playing instructions (keybindings mostly) to README
[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] = {88,88,88};
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         "magic.mod",
28         "getzznew.mod",
29         "4est_fulla3s.mod"
30 };
31
32 // Return 1 if the sound is ready to roll, and 0 if not.
33 int
34 init_sound() {
35         int i;
36         char *s;
37
38         // Initialise output with SDL_mixer
39         if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
40         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
41         return 0;
42         }
43
44         // Preload all the tunes into memory
45         for (i=0; i<NUM_TUNES; i++) {
46                 s = add_data_path(tune_file[i]);
47                 if(s) {
48                         music[i] = Mix_LoadMUS(s);
49                         if(!music[i]) printf("Failed to load %s.\n", s);
50                         free(s);
51                 }
52         }
53
54         // Preload all the wav files into memory
55         for (i=0; i<NUM_SOUNDS; i++) {
56                 s = add_data_path(wav_file[i]);
57                 if(s) {
58                         wav[i] = Mix_LoadWAV(s);
59                         free(s);
60                 }
61         }
62
63         return 1;
64 }
65
66 void
67 play_sound(int i)  {
68         if(!opt_sound) return;
69         Mix_PlayChannel(-1, wav[i], 0);
70 }
71
72 int playing=-1;
73
74
75 void
76 play_tune(int i) {
77         if(!opt_sound) {
78                 return;
79         }
80         if (playing == i) {
81                 return;
82         }
83         if (playing) {
84                 Mix_FadeOutMusic(2500);
85         }
86         if(i == TUNE_GAMEPLAY) {
87                 Mix_FadeInMusic(music[i],-1,2000);
88                 Mix_VolumeMusic(music_volume[i]);
89         }
90
91         playing = i;
92 }
93
94
95 int tune_paused=0;
96
97 void
98 pause_tune() {
99         if(!opt_sound) {
100                 return;
101         }
102         if(playing == TUNE_GAMEPLAY && !tune_paused) {
103                 Mix_PauseMusic();
104                 tune_paused = 1;
105         }
106 }
107
108 void
109 resume_tune() {
110         if(!opt_sound) {
111                 return;
112         }
113         if(playing == TUNE_GAMEPLAY && tune_paused) {
114                 Mix_ResumeMusic();
115                 tune_paused = 0;
116         }
117 }
118
119 /*
120  *
121  * The init_sound() routine is called first.
122  * The play_sound() routine is called with the index number of the sound we wish to play.
123  * The play_tune() routine is called with the index number of the tune we wish to play.
124  *
125  */