1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <SDL/SDL.h>
5 #include <SDL/SDL_mixer.h>
6 #include "datafun.h"
7 #include "globals.h"
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 NUM_TUNES 5
13 #define SOUND_BANG 0
14 #define NUM_SOUNDS 12
15
16 int sfx_enabled;
17
18 static Mix_Music *music[NUM_TUNES];
19 /*! \brief Fractions for calculating the real volume.
20 *
21 * Each of this values is multiplied times the global music volume
22 * variable and then shifted seven bits to the right. With this we can
23 * balance the music volumes a bit, as the song "front_1.mod" is quite
24 * loud in comparison to the others. See play_tune().
25 */
26 static int music_volumefractions[NUM_TUNES] = { 128, 128, 128, 134, 71 };
27 static Mix_Chunk *wav[NUM_SOUNDS];
28 static int audio_rate;
29 static Uint16 audio_format;
30 static int audio_channels;
31 static int playing = -1;
32
33 const char *wav_file[] = {
34 "booom.wav", "cboom.wav", "boom.wav", "bzboom.wav",
35 "speedup.wav", /* 4 */
36 "drip.wav", /* 5 */
37 "fart.1.aiff", "fart.2.aiff", "fart.3.aiff", "fart.4.aiff",
38 "shockwave-water.aiff" /* 10, from ST-45 */, "xbad.aiff" /* 11, modified from ST-15 */
39 };
40
41 const char *tune_file[] = {
42 "magic.mod",
43 "getzznew.mod",
44 "4est_fulla3s.mod",
45 "ramcharg.mod",
46 "front_1.mod",
47 NULL
48 };
49
init_sound()50 int init_sound() {
51 // Return 1 if the sound is ready to roll, and 0 if not.
52 int i;
53
54 #ifdef DEBUG
55 printf("Initialise sound\n");
56 #endif
57 // Initialise output with SDL_mixer
58 if(Mix_OpenAudio
59 (MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 512) < 0) {
60 fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
61 return 0;
62 }
63 // What kind of sound did we get? Ah who cares. As long as it can play
64 // some basic bangs and simple music.
65 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
66 printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
67 (audio_format & 0xFF), (audio_channels > 1) ? "stereo" : "mono");
68 // Preload all the tunes into memory
69 for(i = 0; i < NUM_TUNES; i++) {
70 const char *tune_name = tune_file[i];
71 #ifdef DEBUG
72 printf("Loading tune '%s'.\n", load_file(tune_name));
73 #endif
74 if(tune_name == NULL) {
75 fprintf(stderr, "We are at tune %d and got a NULL pointer.\nBlack hole encountered and can not reach the event horizon...\n", i);
76 break;
77 }
78 if(!(music[i] = Mix_LoadMUS(load_file(tune_name)))) {
79 printf("Failed to load '%s'.\n", load_file(tune_name));
80 }
81 }
82 // Preload all the wav files into memory
83 for(i = 0; i < NUM_SOUNDS; i++) {
84 wav[i] = Mix_LoadWAV(load_file(wav_file[i]));
85 if(wav[i] == NULL) {
86 fprintf(stderr, "Error while loading '%s'.\n", wav_file[i]);
87 } else {
88 #ifdef DEBUG
89 printf("i=$%02x name='%s'\n", i, wav_file[i]);
90 #endif
91 }
92 }
93 sfx_enabled = 1;
94 return 1;
95 }
96
play_sound(int i)97 void play_sound(int i) {
98 #ifdef DEBUG
99 int uc = -1; //used channel
100 #endif
101
102 if(sfx_enabled) {
103 if(i >= NUM_SOUNDS) fprintf(stderr, "Sound number %d is >= than %d!\n", i, NUM_SOUNDS);
104 #ifdef DEBUG
105 uc = Mix_PlayChannel(-1, wav[i], 0);
106 #else
107 (void) Mix_PlayChannel(-1, wav[i], 0);
108 #endif
109 }
110 #ifdef DEBUG
111 printf("play sound %d on first free channel (%d)\n", i, uc);
112 #endif
113 }
114
115
play_tune(int i)116 void play_tune(int i) {
117 int vol;
118
119 if(playing == i)
120 return;
121 if(playing) {
122 Mix_FadeOutMusic(15);
123 #ifdef DEBUG
124 printf("Stop playing %d\n", playing);
125 #endif
126 }
127 if(i >= NUM_TUNES || music[i] == NULL) return; //Illegal song number.
128 #ifdef DEBUG
129 printf("Play music %d\n", i);
130 printf("volume %d fraction %d\n", music_volume, music_volumefractions[i]);
131 #endif
132 Mix_FadeInMusic(music[i], -1, 20);
133 vol = (music_volumefractions[i] * music_volume) >> 7;
134 Mix_VolumeMusic(vol);
135 playing = i;
136 }
137
138 /*
139 *
140 * The init_sound() routine is called first.
141 * The play_sound() routine is called with the index number of the sound we wish to play.
142 * The play_tune() routine is called with the index number of the tune we wish to play.
143 *
144 */
145