-
Notifications
You must be signed in to change notification settings - Fork 6
/
sound.c
707 lines (574 loc) · 17.1 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
*
* Copyright (c) 1994, 2002, 2003 Johannes Prix
* Copyright (c) 1994, 2002, 2003 Reinhard Prix
*
*
* This file is part of Freedroid
*
* Freedroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Freedroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Freedroid; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
/*----------------------------------------------------------------------
*
* Desc: all functions dealing with sound are contained in this file.
*
*----------------------------------------------------------------------*/
#ifndef _sound_c
#define _sound_c
#endif
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"
// The following is the definition of the sound file names used in freedroid
// DO NOT CHANGE THE ORDER OF APPEARENCE IN THIS LIST unless you
// also adjust the order of appearance in defs.h!
const char *SoundSampleFilenames[ALL_SOUNDS] = {
"ERRORSOUND_NILL.NOWAV",
"Blast_Sound_0.wav",
// "Collision_Sound_0.wav", // replaced by damage-dependent-sounds: Collision_[Neutral|GotDamaged|DamagedEnemy]
"Collision_Neutral.wav",
"Collision_GotDamaged.wav",
"Collision_DamagedEnemy.wav",
//"GotIntoBlast_Sound_0.wav", // replaced by GotIntoBlast_Sound_1.wav
"GotIntoBlast_Sound_1.wav",
"MoveElevator_Sound_0.wav",
"Refresh_Sound_0.wav",
"LeaveElevator_Sound_0.wav",
"EnterElevator_Sound_0.wav",
"ThouArtDefeated_Sound_0.wav",
"Got_Hit_Sound_0.wav",
"TakeoverSetCapsule_Sound_0.wav",
"Menu_Item_Selected_Sound_0.wav",
"Move_Menu_Position_Sound_0.wav",
"Takeover_Game_Won_Sound_0.wav",
"Takeover_Game_Deadlock_Sound_0.wav",
"Takeover_Game_Lost_Sound_0.wav",
"Fire_Bullet_Pulse_Sound_0.wav",
"Fire_Bullet_Single_Pulse_Sound_0.wav",
"Fire_Bullet_Military_Sound_0.wav",
"Fire_Bullet_Flash_Sound_0.wav",
"Fire_Bullet_Exterminator_Sound_0.wav",
"Fire_Bullet_Laser_Rifle_Sound.wav",
"Cry_Sound_0.wav",
"Takeover_Sound_0.wav",
"Countdown_Sound.wav",
"EndCountdown_Sound.wav",
"InfluExplosion.wav",
"WhiteNoise.wav",
"Alert.wav",
"Screenshot.wav"
};
#ifdef HAVE_LIBSDL_MIXER
Mix_Chunk *Loaded_WAV_Files[ALL_SOUNDS];
#endif
const char *MusicFiles [NUM_COLORS] = { // we have a background song per color now
#ifdef ANDROID
"AnarchyMenu1.mod.ogg", // RED
"starpaws.mod.ogg", // YELLOW
"The_Last_V8.mod.ogg", // GREEN
"dreamfish-green_beret.mod.ogg", // GRAY
"dreamfish-sanxion.mod.ogg", // BLUE // CRASHES the GCW0 ???
"kollaps-tron.mod.ogg", // GREENBLUE
"dreamfish-uridium2_loader.mod.ogg" // DARK
#else
"AnarchyMenu1.mod", // RED
"starpaws.mod", // YELLOW
"The_Last_V8.mod", // GREEN
"dreamfish-green_beret.mod", // GRAY
#ifdef GCW0
"dreamfish-green_beret.mod", // GRAY
#else
"dreamfish-sanxion.mod", // BLUE // CRASHES the GCW0 ???
#endif
"kollaps-tron.mod", // GREENBLUE
"dreamfish-uridium2_loader.mod" // DARK
#endif // ANDROID
};
#ifdef HAVE_LIBSDL_MIXER
Mix_Music *MusicSongs[NUM_COLORS];
Mix_Music *Tmp_MOD_File;
#endif
void
Init_Audio(void)
{
#ifndef HAVE_LIBSDL_MIXER
return;
#else
char *fpath;
int i;
int audio_rate = MIX_DEFAULT_FREQUENCY; // 22050;
Uint16 audio_format = MIX_DEFAULT_FORMAT;
int audio_channels = 2; // = stereo
// int audio_buffers = 4096;
int audio_buffers = 100; // higher = less risk of sound skips, but more lag
DebugPrintf(1, "\nInitializing SDL Audio Systems....\n");
if ( !sound_on ) return;
// Now SDL_AUDIO is initialized here:
if ( SDL_InitSubSystem ( SDL_INIT_AUDIO ) == -1 )
{
DebugPrintf (0, "WARNING: SDL Sound subsystem could not be initialized.\n\
Continuing with sound disabled\n");
sound_on = FALSE;
return;
}
else
DebugPrintf(1, "SDL Audio initialisation successful.\n");
// Now that we have initialized the audio SubSystem, we must open
// an audio channel. This will be done here (see code from Mixer-Tutorial):
if ( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) )
{
DebugPrintf (0, "WARNING: SDL audio channel could not be opened. \n");
DebugPrintf (0, "SDL Mixer Error: %s\nContinuing with sound disabled\n", Mix_GetError());
sound_on = FALSE;
return;
}
else
DebugPrintf (1, "\nSuccessfully opened SDL audio channel." );
if (Mix_AllocateChannels(20) != 20)
DebugPrintf (0, "\nWARNING: could not get all 20 mixer-channels I asked for...\n");
// Now that the audio channel is opend, its time to load all the
// WAV files into memory, something we NEVER did while using the yiff,
// because the yiff did all the loading, analyzing and playing...
Loaded_WAV_Files[0]=NULL;
for (i = 1; i < ALL_SOUNDS; i++)
{
fpath = find_file (SoundSampleFilenames[ i ], SOUND_DIR, NO_THEME, WARNONLY);
if (fpath) Loaded_WAV_Files[ i ] = Mix_LoadWAV(fpath);
if ( Loaded_WAV_Files[i] == NULL )
{
DebugPrintf (0, "Could not load Sound-sample: %s\n", SoundSampleFilenames[ i ]);
DebugPrintf (0, "WARNING: Continuing with sound disabled. Error = %d\n", Mix_GetError());
sound_on = FALSE;
return;
} // if ( !Loaded_WAV...
else
DebugPrintf (1, "\nSuccessfully loaded file %s.", SoundSampleFilenames[i]);
} // for (i=1; ...
for (i = 0; i < NUM_COLORS; i++)
{
fpath = find_file ( MusicFiles [ i ], SOUND_DIR, NO_THEME, WARNONLY);
if (fpath) MusicSongs [ i ] = Mix_LoadMUS( fpath );
if ( MusicSongs[ i ] == NULL )
{
DebugPrintf ( 0, "\nError loading sound-file: %s\n", MusicFiles[ i ]);
DebugPrintf (0, "SDL Mixer Error: %s\n Continuing with sound disabled\n", Mix_GetError());
sound_on = FALSE;
return;
} // if ( !Loaded_WAV...
else
DebugPrintf ( 1 , "\nSuccessfully loaded file %s.", MusicFiles[ i ]);
} // for
//--------------------
// Now that the music files have been loaded successfully, it's time to set
// the music and sound volumes accoridingly, i.e. as specifies by the users
// configuration.
//
Set_Sound_FX_Volume( GameConfig.Current_Sound_FX_Volume );
// DebugPrintf (1, "done.");
// fflush(stdout);
#endif // HAVE_SDL_MIXER
} // void InitAudio(void)
void
Set_BG_Music_Volume(float NewVolume)
{
#ifndef HAVE_LIBSDL_MIXER
return;
#else
if ( !sound_on ) return;
Mix_VolumeMusic( (int) rintf( NewVolume * MIX_MAX_VOLUME ) );
#endif // HAVE_LIBSDL_MIXER
} // void Set_BG_Music_Volume(float NewVolume)
void
Set_Sound_FX_Volume(float NewVolume)
{
#ifdef HAVE_LIBSDL_MIXER
int i;
#endif
#ifndef HAVE_LIBSDL_MIXER
return;
#else
if ( !sound_on ) return;
// Set the volume IN the loaded files, if SDL is used...
// This is done here for the Files 1,2,3 and 4, since these
// are background music files.
for ( i=1 ; i<ALL_SOUNDS ; i++ )
{
Mix_VolumeChunk( Loaded_WAV_Files[i], (int) rintf(NewVolume* MIX_MAX_VOLUME) );
}
#endif // HAVE_LIBSDL_MIXER
} // void Set_BG_Music_Volume(float NewVolume)
void
CountdownSound (void)
{
Play_Sound (COUNTDOWN_SOUND);
}
void
EndCountdownSound (void)
{
Play_Sound (ENDCOUNTDOWN_SOUND);
}
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
CrySound (void)
{
Play_Sound ( CRY_SOUND );
}
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
TransferSound (void)
{
Play_Sound ( TRANSFER_SOUND );
}
/*@Function============================================================
@Desc:
This function is intended to provide a convenient way of switching
between different backround sounds in freedroid.
If no background sound was yet running, the function should start playing
the given background music.
If some background sound was already playing, the function should shut down
the old background music and start playing the new one.
Technical details:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Switch_Background_Music_To ( const char* filename_raw )
{
#ifdef HAVE_LIBSDL_MIXER
char* fpath;
static int prev_color = -1;
static bool paused = FALSE;
#endif
#ifndef HAVE_LIBSDL_MIXER
return;
#else
if ( !sound_on ) return;
if ( filename_raw == NULL )
{
Mix_PauseMusic(); // pause currently played background music
paused = TRUE;
return;
}
// rp: lets cheat when using Mingw32
#if (!defined __WIN32__ && !defined HAVE_LIBVORBIS)
if (strstr (filename_raw, ".ogg"))
{
DebugPrintf (1, "\n\nWARNING: no ogg vorbis libs were found when configuring,\n\
can't play %s!\n", filename_raw);
return;
}
#endif
// New feature: choose background music by level-color:
// if filename_raw==BYCOLOR then chose bg_music[color]
// NOTE: if new level-color is the same as before, just resume paused music!
if (!strcmp( filename_raw, BYCOLOR))
{
if (paused && (prev_color == CurLevel->color) ) // current level-song was just paused
{
Mix_ResumeMusic ();
paused = FALSE;
}
else
{
Mix_PlayMusic (MusicSongs[CurLevel->color], -1);
paused = FALSE;
prev_color = CurLevel->color;
}
}
else // not using BYCOLOR mechanism: just play specified song
{
if (Tmp_MOD_File) Mix_FreeMusic(Tmp_MOD_File);
fpath = find_file (filename_raw, SOUND_DIR, NO_THEME, WARNONLY);
if ( fpath == NULL ) {
DebugPrintf (0, "\nError loading sound-file: %s\n", filename_raw);
return;
}
Tmp_MOD_File = Mix_LoadMUS (fpath);
if ( Tmp_MOD_File == NULL )
{
DebugPrintf (0, "SDL Mixer Error: %s\n Continuing with sound disabled\n", Mix_GetError());
return;
}
Mix_PlayMusic (Tmp_MOD_File, -1);
}
Mix_VolumeMusic ( (int) rintf( GameConfig.Current_BG_Music_Volume * MIX_MAX_VOLUME ) );
#endif // HAVE_LIBSDL_MIXER
}; // void Switch_Background_Music_To(int Tune)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Play_Sound (int Tune)
{
#ifndef HAVE_LIBSDL_MIXER
return;
#else
int Newest_Sound_Channel=0;
if ( !sound_on ) return;
Newest_Sound_Channel = Mix_PlayChannel (-1, Loaded_WAV_Files[Tune], 0);
if ( Newest_Sound_Channel == -1 )
{
DebugPrintf (0, "WARNING: Could not play sound-sample: %s Error: %s\n\
This usually just means that too many samples where played at the same time\n",
SoundSampleFilenames[ Tune ] , Mix_GetError() );
} // if ( ... = -1
else
DebugPrintf( 2 , "\nSuccessfully playing file %s.", SoundSampleFilenames[ Tune ]);
#endif // HAVE_LIBSDL_MIXER
} // void Play_Sound(int Tune)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
GotHitSound (void)
{
if (!sound_on) return;
Play_Sound (GOT_HIT_SOUND);
} // void GotHitSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
GotIntoBlastSound (void)
{
if (!sound_on) return;
Play_Sound (GOT_INTO_BLAST_SOUND);
return;
} // void GotIntoBlastSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
RefreshSound (void)
{
if (!sound_on) return;
Play_Sound (REFRESH_SOUND);
return;
} // void RefreshSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
MoveLiftSound (void)
{
if (!sound_on) return;
Play_Sound (MOVE_ELEVATOR_SOUND);
} // void MoveLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
MenuItemSelectedSound (void)
{
if (!sound_on) return;
Play_Sound (MENU_ITEM_SELECTED_SOUND);
} // void MoveLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
MoveMenuPositionSound (void)
{
if (!sound_on) return;
Play_Sound (MOVE_MENU_POSITION_SOUND);
} // void MoveLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
ThouArtDefeatedSound (void)
{
if (!sound_on) return;
Play_Sound (THOU_ART_DEFEATED_SOUND);
} // void MoveLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
EnterLiftSound (void)
{
if (!sound_on) return;
Play_Sound (ENTER_ELEVATOR_SOUND);
return;
} // void EnterLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
LeaveLiftSound (void)
{
if (!sound_on) return;
Play_Sound (LEAVE_ELEVATOR_SOUND);
return;
} // void LeaveLiftSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Fire_Bullet_Sound (int BulletType)
{
if (!sound_on) return;
switch (BulletType)
{
case PULSE:
Play_Sound ( FIRE_BULLET_PULSE_SOUND );
break;
case SINGLE_PULSE:
Play_Sound ( FIRE_BULLET_SINGLE_PULSE_SOUND );
break;
case MILITARY:
Play_Sound ( FIRE_BULLET_MILITARY_SOUND );
break;
case FLASH:
Play_Sound ( FIRE_BULLET_FLASH_SOUND );
break;
case EXTERMINATOR:
Play_Sound ( FIRE_BULLET_EXTERMINATOR_SOUND );
break;
case LASER_RIFLE:
Play_Sound ( FIRE_BULLET_LASER_RIFLE_SOUND );
break;
}
} // void FireBulletSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Takeover_Set_Capsule_Sound (void)
{
if (!sound_on) return;
Play_Sound (TAKEOVER_SET_CAPSULE_SOUND);
} // void FireBulletSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Takeover_Game_Won_Sound (void)
{
if (!sound_on) return;
Play_Sound ( TAKEOVER_GAME_WON_SOUND );
} // void FireBulletSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Takeover_Game_Deadlock_Sound (void)
{
if (!sound_on) return;
Play_Sound ( TAKEOVER_GAME_DEADLOCK_SOUND );
} // void FireBulletSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
Takeover_Game_Lost_Sound (void)
{
if (!sound_on) return;
Play_Sound ( TAKEOVER_GAME_LOST_SOUND );
} // void FireBulletSound(void)
void
CollisionGotDamagedSound ( void )
{
if ( !sound_on ) return;
Play_Sound ( COLLISION_GOT_DAMAGED_SOUND );
}
void
CollisionDamagedEnemySound ( void )
{
if ( !sound_on ) return;
Play_Sound ( COLLISION_DAMAGED_ENEMY_SOUND );
}
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
BounceSound (void)
{
if (!sound_on) return;
// Play_Sound (COMBAT_BACKGROUND_MUSIC_SOUND );
Play_Sound ( COLLISIONSOUND );
} // void BounceSound(void)
/*@Function============================================================
@Desc:
@Ret:
@Int:
* $Function----------------------------------------------------------*/
void
DruidBlastSound (void)
{
if (!sound_on) return;
Play_Sound (BLASTSOUND);
} // void BounceSound(void)
void
FreeSounds ( void )
{
#ifdef HAVE_LIBSDL_MIXER
for (size_t i = 0; i < NUM_ELEM(Loaded_WAV_Files); i++) {
if ( Loaded_WAV_Files[i] != NULL ) { Mix_FreeChunk ( Loaded_WAV_Files[i] ); }
}
for ( size_t i = 0; i < NUM_ELEM(MusicSongs); i ++ ) {
if ( MusicSongs[i] != NULL ) { Mix_FreeMusic ( MusicSongs[i] ); }
}
if ( Tmp_MOD_File ) { Mix_FreeMusic ( Tmp_MOD_File ); }
Mix_CloseAudio ();
SDL_CloseAudio ();
#endif
return;
}
#undef _sound_c