-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoundmanager.c
348 lines (306 loc) · 7.92 KB
/
soundmanager.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
/*
* $Id: soundmanager.c,v 1.3 2003/04/26 03:24:16 kenta Exp $
*
* Copyright 2003 Kenta Cho. All rights reserved.
*/
/**
* BGM/SE manager(using SDL_mixer).
*
* @version $Revision: 1.3 $
*/
#include "SDL.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include "SDL_mixer.h"
#include "soundmanager.h"
//senquack - new gp2x volume control
#if defined(GP2X) || defined(WIZ)
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#endif
//senquack - for new settings:
#include "rr.h"
static int useAudio = 0;
#define MUSIC_NUM 3
#define SHARE_LOC "rr_share/"
static char *musicFileName[MUSIC_NUM] = {
//senquack - first ogg file freezes open2x (and maybe other) ARM platforms, changed to wav
#if defined(GP2X) || defined(WIZ)
"stg_a.wav", "stg_b.ogg", "stg_c.ogg",
#else
"stg_a.ogg", "stg_b.ogg", "stg_c.ogg",
#endif
};
static Mix_Music *music[MUSIC_NUM];
#define CHUNK_NUM 16
static char *chunkFileName[CHUNK_NUM] = {
"laser_start.wav", "laser.wav", "damage.wav", "bomb.wav",
"destroied.wav", "explosion1.wav", "explosion2.wav", "miss.wav",
"extend.wav",
"grz.wav", "grzinv.wav",
"shot.wav", "change.wav",
"reflec1.wav", "reflec2.wav", "ref_ready.wav",
};
static Mix_Chunk *chunk[CHUNK_NUM];
static int chunkChannel[CHUNK_NUM] = {
0, 1, 2, 3,
4, 5, 6, 7, 4,
6, 7,
6, 7,
7, 7, 7,
};
#if defined(GP2X) || defined(WIZ)
//DKS - new
//senquack - for GP2X volume control:
static float gp2x_current_volume = INIT_VOLUME;
static void gp2x_set_volume (int newvol)
{
printf ("NEW VOLUME:%d\n", newvol);
fflush (stdout);
if ((newvol >= 0) && (newvol <= 100)) {
unsigned long soundDev = open ("/dev/mixer", O_RDWR);
if (soundDev) {
int vol = ((newvol << 8) | newvol);
ioctl (soundDev, SOUND_MIXER_WRITE_PCM, &vol);
close (soundDev);
}
}
}
//senquack - called at startup so we can
// set it back to what it was when we exit.
// Returns 0-100, current mixer volume, -1 on error.
static int gp2x_get_volume (void)
{
int vol = -1;
unsigned long soundDev = open ("/dev/mixer", O_RDONLY);
if (soundDev) {
ioctl (soundDev, SOUND_MIXER_READ_PCM, &vol);
close (soundDev);
if (vol != -1) {
//just return one channel , not both channels, they're hopefully the same anyways
return (vol & 0xFF);
}
}
return vol;
}
//senquack
//Will change the volume level up or down, if amount is positive or negative. Will do volume level clamping.
void gp2x_change_volume (float amount)
{
//senquack - limit amount volume can be changed when we're down low already..
// So headphone users can set a good volume
if ((amount > 0.25f) && gp2x_current_volume <= 20.0) {
amount = 0.25f;
} else if ((amount < 0.25f) && gp2x_current_volume <= 20.0) {
amount = -0.25f;
}
gp2x_current_volume += amount;
if (gp2x_current_volume > 100.0f) {
gp2x_current_volume = 100.0f;
} else if (gp2x_current_volume < 0) {
gp2x_current_volume = 0;
}
gp2x_set_volume ((int) gp2x_current_volume);
}
#endif // GP2X volume stuff
void
closeSound ()
{
int i;
if (!useAudio)
return;
if (Mix_PlayingMusic ()) {
Mix_HaltMusic ();
}
for (i = 0; i < MUSIC_NUM; i++) {
if (music[i]) {
Mix_FreeMusic (music[i]);
}
}
for (i = 0; i < CHUNK_NUM; i++) {
if (chunk[i]) {
Mix_FreeChunk (chunk[i]);
}
}
Mix_CloseAudio ();
}
// Initialize the sound.
//senquack - trying to fix glibc memory corruption error:
//static void loadSounds() {
// int i;
// char name[56];
//
// for ( i=0 ; i<MUSIC_NUM ; i++ ) {
// strcpy(name, SHARE_LOC);
// strcat(name, "sounds/");
// strcat(name, musicFileName[i]);
// if ( NULL == (music[i] = Mix_LoadMUS(name)) ) {
// fprintf(stderr, "Couldn't load: %s\n", name);
// useAudio = 0;
// return;
// }
// }
// for ( i=0 ; i<CHUNK_NUM ; i++ ) {
// strcpy(name, SHARE_LOC);
// strcat(name, "/sounds/");
// strcat(name, chunkFileName[i]);
// if ( NULL == (chunk[i] = Mix_LoadWAV(name)) ) {
// fprintf(stderr, "Couldn't load: %s\n", name);
// useAudio = 0;
// return;
// }
// }
//}
static void
loadSounds ()
{
//senquack
printf ("entering loadSounds()\n");
fflush (stdout);
int i;
char name[56];
for (i = 0; i < MUSIC_NUM; i++) {
strcpy (name, SHARE_LOC);
strcat (name, "sounds/");
strcat (name, musicFileName[i]);
//senquack
printf ("trying to load %s in loadSounds()\n", name);
fflush (stdout);
if (NULL == (music[i] = Mix_LoadMUS (name))) {
//senquack
// fprintf(stderr, "Couldn't load: %s\n", name);
printf ("Couldn't load: %s\n", name);
printf ("Error: %s\n", Mix_GetError ());
fflush (stdout);
useAudio = 0;
return;
}
}
//senquack
printf ("loaded music in loadSounds()\n");
fflush (stdout);
for (i = 0; i < CHUNK_NUM; i++) {
strcpy (name, SHARE_LOC);
strcat (name, "/sounds/");
strcat (name, chunkFileName[i]);
if (NULL == (chunk[i] = Mix_LoadWAV (name))) {
fprintf (stderr, "Couldn't load: %s\n", name);
useAudio = 0;
return;
}
}
//senquack
printf ("leaving loadSounds()\n");
fflush (stdout);
}
void
initSound ()
{
int audio_rate;
Uint16 audio_format;
int audio_channels;
int audio_buffers;
if (SDL_InitSubSystem (SDL_INIT_AUDIO) < 0) {
fprintf (stderr, "Unable to initialize SDL_AUDIO: %s\n",
SDL_GetError ());
return;
}
//senquack - altering for GP2X: (NOTE: channels = 1 is correct, the original OGG music is mono)
#if defined(GP2X) || defined(WIZ)
audio_rate = 44100;
audio_format = AUDIO_S16;
audio_channels = 1;
audio_buffers = 512;
//senquack - MANDATORY TODO - add GCW define to Makefile
#elif defined (GCW)
audio_rate = 44100;
audio_format = AUDIO_S16;
audio_channels = 1;
audio_buffers = 1024;
#else
audio_rate = 44100;
audio_format = AUDIO_S16;
audio_channels = 1;
audio_buffers = 4096;
#endif
if (Mix_OpenAudio (audio_rate, audio_format, audio_channels, audio_buffers)
< 0) {
fprintf (stderr, "Couldn't open audio: %s\n", SDL_GetError ());
return;
} else {
Mix_QuerySpec (&audio_rate, &audio_format, &audio_channels);
}
#if defined(GP2X) || defined(WIZ)
//senquack - making sound nicer in Wiz version than GP2X version
gp2x_set_volume (INIT_VOLUME);
#endif
useAudio = 1;
loadSounds ();
//senquack - adjust volume to sensible level
Mix_Volume(-1,30);
}
// Play/Stop the music/chunk.
void
playMusic (int idx)
{
//senquack
// if ( !useAudio ) return;
if (!useAudio || !settings.music)
return;
Mix_PlayMusic (music[idx], -1);
//senquack - adjust volume to sensible level
Mix_VolumeMusic(30);
}
void
fadeMusic ()
{
//senquack
// if ( !useAudio ) return;
if (!useAudio || !settings.music)
return;
Mix_FadeOutMusic (1280);
}
void
stopMusic ()
{
//senquack
// if ( !useAudio ) return;
if (!useAudio || !settings.music)
return;
if (Mix_PlayingMusic ()) {
//senquack - trying to narrow down problem
Mix_HaltMusic ();
}
}
//senquack - new logic to prevent playing of the laser.wav, as it is
// barely audible and has annoying clicks in it
//void playChunk(int idx) {
// if ( !useAudio ) return;
// Mix_PlayChannel(chunkChannel[idx], chunk[idx], 0);
//}
void
playChunk (int idx)
{
// if (!useAudio)
// return;
if ( !useAudio || idx == 1) return;
Mix_PlayChannel (chunkChannel[idx], chunk[idx], 0);
}
//senquack - new logic to prevent playing of the laser.wav, as it is
// barely audible and has annoying clicks in it
//void haltChunk(int idx) {
// if ( !useAudio ) return;
// Mix_HaltChannel(chunkChannel[idx]);
//}
void
haltChunk (int idx)
{
// if (!useAudio)
// return;
if ( !useAudio || idx == 1) return;
Mix_HaltChannel (chunkChannel[idx]);
}