-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
executable file
·390 lines (340 loc) · 7.6 KB
/
sound.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* sound.c - Platform Independant Sound Support - Apr. 1995
*
* Copyright 1994-1995 Sujal M. Patel ([email protected])
* Conditions in "copyright.h"
*
* Modified for koules and bugfixed by Jan Hubicka
*/
#ifdef SOUND
//DKS
//#include <stdio.h>
//#ifdef __STDC__
//#include <stdlib.h>
//#endif
//#include <unistd.h>
//#include <sys/time.h>
//#include <fcntl.h>
//#include <signal.h>
//#include <sys/stat.h>
//DKS
#include "SDL.h"
#include "SDL_mixer.h"
#include <stdio.h>
#include <stdlib.h>
//DKS from koules.c (gotta clean up header, can't include it or get failed compiles)
extern int sndinit;
extern int sound;
//DKS - pasted here from koules.h for reference.. these are the sound #'s
//#define S_START 0
//#define S_END 1
//#define S_COLIZE 2
//#define S_DESTROY_BALL 3
//#define S_DESTROY_ROCKET 4
//#define S_CREATOR1 5
//#define S_CREATOR2 6
//DKS
//char *unixSoundPath = SOUNDDIR;
//char *unixSoundDev = SOUNDDEV;
#define playSounds 1
//DKS - Added for SDL
#define NUM_SOUNDS 7
static Mix_Chunk *sounds[NUM_SOUNDS]; // Array holding our 7 sounds
#ifdef GP2X
// Volume gets set between 0-128
float cur_volume = 1;
#endif
//DKS
//static int soundfd;
static Uint8 audioOK = 1;
//DKS static char sound_flags[20]; /* Sound Flag for sound 1-19 */
//DKS int child;
int music_enabled = 1;
////DKS - added music:
//#define NUM_SONGS 6
//static int cur_song = 0;
//static Mix_Music *songs[NUMSONGS];
//static char* song_names[NUM_SONGS] = { "1_5.mp3", "2_0.mp3", "3_1.mp3", "4_0.mp3", "5_0.mp3", "6_0.mp3" };
//DKS - added music:
static Mix_Music *song = NULL;
static const char* songname = "sounds/music.ogg";
void init_music(void)
{
if (audioOK)
{
printf("Initializing music.. Here is the list of available decoders:\n");
// print music decoders available
int i,max=Mix_GetNumMusicDecoders();
for(i=0; i<max; ++i)
printf("Music decoder %d is for %s\n",i, Mix_GetMusicDecoder(i));
song = Mix_LoadMUS(songname);
// Mix_VolumeMusic(MIX_MAX_VOLUME);
Mix_VolumeMusic(80);
if (song) {
printf("Successfully loaded music file: %s\n", songname);
} else {
printf("Failed to load music file: %s\nMix Error:%s\n", songname,Mix_GetError());
}
}
}
void free_music(void)
{
if (audioOK && song)
{
printf("Freeing music..\n");
Mix_HaltMusic();
Mix_FreeMusic(song);
}
}
void start_music(void)
{
if (music_enabled && audioOK && song)
{
if (Mix_PlayMusic(song, -1)==-1) {
printf("Mix_FadeInMusic: %s\n", Mix_GetError());
// well, there's no music, but most games don't break without music...
}
Mix_VolumeMusic(MIX_MAX_VOLUME);
}
}
void pause_music(void)
{
if (audioOK)
Mix_PauseMusic();
}
void resume_music(void)
{
if (music_enabled && audioOK)
Mix_ResumeMusic();
}
void stop_music(void)
{
if (audioOK)
Mix_HaltMusic();
}
void toggle_music(void)
{
if (music_enabled)
{
music_enabled = 0;
stop_music();
} else {
music_enabled = 1;
start_music();
}
}
//DKS - why do we need this??
//void
//test_sound ()
//{
// if (audioOK)
// if (kill (child, 0))
// {
// audioOK = 0;
// close (soundfd);
// };
//}
//void
//init_sound ()
//{
// int i, fd[2];
// char *argv[4];
// char filename[512];
//
// signal (SIGCHLD, SIG_IGN);
// signal (SIGPIPE, SIG_IGN);
//
// if (unixSoundPath[0] == '?')
// {
// audioOK = 0;
// return;
// };
//
// /* Create a pipe, set the write end to close when we exec the sound server,
// and set both (is the write end necessary?) ends to non-blocking */
// pipe (fd);
// soundfd = fd[1];
//
// if (!(child = fork ()))
// {
// close (fd[1]);
// dup2 (fd[0], STDIN_FILENO);
// close (fd[0]);
// sprintf (filename, SOUNDSERVER);
// argv[0] = filename;
// argv[1] = unixSoundPath;
// argv[2] = unixSoundDev;
// argv[3] = NULL;
// execvp (filename, argv);
// fprintf (stderr, "Couldn't Execute Sound Server %s!\n", filename);
// exit (0);
// };
// close (fd[0]);
//
// for (i = 0; i < 19; i++)
// sound_flags[i] = 0;
//}
//DKS - new
void adjust_volume() {
#ifdef GP2X
if (audioOK)
Mix_Volume(-1, (int) (cur_volume));
#endif
}
//DKS modified and replaced
void
init_sound ()
{
//DKS - init SDL audio and check if it's OK. audioOK defaults to 1
// which means it's OK.
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
printf("Problem initializing SDL audio. Sound disabled.\n");
audioOK = 0;
return;
} else {
// Initialize SDL_mixer for GP2X, buffer is set lower than PC
#ifdef GP2X
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 128) == -1) {
audioOK = 0;
return;
}
#else
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 1024) == -1) {
audioOK = 0;
return;
}
#endif
int errors = 0;
if (!(sounds[0] = Mix_LoadWAV("sounds/start.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[1] = Mix_LoadWAV("sounds/end.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[2] = Mix_LoadWAV("sounds/colize.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[3] = Mix_LoadWAV("sounds/destroy1.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[4] = Mix_LoadWAV("sounds/destroy2.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[5] = Mix_LoadWAV("sounds/creator1.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (!(sounds[6] = Mix_LoadWAV("sounds/creator2.wav")))
{
errors++;
printf("Error: Mix_LoadWAV: %s\n", Mix_GetError());
}
if (errors) audioOK = 0;
// //DKS - for GCW, set Mixer channel volumes to less than max
#ifdef GCW
Mix_Volume(-1,24); // The game's stock sounds are VERY loud.
#else
Mix_Volume(-1,MIX_MAX_VOLUME/2);
#endif
if (!audioOK) {
printf("Problem loading WAVs from sound/ dir. Sound disabled.\n");
} else {
// OK, everything is fine, set the volume to our reasonable default
#ifdef GP2X
adjust_volume();
#endif
}
}
sndinit = audioOK; //sndinit tells rest of program whether sound is functional
sound = audioOK; // Disable sound in rest of program if necessary.
if (audioOK)
init_music();
return;
}
//DKS
//int
//play_sound (k)
// int k;
//{
// char c;
// test_sound ();
// c = k;
// if ((playSounds) && (audioOK))
// write (soundfd, &c, sizeof (c));
// return 0;
//}
//DKS
void play_sound (int k)
{
//test_sound ();
if (audioOK) {
Mix_PlayChannel(-1, sounds[k], 0);
}
return;
}
//DKS - do we need this??
//void
//maybe_play_sound (k)
// int k;
//{
// char c;
//
// if (sound_flags[k] & 1)
// return;
//
// sound_flags[k] |= 1;
// test_sound ();
// c = (unsigned char) (k);
// if ((playSounds) && (audioOK))
// write (soundfd, &c, sizeof (c));
//}
//DKS - do we need this?
//void
//sound_completed (k)
// int k;
//{
// sound_flags[k] &= ~1;
//}
//void
//kill_sound ()
//{
// signed char c;
//
// c = -1;
// test_sound ();
// if ((playSounds) && (audioOK))
// write (soundfd, &c, sizeof (c));
//}
//DKS - new
void pause_sound() {
Mix_Pause(-1);
}
//DKS - new
void resume_sound() {
Mix_Resume(-1);
}
//DKS - rewrote for SDL
void kill_sound () {
Mix_CloseAudio();
Mix_FreeChunk(sounds[0]);
Mix_FreeChunk(sounds[1]);
Mix_FreeChunk(sounds[2]);
Mix_FreeChunk(sounds[3]);
Mix_FreeChunk(sounds[4]);
Mix_FreeChunk(sounds[5]);
Mix_FreeChunk(sounds[6]);
free_music();
return;
}
#endif