-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenAL.patch
5995 lines (5850 loc) · 208 KB
/
OpenAL.patch
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
full diff between my 'OpenAL' branch and the upstream 'development' branch of the upstream repo at the time when the modifications were made.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 21f3e3b46..ef0cfe59b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -55,9 +55,9 @@ else()
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
#add_compile_options(-fpermissive)
add_compile_options(-Wno-narrowing)
- add_compile_options(-Wall)
- add_compile_options(-Wextra)
- add_compile_options(-pedantic)
+ #add_compile_options(-Wall)
+ #add_compile_options(-Wextra)
+ #add_compile_options(-pedantic)
add_compile_options(-Wno-unknown-pragmas)
if (USE_SANITIZERS)
add_compile_options(-fsanitize=address)
diff --git a/README_OpenAL b/README_OpenAL
new file mode 100644
index 000000000..ef33e8e6a
--- /dev/null
+++ b/README_OpenAL
@@ -0,0 +1,36 @@
+
+About OpenAL
+
+ OpenAL is an API designed for rendering positional audio and environmental effects.
+ It's development started 4 years after the Magic Carpet's release, however I am certain that if the release dates would have been reversed then Bullfrog would have implemented this API in all their games.
+In MC2 the structures were ready to support left/right volume panning and there is an effort to decrease the volume for distant creatures. Fortunately they did not waste too much code trying to implement an in-house 3D positional sound system and were quite happy to simply provide arround 200 PCM sound samples that will end up being triggered during gameplay and rendered as mono sounds with just variable gain.
+
+runtime dependencies
+
+ linux needs the openal package installed. windows needs a driver called 'OpenAL x.y Windows Installer (zip)' available here: https://www.openal.org/downloads/
+
+OpenAL port
+
+ The ~200 samples can be divided into three main categories:
+ - creature-bound howls/squeals/screams/buzzing sound bytes. used by dragons, goats, bees, worms, wyverns, manticores, fireflies, spiders, devils, mana worms, moon dwellers, troglodites, cymmerians, zombies and hydras. these creature's positions are tracked frame-by-frame and if the listener is within AL_DIST_MIN_PLAY pixels distance then the associated sound chunk is sheduled to be played at random intervals. the initial sound's amplitude and the trigger interval is customized by the port, but the listener's left/right panning and the distance compensated acoustic attenuation is fully controlled by OpenAL.
+ - spells and some creature attacks. these are not tracked since they come with some distancing effects pre-applied. the sound source is placed at the listener's position at the time of when the sound sample was triggered. so if the player moves while being attacked then those samples will trail behind.
+ - environmental sounds (wind, markets, oceans, crickets, drips). these are triggered based on the area in which the player is flying. it makes little sense to set these as puctiform sound sources as for the previous categories, so they are converted to stereo before being fed to OpenAL. this means that no positional effect is applied and the player will hear these sounds with a constant volume which is controlled via config.ini:openal_environment_volume
+
+ Every PCM sound sample that can be heard is thus an OpenAL 'sound source'. the player is what the API calls a 'listener'. the listener's position and direction are updated every frame and we have seen that some sound sources get their position updated with the same frequency. once a sound is triggered by the scheduler or by the recode, a sound source is created and thus a play channel is used. it seems like most soundcards support up to about 30 concurrent sources (sometimes called 'voices'). in order to be able to hear a more diverse range of sounds, a limit of how many times one sample can be played concurrently is implemented and can be customized via config.ini:openal_same_chunk_concurrency. experimentation with this setting is welcome.
+
+ OpenAL's alternative to Creative EAX is called EFX. it can provide nice reverb effects for better game immersion. it can be enabled or disabled via the config.ini:openal_efx option.
+
+
+speech audio tracks
+
+ The Magic Carpet 2 game comes on a mixed-mode CD containing 28 tracks. The first track holds the game data in ISO9660 format, while the rest are audio tracks of a narrator voice-over that is triggered from time to time during gameplay. This port keeps them in the 'speech' directory, in raw form.
+ The problem is that GoG and most other sources provide flawed rips of these data tracks. So if you happen to hear short pop sounds while speech is enabled or if the speech is misaligned (it ends too soon), then either try to rip an original CD yourself (via the provided assets/scrips/check_install.sh script) or mix-and-match tracks from GoG and other sources. you can load these files into a sound editor like audacity (by importing them as raw data in little endian, 44100 stereo signed 16 bit pcm format) and simply identify the zones where digital noise has crept over the audio.
+
+ this is a list of audio tracks and their status on the GoG-provided image:
+
+ track#2 - no artefacts and perfectly aligned
+ track#3 - track#28 - all have noisy artefacts in the first 400 ms at the beginning of the track, so it is advised to obtain these tracks from a different rip.
+
+ enabling speech is as simple as providing a relative directory as config.ini:speech_folder.
+ it's volume level is controllerd by config.ini:openal_speech_volume (must be provided as an integer between 0 and 127). 0 completely disables the speech subsystem.
+
diff --git a/config.ini b/config.ini
index 7b6e51d91..07a908eb9 100644
--- a/config.ini
+++ b/config.ini
@@ -13,6 +13,12 @@ oggmusic = true ; using AWE32 record of MIDI music, for this function is hqsound
oggmusicFolder = music-ogg ; directory with music, you can rewrite with own music too. Path is relative to .exe
oggmusicalternative = true ; use original and alternative sound tracks
fixspeedsound = false ; set true when sounds play double speed
+speech_folder = speech ;
+openal_environment_volume = 48 ; // see README_OpenAL for a description. integer between 0 - 127
+openal_speech_volume = 100 ; // see README_OpenAL for a description. integer between 0 - 127
+openal_efx = true ; enable reverb effect
+openal_same_chunk_concurrency = 6 ; up to how many concurrent plays of a given sample should exist in any given moment
+
[graphics]
displayIndex = 0 ; Decides which display to use, if it cannot find a display at the index, it will find the first one big enough.
diff --git a/remc2/CMakeLists.txt b/remc2/CMakeLists.txt
index 7ab03f8d2..2ec6563bc 100644
--- a/remc2/CMakeLists.txt
+++ b/remc2/CMakeLists.txt
@@ -132,8 +132,11 @@ set(SOURCE_FILES__portability
"portability/port_inputs.h"
"portability/port_outputs.cpp"
"portability/port_outputs.h"
+ "portability/port_sound_lut.h"
"portability/port_sdl_sound.cpp"
"portability/port_sdl_sound.h"
+ "portability/port_openal.cpp"
+ "portability/port_openal.h"
"portability/port_sdl_vga_mouse.cpp"
"portability/port_sdl_vga_mouse.h"
"portability/port_sdl_joystick.cpp"
@@ -212,8 +215,10 @@ if (UNIX)
SDL2::Mixer
${PNG_LIBRARY}
findfirst
- ADLMIDI_static
Boost::system
+ spdlog
+ fmt
+ openal
)
target_link_directories(${PROJECT_NAME} PUBLIC
)
diff --git a/remc2/engine/Sound.cpp b/remc2/engine/Sound.cpp
index 0333d51f1..949a4067a 100644
--- a/remc2/engine/Sound.cpp
+++ b/remc2/engine/Sound.cpp
@@ -1069,6 +1069,8 @@ void AilRestoreUSE16ISR_91E90(int32_t isr)
//----- (00099C10) --------------------------------------------------------
void EndSounds_99C10()//27ac10
{
+ uint16_t i;
+
if (soundLoaded_E379A)
{
EndSample_8D8F0();
@@ -1079,8 +1081,17 @@ void EndSounds_99C10()//27ac10
FreeMem_83E80((uint8_t*)soundIndex_E37A0);
numOfLoadedSounds_E37A4 = 0;
}
- if (soundBuffer1_E37A8)
+ if (soundBuffer1_E37A8) {
+ for (i = 0; i < hDigSoundEffectsDriver_180B48->n_samples_24; i++) {
+ if (hDigSoundEffectsDriver_180B48->samples_23[i].start_44mhz) {
+ free(hDigSoundEffectsDriver_180B48->samples_23[i].start_44mhz);
+ }
+ if (hDigSoundEffectsDriver_180B48->samples_23[i].wavbuff) {
+ free(hDigSoundEffectsDriver_180B48->samples_23[i].wavbuff);
+ }
+ }
FreeMem_83E80(soundBuffer1_E37A8);
+ }
soundAble_E3798 = false;
soundActive_E3799 = false;
soundLoaded_E379A = false;
@@ -1815,6 +1826,10 @@ bool LoadSound_84300(uint8_t soundIndex)//265300
DataFileIO::Close(file);
return true;
}
+#ifdef SOUND_OPENAL
+ alsound_clear_cache();
+ alsound_set_env(soundIndex, AL_SET_BANK);
+#endif
actualSound_E37AD = soundIndex;
DataFileIO::Close(file);
}
@@ -1887,19 +1902,17 @@ bool ReadAndDecompressSound(FILE* file, uint8_t soundIndex2)//2654f0
numOfLoadedSounds_E37A4 = (soundBank2[soundIndex2].sizeBytes_8) / sizeof(shadow_sub2type_E37A0_sound_buffer2);
DataFileIO::Seek(file, soundBank2[soundIndex2].dword_4, 0);
DataFileIO::Read(file, soundBuffer1_E37A8, 8);
- if (soundBuffer1_E37A8[0] != 'R' || soundBuffer1_E37A8[1] != 'N' || soundBuffer1_E37A8[2] != 'C')
- {
+ if (soundBuffer1_E37A8[0] != 'R' || soundBuffer1_E37A8[1] != 'N' || soundBuffer1_E37A8[2] != 'C') {
DataFileIO::Read(file, (soundBuffer1_E37A8 + 8), soundBank2[soundIndex2].dword_12 - 8);
- }
- else
- {
+ } else {
DataFileIO::Read(file, (soundBuffer1_E37A8 + 8), soundBuffer1_E37A8[7] + ((soundBuffer1_E37A8[6] + ((soundBuffer1_E37A8[5] + (soundBuffer1_E37A8[4] << 8)) << 8)) << 8) - 8);
DataFileIO::Decompress(soundBuffer1_E37A8, soundBuffer1_E37A8);
}
DataFileIO::Seek(file, soundBank2[soundIndex2].dword_0, 0);
DataFileIO::Read(file, (uint8_t*)shadow_str_E37A0_sound_buffer2, 8);
- if (shadow_str_E37A0_sound_buffer2->byte_0 != 'R' || shadow_str_E37A0_sound_buffer2->byte_1 != 'N' || shadow_str_E37A0_sound_buffer2->byte_2 != 'C')//R N C
- {
+ if (shadow_str_E37A0_sound_buffer2->byte_0 != 'R' || shadow_str_E37A0_sound_buffer2->byte_1 != 'N' || shadow_str_E37A0_sound_buffer2->byte_2 != 'C') {
+ // go back to the start of the bank, since this block has no header
+ DataFileIO::Seek(file, soundBank2[soundIndex2].dword_0, 0);
DataFileIO::Read(file, (uint8_t*)&shadow_str_E37A0_sound_buffer2->str_8, soundBank2[soundIndex2].sizeBytes_8 - 8);
}
else
@@ -1915,17 +1928,10 @@ bool ReadAndDecompressSound(FILE* file, uint8_t soundIndex2)//2654f0
soundIndex_E37A0->byte_5 = shadow_str_E37A0_sound_buffer2->byte_5;
soundIndex_E37A0->byte_6 = shadow_str_E37A0_sound_buffer2->byte_6;
soundIndex_E37A0->byte_7 = shadow_str_E37A0_sound_buffer2->byte_7;
- for(int i = 0; i < 10; i++)
- soundIndex_E37A0->str_8.stub[i] = shadow_str_E37A0_sound_buffer2->str_8.stub[i];
- for (int i = 0; i < 96; i++)
+ for (int i = 0; i < numOfLoadedSounds_E37A4; i++)
{
soundIndex_E37A0->str_8.wavs_10[i].wavData_0 = (uint8_t*)shadow_str_E37A0_sound_buffer2->str_8.wavs_10[i].wavData_0;
- for (int j = 0; j < 4; j++)
- {
- soundIndex_E37A0->str_8.wavs_10[i].stub_4[j] = shadow_str_E37A0_sound_buffer2->str_8.wavs_10[i].stub_4[j];
- }
-
soundIndex_E37A0->str_8.wavs_10[i].wavSize_8 = shadow_str_E37A0_sound_buffer2->str_8.wavs_10[i].wavSize_8;
soundIndex_E37A0->str_8.wavs_10[i].word_12 = shadow_str_E37A0_sound_buffer2->str_8.wavs_10[i].word_12;
for (int j = 0; j < 18; j++)
@@ -3136,10 +3142,42 @@ void InitSample_A38E0(HSAMPLE S)//2848e0
S->sam_var[530] = 0;
S->sam_var[531] = 0;
S->sam_var532_EOS_ptr = 0;
+ S->id = -1;
InitSampleVolume_A2110(S);
}
}
+/// \brief prepare the chunk buffer for the openal subsystem
+/// for chunks that cannot be localized (environment samples) a stereo sample is generated
+/// \param S sample to operate on
+void init_openal_sample(HSAMPLE S)
+{
+ uint8_t actval;
+ uint16_t format;
+
+ format = alsound_get_chunk_flags(S->id);
+
+ if (S->wavbuff != nullptr) {
+ free(S->wavbuff);
+ S->wavbuff = nullptr;
+ }
+
+ if (format & AL_FORMAT_STEREO8_22050) {
+ S->wavbuff = malloc(S->len_4_5[0] * 2);
+ if (!S->wavbuff) {
+ return;
+ }
+
+ for (int i = 0; i < S->len_4_5[0]; i++) {
+ actval = ((uint8_t *) S->start_2_3[0])[i];
+ (*(int8_t *) & ((uint8_t *) S->wavbuff)[0 + i * 2]) = actval;
+ (*(int8_t *) & ((uint8_t *) S->wavbuff)[1 + i * 2]) = actval;
+ }
+ }
+
+ //Logger->info("init_openal_sample id {} fmt {} sz {}", S->id, format, S->len_4_5[0]);
+}
+
void InitHqsound(HSAMPLE S) {
//test mark
bool same_mark = true;
@@ -3162,8 +3200,10 @@ void InitHqsound(HSAMPLE S) {
S->mark44mark[i] = ((uint8_t*)S->start_2_3[0])[i];
}
- if (S->start_44mhz != nullptr)
+ if (S->start_44mhz != nullptr) {
free(S->start_44mhz);
+ S->start_44mhz = NULL;
+ }
if (fixspeedsound)
S->start_44mhz = malloc(S->len_4_5[0] * 2 * 2 * 2 * 2);
else
@@ -3208,8 +3248,13 @@ void SetSampleAddress_A3A30(HSAMPLE S, uint8_t* start, uint32_t len)
S->start_2_3[1] = 0;
S->len_4_5[0] = len;
S->len_4_5[1] = 0;
- if (hqsound)
+#ifdef SOUND_OPENAL
+ init_openal_sample(S);
+#else
+ if (hqsound) {
InitHqsound(S);
+ }
+#endif
}
}
@@ -5085,7 +5130,7 @@ void sub_8F100_sound_proc19(uint32_t flags, __int16 index, int volume, int volum
if (!soundAble_E3798
|| !soundActive_E3799
|| index > (signed int)indexLoadedSound_180B50
- || !_stricmp((const char*)&soundIndex_E37A0->str_8.wavs_10[index -1].filename_14, "null.wav"))
+ || !_stricmp((const char*)&soundIndex_E37A0->str_8.wavs_10[index].filename_14, "null.wav"))
{
return;
}
@@ -5171,6 +5216,7 @@ void sub_8F100_sound_proc19(uint32_t flags, __int16 index, int volume, int volum
for (int i = 0; i < 100; i++)
Logger->trace("{}", debug_sound_buff[i]);
}
+ (*soundBuffer1)->id = index;
AilSetSampleFile_938C0(*soundBuffer1, soundIndex_E37A0->str_8.wavs_10[index].wavData_0, 1);
}
AilSetSampleVolume_93E30(*soundBuffer1, volume);
@@ -5518,4 +5564,24 @@ void WriteWaveToFile(wav_t* wav, const char* name)
fclose(wavFile);
}
-}
\ No newline at end of file
+}
+
+uint8_t get_sample_ptr(const uint8_t index, uint8_t **data, int32_t *len)
+{
+ *len = 0;
+ *data = NULL;
+ int i;
+ uint8_t *pWaveData = soundIndex_E37A0->str_8.wavs_10[index].wavData_0;
+
+ i = 12;
+ for (int j = _strnicmp((const char *)&pWaveData[12], "data", 4); j; j = _strnicmp((const char *)&pWaveData[i], "data", 4)) {
+ i += (*(int32_t *) & pWaveData[i + 4] & 1) + *(int32_t *) & pWaveData[i + 4] + 8;
+ }
+
+ *data = &pWaveData[i + 8];
+ *len = *(int32_t *) & pWaveData[i + 4];
+
+ //Logger->info("get_sample_ptr id {} sz {}", index, *len);
+ return EXIT_SUCCESS;
+}
+
diff --git a/remc2/engine/Sound.h b/remc2/engine/Sound.h
index 33d439477..14669b0b9 100644
--- a/remc2/engine/Sound.h
+++ b/remc2/engine/Sound.h
@@ -257,5 +257,6 @@ void SetSoundFreq_9A230(int a1);
void WriteWaveToFile(wav_t* wav, const char* name);
void AIL_fix();
const char* mygetenv(const char* a1);
+uint8_t get_sample_ptr(const uint8_t index, uint8_t ** data, int32_t * len);
-#endif //MAIN_SOUND
\ No newline at end of file
+#endif //MAIN_SOUND
diff --git a/remc2/engine/ail_sound.h b/remc2/engine/ail_sound.h
index dadd4a766..0638919ce 100644
--- a/remc2/engine/ail_sound.h
+++ b/remc2/engine/ail_sound.h
@@ -119,6 +119,8 @@ int32_t sam_var[1000];
void* start_44mhz;//8
uint8_t mark44mark[sample_mark];
//Mix_Chunk chunk;
+ uint8_t id; // chunk identifier used in openal functions
+ void *wavbuff; // stereo wav chunk data used in openal functions
}
SAMPLE;
typedef MSS_STRUCT _SAMPLE * HSAMPLE; // Handle to sample
diff --git a/remc2/engine/engine_support.h b/remc2/engine/engine_support.h
index 0ecb437b0..2098dcd45 100644
--- a/remc2/engine/engine_support.h
+++ b/remc2/engine/engine_support.h
@@ -22,6 +22,7 @@
#include "../portability/port_time.h"
#include "../portability/port_sdl_vga_mouse.h"
#include "../portability/port_sdl_joystick.h"
+#include "../portability/port_openal.h"
#include "../portability/port_outputs.h"
#include "../portability/port_show_perifery.h"
diff --git a/remc2/engine/global_types.h b/remc2/engine/global_types.h
index 4403c682b..b86eaba51 100644
--- a/remc2/engine/global_types.h
+++ b/remc2/engine/global_types.h
@@ -362,6 +362,10 @@ typedef struct _str_0x6E8E {//lenght a8//THING
type_str_160* dword_0xA0_160x;//160 //special settings
//uint16_t word_0xA2_162;//162
type_str_164* dword_0xA4_164x;//100 // adress of xx
+ int16_t play_ch; ///< play channel currently used to play this entity's sound sample
+ uint64_t play_mark; ///< when this entity is supposed to play it's sound sample (in ms)
+ uint32_t dist; ///< distance between this entity and the listener
+ uint64_t dist_mark; ///< when should the distance be recalculated (in ms)
}
type_event_0x6E8E;
diff --git a/remc2/engine/read_config.cpp b/remc2/engine/read_config.cpp
index 74f8171f8..7712d7d1e 100644
--- a/remc2/engine/read_config.cpp
+++ b/remc2/engine/read_config.cpp
@@ -1,4 +1,5 @@
#include "read_config.h"
+#include "../sub_main.h"
int config_skip_screen;
int texturepixels = 32;
@@ -144,6 +145,18 @@ bool readini() {
std::string readstr = reader.GetString("sound", "oggmusicFolder", "");
strcpy(oggmusicFolder, (char*)readstr.c_str());
+ std::string speech_folder_str = reader.GetString("sound", "speech_folder", "");
+ strcpy(speech_folder, (char *)speech_folder_str.c_str());
+ oac.speech_volume = reader.GetInteger("sound", "openal_speech_volume", 102);
+
+ if ((oac.speech_volume == 0) || (speech_folder_str.length() < 3)) {
+ disable_speech();
+ }
+
+ oac.env_volume = reader.GetInteger("sound", "openal_environment_volume", 52);
+ oac.efx_enabled = reader.GetBoolean("sound", "openal_efx", false);
+ oac.same_chunk_concurrency = reader.GetInteger("sound", "openal_same_chunk_concurrency", 5);
+
std::string readstr3 = reader.GetString("graphics", "bigGraphicsFolder", "");
strcpy(bigGraphicsFolder, (char*)readstr3.c_str());
@@ -217,100 +230,101 @@ bool readini() {
fmvFps = reader.GetInteger("game", "fmvFps", 20);
loggingLevel = reader.GetString("game", "loggingLevel", "Info");
- gpc.axis_yaw = reader.GetInteger("gamepad", "axis_yaw", GAMEPAD_ITEM_DISABLED);
- gpc.axis_pitch = reader.GetInteger("gamepad", "axis_pitch", GAMEPAD_ITEM_DISABLED);
- gpc.axis_long = reader.GetInteger("gamepad", "axis_long", GAMEPAD_ITEM_DISABLED);
- gpc.axis_trans = reader.GetInteger("gamepad", "axis_trans", GAMEPAD_ITEM_DISABLED);
- gpc.axis_nav_ns = reader.GetInteger("gamepad", "axis_nav_ns", GAMEPAD_ITEM_DISABLED);
- gpc.axis_nav_ew = reader.GetInteger("gamepad", "axis_nav_ew", GAMEPAD_ITEM_DISABLED);
- gpc.axis_fire_R = reader.GetInteger("gamepad", "axis_fire_R", GAMEPAD_ITEM_DISABLED);
- gpc.axis_fire_L = reader.GetInteger("gamepad", "axis_fire_L", GAMEPAD_ITEM_DISABLED);
-
- gp_temp = reader.GetBoolean("gamepad", "axis_yaw_inv", 0);
- if (gpc.axis_yaw) {
- gpc.axis_yaw -= 1; // go back to SDL axis notation
- gpc.axis_yaw_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "axis_pitch_inv", 0);
- if (gpc.axis_pitch) {
- gpc.axis_pitch -= 1; // go back to SDL axis notation
- gpc.axis_pitch_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "axis_long_inv", 0);
- if (gpc.axis_long) {
- gpc.axis_long -= 1; // go back to SDL axis notation
- gpc.axis_long_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "axis_trans_inv", 0);
- if (gpc.axis_trans) {
- gpc.axis_trans -= 1; // go back to SDL axis notation
- gpc.axis_trans_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "axis_nav_ns_inv", 0);
- if (gpc.axis_nav_ns) {
- gpc.axis_nav_ns -= 1; // go back to SDL axis notation
- gpc.axis_nav_ns_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "axis_nav_ew_inv", 0);
- if (gpc.axis_nav_ew) {
- gpc.axis_nav_ew -= 1; // go back to SDL axis notation
- gpc.axis_nav_ew_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- if (gpc.axis_fire_R) {
- gpc.axis_fire_R -= 1; // go back to SDL axis notation
- gpc.axis_fire_R_conf = GAMEPAD_ITEM_ENABLED;
- }
-
- if (gpc.axis_fire_L) {
- gpc.axis_fire_L -= 1; // go back to SDL axis notation
- gpc.axis_fire_L_conf = GAMEPAD_ITEM_ENABLED;
- }
-
- gpc.button_fire_L = reader.GetInteger("gamepad", "button_fire_L", 0);
- gpc.button_fire_R = reader.GetInteger("gamepad", "button_fire_R", 0);
- gpc.controller_id = reader.GetInteger("gamepad", "controller_id", 0);
- gpc.button_spell = reader.GetInteger("gamepad", "button_spell", 0);
- gpc.button_minimap = reader.GetInteger("gamepad", "button_minimap", 0);
- gpc.button_fwd = reader.GetInteger("gamepad", "button_fwd", 0);
- gpc.button_back = reader.GetInteger("gamepad", "button_back", 0);
- gpc.button_pause_menu = reader.GetInteger("gamepad", "button_pause_menu", 0);
- gpc.button_esc = reader.GetInteger("gamepad", "button_esc", 0);
- gpc.button_menu_select = reader.GetInteger("gamepad", "button_menu_select", 0);
-
- gpc.axis_yaw_sensitivity = ReadZones(reader.GetString("gamepad", "axis_yaw_sensitivity", ""));
- gpc.axis_yaw_dead_zone = reader.GetInteger("gamepad", "axis_yaw_dead_zone", 3000);
- gpc.axis_pitch_sensitivity = ReadZones(reader.GetString("gamepad", "axis_pitch_sensitivity", ""));
- gpc.axis_pitch_dead_zone = reader.GetInteger("gamepad", "axis_pitch_dead_zone", 3000);
- gpc.axis_long_dead_zone = reader.GetInteger("gamepad", "axis_long_dead_zone", 3000);
- gpc.axis_trans_dead_zone = reader.GetInteger("gamepad", "axis_trans_dead_zone", 3000);
- gpc.axis_long_nav_dead_zone = reader.GetInteger("gamepad", "axis_long_nav_dead_zone", 6000);
- gpc.axis_trans_nav_dead_zone = reader.GetInteger("gamepad", "axis_trans_nav_dead_zone", 6000);
-
- gpc.trigger_dead_zone = reader.GetInteger("gamepad", "trigger_dead_zone", 3000);
-
- gpc.hat_nav = reader.GetInteger("gamepad", "hat_nav", GAMEPAD_ITEM_DISABLED);
- gpc.hat_mov = reader.GetInteger("gamepad", "hat_mov", GAMEPAD_ITEM_DISABLED);
-
- gp_temp = reader.GetBoolean("gamepad", "hat_nav_inv", 0);
- if (gpc.hat_nav) {
- gpc.hat_nav -= 1; // go back to SDL axis notation
- gpc.hat_nav_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gp_temp = reader.GetBoolean("gamepad", "hat_mov_inv", 0);
- if (gpc.hat_mov) {
- gpc.hat_mov -= 1; // go back to SDL axis notation
- gpc.hat_mov_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
- }
-
- gpc.haptic_enabled = reader.GetBoolean("gamepad", "haptic_enabled", false);
- gpc.haptic_gain_max = reader.GetInteger("gamepad", "haptic_max_gain", 75);
- return true;
+ gpc.axis_yaw = reader.GetInteger("gamepad", "axis_yaw", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_pitch = reader.GetInteger("gamepad", "axis_pitch", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_long = reader.GetInteger("gamepad", "axis_long", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_trans = reader.GetInteger("gamepad", "axis_trans", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_nav_ns = reader.GetInteger("gamepad", "axis_nav_ns", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_nav_ew = reader.GetInteger("gamepad", "axis_nav_ew", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_fire_R = reader.GetInteger("gamepad", "axis_fire_R", GAMEPAD_ITEM_DISABLED);
+ gpc.axis_fire_L = reader.GetInteger("gamepad", "axis_fire_L", GAMEPAD_ITEM_DISABLED);
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_yaw_inv", 0);
+ if (gpc.axis_yaw) {
+ gpc.axis_yaw -= 1; // go back to SDL axis notation
+ gpc.axis_yaw_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_pitch_inv", 0);
+ if (gpc.axis_pitch) {
+ gpc.axis_pitch -= 1; // go back to SDL axis notation
+ gpc.axis_pitch_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_long_inv", 0);
+ if (gpc.axis_long) {
+ gpc.axis_long -= 1; // go back to SDL axis notation
+ gpc.axis_long_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_trans_inv", 0);
+ if (gpc.axis_trans) {
+ gpc.axis_trans -= 1; // go back to SDL axis notation
+ gpc.axis_trans_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_nav_ns_inv", 0);
+ if (gpc.axis_nav_ns) {
+ gpc.axis_nav_ns -= 1; // go back to SDL axis notation
+ gpc.axis_nav_ns_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "axis_nav_ew_inv", 0);
+ if (gpc.axis_nav_ew) {
+ gpc.axis_nav_ew -= 1; // go back to SDL axis notation
+ gpc.axis_nav_ew_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ if (gpc.axis_fire_R) {
+ gpc.axis_fire_R -= 1; // go back to SDL axis notation
+ gpc.axis_fire_R_conf = GAMEPAD_ITEM_ENABLED;
+ }
+
+ if (gpc.axis_fire_L) {
+ gpc.axis_fire_L -= 1; // go back to SDL axis notation
+ gpc.axis_fire_L_conf = GAMEPAD_ITEM_ENABLED;
+ }
+
+ gpc.controller_id = reader.GetInteger("gamepad", "controller_id", 0);
+ gpc.button_fire_L = reader.GetInteger("gamepad", "button_fire_L", 0);
+ gpc.button_fire_R = reader.GetInteger("gamepad", "button_fire_R", 0);
+ gpc.button_spell = reader.GetInteger("gamepad", "button_spell", 0);
+ gpc.button_minimap = reader.GetInteger("gamepad", "button_minimap", 0);
+ gpc.button_fwd = reader.GetInteger("gamepad", "button_fwd", 0);
+ gpc.button_back = reader.GetInteger("gamepad", "button_back", 0);
+ gpc.button_pause_menu = reader.GetInteger("gamepad", "button_pause_menu", 0);
+ gpc.button_esc = reader.GetInteger("gamepad", "button_esc", 0);
+ gpc.button_menu_select = reader.GetInteger("gamepad", "button_menu_select", 0);
+ gpc.inflection_x = reader.GetInteger("gamepad", "inflection_x", 0);
+ gpc.inflection_y = reader.GetInteger("gamepad", "inflection_y", 0);
+
+ gpc.axis_yaw_dead_zone = reader.GetInteger("gamepad", "axis_yaw_dead_zone", 3000);
+ gpc.axis_pitch_dead_zone = reader.GetInteger("gamepad", "axis_pitch_dead_zone", 3000);
+ gpc.axis_long_nav_dead_zone = reader.GetInteger("gamepad", "axis_long_nav_dead_zone", 6000);
+ gpc.axis_trans_nav_dead_zone = reader.GetInteger("gamepad", "axis_trans_nav_dead_zone", 6000);
+ gpc.axis_long_dead_zone = reader.GetInteger("gamepad", "axis_long_dead_zone", 12000);
+ gpc.axis_trans_dead_zone = reader.GetInteger("gamepad", "axis_trans_dead_zone", 12000);
+
+ gpc.trigger_dead_zone = reader.GetInteger("gamepad", "trigger_dead_zone", 3000);
+
+ gpc.hat_nav = reader.GetInteger("gamepad", "hat_nav", GAMEPAD_ITEM_DISABLED);
+ gpc.hat_mov = reader.GetInteger("gamepad", "hat_mov", GAMEPAD_ITEM_DISABLED);
+
+ gp_temp = reader.GetBoolean("gamepad", "hat_nav_inv", 0);
+ if (gpc.hat_nav) {
+ gpc.hat_nav -= 1; // go back to SDL axis notation
+ gpc.hat_nav_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gp_temp = reader.GetBoolean("gamepad", "hat_mov_inv", 0);
+ if (gpc.hat_mov) {
+ gpc.hat_mov -= 1; // go back to SDL axis notation
+ gpc.hat_mov_conf = GAMEPAD_ITEM_ENABLED | (gp_temp ? GAMEPAD_AXIS_INVERTED : 0);
+ }
+
+ gpc.haptic_enabled = reader.GetBoolean("gamepad", "haptic_enabled", false);
+ gpc.haptic_gain_max = reader.GetInteger("gamepad", "haptic_max_gain", 75);
+
+ return true;
};
diff --git a/remc2/engine/read_config.h b/remc2/engine/read_config.h
index 04683436d..475b49688 100644
--- a/remc2/engine/read_config.h
+++ b/remc2/engine/read_config.h
@@ -61,50 +61,59 @@ extern bool assignToSpecificCores;
#define GAMEPAD_AXIS_INVERTED 0x2
struct gamepad_config {
- uint16_t axis_yaw;
- uint16_t axis_pitch;
- uint16_t axis_long;
- uint16_t axis_trans;
- uint16_t axis_nav_ns;
- uint16_t axis_nav_ew;
- uint16_t axis_fire_R;
- uint16_t axis_fire_L;
- uint8_t axis_yaw_conf;
- uint8_t axis_pitch_conf;
- uint8_t axis_long_conf;
- uint8_t axis_trans_conf;
- uint8_t axis_nav_ns_conf;
- uint8_t axis_nav_ew_conf;
- uint8_t axis_fire_R_conf;
- uint8_t axis_fire_L_conf;
- std::vector<Maths::Zone> axis_yaw_sensitivity;
- uint16_t axis_yaw_dead_zone;
- std::vector<Maths::Zone> axis_pitch_sensitivity;
- uint16_t axis_pitch_dead_zone;
- uint16_t axis_long_dead_zone;
- uint16_t axis_trans_dead_zone;
- uint16_t axis_long_nav_dead_zone;
- uint16_t axis_trans_nav_dead_zone;
- uint16_t trigger_dead_zone;
- uint16_t hat_nav;
- uint16_t hat_mov;
- uint8_t hat_mov_conf;
- uint8_t hat_nav_conf;
- uint16_t controller_id;
- uint16_t button_spell;
- uint16_t button_minimap;
- uint16_t button_fire_L;
- uint16_t button_fire_R;
- uint16_t button_fwd;
- uint16_t button_back;
- uint16_t button_pause_menu;
- uint16_t button_esc;
- uint16_t button_menu_select;
- bool haptic_enabled;
- uint16_t haptic_gain_max;
+ uint16_t controller_id;
+ uint16_t axis_yaw;
+ uint16_t axis_pitch;
+ uint16_t axis_long;
+ uint16_t axis_trans;
+ uint16_t axis_nav_ns;
+ uint16_t axis_nav_ew;
+ uint16_t axis_fire_R;
+ uint16_t axis_fire_L;
+ uint8_t axis_yaw_conf;
+ uint8_t axis_pitch_conf;
+ uint8_t axis_long_conf;
+ uint8_t axis_trans_conf;
+ uint8_t axis_nav_ns_conf;
+ uint8_t axis_nav_ew_conf;
+ uint8_t axis_fire_R_conf;
+ uint8_t axis_fire_L_conf;
+ uint16_t axis_yaw_dead_zone;
+ uint16_t axis_pitch_dead_zone;
+ uint16_t axis_long_dead_zone;
+ uint16_t axis_trans_dead_zone;
+ uint16_t axis_long_nav_dead_zone;
+ uint16_t axis_trans_nav_dead_zone;
+ uint16_t trigger_dead_zone;
+ uint16_t hat_nav;
+ uint16_t hat_mov;
+ uint8_t hat_mov_conf;
+ uint8_t hat_nav_conf;
+ uint16_t button_spell;
+ uint16_t button_minimap;
+ uint16_t button_fire_L;
+ uint16_t button_fire_R;
+ uint16_t button_fwd;
+ uint16_t button_back;
+ uint16_t button_pause_menu;
+ uint16_t button_esc;
+ uint16_t button_menu_select;
+ bool haptic_enabled;
+ uint16_t haptic_gain_max;
+ uint8_t inflection_x; ///< middle-band wideness (percentage 70-99)
+ uint8_t inflection_y; ///< f(x) at the edges of the middle-band (percentage 10-90)
};
typedef struct gamepad_config gamepad_config_t;
extern gamepad_config_t gpc;
-#endif //READ_CONFIG
+struct openal_config {
+ bool efx_enabled;
+ uint16_t speech_volume;
+ uint16_t env_volume;
+ uint16_t same_chunk_concurrency;
+};
+typedef struct openal_config openal_config_t;
+extern openal_config_t oac;
+
+#endif //READ_CONFIG
diff --git a/remc2/portability/port_openal.cpp b/remc2/portability/port_openal.cpp
new file mode 100644
index 000000000..dd43c27df
--- /dev/null
+++ b/remc2/portability/port_openal.cpp
@@ -0,0 +1,964 @@
+
+// OpenAL 3D audio implementation
+//
+// includes reverb effect for added ambiance and the makings of a few positional elements
+//
+// resources:
+// OpenAL official doc https://www.openal.org/documentation/
+// OpenAL-soft doc https://github.com/kcat/openal-soft/wiki/Programmer%27s-Guide
+// nice guide https://indiegamedev.net/2020/04/12/the-complete-guide-to-openal-with-c-part-3-positioning-sounds/
+// EFX code based on https://github.com/kcat/openal-soft/blob/master/examples/alreverb.c
+//
+// Authors:
+// 2023 - Petre Rodan (complete rewrite)
+//
+
+#include <stdlib.h>
+#include "AL/al.h"
+#include "AL/alc.h"
+#include "AL/alext.h"
+#include "AL/efx.h"
+#include "AL/efx-presets.h"
+#include "../engine/ail_sound.h"
+#include "../engine/global_types.h"
+#include "../engine/read_config.h"
+#include "../engine/Sound.h"
+#include "port_sound_lut.h"
+#include "port_sdl_sound.h"
+#include "port_openal.h"
+
+///< al_chunk_cache_t flags
+#define OPENAL_FLG_LOADED 0x1 ///< if chunk was properly loaded via alBufferData()
+
+#define AL_DIST_REFRESH_INTERVAL 200 ///< after how many ms shoud the distance between creatures and the listener should be refreshed
+#define AL_DIST_MIN_PLAY 10000 ///< minimal distance to the player needed for creature to play it's sample
+
+// Effect object functions
+static LPALGENEFFECTS alGenEffects;
+static LPALDELETEEFFECTS alDeleteEffects;
+static LPALISEFFECT alIsEffect;
+static LPALEFFECTI alEffecti;
+static LPALEFFECTIV alEffectiv;
+static LPALEFFECTF alEffectf;
+static LPALEFFECTFV alEffectfv;
+static LPALGETEFFECTI alGetEffecti;
+static LPALGETEFFECTIV alGetEffectiv;
+static LPALGETEFFECTF alGetEffectf;
+static LPALGETEFFECTFV alGetEffectfv;
+
+// Auxiliary Effect Slot object functions
+static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
+static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
+static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
+static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
+static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
+static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
+static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
+static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
+static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
+static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
+static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
+
+struct al_chunk {
+ int16_t id; ///< chunk identifier
+ ALint state; ///< 0, AL_PLAYING or something in between
+ ALuint alSource; ///< openal source identifier
+ ALsizei size; ///< chunk size
+ event_t *entity; ///< what entity has created the sound source
+};
+typedef struct al_chunk al_chunk_t; ///< element of the currently playing chunks array
+
+struct al_chunk_cache {
+ int16_t id; ///< chunk identifier
+ uint16_t flags; ///< 0 or OPENAL_FLG_LOADED
+ ALuint bufferName; ///< openal buffer identifier
+ ALsizei size; ///< chunk size
+};
+typedef struct al_chunk_cache al_chunk_cache_t; ///< element of the cached chunks array
+
+struct al_env {
+ uint8_t initialized; ///< '1' if the OpenAL-soft library was properly initialized
+ uint8_t efx_initialized; ///< '1' if the ALC_EXT_EFX extension is usable
+ uint8_t scheduling_enabled; ///< state of the chunk scheduling
+ int8_t bank; ///< current sound bank
+ int8_t reverb_type; ///< should match the current MapType
+ uint32_t frame_cnt; ///< frame counter
+ axis_3d listener_c; ///< the listener's coordinates in game space (x, y, z)
+ axis_4d listener_o; ///< the listener's orientation values (yaw, pitch, roll)
+};
+typedef struct al_env al_env_t; ///< random collection of global variables
+
+struct al_next_vol { ///< sometimes the volume of a chunk is received before the chunk itself, so keep that info here
+ int16_t chunk_id; ///< chunk identifier
+ float gain; ///< volume (0-127) converted into gain (0-1.0)
+};
+typedef struct al_next_vol al_next_vol_t;
+
+ALCcontext *context;
+ALuint al_slot; ///< effect slot
+ALuint al_effect;
+al_chunk_t alc[OPENAL_C_SZ] = { }; ///< currently playing chunks array
+al_chunk_cache_t alcc[OPENAL_CC_SZ] = { }; ///< cached chunks array
+int8_t al_con[OPENAL_CC_SZ] = { }; ///< concurrency array (how many times is a particular chunk played currently)
+al_env_t ale = { }; ///< the random collection of global variables
+
+openal_config_t oac; ///< subsystem configuration read from config.ini
+
+// alsound_set_sample_volume for a chunk precedes alsound_play
+// for that particular chunk. so store it's volume in alnv:
+al_next_vol_t alnv = { };
+
+const char *alsound_get_error_str(ALCenum error);
+ALCenum alsound_error_check(const char *msg);
+static ALuint alsound_load_effect(const EFXEAXREVERBPROPERTIES * reverb);
+
+/// \brief find a chunk_id in the currently playing chunks array
+/// \param chunk_id identifier
+/// \return location in the array or -1 if chunk not present
+int16_t alsound_find_alc_sample(const int32_t id)
+{
+ int16_t i;
+ //Logger->info("alsound_sample_status looking for {}", chunk_id);
+ if (id > 128) {
+ for (i = OPENAL_C_SZ; i > 0; i--) {
+ if ((alc[i - 1].state == AL_PLAYING) && (alc[i - 1].size == id)) {
+ return i - 1;
+ }
+ }
+ } else {
+ for (i = OPENAL_C_SZ; i > 0; i--) {
+ if ((alc[i - 1].state == AL_PLAYING) && (alc[i - 1].id == id)) {
+ return i - 1;
+ }
+ }
+ }
+ return -1;
+}
+
+/// \brief establish the presence of a chunk_id in the currently playing chunks array
+/// \param chunk_id identifier
+/// \return 1 if sample is playing and 0 otherwise
+uint8_t alsound_sample_status(const int32_t id)
+{
+ if (alsound_find_alc_sample(id) > -1) {
+ return 1;
+ }
+ return 0;
+}
+
+/// \brief delete source based on play channel id
+/// \param channel_id alc[] array index
+void alsound_delete_source(const int16_t channel_id)
+{
+ //Logger->info("alsound_delete_source {}", channel_id);
+ alDeleteSources(1, &alc[channel_id].alSource);
+ alsound_error_check("alsound_delete_source alDeleteSources");
+ if (alc[channel_id].entity) {
+ //Logger->info("delete_source {}", channel_id);
+ alc[channel_id].entity->play_ch = -1;
+ alc[channel_id].entity = 0;
+ }
+ al_con[alc[channel_id].id]--;
+ alc[channel_id].state = 0;
+ alc[channel_id].size = 0;
+ alc[channel_id].id = -1;
+}
+
+/// \brief stop chunk from playing based on chunk id
+/// \param chunk_id identifier
+void alsound_end_sample(const int32_t chunk_id)
+{
+ int16_t ret;
+ ret = alsound_find_alc_sample(chunk_id);
+ if (ret > -1) {
+ //Logger->info("alsound_end_sample id {} ch {}", chunk_id, ret);
+ alsound_delete_source(ret);
+ }
+}
+
+/// \brief change the volume of a chunk that is currently playing
+/// \param chunk_id identifier
+/// \param volume 0-127
+void alsound_set_sample_volume(const int32_t chunk_id, const int32_t volume)
+{
+ int16_t ret;
+ float gain = (float)volume / 127.0f;
+
+ if (ale.bank < 3) {
+ if (alct[ale.bank][chunk_id].flags & AL_IGNORE_RECODE) {
+ return;
+ }
+ }
+
+ ret = alsound_find_alc_sample(chunk_id);
+ if (ret > -1) {
+ alSourcef(alc[ret].alSource, AL_GAIN, gain);
+ alsound_error_check("set_sample_volume alSourcef");
+ //Logger->info("alsound_set_sample_volume {} {}", chunk_id, gain);
+ }
+
+ alnv.chunk_id = chunk_id;
+ alnv.gain = gain;
+}
+
+/// \brief initialize OpenAL and it's EFX subsystem
+void alsound_init()
+{
+ ALboolean enumeration;
+ const ALCchar *defaultDeviceName;
+ ALCdevice *device;
+ EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
+
+ enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT");
+ if (enumeration == AL_FALSE) {
+ Logger->error("OpenAL: enumeration extension not available");
+ }
+
+ defaultDeviceName = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
+
+ device = alcOpenDevice(defaultDeviceName);
+ if (!device) {
+ Logger->error("OpenAL: unable to open device");
+ return;
+ }
+
+ Logger->info("OpenAL device: {}", alcGetString(device, ALC_DEVICE_SPECIFIER));
+ alGetError();
+
+ context = alcCreateContext(device, NULL);
+ if (!alcMakeContextCurrent(context)) {
+ Logger->error("OpenAL: unable to create default context");
+ return;
+ }
+
+ if (alIsExtensionPresent("EAX-RAM") == AL_TRUE) {
+ Logger->info("EAX-RAM was found!");
+ }
+
+ //alDistanceModel(AL_NONE);
+ alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
+ alsound_error_check("alDistanceModel");
+ ale.frame_cnt = 0;
+ ale.initialized = 1;
+
+ if (oac.efx_enabled) {
+ if (!alcIsExtensionPresent(device, "ALC_EXT_EFX")) {
+ Logger->error("OpenAL: EFX not supported");
+ } else {
+ alGenEffects = (LPALGENEFFECTS) alGetProcAddress("alGenEffects");
+ alDeleteEffects = (LPALDELETEEFFECTS) alGetProcAddress("alDeleteEffects");
+ alIsEffect = (LPALISEFFECT) alGetProcAddress("alIsEffect");
+ alEffecti = (LPALEFFECTI) alGetProcAddress("alEffecti");
+ alEffectiv = (LPALEFFECTIV) alGetProcAddress("alEffectiv");
+ alEffectf = (LPALEFFECTF) alGetProcAddress("alEffectf");
+ alEffectfv = (LPALEFFECTFV) alGetProcAddress("alEffectfv");
+ alGetEffecti = (LPALGETEFFECTI) alGetProcAddress("alGetEffecti");
+ alGetEffectiv = (LPALGETEFFECTIV) alGetProcAddress("alGetEffectiv");
+ alGetEffectf = (LPALGETEFFECTF) alGetProcAddress("alGetEffectf");
+ alGetEffectfv = (LPALGETEFFECTFV) alGetProcAddress("alGetEffectfv");
+
+ alGenAuxiliaryEffectSlots = (LPALGENAUXILIARYEFFECTSLOTS) alGetProcAddress("alGenAuxiliaryEffectSlots");
+ alDeleteAuxiliaryEffectSlots = (LPALDELETEAUXILIARYEFFECTSLOTS) alGetProcAddress("alDeleteAuxiliaryEffectSlots");
+ alIsAuxiliaryEffectSlot = (LPALISAUXILIARYEFFECTSLOT) alGetProcAddress("alIsAuxiliaryEffectSlot");
+ alAuxiliaryEffectSloti = (LPALAUXILIARYEFFECTSLOTI) alGetProcAddress("alAuxiliaryEffectSloti");
+ alAuxiliaryEffectSlotiv = (LPALAUXILIARYEFFECTSLOTIV) alGetProcAddress("alAuxiliaryEffectSlotiv");
+ alAuxiliaryEffectSlotf = (LPALAUXILIARYEFFECTSLOTF) alGetProcAddress("alAuxiliaryEffectSlotf");
+ alAuxiliaryEffectSlotfv = (LPALAUXILIARYEFFECTSLOTFV) alGetProcAddress("alAuxiliaryEffectSlotfv");
+ alGetAuxiliaryEffectSloti = (LPALGETAUXILIARYEFFECTSLOTI) alGetProcAddress("alGetAuxiliaryEffectSloti");
+ alGetAuxiliaryEffectSlotiv = (LPALGETAUXILIARYEFFECTSLOTIV) alGetProcAddress("alGetAuxiliaryEffectSlotiv");
+ alGetAuxiliaryEffectSlotf = (LPALGETAUXILIARYEFFECTSLOTF) alGetProcAddress("alGetAuxiliaryEffectSlotf");
+ alGetAuxiliaryEffectSlotfv = (LPALGETAUXILIARYEFFECTSLOTFV) alGetProcAddress("alGetAuxiliaryEffectSlotfv");
+
+ al_effect = alsound_load_effect(&reverb);
+ if (!al_effect) {
+ Logger->error("OpenAL: cannot load effect");
+ } else {
+ al_slot = 0;
+ alGenAuxiliaryEffectSlots(1, &al_slot);
+ alAuxiliaryEffectSloti(al_slot, AL_EFFECTSLOT_EFFECT, (ALint) al_effect);
+ if (alGetError() == AL_NO_ERROR) {
+ Logger->info("OpenAL: EFX init success");
+ ale.efx_initialized = 1;
+ }
+ }
+ }
+ }
+
+ srand((unsigned int)time(NULL));
+}
+
+/// \brief set environmental variables
+/// \param value multiple-role input
+/// \param flag role-defining option: AL_SET_BANK
+void alsound_set_env(const int32_t value, const uint8_t flag)
+{
+ switch (flag) {
+ case AL_SET_BANK:
+ ale.bank = value;
+ break;
+ default:
+ break;
+ }
+}