JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
480690d10394493ee85db2c0a55175b35cc941cc
[vor.git] / sound.c
1 #include <SDL/SDL.h>
2 #include <SDL/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 #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 *add_path(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
43 init_sound() {
44         // Return 1 if the sound is ready to roll, and 0 if not.
45
46         int i;
47         debug(printf ("Initialise sound\n"));
48
49         // Initialise output with SDL_mixer
50         if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
51         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
52         return 0;
53         }
54
55         debug(
56                         // What kind of sound did we get?  Ah who cares. As long as it can play
57                         // some basic bangs and simple music.
58                         Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
59                         printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
60                                 (audio_format&0xFF),
61                                 (audio_channels > 1) ? "stereo" : "mono");
62                         ) 
63
64         // Preload all the tunes into memory
65         for (i=0; i<NUM_TUNES; i++) {
66                 if (!(music[i] = Mix_LoadMUS(add_path(tune_file[i])))) {
67                         printf ("Failed to load %s\n",add_path(tune_file[i]));
68                 }
69         }
70
71         // Preload all the wav files into memory
72         for (i=0; i<NUM_SOUNDS; i++) {
73                 wav[i] = Mix_LoadWAV(add_path(wav_file[i]));
74         }
75
76         return 1;
77 }
78
79 void
80 play_sound(int i)  {
81         if(!opt_sound) return;
82         debug(printf ("play sound %d on first free channel\n",i));
83         Mix_PlayChannel(-1, wav[i], 0);
84 }/*}}}*/
85
86 int playing=-1;
87
88
89 void
90 play_tune(int i) {/*{{{*/
91         if(!opt_music) return;
92         if (playing==i)
93         return;
94         if (playing) {
95                 Mix_FadeOutMusic(1500);
96                 debug(printf("Stop playing %d\n",playing));
97         }
98         debug(
99                         printf ("Play music %d\n",i);
100                         printf ("volume %d\n",music_volume[i]);
101                         )
102         Mix_FadeInMusic(music[i],-1,2000);
103         Mix_VolumeMusic(music_volume[i]);
104
105         playing = i;
106 }
107
108 /*
109  *
110  * The init_sound() routine is called first.
111  * The play_sound() routine is called with the index number of the sound we wish to play.
112  * The play_tune() routine is called with the index number of the tune we wish to play.
113  *
114  */