forked from KS-Presto/Wolf4SDL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathid_vieasm.c
852 lines (676 loc) · 17.7 KB
/
id_vieasm.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
VODKA-INDUCED ENTERTAINMENT ADVANCED SOUND MANAGER v0.9.1
BY GERARD 'ALUMIUN' WATSON
MODIFICATION BY: André "WOLF3S" Guilherme
Provides high quality sound and music using SDL_MIXER AND SDL2_MIXER 2.6.1
Built on SDL_MIXER 1.2.5 and SDL2_MIXER 2.6.1
Supports all formats supported by SDL_mixer.
For more details, please see the SDL_mixer manual.
Changes
-------
v0.9.2 - Mix Everything In One Header File and Source File
- Port to C.
- Port to SDL2 Mixer
v0.9.1 - Fixed the reverse stereo channel bug
- Made ASM_ChangeVolume a lot faster and clipped the volume levels
- Added VERBOSE defines
v0.9 - Initial release
PLEASE ALSO NOTE THAT THIS IS STILL A BETA. IT IS LIKELY TO HAVE LOTS OF BUGS.
IF YOU FIND ANY, PLEASE TELL ME SO I CAN FIX THEM!
*/
/* Initialization stuff */
#include "id_vieasm.h"
#ifdef VIEASM
#include "asmcref.h" /* Duh. */
const char* ASM_Verstring = "v0.9.2 Beta"; /* Version string */
unsigned char sndvol, musvol; /* Volumes for sound */
int origchannels, maxchannels, lastchan; /* Channel variables */
boolean chanused[ASM_ABSMAXCHANNELS]; /* Is channel used? */
static Mix_Music* music = 0, * switchto = 0; /* Music references */
int fadetime; /* Milliseconds to fade in\out */
boolean deviceopen = false; /* Is device open? */
boolean switching = false; /* Is switching music tracks? */
boolean reversemode = false; /* Reverse stereomode */
boolean nosound;
SDMode SoundMode;
SMMode MusicMode;
SDSMode DigiMode;
static int LeftPosition;
static int RightPosition;
static boolean ambience;
sample ASM_Audiosegs[NUMSOUNDS];
int ambientsnds[NUMAMBIENTS];
boolean AdLibPresent, SoundBlasterPresent, SBProPresent, SoundPositioned;
globalsoundpos channelSoundPos[ASM_ABSMAXCHANNELS];
/* ASM_IsOpen */
/* Returns true if the device is open, false otherwise */
boolean ASM_IsOpen(void)
{
return (deviceopen) ? true : false;
}
void ASM_AdjustChannels(int reqchan)
{
if (reqchan <= maxchannels)
{
int i = (reqchan > origchannels) ? reqchan : origchannels;
Mix_AllocateChannels(i + 1);
lastchan = i;
}
}
/* ASM_GetFreeChannel */
/* Returns the first free channel, calls ASM_AdjustChannels if another is needed */
int ASM_GetFreeChannel(void)
{
int i;
for (i = 0; i <= maxchannels - 1; i++)
if (chanused[i] == false)
{
chanused[i] = true;
if (i >= lastchan)
ASM_AdjustChannels(i);
return i;
}
return -1;
}
/* ASM_ChannelDone */
/* Callback for Mix_ChannelFinished */
/* Turns off channel and adjust if necessary */
void ASM_ChannelDone(int channel)
{
chanused[channel] = false;
if (channel == (lastchan - 1) && (lastchan - 1) > origchannels - 1)
ASM_AdjustChannels(lastchan);
}
/* ASM_Open */
/* Opens audio device at given specs, clears used */
boolean ASM_Open(int frequency, int channels, int maxchan, int buffersize, unsigned char sndvolume, unsigned char musvolume, boolean reverse)
{
if (ASM_IsOpen()) /* Device is already open! */
return false;
#ifdef VERBOSE
SDL_version compile_version;
printf("----------\n"
"Vodka-Induced Entertainment Advanced Sound Manager %s\n"
"Developed for WolfSDL v1.6\n"
"And Updated to use on Wolf4SDL v2.2\n"
"By Gerard 'AlumiuN' Watson\n"
"\n"
, ASM_Verstring);
MIX_VERSION(&compile_version);
printf("Compiled with SDL_mixer version: %d.%d.%d\n",
compile_version.major, compile_version.minor, compile_version.patch);
const SDL_version* link_version = Mix_Linked_Version();
printf("Running with SDL_mixer version: %d.%d.%d\n"
"\n"
, link_version->major, link_version->minor, link_version->patch);
printf("Opened with:\n"
" frequency - %d Hz\n"
" channels - %d\n"
" max channels - %d\n"
" buffersize - %d bytes\n"
" sound volume - %d\n"
" music volume - %d\n"
" reverse stereo - %s\n"
"----------\n"
, frequency, channels, maxchan, buffersize, sndvolume, musvolume, (reverse) ? "true" : "false");
#endif
if (Mix_OpenAudio(frequency, AUDIO_S8, 2, buffersize) == -1)
{
#ifdef VERBOSE
printf("ASM_Open: %s\n", Mix_GetError());
#endif
return false;
}
if ((maxchan > ASM_ABSMAXCHANNELS) || (channels > maxchan))
{
#ifdef VERBOSE
printf("ASM_Open: Invalid maxchan value!\n");
#endif
return false;
}
Mix_AllocateChannels(channels);
memset(chanused, 0, sizeof(chanused));
lastchan = origchannels = channels;
maxchannels = maxchan;
if (reverse)
reversemode = true;
deviceopen = true;
ASM_ChangeVolume(sndvolume, musvolume);
Mix_ChannelFinished(ASM_ChannelDone);
return true;
}
/* ASM_Close */
/* Closes the audio device */
void ASM_Close(void)
{
#ifdef VERBOSE
printf("ASM_Close: Audio device closed.\n");
#endif
Mix_CloseAudio();
deviceopen = false;
}
/* ASM_HaltSound */
/* Ends all playing sounds, clears all channels, frees all chunks, etc. */
void ASM_HaltSound(void)
{
ASM_AbortIfClosed;
Mix_HaltChannel(-1);
memset(chanused, 0, sizeof(chanused));
ASM_AdjustChannels(0);
}
/* ASM_Pause */
/* Pauses all sound */
void ASM_Pause(void)
{
ASM_AbortIfClosed;
ASM_PauseSound();
ASM_PauseMusic();
}
/* ASM_Resume */
/* Resumes all sound */
void ASM_Resume(void)
{
ASM_AbortIfClosed;
ASM_ResumeSound();
ASM_ResumeMusic();
}
/* ASM_Halt */
/* Stops all sound, frees all chunks, etc. */
void ASM_Halt(void)
{
ASM_AbortIfClosed;
ASM_HaltSound();
ASM_HaltMusic();
}
/* ASM_PlayMusic */
/* Plays a music sample from a file */
boolean ASM_PlayMusic(char* musfile)
{
ASM_AbortIfClosed false;
if (Mix_PlayingMusic())
{
Mix_HaltMusic();
Mix_FreeMusic(music);
}
music = Mix_LoadMUS(musfile);
if (!music)
{
#ifdef VERBOSE
printf("ASM_PlayMusic: %s\n", Mix_GetError());
#endif
return false;
}
Mix_VolumeMusic(musvol);
if (Mix_PlayMusic(music, -1) == -1)
{
#ifdef VERBOSE
printf("ASM_PlayMusic: %s\n", Mix_GetError());
#endif
return false;
}
return true;
}
/*
** ASM_ChangeVolume
** Changes the volume of:
** Sound - iterates through each channel and sets volume accordingly
** Music - just calls Mix_VolumeMusic
*/
void ASM_ChangeVolume(unsigned char sndvolume, unsigned char musvolume)
{
ASM_AbortIfClosed;
sndvol = (sndvolume > 128) ? 128 : sndvolume;
musvol = (musvolume > 128) ? 128 : musvolume;
Mix_Volume(-1, sndvol);
Mix_VolumeMusic(musvol);
}
/* ASM_ReturnVolume */
/* Takes two byte pointers and returns the internal sound and music volumes using them */
void ASM_ReturnVolume(unsigned char* retsnd, unsigned char* retmus)
{
*retsnd = 0;
*retmus = 0;
ASM_AbortIfClosed;
*retsnd = sndvol;
*retmus = musvol;
}
/* ASM_SwitchMus */
/* Changes music to another track, either directly or via a fade-in/out */
boolean ASM_SwitchMus(char* loadmus, int fadems, boolean fade)
{
ASM_AbortIfClosed false;
switchto = Mix_LoadMUS(loadmus);
if (switchto == NULL)
{
#ifdef VERBOSE
printf("ASM_SwitchMus: %s\n", Mix_GetError());
#endif
return false;
}
if (fade != true)
{
Mix_HaltMusic();
Mix_FreeMusic(music);
music = switchto;
Mix_VolumeMusic(musvol);
if (Mix_PlayMusic(music, -1) == -1)
{
#ifdef VERBOSE
printf("ASM_SwitchMus: %s\n", Mix_GetError());
#endif
return false;
}
return true;
}
Mix_FadeOutMusic(fadems);
fadetime = fadems;
switching = true;
return true;
}
/* ASM_FadeInMus */
/* Fades in a piece of music, stopping whatever was playing before it */
boolean ASM_FadeInMus(char* loadmus, int fadems)
{
ASM_AbortIfClosed false;
if (Mix_PlayingMusic())
Mix_HaltMusic();
if (music)
Mix_FreeMusic(music);
music = Mix_LoadMUS(loadmus);
if (music == NULL)
{
#ifdef VERBOSE
printf("ASM_FadeInMus: %s\n", Mix_GetError());
#endif
return false;
}
Mix_FadeInMusic(music, -1, fadems);
return true;
}
/* ASM_FadeOutMus */
/* Fades out and stops the music currently playing */
void ASM_FadeOutMus(int fadems)
{
ASM_AbortIfClosed;
if (!Mix_PlayingMusic())
return;
Mix_FadeOutMusic(fadems);
}
/* ASM_Cache */
/* Loads a sound into memory and returns it as a sample */
sample ASM_Cache(char* sndfile, const char* name)
{
sample sound;
Mix_Chunk* chunk;
sound.chunk = NULL;
sound.name = NULL;
ASM_AbortIfClosed sound;
chunk = Mix_LoadWAV(sndfile);
if (chunk == NULL)
{
#ifdef VERBOSE
printf("ASM_Cache: %s on sound %s\n", Mix_GetError(), sndfile);
#endif
return sound;
}
sound.chunk = chunk;
sound.name = (char*)malloc((size_t)strlen(name) + 1);
strcpy(sound.name, name);
return sound;
}
/* ASM_CacheFromMem */
/* Loads a sound from a pointer into a sample, returning that sample */
/* Also frees the original data */
sample ASM_CacheFromMem(void* ptr, int size, const char* name)
{
sample sound;
Mix_Chunk* chunk;
sound.chunk = NULL;
sound.name = NULL;
ASM_AbortIfClosed sound;
chunk = Mix_LoadWAV_RW(SDL_RWFromMem(ptr, size), 1);
if (chunk == NULL)
{
#ifdef VERBOSE
printf("ASM_CacheFromMem: %s on sound %s\n", Mix_GetError(), name);
#endif
return sound;
}
sound.chunk = chunk;
sound.name = (char*)malloc((size_t)strlen(name) + 1);
strcpy(sound.name, name);
return sound;
}
/* ASM_Uncache */
/* Removes a sound from memory */
void ASM_Uncache(sample sound)
{
ASM_AbortIfClosed;
if (sound.chunk == NULL)
return;
Mix_FreeChunk(sound.chunk);
sound.chunk = NULL;
free(sound.name);
sound.name = NULL;
}
/* ASM_PlaySound */
/* Play a sound in memory loaded with ASM_Cache or Mix_LoadWAV */
/* Returns -1 on errors or channel number */
int ASM_PlaySound(sample sound, int angle, unsigned char distance, boolean ambient)
{
int chanon;
ASM_AbortIfClosed - 1;
if (sound.chunk == NULL)
{
#ifdef VERBOSE
printf("ASM_PlaySound: \"%s\" not cached!\n", sound.name);
#endif
return -1;
}
chanon = ASM_GetFreeChannel();
if (chanon == -1)
{
#ifdef VERBOSE
printf("ASM_PlaySound: No free channels!\n");
#endif
return -1;
}
Mix_Volume(chanon, sndvol);
if (Mix_PlayChannel(chanon, sound.chunk, (ambient) ? -1 : 0) == -1)
{
#ifdef VERBOSE
printf("ASM_PlaySound: %s\n", Mix_GetError());
#endif
return -1;
}
Mix_SetPosition(chanon, angle, distance);
if (reversemode)
Mix_SetReverseStereo(chanon, 1);
else
Mix_SetReverseStereo(chanon, 0);
return chanon;
}
/* ASM_StopChannel */
/* Stops sound on a given channel */
void ASM_StopChannel(int channel)
{
ASM_AbortIfClosed;
Mix_HaltChannel(channel);
}
/* ASM_SwitchStep */
/* Used each program step for switching music tracks */
void ASM_SwitchStep(void)
{
ASM_AbortIfClosed;
if (Mix_PlayingMusic())
return;
if (switching) /* If music is stopped and we want to switch */
{
Mix_FreeMusic(music);
music = switchto;
switchto = 0;
Mix_FadeInMusic(music, -1, fadetime);
switching = false;
}
}
/* ASM_ReverseStereo */
/* Sets reverse stereo mode after initialization */
void ASM_ReverseStereo(boolean reverse)
{
ASM_AbortIfClosed;
Mix_SetReverseStereo(MIX_CHANNEL_POST, (reverse) ? 1 : 0);
}
/* ASM_CurChannels */
/* Returns the number of channels allocated */
int ASM_CurChannels(void)
{
return lastchan;
}
/* SD_MusIsOn */
/* Returns true if music is on, false otherwise */
boolean SD_MusIsOn(void)
{
return (MusicMode == smm_AdLib) ? true : false;
}
boolean SD_SndIsOn(void)
{
return (SoundMode == sdm_AdLib) ? true : false;
}
void SD_ChannelDone(int channel)
{
ASM_ChannelDone(channel);
channelSoundPos[channel].valid = 0;
}
void SD_Startup(void)
{
if (nosound)
return;
if (!ASM_Open(param_samplerate, MIX_CHANNELS, ASM_ABSMAXCHANNELS / 2, param_audiobuffer,
128, 128, false))
{
printf("SD_Startup: Unable to open audio device. Trying defaults.\n");
if (!ASM_Open(22050, 16, 128, 2048, 128, 128, false))
{
printf("SD_Startup: Unable to open audio device with defaults.\n"
" Try closing any other applications that might\n"
" be using the audio driver and try again, or\n"
" start the program with the '--nosound' switch.\n");
exit(1);
}
}
Mix_ChannelFinished(SD_ChannelDone);
memset(ASM_Audiosegs, 0, sizeof(ASM_Audiosegs));
}
void SD_Shutdown(void)
{
int i;
for (i = 0; i < NUMSOUNDS; i++)
if (ASM_Audiosegs[i].chunk != NULL)
UNCACHEAUDIOCHUNK(i);
ASM_Close();
}
boolean SD_MusicPlaying(void)
{
ASM_AbortIfClosed false;
SD_AbortIfMusOff false;
return Mix_PlayingMusic();
}
/* This has changed slightly - it returns 0 on an error, 1 on a successful load */
int CA_CacheAudioChunk(int chunk)
{
char* file;
if (chunk >= NUMSOUNDS)
{
#ifdef VERBOSE
printf("CA_CacheAudioChunk: Invalid sample number %d!", chunk);
#endif
return 0;
}
file = (char*)malloc((size_t)(strlen(sounddir) + strlen(ASM_Soundnames[chunk])) + 1);
strcpy(file, sounddir);
strcat(file, ASM_Soundnames[chunk]);
ASM_Audiosegs[chunk] = ASM_Cache(file, ASM_Soundnames[chunk]);
free(file);
return 1;
}
void CA_LoadAllSounds(void)
{
int i;
for (i = 0; i < NUMSOUNDS; i++)
{
if (ASM_Audiosegs[i].chunk != NULL)
continue;
CA_CacheAudioChunk(i);
}
}
void SD_MusicOn(void)
{
ASM_AbortIfClosed;
SD_AbortIfMusOff;
ASM_ResumeMusic();
}
int SD_MusicOff(void)
{
ASM_AbortIfClosed 0;
ASM_PauseMusic();
return 0;
}
void SD_StartMusic(int chunk)
{
char* file;
ASM_AbortIfClosed;
SD_AbortIfMusOff;
if (chunk >= NUMSOUNDS)
{
#ifdef VERBOSE
printf("SD_StartMusic: Invalid chunk number %d!", chunk);
#endif
return;
}
file = (char*)malloc((size_t)(strlen(musicdir) + strlen(ASM_Musicnames[chunk])) + 1);
strcpy(file, musicdir);
strcat(file, ASM_Musicnames[chunk]);
ASM_PlayMusic(file);
}
boolean SD_SetMusicMode(SMMode mode)
{
if (mode == MusicMode)
return true;
if (mode == smm_Off)
{
MusicMode = smm_Off;
SD_MusicOff();
}
else
{
MusicMode = smm_AdLib;
SD_MusicOn();
}
return true;
}
boolean SD_SetSoundMode(SDMode mode)
{
if (mode == SoundMode)
return true;
ASM_HaltSound();
if (mode == sdm_Off)
SoundMode = sdm_Off;
else if (mode == sdm_PC)
SoundMode = sdm_PC;
else
SoundMode = sdm_AdLib;
return true;
}
void SD_StopSound(void)
{
ASM_AbortIfClosed;
ASM_HaltSound();
}
void SD_PositionSound(int leftvol, int rightvol)
{
LeftPosition = leftvol;
RightPosition = rightvol;
}
void SD_SetPosition(int channel, int leftpos, int rightpos)
{
if ((leftpos < 0) || (leftpos > 15) || (rightpos < 0) || (rightpos > 15) || ((leftpos == 15) && (rightpos == 15)))
{
#ifdef VERBOSE
printf("SD_SetPosition: Illegal position: leftpos - %d, rightpos - %d", leftpos, rightpos);
#endif
return;
}
Mix_SetPanning(channel, ((15 - leftpos) << 4) + 15, ((15 - rightpos) << 4) + 15);
}
int SD_GetAmbIndex(void)
{
int i;
for (i = 0; i < NUMAMBIENTS; i++)
if (ambientsnds[i] != 0)
return i;
return -1;
}
unsigned char SD_PlaySound(soundnames sound)
{
int lp = LeftPosition;
int rp = RightPosition;
boolean amb = ambience;
int channel;
SD_AbortIfSndOff 0;
LeftPosition = RightPosition = ambience = 0;
if (sound >= NUMSOUNDS)
{
#ifdef VERBOSE
printf("SD_PlaySound: Invalid sound number %d!\n", sound);
#endif
return 0;
}
if (ASM_Audiosegs[sound].chunk == NULL)
{
#ifdef VERBOSE
printf("SD_PlaySound: Sound %d not cached!\n", sound);
#endif
return 0;
}
channel = SD_PlayDigitized(sound, lp, rp, amb);
return channel;
}
int SD_PlayDigitized(unsigned short which, int leftpos, int rightpos, boolean ambient)
{
int channel;
SD_AbortIfSndOff 0;
channel = ASM_PlayDirect(ASM_Audiosegs[which], ambient);
if (ambient)
ambientsnds[SD_GetAmbIndex()] = channel;
ambient = false;
SD_SetPosition(channel, leftpos, rightpos);
return channel;
}
int SD_AmbientSound(void)
{
int ambindex = SD_GetAmbIndex();
if (ambindex > -1)
ambience = true;
return ambindex;
}
void SD_StopAmbient(int ambindex)
{
if (ambindex >= NUMAMBIENTS)
return;
ASM_StopChannel(ambientsnds[ambindex]);
}
void SD_SetDigiDevice(SDSMode mode)
{
boolean devicenotpresent;
int i;
if (mode == DigiMode)
return;
SD_StopDigitized();
devicenotpresent = false;
switch (mode)
{
case sds_SoundBlaster:
if (!SoundBlasterPresent)
devicenotpresent = true;
break;
case sds_PC:
if (!SoundPositioned)
devicenotpresent = true;
break;
case sds_Off:
for (i = 0; i < DigiMode; i++)
{
SD_Shutdown();
}
}
if (!devicenotpresent)
{
DigiMode = mode;
}
}
wlinline void Delay(int wolfticks)
{
if (wolfticks > 0)
SDL_Delay((unsigned int)wolfticks * 1000 / TickBase);
}
#endif /* VIEASM */