forked from Croteam-official/Serious-Engine
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSoundLibrary.cpp
1761 lines (1506 loc) · 59.7 KB
/
SoundLibrary.cpp
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
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "Engine/StdH.h"
// !!! FIXME : rcg12162001 This file really needs to be ripped apart and
// !!! FIXME : rcg12162001 into platform/driver specific subdirectories.
// !!! FIXME : rcg10132001 what is this file?
#ifdef PLATFORM_WIN32
#include "initguid.h"
#endif
// !!! FIXME : Move all the SDL stuff to a different file...
#ifdef PLATFORM_UNIX
#include "SDL.h"
#endif
#include <Engine/Engine.h>
#include <Engine/Sound/SoundLibrary.h>
#include <Engine/Base/Translation.h>
#include <Engine/Base/Shell.h>
#include <Engine/Base/Memory.h>
#include <Engine/Base/ErrorReporting.h>
#include <Engine/Base/ListIterator.inl>
#include <Engine/Base/Console.h>
#include <Engine/Base/Console_internal.h>
#include <Engine/Base/Statistics_Internal.h>
#include <Engine/Base/IFeel.h>
#include <Engine/Sound/SoundProfile.h>
#include <Engine/Sound/SoundListener.h>
#include <Engine/Sound/SoundData.h>
#include <Engine/Sound/SoundObject.h>
#include <Engine/Sound/SoundDecoder.h>
#include <Engine/Network/Network.h>
#include <Engine/Templates/StaticArray.cpp>
#include <Engine/Templates/StaticStackArray.cpp>
template class CStaticArray<CSoundListener>;
#ifdef _MSC_VER
#pragma comment(lib, "winmm.lib")
#endif
// pointer to global sound library object
CSoundLibrary *_pSound = NULL;
// console variables
FLOAT snd_tmMixAhead = 0.2f; // mix-ahead in seconds
FLOAT snd_fSoundVolume = 1.0f; // master volume for sound playing [0..1]
FLOAT snd_fMusicVolume = 1.0f; // master volume for music playing [0..1]
// NOTES:
// - these 3d sound parameters have been set carefully, take extreme if changing !
// - ears distance of 20cm causes phase shift of up to 0.6ms which is very noticable
// and is more than enough, too large values cause too much distorsions in other effects
// - pan strength needs not to be very strong, since lrfilter has panning-like influence also
// - if down filter is too large, it makes too much influence even on small elevation changes
// and messes the situation completely
FLOAT snd_fDelaySoundSpeed = 1E10; // sound speed used for delay [m/s]
FLOAT snd_fDopplerSoundSpeed = 330.0f; // sound speed used for doppler [m/s]
FLOAT snd_fEarsDistance = 0.2f; // distance between listener's ears
FLOAT snd_fPanStrength = 0.1f; // panning modifier (0=none, 1= full)
FLOAT snd_fLRFilter = 3.0f; // filter for left-right
FLOAT snd_fBFilter = 5.0f; // filter for back
FLOAT snd_fUFilter = 1.0f; // filter for up
FLOAT snd_fDFilter = 3.0f; // filter for down
ENGINE_API INDEX snd_iFormat = 3;
INDEX snd_bMono = FALSE;
static INDEX snd_iDevice = -1;
static INDEX snd_iInterface = 2; // 0=WaveOut, 1=DirectSound, 2=EAX
static INDEX snd_iMaxOpenRetries = 3;
static INDEX snd_iMaxExtraChannels = 32;
static FLOAT snd_tmOpenFailDelay = 0.5f;
static FLOAT snd_fEAXPanning = 0.0f;
static FLOAT snd_fNormalizer = 0.9f;
static FLOAT _fLastNormalizeValue = 1;
#ifdef PLATFORM_WIN32
extern HWND _hwndMain; // global handle for application window
static HWND _hwndCurrent = NULL;
static HINSTANCE _hInstDS = NULL;
#else
static CTString snd_strDeviceName;
#endif
static BOOL _bMuted = FALSE;
static INDEX _iLastEnvType = 1234;
static FLOAT _fLastEnvSize = 1234;
#ifdef PLATFORM_WIN32
static FLOAT _fLastPanning = 1234;
static INDEX _iWriteOffset = 0;
static INDEX _iWriteOffset2 = 0;
// TEMP! - for writing mixer buffer to file
static FILE *_filMixerBuffer;
static BOOL _bOpened = FALSE;
#endif
#define WAVEOUTBLOCKSIZE 1024
#define MINPAN (1.0f)
#define MAXPAN (9.0f)
/**
* ----------------------------
* Sound Library functions
* ----------------------------
**/
// rcg12162001 Simple Directmedia Layer sound implementation.
#ifdef PLATFORM_UNIX
static Uint8 sdl_silence = 0;
static volatile SLONG sdl_backbuffer_allocation = 0;
static Uint8 *sdl_backbuffer = NULL;
static volatile SLONG sdl_backbuffer_pos = 0;
static volatile SLONG sdl_backbuffer_remain = 0;
static SDL_AudioDeviceID sdl_audio_device = 0;
static void sdl_audio_callback(void *userdata, Uint8 *stream, int len)
{
ASSERT(!_bDedicatedServer);
ASSERT(sdl_backbuffer != NULL);
ASSERT(sdl_backbuffer_remain <= sdl_backbuffer_allocation);
ASSERT(sdl_backbuffer_remain >= 0);
ASSERT(sdl_backbuffer_pos < sdl_backbuffer_allocation);
ASSERT(sdl_backbuffer_pos >= 0);
// "avail" is just the byte count before the end of the buffer.
// "cpysize" is how many bytes can actually be copied.
int avail = sdl_backbuffer_allocation - sdl_backbuffer_pos;
int cpysize = (len < sdl_backbuffer_remain) ? len : sdl_backbuffer_remain;
Uint8 *src = sdl_backbuffer + sdl_backbuffer_pos;
if (avail < cpysize) // Copy would pass end of ring buffer?
cpysize = avail;
if (cpysize > 0) {
memcpy(stream, src, cpysize); // move first block to SDL stream.
sdl_backbuffer_remain -= cpysize;
ASSERT(sdl_backbuffer_remain >= 0);
len -= cpysize;
ASSERT(len >= 0);
stream += cpysize;
sdl_backbuffer_pos += cpysize;
} // if
// See if we need to rotate to start of ring buffer...
ASSERT(sdl_backbuffer_pos <= sdl_backbuffer_allocation);
if (sdl_backbuffer_pos == sdl_backbuffer_allocation) {
sdl_backbuffer_pos = 0;
// we might need to feed SDL more data now...
if (len > 0) {
cpysize = (len < sdl_backbuffer_remain) ? len : sdl_backbuffer_remain;
if (cpysize > 0) {
memcpy(stream, sdl_backbuffer, cpysize); // move 2nd block.
sdl_backbuffer_pos += cpysize;
ASSERT(sdl_backbuffer_pos < sdl_backbuffer_allocation);
sdl_backbuffer_remain -= cpysize;
ASSERT(sdl_backbuffer_remain >= 0);
len -= cpysize;
ASSERT(len >= 0);
stream += cpysize;
} // if
} // if
} // if
// SDL _still_ needs more data than we've got! Fill with silence. (*shrug*)
if (len > 0) {
ASSERT(sdl_backbuffer_remain == 0);
memset(stream, sdl_silence, len);
} // if
} // sdl_audio_callback
// initialize the SDL audio subsystem.
static BOOL StartUp_SDLaudio( CSoundLibrary &sl, BOOL bReport=TRUE)
{
bReport=TRUE; // !!! FIXME ...how do you configure this externally?
// not using DirectSound (obviously)
sl.sl_bUsingDirectSound = FALSE;
sl.sl_bUsingEAX = FALSE;
snd_iDevice = 0;
ASSERT(!_bDedicatedServer);
if (_bDedicatedServer) {
CPrintF("Dedicated server; not initializing audio.\n");
return FALSE;
}
if( bReport) CPrintF(TRANSV("SDL audio initialization ...\n"));
SDL_AudioSpec desired, obtained;
SDL_zero(desired);
SDL_zero(obtained);
Sint16 bps = sl.sl_SwfeFormat.wBitsPerSample;
if (bps <= 8)
desired.format = AUDIO_U8;
else if (bps <= 16)
desired.format = AUDIO_S16LSB;
else if (bps <= 32)
desired.format = AUDIO_S32LSB;
else {
CPrintF(TRANSV("Unsupported bits-per-sample: %d\n"), bps);
return FALSE;
}
desired.freq = sl.sl_SwfeFormat.nSamplesPerSec;
// I dunno if this is the best idea, but I'll give it a try...
// should probably check a cvar for this...
if (desired.freq <= 11025)
desired.samples = 512;
else if (desired.freq <= 22050)
desired.samples = 1024;
else if (desired.freq <= 44100)
desired.samples = 2048;
else
desired.samples = 4096; // (*shrug*)
desired.channels = sl.sl_SwfeFormat.nChannels;
desired.userdata = &sl;
desired.callback = sdl_audio_callback;
// !!! FIXME rcg12162001 We force SDL to convert the audio stream on the
// !!! FIXME rcg12162001 fly to match sl.sl_SwfeFormat, but I'm curious
// !!! FIXME rcg12162001 if the Serious Engine can handle it if we changed
// !!! FIXME rcg12162001 sl.sl_SwfeFormat to match what the audio hardware
// !!! FIXME rcg12162001 can handle. I'll have to check later.
sdl_audio_device = SDL_OpenAudioDevice(snd_strDeviceName.IsEmpty() ? NULL : (const char *) snd_strDeviceName, 0, &desired, &obtained, 0);
if (!sdl_audio_device) {
CPrintF( TRANSV("SDL_OpenAudioDevice() error: %s\n"), SDL_GetError());
return FALSE;
}
sdl_silence = obtained.silence;
sdl_backbuffer_allocation = (obtained.size * 4);
sdl_backbuffer = (Uint8 *)AllocMemory(sdl_backbuffer_allocation);
sdl_backbuffer_remain = 0;
sdl_backbuffer_pos = 0;
// report success
if( bReport) {
STUBBED("Report actual SDL device name?");
CPrintF( TRANSV(" opened device: %s\n"), "SDL audio stream");
CPrintF( TRANSV(" %dHz, %dbit, %s\n"),
sl.sl_SwfeFormat.nSamplesPerSec,
sl.sl_SwfeFormat.wBitsPerSample,
SDL_GetCurrentAudioDriver());
}
// determine whole mixer buffer size from mixahead console variable
sl.sl_slMixerBufferSize = (SLONG)(ceil(snd_tmMixAhead*sl.sl_SwfeFormat.nSamplesPerSec) *
sl.sl_SwfeFormat.wBitsPerSample/8 * sl.sl_SwfeFormat.nChannels);
// align size to be next multiply of WAVEOUTBLOCKSIZE
sl.sl_slMixerBufferSize += WAVEOUTBLOCKSIZE - (sl.sl_slMixerBufferSize % WAVEOUTBLOCKSIZE);
// decoder buffer always works at 44khz
sl.sl_slDecodeBufferSize = sl.sl_slMixerBufferSize *
((44100+sl.sl_SwfeFormat.nSamplesPerSec-1)/sl.sl_SwfeFormat.nSamplesPerSec);
if( bReport) {
CPrintF(TRANSV(" parameters: %d Hz, %d bit, stereo, mix-ahead: %gs\n"),
sl.sl_SwfeFormat.nSamplesPerSec, sl.sl_SwfeFormat.wBitsPerSample, snd_tmMixAhead);
CPrintF(TRANSV(" output buffers: %d x %d bytes\n"), 2, obtained.size);
CPrintF(TRANSV(" mpx decode: %d bytes\n"), sl.sl_slDecodeBufferSize);
}
// initialize mixing and decoding buffer
sl.sl_pslMixerBuffer = (SLONG*)AllocMemory( sl.sl_slMixerBufferSize *2); // (*2 because of 32-bit buffer)
sl.sl_pswDecodeBuffer = (SWORD*)AllocMemory( sl.sl_slDecodeBufferSize+4); // (+4 because of linear interpolation of last samples)
// the audio callback can now safely fill the audio stream with silence
// until there is actual audio data to mix...
SDL_PauseAudioDevice(sdl_audio_device, 0);
// done
return TRUE;
} // StartUp_SDLaudio
// SDL audio shutdown procedure
static void ShutDown_SDLaudio( CSoundLibrary &sl)
{
SDL_PauseAudioDevice(sdl_audio_device, 1);
if (sdl_backbuffer != NULL) {
FreeMemory(sdl_backbuffer);
sdl_backbuffer = NULL;
}
if (sl.sl_pslMixerBuffer != NULL) {
FreeMemory( sl.sl_pslMixerBuffer);
sl.sl_pslMixerBuffer = NULL;
}
if (sl.sl_pswDecodeBuffer != NULL) {
FreeMemory(sl.sl_pswDecodeBuffer);
sl.sl_pswDecodeBuffer = NULL;
}
SDL_CloseAudioDevice(sdl_audio_device);
sdl_audio_device = 0;
} // ShutDown_SDLaudio
// SDL_LockAudio() must be in effect when calling this!
// ...and stay in effect until after CopyMixerBuffer_SDLaudio() is called!
static SLONG PrepareSoundBuffer_SDLaudio( CSoundLibrary &sl)
{
ASSERT(sdl_backbuffer_remain >= 0);
ASSERT(sdl_backbuffer_remain <= sdl_backbuffer_allocation);
return(sdl_backbuffer_allocation - sdl_backbuffer_remain);
} // PrepareSoundBuffer_SDLaudio
// SDL_LockAudio() must be in effect when calling this!
// ...and have been in effect since PrepareSoundBuffer_SDLaudio was called!
static void CopyMixerBuffer_SDLaudio( CSoundLibrary &sl, SLONG datasize)
{
ASSERT((sdl_backbuffer_allocation - sdl_backbuffer_remain) >= datasize);
SLONG fillpos = sdl_backbuffer_pos + sdl_backbuffer_remain;
if (fillpos > sdl_backbuffer_allocation)
fillpos -= sdl_backbuffer_allocation;
SLONG cpysize = datasize;
if ( (cpysize + fillpos) > sdl_backbuffer_allocation)
cpysize = sdl_backbuffer_allocation - fillpos;
Uint8 *src = sdl_backbuffer + fillpos;
CopyMixerBuffer_stereo(0, src, cpysize);
datasize -= cpysize;
sdl_backbuffer_remain += cpysize;
if (datasize > 0) { // rotate to start of ring buffer?
CopyMixerBuffer_stereo(cpysize, sdl_backbuffer, datasize);
sdl_backbuffer_remain += datasize;
} // if
ASSERT(sdl_backbuffer_remain <= sdl_backbuffer_allocation);
} // CopyMixerBuffer_SDLaudio
#endif // defined PLATFORM_UNIX (SDL audio implementation)
/*
* Construct uninitialized sound library.
*/
CSoundLibrary::CSoundLibrary(void)
{
sl_csSound.cs_iIndex = 3000;
// access to the list of handlers must be locked
CTSingleLock slHooks(&_pTimer->tm_csHooks, TRUE);
// synchronize access to sounds
CTSingleLock slSounds(&sl_csSound, TRUE);
// clear sound format
memset( &sl_SwfeFormat, 0, sizeof(WAVEFORMATEX));
sl_EsfFormat = SF_NONE;
// reset buffer ptrs
sl_pslMixerBuffer = NULL;
sl_pswDecodeBuffer = NULL;
sl_pubBuffersMemory = NULL;
#ifdef PLATFORM_WIN32
// clear wave out data
sl_hwoWaveOut = NULL;
// clear direct sound data
_hInstDS = NULL;
sl_pDS = NULL;
sl_pKSProperty = NULL;
sl_pDSPrimary = NULL;
sl_pDSSecondary = NULL;
sl_pDSSecondary2 = NULL;
sl_pDSListener = NULL;
sl_pDSSourceLeft = NULL;
sl_pDSSourceRight = NULL;
#endif
sl_bUsingDirectSound = FALSE;
sl_bUsingEAX = FALSE;
}
/*
* Destruct (and clean up).
*/
CSoundLibrary::~CSoundLibrary(void)
{
// access to the list of handlers must be locked
CTSingleLock slHooks(&_pTimer->tm_csHooks, TRUE);
// synchronize access to sounds
CTSingleLock slSounds(&sl_csSound, TRUE);
// clear sound enviroment
Clear();
// clear any installed sound decoders
CSoundDecoder::EndPlugins();
}
// post sound console variables' functions
static FLOAT _tmLastMixAhead = 1234;
static INDEX _iLastFormat = 1234;
static INDEX _iLastDevice = 1234;
static INDEX _iLastAPI = 1234;
static void SndPostFunc(void *pArgs)
{
// clamp variables
snd_tmMixAhead = Clamp( snd_tmMixAhead, 0.1f, 0.9f);
snd_iFormat = Clamp( snd_iFormat, (INDEX)CSoundLibrary::SF_NONE, (INDEX)CSoundLibrary::SF_44100_16);
snd_iDevice = Clamp( snd_iDevice, -1, 15);
snd_iInterface = Clamp( snd_iInterface, 0, 2);
// if any variable has been changed
if( _tmLastMixAhead!=snd_tmMixAhead || _iLastFormat!=snd_iFormat
|| _iLastDevice!=snd_iDevice || _iLastAPI!=snd_iInterface) {
// reinit sound format
_pSound->SetFormat( (enum CSoundLibrary::SoundFormat)snd_iFormat, TRUE);
}
}
/*
* some internal functions
*/
#ifdef PLATFORM_WIN32
// DirectSound shutdown procedure
static void ShutDown_dsound( CSoundLibrary &sl)
{
// free direct sound buffer(s)
sl.sl_bUsingDirectSound = FALSE;
sl.sl_bUsingEAX = FALSE;
if( sl.sl_pDSSourceRight!=NULL) {
sl.sl_pDSSourceRight->Release();
sl.sl_pDSSourceRight = NULL;
}
if( sl.sl_pDSSourceLeft != NULL) {
sl.sl_pDSSourceLeft->Release();
sl.sl_pDSSourceLeft = NULL;
}
if( sl.sl_pDSListener != NULL) {
sl.sl_pDSListener->Release();
sl.sl_pDSListener = NULL;
}
if( sl.sl_pDSSecondary2 != NULL) {
sl.sl_pDSSecondary2->Stop();
sl.sl_pDSSecondary2->Release();
sl.sl_pDSSecondary2 = NULL;
}
if( sl.sl_pDSSecondary != NULL) {
sl.sl_pDSSecondary->Stop();
sl.sl_pDSSecondary->Release();
sl.sl_pDSSecondary = NULL;
}
if( sl.sl_pDSPrimary!=NULL) {
sl.sl_pDSPrimary->Stop();
sl.sl_pDSPrimary->Release();
sl.sl_pDSPrimary = NULL;
}
if( sl.sl_pKSProperty != NULL) {
sl.sl_pKSProperty->Release();
sl.sl_pKSProperty = NULL;
}
// free direct sound object
if( sl.sl_pDS!=NULL) {
// reset cooperative level
if( _hwndCurrent!=NULL) sl.sl_pDS->SetCooperativeLevel( _hwndCurrent, DSSCL_NORMAL);
sl.sl_pDS->Release();
sl.sl_pDS = NULL;
}
// free direct sound library
if( _hInstDS != NULL) {
FreeLibrary(_hInstDS);
_hInstDS = NULL;
}
// free memory
if( sl.sl_pslMixerBuffer!=NULL) {
FreeMemory( sl.sl_pslMixerBuffer);
sl.sl_pslMixerBuffer = NULL;
}
if( sl.sl_pswDecodeBuffer!=NULL) {
FreeMemory( sl.sl_pswDecodeBuffer);
sl.sl_pswDecodeBuffer = NULL;
}
}
#endif
/*
* Set wave format from library format
*/
static void SetWaveFormat( CSoundLibrary::SoundFormat EsfFormat, WAVEFORMATEX &wfeFormat)
{
// change Library Wave Format
memset( &wfeFormat, 0, sizeof(WAVEFORMATEX));
wfeFormat.wFormatTag = WAVE_FORMAT_PCM;
wfeFormat.nChannels = 2;
wfeFormat.wBitsPerSample = 16;
switch( EsfFormat) {
case CSoundLibrary::SF_11025_16: wfeFormat.nSamplesPerSec = 11025; break;
case CSoundLibrary::SF_22050_16: wfeFormat.nSamplesPerSec = 22050; break;
case CSoundLibrary::SF_44100_16: wfeFormat.nSamplesPerSec = 44100; break;
case CSoundLibrary::SF_NONE: ASSERTALWAYS( "Can't set to NONE format"); break;
default: ASSERTALWAYS( "Unknown Sound format"); break;
}
wfeFormat.nBlockAlign = (wfeFormat.wBitsPerSample / 8) * wfeFormat.nChannels;
wfeFormat.nAvgBytesPerSec = wfeFormat.nSamplesPerSec * wfeFormat.nBlockAlign;
}
/*
* Set library format from wave format
*/
static void SetLibraryFormat( CSoundLibrary &sl)
{
// if library format is none return
if( sl.sl_EsfFormat == CSoundLibrary::SF_NONE) return;
// else check wave format to determine library format
ULONG ulFormat = sl.sl_SwfeFormat.nSamplesPerSec;
// find format
switch( ulFormat) {
case 11025: sl.sl_EsfFormat = CSoundLibrary::SF_11025_16; break;
case 22050: sl.sl_EsfFormat = CSoundLibrary::SF_22050_16; break;
case 44100: sl.sl_EsfFormat = CSoundLibrary::SF_44100_16; break;
// unknown format
default:
ASSERTALWAYS( "Unknown sound format");
FatalError( TRANS("Unknown sound format"));
sl.sl_EsfFormat = CSoundLibrary::SF_ILLEGAL;
}
}
#ifdef PLATFORM_WIN32
static BOOL DSFail( CSoundLibrary &sl, char *strError)
{
CPrintF(strError);
ShutDown_dsound(sl);
snd_iInterface=1; // if EAX failed -> try DirectSound
return FALSE;
}
// some helper functions for DirectSound
static BOOL DSInitSecondary( CSoundLibrary &sl, LPDIRECTSOUNDBUFFER &pBuffer, SLONG slSize)
{
// eventuallt adjust for EAX
DWORD dwFlag3D = NONE;
if( snd_iInterface==2) {
dwFlag3D = DSBCAPS_CTRL3D;
sl.sl_SwfeFormat.nChannels=1; // mono output
sl.sl_SwfeFormat.nBlockAlign/=2;
sl.sl_SwfeFormat.nAvgBytesPerSec/=2;
slSize/=2;
}
DSBUFFERDESC dsBuffer;
memset( &dsBuffer, 0, sizeof(dsBuffer));
dsBuffer.dwSize = sizeof(DSBUFFERDESC);
dsBuffer.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | dwFlag3D;
dsBuffer.dwBufferBytes = slSize;
dsBuffer.lpwfxFormat = &sl.sl_SwfeFormat;
HRESULT hResult = sl.sl_pDS->CreateSoundBuffer( &dsBuffer, &pBuffer, NULL);
if( snd_iInterface==2) {
// revert back to original wave format (stereo)
sl.sl_SwfeFormat.nChannels=2;
sl.sl_SwfeFormat.nBlockAlign*=2;
sl.sl_SwfeFormat.nAvgBytesPerSec*=2;
}
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot create secondary buffer.\n"));
return TRUE;
}
static BOOL DSLockBuffer( CSoundLibrary &sl, LPDIRECTSOUNDBUFFER pBuffer, SLONG slSize, LPVOID &lpData, DWORD &dwSize)
{
INDEX ctRetries = 1000; // too much?
if( sl.sl_bUsingEAX) slSize/=2; // buffer is mono in case of EAX
FOREVER {
HRESULT hResult = pBuffer->Lock( 0, slSize, &lpData, &dwSize, NULL, NULL, 0);
if( hResult==DS_OK && slSize==dwSize) return TRUE;
if( hResult!=DSERR_BUFFERLOST) return DSFail( sl, TRANS(" ! DirectSound error: Cannot lock sound buffer.\n"));
if( ctRetries-- == 0) return DSFail( sl, TRANS(" ! DirectSound error: Couldn't restore sound buffer.\n"));
pBuffer->Restore();
}
}
static void DSPlayBuffers( CSoundLibrary &sl)
{
DWORD dw;
BOOL bInitiatePlay = FALSE;
ASSERT( sl.sl_pDSSecondary!=NULL && sl.sl_pDSPrimary!=NULL);
if( sl.sl_bUsingEAX && sl.sl_pDSSecondary2->GetStatus(&dw)==DS_OK && !(dw&DSBSTATUS_PLAYING)) bInitiatePlay = TRUE;
if( sl.sl_pDSSecondary->GetStatus(&dw)==DS_OK && !(dw&DSBSTATUS_PLAYING)) bInitiatePlay = TRUE;
if( sl.sl_pDSPrimary->GetStatus(&dw)==DS_OK && !(dw&DSBSTATUS_PLAYING)) bInitiatePlay = TRUE;
// done if all buffers are already playing
if( !bInitiatePlay) return;
// stop buffers (in case some buffers are playing
sl.sl_pDSPrimary->Stop();
sl.sl_pDSSecondary->Stop();
if( sl.sl_bUsingEAX) sl.sl_pDSSecondary2->Stop();
// check sound buffer lock and clear sound buffer(s)
LPVOID lpData;
DWORD dwSize;
if( !DSLockBuffer( sl, sl.sl_pDSSecondary, sl.sl_slMixerBufferSize, lpData, dwSize)) return;
memset( lpData, 0, dwSize);
sl.sl_pDSSecondary->Unlock( lpData, dwSize, NULL, 0);
if( sl.sl_bUsingEAX) {
if( !DSLockBuffer( sl, sl.sl_pDSSecondary2, sl.sl_slMixerBufferSize, lpData, dwSize)) return;
memset( lpData, 0, dwSize);
sl.sl_pDSSecondary2->Unlock( lpData, dwSize, NULL, 0);
// start playing EAX additional buffer
sl.sl_pDSSecondary2->Play( 0, 0, DSBPLAY_LOOPING);
}
// start playing standard DirectSound buffers
sl.sl_pDSPrimary->Play( 0, 0, DSBPLAY_LOOPING);
sl.sl_pDSSecondary->Play( 0, 0, DSBPLAY_LOOPING);
_iWriteOffset = 0;
_iWriteOffset2 = 0;
// adjust starting offsets for EAX
if( sl.sl_bUsingEAX) {
DWORD dwCursor1, dwCursor2;
SLONG slMinDelta = MAX_SLONG;
for( INDEX i=0; i<10; i++) { // shoud be enough to screw interrupts
sl.sl_pDSSecondary->GetCurrentPosition( &dwCursor1, NULL);
sl.sl_pDSSecondary2->GetCurrentPosition( &dwCursor2, NULL);
SLONG slDelta1 = dwCursor2-dwCursor1;
sl.sl_pDSSecondary2->GetCurrentPosition( &dwCursor2, NULL);
sl.sl_pDSSecondary->GetCurrentPosition( &dwCursor1, NULL);
SLONG slDelta2 = dwCursor2-dwCursor1;
SLONG slDelta = (slDelta1+slDelta2) /2;
if( slDelta<slMinDelta) slMinDelta = slDelta;
//CPrintF( "D1=%5d, D2=%5d, AD=%5d, MD=%5d\n", slDelta1, slDelta2, slDelta, slMinDelta);
}
if( slMinDelta<0) _iWriteOffset = -slMinDelta*2; // 2 because of offset is stereo
if( slMinDelta>0) _iWriteOffset2 = +slMinDelta*2;
_iWriteOffset += _iWriteOffset & 3; // round to 4 bytes
_iWriteOffset2 += _iWriteOffset2 & 3;
// assure that first writing offsets are inside buffers
if( _iWriteOffset >=sl.sl_slMixerBufferSize) _iWriteOffset -= sl.sl_slMixerBufferSize;
if( _iWriteOffset2>=sl.sl_slMixerBufferSize) _iWriteOffset2 -= sl.sl_slMixerBufferSize;
ASSERT( _iWriteOffset >=0 && _iWriteOffset <sl.sl_slMixerBufferSize);
ASSERT( _iWriteOffset2>=0 && _iWriteOffset2<sl.sl_slMixerBufferSize);
}
}
// init and set DirectSound format (internal)
static BOOL StartUp_dsound( CSoundLibrary &sl, BOOL bReport=TRUE)
{
// startup
sl.sl_bUsingDirectSound = FALSE;
ASSERT( _hInstDS==NULL && sl.sl_pDS==NULL);
ASSERT( sl.sl_pDSSecondary==NULL && sl.sl_pDSPrimary==NULL);
// update window handle (just in case)
HRESULT (WINAPI *pDirectSoundCreate)(GUID FAR *lpGUID, LPDIRECTSOUND FAR *lplpDS, IUnknown FAR *pUnkOuter);
if( bReport) CPrintF(TRANSV("Direct Sound initialization ...\n"));
ASSERT( _hInstDS==NULL);
_hInstDS = LoadLibraryA( "dsound.dll");
if( _hInstDS==NULL) {
CPrintF( TRANS(" ! DirectSound error: Cannot load 'DSOUND.DLL'.\n"));
return FALSE;
}
// get main procedure address
pDirectSoundCreate = (HRESULT(WINAPI *)(GUID FAR *, LPDIRECTSOUND FAR *, IUnknown FAR *))GetProcAddress( _hInstDS, "DirectSoundCreate");
if( pDirectSoundCreate==NULL) return DSFail( sl, TRANS(" ! DirectSound error: Cannot get procedure address.\n"));
// init dsound
HRESULT hResult;
hResult = pDirectSoundCreate( NULL, &sl.sl_pDS, NULL);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot create object.\n"));
// get capabilities
DSCAPS dsCaps;
dsCaps.dwSize = sizeof(dsCaps);
hResult = sl.sl_pDS->GetCaps( &dsCaps);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot determine capabilites.\n"));
// fail if in emulation mode
if( dsCaps.dwFlags & DSCAPS_EMULDRIVER) {
CPrintF( TRANS(" ! DirectSound error: No driver installed.\n"));
ShutDown_dsound(sl);
return FALSE;
}
// set cooperative level to priority
_hwndCurrent = _hwndMain;
hResult = sl.sl_pDS->SetCooperativeLevel( _hwndCurrent, DSSCL_PRIORITY);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot set cooperative level.\n"));
// prepare 3D flag if EAX
DWORD dwFlag3D = NONE;
if( snd_iInterface==2) dwFlag3D = DSBCAPS_CTRL3D;
// create primary sound buffer (must have one)
DSBUFFERDESC dsBuffer;
memset( &dsBuffer, 0, sizeof(dsBuffer));
dsBuffer.dwSize = sizeof(dsBuffer);
dsBuffer.dwFlags = DSBCAPS_PRIMARYBUFFER | dwFlag3D;
dsBuffer.dwBufferBytes = 0;
dsBuffer.lpwfxFormat = NULL;
hResult = sl.sl_pDS->CreateSoundBuffer( &dsBuffer, &sl.sl_pDSPrimary, NULL);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot create primary sound buffer.\n"));
// set primary buffer format
WAVEFORMATEX wfx = sl.sl_SwfeFormat;
hResult = sl.sl_pDSPrimary->SetFormat(&wfx);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! DirectSound error: Cannot set primary sound buffer format.\n"));
// startup secondary sound buffer(s)
SLONG slBufferSize = (SLONG)(ceil(snd_tmMixAhead*sl.sl_SwfeFormat.nSamplesPerSec) *
sl.sl_SwfeFormat.wBitsPerSample/8 * sl.sl_SwfeFormat.nChannels);
if( !DSInitSecondary( sl, sl.sl_pDSSecondary, slBufferSize)) return FALSE;
// set some additionals for EAX
if( snd_iInterface==2)
{
// 2nd secondary buffer
if( !DSInitSecondary( sl, sl.sl_pDSSecondary2, slBufferSize)) return FALSE;
// set 3D for all buffers
HRESULT hr1,hr2,hr3,hr4;
hr1 = sl.sl_pDSPrimary->QueryInterface( IID_IDirectSound3DListener, (LPVOID*)&sl.sl_pDSListener);
hr2 = sl.sl_pDSSecondary->QueryInterface( IID_IDirectSound3DBuffer, (LPVOID*)&sl.sl_pDSSourceLeft);
hr3 = sl.sl_pDSSecondary2->QueryInterface( IID_IDirectSound3DBuffer, (LPVOID*)&sl.sl_pDSSourceRight);
if( hr1!=DS_OK || hr2!=DS_OK || hr3!=DS_OK) return DSFail( sl, TRANS(" ! DirectSound3D error: Cannot set 3D sound buffer.\n"));
hr1 = sl.sl_pDSListener->SetPosition( 0,0,0, DS3D_DEFERRED);
hr2 = sl.sl_pDSListener->SetOrientation( 0,0,1, 0,1,0, DS3D_DEFERRED);
hr3 = sl.sl_pDSListener->SetRolloffFactor( 1, DS3D_DEFERRED);
if( hr1!=DS_OK || hr2!=DS_OK || hr3!=DS_OK) return DSFail( sl, TRANS(" ! DirectSound3D error: Cannot set 3D parameters for listener.\n"));
hr1 = sl.sl_pDSSourceLeft->SetMinDistance( MINPAN, DS3D_DEFERRED);
hr2 = sl.sl_pDSSourceLeft->SetMaxDistance( MAXPAN, DS3D_DEFERRED);
hr3 = sl.sl_pDSSourceRight->SetMinDistance( MINPAN, DS3D_DEFERRED);
hr4 = sl.sl_pDSSourceRight->SetMaxDistance( MAXPAN, DS3D_DEFERRED);
if( hr1!=DS_OK || hr2!=DS_OK || hr3!=DS_OK || hr4!=DS_OK) {
return DSFail( sl, TRANS(" ! DirectSound3D error: Cannot set 3D parameters for sound source.\n"));
}
// apply
hResult = sl.sl_pDSListener->CommitDeferredSettings();
if( hResult!=DS_OK) return DSFail( sl, TRANS(" ! DirectSound3D error: Cannot apply 3D parameters.\n"));
// reset EAX parameters
_fLastPanning = 1234;
_iLastEnvType = 1234;
_fLastEnvSize = 1234;
// query property interface to EAX
hResult = sl.sl_pDSSourceLeft->QueryInterface( IID_IKsPropertySet, (LPVOID*)&sl.sl_pKSProperty);
if( hResult != DS_OK) return DSFail( sl, TRANS(" ! EAX error: Cannot set property interface.\n"));
// query support
ULONG ulSupport = 0;
hResult = sl.sl_pKSProperty->QuerySupport( DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ENVIRONMENT, &ulSupport);
if( hResult != DS_OK || !(ulSupport&KSPROPERTY_SUPPORT_SET)) return DSFail( sl, TRANS(" ! EAX error: Cannot query property support.\n"));
hResult = sl.sl_pKSProperty->QuerySupport( DSPROPSETID_EAX_ListenerProperties, DSPROPERTY_EAXLISTENER_ENVIRONMENTSIZE, &ulSupport);
if( hResult != DS_OK || !(ulSupport&KSPROPERTY_SUPPORT_SET)) return DSFail( sl, TRANS(" ! EAX error: Cannot query property support.\n"));
// made it - EAX's on!
sl.sl_bUsingEAX = TRUE;
}
// mark that dsound is operative and set mixer buffer size (decoder buffer always works at 44khz)
_iWriteOffset = 0;
_iWriteOffset2 = 0;
sl.sl_bUsingDirectSound = TRUE;
sl.sl_slMixerBufferSize = slBufferSize;
sl.sl_slDecodeBufferSize = sl.sl_slMixerBufferSize *
((44100+sl.sl_SwfeFormat.nSamplesPerSec-1) /sl.sl_SwfeFormat.nSamplesPerSec);
// allocate mixing and decoding buffers
sl.sl_pslMixerBuffer = (SLONG*)AllocMemory( sl.sl_slMixerBufferSize *2); // (*2 because of 32-bit buffer)
sl.sl_pswDecodeBuffer = (SWORD*)AllocMemory( sl.sl_slDecodeBufferSize+4); // (+4 because of linear interpolation of last samples)
// report success
if( bReport) {
CTString strDevice = TRANS("default device");
if( snd_iDevice>=0) strDevice.PrintF( TRANS("device %d"), snd_iDevice);
CPrintF( TRANS(" %dHz, %dbit, %s, mix-ahead: %gs\n"),
sl.sl_SwfeFormat.nSamplesPerSec, sl.sl_SwfeFormat.wBitsPerSample, strDevice, snd_tmMixAhead);
CPrintF(TRANSV(" mixer buffer size: %d KB\n"), sl.sl_slMixerBufferSize /1024);
CPrintF(TRANSV(" decode buffer size: %d KB\n"), sl.sl_slDecodeBufferSize/1024);
// EAX?
CTString strEAX = TRANS("Disabled");
if( sl.sl_bUsingEAX) strEAX = TRANS("Enabled");
CPrintF( TRANS(" EAX: %s\n"), strEAX);
}
// done
return TRUE;
}
// set WaveOut format (internal)
static INDEX _ctChannelsOpened = 0;
static BOOL StartUp_waveout( CSoundLibrary &sl, BOOL bReport=TRUE)
{
// not using DirectSound (obviously)
sl.sl_bUsingDirectSound = FALSE;
sl.sl_bUsingEAX = FALSE;
if( bReport) CPrintF(TRANSV("WaveOut initialization ...\n"));
// set maximum total number of retries for device opening
INDEX ctMaxRetries = snd_iMaxOpenRetries;
_ctChannelsOpened = 0;
MMRESULT res;
// repeat
FOREVER {
// try to open wave device
HWAVEOUT hwo;
res = waveOutOpen( &hwo, (snd_iDevice<0)?WAVE_MAPPER:snd_iDevice, &sl.sl_SwfeFormat, NULL, NULL, NONE);
// if opened
if( res == MMSYSERR_NOERROR) {
_ctChannelsOpened++;
// if first one
if (_ctChannelsOpened==1) {
// remember as used waveout
sl.sl_hwoWaveOut = hwo;
// if extra channel
} else {
// remember under extra
sl.sl_ahwoExtra.Push() = hwo;
}
// if no extra channels should be taken
if (_ctChannelsOpened>=snd_iMaxExtraChannels+1) {
// no more tries
break;
}
// if cannot open
} else {
// decrement retry counter
ctMaxRetries--;
// if no more retries
if (ctMaxRetries<0) {
// quit trying
break;
// if more retries left
} else {
// wait a bit (probably sound-scheme is playing)
Sleep(int(snd_tmOpenFailDelay*1000));
}
}
}
// if couldn't set format
if( _ctChannelsOpened==0 && res != MMSYSERR_NOERROR) {
// report error
CTString strError;
switch (res) {
case MMSYSERR_ALLOCATED: strError = TRANS("Device already in use."); break;
case MMSYSERR_BADDEVICEID: strError = TRANS("Bad device number."); break;
case MMSYSERR_NODRIVER: strError = TRANS("No driver installed."); break;
case MMSYSERR_NOMEM: strError = TRANS("Memory allocation problem."); break;
case WAVERR_BADFORMAT: strError = TRANS("Unsupported data format."); break;
case WAVERR_SYNC: strError = TRANS("Wrong flag?"); break;
default: strError.PrintF( "%d", res);
};
CPrintF( TRANS(" ! WaveOut error: %s\n"), strError);
return FALSE;
}
// get waveout capabilities
WAVEOUTCAPS woc;
memset( &woc, 0, sizeof(woc));
res = waveOutGetDevCaps((int)sl.sl_hwoWaveOut, &woc, sizeof(woc));
// report success
if( bReport) {
CTString strDevice = TRANS("default device");
if( snd_iDevice>=0) strDevice.PrintF( TRANS("device %d"), snd_iDevice);
CPrintF( TRANS(" opened device: %s\n"), woc.szPname);
CPrintF( TRANS(" %dHz, %dbit, %s\n"),
sl.sl_SwfeFormat.nSamplesPerSec, sl.sl_SwfeFormat.wBitsPerSample, strDevice);
}
// determine whole mixer buffer size from mixahead console variable
sl.sl_slMixerBufferSize = (SLONG)(ceil(snd_tmMixAhead*sl.sl_SwfeFormat.nSamplesPerSec) *
sl.sl_SwfeFormat.wBitsPerSample/8 * sl.sl_SwfeFormat.nChannels);
// align size to be next multiply of WAVEOUTBLOCKSIZE
sl.sl_slMixerBufferSize += WAVEOUTBLOCKSIZE - (sl.sl_slMixerBufferSize % WAVEOUTBLOCKSIZE);
// determine number of WaveOut buffers
const INDEX ctWOBuffers = sl.sl_slMixerBufferSize / WAVEOUTBLOCKSIZE;
// decoder buffer always works at 44khz
sl.sl_slDecodeBufferSize = sl.sl_slMixerBufferSize *
((44100+sl.sl_SwfeFormat.nSamplesPerSec-1)/sl.sl_SwfeFormat.nSamplesPerSec);
if( bReport) {
CPrintF(TRANSV(" parameters: %d Hz, %d bit, stereo, mix-ahead: %gs\n"),
sl.sl_SwfeFormat.nSamplesPerSec, sl.sl_SwfeFormat.wBitsPerSample, snd_tmMixAhead);
CPrintF(TRANSV(" output buffers: %d x %d bytes\n"), ctWOBuffers, WAVEOUTBLOCKSIZE),
CPrintF(TRANSV(" mpx decode: %d bytes\n"), sl.sl_slDecodeBufferSize),
CPrintF(TRANSV(" extra sound channels taken: %d\n"), _ctChannelsOpened-1);
}
// initialise waveout sound buffers
sl.sl_pubBuffersMemory = (UBYTE*)AllocMemory( sl.sl_slMixerBufferSize);
memset( sl.sl_pubBuffersMemory, 0, sl.sl_slMixerBufferSize);
sl.sl_awhWOBuffers.New(ctWOBuffers);
for( INDEX iBuffer = 0; iBuffer<sl.sl_awhWOBuffers.Count(); iBuffer++) {
WAVEHDR &wh = sl.sl_awhWOBuffers[iBuffer];
wh.lpData = (char*)(sl.sl_pubBuffersMemory + iBuffer*WAVEOUTBLOCKSIZE);
wh.dwBufferLength = WAVEOUTBLOCKSIZE;
wh.dwFlags = 0;
}
// initialize mixing and decoding buffer
sl.sl_pslMixerBuffer = (SLONG*)AllocMemory( sl.sl_slMixerBufferSize *2); // (*2 because of 32-bit buffer)
sl.sl_pswDecodeBuffer = (SWORD*)AllocMemory( sl.sl_slDecodeBufferSize+4); // (+4 because of linear interpolation of last samples)
// done
return TRUE;
}
#endif
/*
* set sound format
*/
static void SetFormat_internal( CSoundLibrary &sl, CSoundLibrary::SoundFormat EsfNew, BOOL bReport)
{
// access to the list of handlers must be locked
CTSingleLock slHooks(&_pTimer->tm_csHooks, TRUE);
// synchronize access to sounds
CTSingleLock slSounds(&sl.sl_csSound, TRUE);
// remember library format
sl.sl_EsfFormat = EsfNew;
// release library
sl.ClearLibrary();
// if none skip initialization
_fLastNormalizeValue = 1;
if( bReport) CPrintF(TRANSV("Setting sound format ...\n"));
if( sl.sl_EsfFormat == CSoundLibrary::SF_NONE) {
if( bReport) CPrintF(TRANSV(" (no sound)\n"));
return;
}
// set wave format from library format
SetWaveFormat( EsfNew, sl.sl_SwfeFormat);
snd_iDevice = Clamp( snd_iDevice, -1, (INDEX)(sl.sl_ctWaveDevices-1));
snd_tmMixAhead = Clamp( snd_tmMixAhead, 0.1f, 0.9f);
snd_iInterface = Clamp( snd_iInterface, 0, 2);
BOOL bSoundOK = FALSE;
#ifdef PLATFORM_WIN32
if( snd_iInterface==2) {
// if wanted, 1st try to set EAX
bSoundOK = StartUp_dsound( sl, bReport);
}
if( !bSoundOK && snd_iInterface==1) {
// if wanted, 2nd try to set DirectSound
bSoundOK = StartUp_dsound( sl, bReport);
}
// if DirectSound failed or not wanted
if( !bSoundOK) {
// try waveout
bSoundOK = StartUp_waveout( sl, bReport);
snd_iInterface = 0; // mark that DirectSound didn't make it
}
#else