forked from RobertBeckebans/RBDOOM-3-BFG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi_sound_win32.cpp
1104 lines (913 loc) · 26.9 KB
/
i_sound_win32.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
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "Precompiled.h"
#include "globaldata.h"
//
// DESCRIPTION:
// System interface for sound.
//
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <sys/types.h>
#include <fcntl.h>
// Timer stuff. Experimental.
#include <time.h>
#include <signal.h>
#include "z_zone.h"
#include "i_system.h"
#include "i_sound.h"
#include "m_argv.h"
#include "m_misc.h"
#include "w_wad.h"
#include "d_main.h"
#include "doomdef.h"
#include "../timidity/timidity.h"
#include "../timidity/controls.h"
#include "sound/snd_local.h"
#ifdef _MSC_VER // DG: xaudio can only be used with MSVC
#include <xaudio2.h>
#include <x3daudio.h>
#endif // DG end
#pragma warning ( disable : 4244 )
#define MIDI_CHANNELS 2
#if 1
#define MIDI_RATE 22050
#define MIDI_SAMPLETYPE XAUDIOSAMPLETYPE_8BITPCM
#define MIDI_FORMAT AUDIO_U8
#define MIDI_FORMAT_BYTES 1
#else
#define MIDI_RATE 48000
#define MIDI_SAMPLETYPE XAUDIOSAMPLETYPE_16BITPCM
#define MIDI_FORMAT AUDIO_S16MSB
#define MIDI_FORMAT_BYTES 2
#endif
#ifdef _MSC_VER // DG: xaudio can only be used with MSVC
IXAudio2SourceVoice* pMusicSourceVoice;
#endif
MidiSong* doomMusic;
byte* musicBuffer;
int totalBufferSize;
HANDLE hMusicThread;
bool waitingForMusic;
bool musicReady;
typedef struct tagActiveSound_t {
IXAudio2SourceVoice* m_pSourceVoice; // Source voice
X3DAUDIO_DSP_SETTINGS m_DSPSettings;
X3DAUDIO_EMITTER m_Emitter;
X3DAUDIO_CONE m_Cone;
int id;
int valid;
int start;
int player;
bool localSound;
mobj_t *originator;
} activeSound_t;
// cheap little struct to hold a sound
typedef struct {
int vol;
int player;
int pitch;
int priority;
mobj_t *originator;
mobj_t *listener;
} soundEvent_t;
// array of all the possible sounds
// in split screen we only process the loudest sound of each type per frame
soundEvent_t soundEvents[128];
extern int PLAYERCOUNT;
// Real volumes
const float GLOBAL_VOLUME_MULTIPLIER = 0.5f;
float x_SoundVolume = GLOBAL_VOLUME_MULTIPLIER;
float x_MusicVolume = GLOBAL_VOLUME_MULTIPLIER;
// The actual lengths of all sound effects.
static int lengths[NUMSFX];
activeSound_t activeSounds[NUM_SOUNDBUFFERS] = {0};
int S_initialized = 0;
bool Music_initialized = false;
// XAUDIO
float g_EmitterAzimuths [] = { 0.f };
static int numOutputChannels = 0;
static bool soundHardwareInitialized = false;
// DG: xaudio can only be used with MSVC
#ifdef _MSC_VER
X3DAUDIO_HANDLE X3DAudioInstance;
X3DAUDIO_LISTENER doom_Listener;
#endif
//float localSoundVolumeEntries[] = { 0.f, 0.f, 0.9f, 0.5f, 0.f, 0.f };
float localSoundVolumeEntries[] = { 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f };
void I_InitSoundChannel( int channel, int numOutputChannels_ );
/*
======================
getsfx
======================
*/
// This function loads the sound data from the WAD lump,
// for single sound.
//
void* getsfx ( const char* sfxname, int* len )
{
unsigned char* sfx;
unsigned char* sfxmem;
int size;
char name[20];
int sfxlump;
float scale = 1.0f;
// Get the sound data from the WAD, allocate lump
// in zone memory.
sprintf(name, "ds%s", sfxname);
// Scale down the plasma gun, it clips
if ( strcmp( sfxname, "plasma" ) == 0 ) {
scale = 0.75f;
}
if ( strcmp( sfxname, "itemup" ) == 0 ) {
scale = 1.333f;
}
// If sound requested is not found in current WAD, use pistol as default
if ( W_CheckNumForName(name) == -1 )
sfxlump = W_GetNumForName("dspistol");
else
sfxlump = W_GetNumForName(name);
// Sound lump headers are 8 bytes.
const int SOUND_LUMP_HEADER_SIZE_IN_BYTES = 8;
size = W_LumpLength( sfxlump ) - SOUND_LUMP_HEADER_SIZE_IN_BYTES;
sfx = (unsigned char*)W_CacheLumpNum( sfxlump, PU_CACHE_SHARED );
const unsigned char * sfxSampleStart = sfx + SOUND_LUMP_HEADER_SIZE_IN_BYTES;
// Allocate from zone memory.
//sfxmem = (float*)DoomLib::Z_Malloc( size*(sizeof(float)), PU_SOUND_SHARED, 0 );
sfxmem = (unsigned char*)malloc( size * sizeof(unsigned char) );
// Now copy, and convert to Xbox360 native float samples, do initial volume ramp, and scale
for ( int i=0; i<size; i++ ) {
sfxmem[i] = sfxSampleStart[i];// * scale;
}
// Remove the cached lump.
Z_Free( sfx );
// Set length.
*len = size;
// Return allocated padded data.
return (void *) (sfxmem);
}
/*
======================
I_SetChannels
======================
*/
void I_SetChannels() {
// Original Doom set up lookup tables here
}
/*
======================
I_SetSfxVolume
======================
*/
void I_SetSfxVolume(int volume) {
x_SoundVolume = ((float)volume / 15.f) * GLOBAL_VOLUME_MULTIPLIER;
}
/*
======================
I_GetSfxLumpNum
======================
*/
//
// Retrieve the raw data lump index
// for a given SFX name.
//
int I_GetSfxLumpNum(sfxinfo_t* sfx)
{
char namebuf[9];
sprintf(namebuf, "ds%s", sfx->name);
return W_GetNumForName(namebuf);
}
/*
======================
I_StartSound2
======================
*/
// Starting a sound means adding it
// to the current list of active sounds
// in the internal channels.
// As the SFX info struct contains
// e.g. a pointer to the raw data,
// it is ignored.
// As our sound handling does not handle
// priority, it is ignored.
// Pitching (that is, increased speed of playback) is set
//
int I_StartSound2 ( int id, int player, mobj_t *origin, mobj_t *listener_origin, int pitch, int priority ) {
if ( !soundHardwareInitialized ) {
return id;
}
int i;
XAUDIO2_VOICE_STATE state;
activeSound_t* sound = 0;
int oldest = 0, oldestnum = -1;
// these id's should not overlap
if ( id == sfx_sawup || id == sfx_sawidl || id == sfx_sawful || id == sfx_sawhit || id == sfx_stnmov ) {
// Loop all channels, check.
for (i=0 ; i < NUM_SOUNDBUFFERS ; i++)
{
sound = &activeSounds[i];
if (sound->valid && ( sound->id == id && sound->player == player ) ) {
I_StopSound( sound->id, player );
break;
}
}
}
// find a valid channel, or one that has finished playing
for (i = 0; i < NUM_SOUNDBUFFERS; ++i) {
sound = &activeSounds[i];
if (!sound->valid)
break;
if (!oldest || oldest > sound->start) {
oldestnum = i;
oldest = sound->start;
}
sound->m_pSourceVoice->GetState( &state );
if ( state.BuffersQueued == 0 ) {
break;
}
}
// none found, so use the oldest one
if (i == NUM_SOUNDBUFFERS)
{
i = oldestnum;
sound = &activeSounds[i];
}
// stop the sound with a FlushPackets
sound->m_pSourceVoice->Stop();
sound->m_pSourceVoice->FlushSourceBuffers();
// Set up packet
XAUDIO2_BUFFER Packet = { 0 };
Packet.Flags = XAUDIO2_END_OF_STREAM;
Packet.AudioBytes = lengths[id];
Packet.pAudioData = (BYTE*)S_sfx[id].data;
Packet.PlayBegin = 0;
Packet.PlayLength = 0;
Packet.LoopBegin = XAUDIO2_NO_LOOP_REGION;
Packet.LoopLength = 0;
Packet.LoopCount = 0;
Packet.pContext = NULL;
// Set voice volumes
sound->m_pSourceVoice->SetVolume( x_SoundVolume );
// Set voice pitch
sound->m_pSourceVoice->SetFrequencyRatio( 1 + ((float)pitch-128.f)/95.f );
// Set initial spatialization
if ( origin && origin != listener_origin ) {
// Update Emitter Position
sound->m_Emitter.Position.x = (float)(origin->x >> FRACBITS);
sound->m_Emitter.Position.y = 0.f;
sound->m_Emitter.Position.z = (float)(origin->y >> FRACBITS);
// Calculate 3D positioned speaker volumes
DWORD dwCalculateFlags = X3DAUDIO_CALCULATE_MATRIX;
X3DAudioCalculate( X3DAudioInstance, &doom_Listener, &sound->m_Emitter, dwCalculateFlags, &sound->m_DSPSettings );
// Pan the voice according to X3DAudio calculation
sound->m_pSourceVoice->SetOutputMatrix( NULL, 1, numOutputChannels, sound->m_DSPSettings.pMatrixCoefficients );
sound->localSound = false;
} else {
// Local(or Global) sound, fixed speaker volumes
sound->m_pSourceVoice->SetOutputMatrix( NULL, 1, numOutputChannels, localSoundVolumeEntries );
sound->localSound = true;
}
// Submit packet
HRESULT hr;
if( FAILED( hr = sound->m_pSourceVoice->SubmitSourceBuffer( &Packet ) ) ) {
int fail = 1;
}
// Play the source voice
if( FAILED( hr = sound->m_pSourceVoice->Start( 0 ) ) ) {
int fail = 1;
}
// set id, and start time
sound->id = id;
sound->start = ::g->gametic;
sound->valid = 1;
sound->player = player;
sound->originator = origin;
return id;
}
/*
======================
I_ProcessSoundEvents
======================
*/
void I_ProcessSoundEvents() {
for( int i = 0; i < 128; i++ ) {
if( soundEvents[i].pitch ) {
I_StartSound2( i, soundEvents[i].player, soundEvents[i].originator, soundEvents[i].listener, soundEvents[i].pitch, soundEvents[i].priority );
}
}
memset( soundEvents, 0, sizeof( soundEvents ) );
}
/*
======================
I_StartSound
======================
*/
int I_StartSound ( int id, mobj_t *origin, mobj_t *listener_origin, int vol, int pitch, int priority ) {
// only allow player 0s sounds in intermission and finale screens
if( ::g->gamestate != GS_LEVEL && DoomLib::GetPlayer() != 0 ) {
return 0;
}
// if we're only one player or we're trying to play the chainsaw sound, do it normal
// otherwise only allow one sound of each type per frame
if( PLAYERCOUNT == 1 || id == sfx_sawup || id == sfx_sawidl || id == sfx_sawful || id == sfx_sawhit ) {
return I_StartSound2( id, ::g->consoleplayer, origin, listener_origin, pitch, priority );
}
else {
if( soundEvents[ id ].vol < vol ) {
soundEvents[ id ].player = DoomLib::GetPlayer();
soundEvents[ id ].pitch = pitch;
soundEvents[ id ].priority = priority;
soundEvents[ id ].vol = vol;
soundEvents[ id ].originator = origin;
soundEvents[ id ].listener = listener_origin;
}
return id;
}
}
/*
======================
I_StopSound
======================
*/
void I_StopSound (int handle, int player)
{
// You need the handle returned by StartSound.
// Would be looping all channels,
// tracking down the handle,
// an setting the channel to zero.
int i;
activeSound_t* sound = 0;
for (i = 0; i < NUM_SOUNDBUFFERS; ++i)
{
sound = &activeSounds[i];
if (!sound->valid || sound->id != handle || (player >= 0 && sound->player != player) )
continue;
break;
}
if (i == NUM_SOUNDBUFFERS)
return;
// stop the sound
if ( sound->m_pSourceVoice != NULL ) {
sound->m_pSourceVoice->Stop( 0 );
}
sound->valid = 0;
sound->player = -1;
}
/*
======================
I_SoundIsPlaying
======================
*/
int I_SoundIsPlaying(int handle) {
if ( !soundHardwareInitialized ) {
return 0;
}
int i;
XAUDIO2_VOICE_STATE state;
activeSound_t* sound;
for (i = 0; i < NUM_SOUNDBUFFERS; ++i)
{
sound = &activeSounds[i];
if (!sound->valid || sound->id != handle)
continue;
sound->m_pSourceVoice->GetState( &state );
if ( state.BuffersQueued > 0 ) {
return 1;
}
}
return 0;
}
/*
======================
I_UpdateSound
======================
*/
// Update Listener Position and go through all the
// channels and update speaker volumes for 3D sound.
void I_UpdateSound() {
if ( !soundHardwareInitialized ) {
return;
}
int i;
XAUDIO2_VOICE_STATE state;
activeSound_t* sound;
for ( i=0; i < NUM_SOUNDBUFFERS; i++ ) {
sound = &activeSounds[i];
if ( !sound->valid || sound->localSound ) {
continue;
}
sound->m_pSourceVoice->GetState( &state );
if ( state.BuffersQueued > 0 ) {
mobj_t *playerObj = ::g->players[ sound->player ].mo;
// Update Listener Orientation and Position
angle_t pAngle = playerObj->angle;
fixed_t fx, fz;
pAngle >>= ANGLETOFINESHIFT;
fx = finecosine[pAngle];
fz = finesine[pAngle];
doom_Listener.OrientFront.x = (float)(fx) / 65535.f;
doom_Listener.OrientFront.y = 0.f;
doom_Listener.OrientFront.z = (float)(fz) / 65535.f;
doom_Listener.Position.x = (float)(playerObj->x >> FRACBITS);
doom_Listener.Position.y = 0.f;
doom_Listener.Position.z = (float)(playerObj->y >> FRACBITS);
// Update Emitter Position
sound->m_Emitter.Position.x = (float)(sound->originator->x >> FRACBITS);
sound->m_Emitter.Position.y = 0.f;
sound->m_Emitter.Position.z = (float)(sound->originator->y >> FRACBITS);
// Calculate 3D positioned speaker volumes
DWORD dwCalculateFlags = X3DAUDIO_CALCULATE_MATRIX;
X3DAudioCalculate( X3DAudioInstance, &doom_Listener, &sound->m_Emitter, dwCalculateFlags, &sound->m_DSPSettings );
// Pan the voice according to X3DAudio calculation
sound->m_pSourceVoice->SetOutputMatrix( NULL, 1, numOutputChannels, sound->m_DSPSettings.pMatrixCoefficients );
}
}
}
/*
======================
I_UpdateSoundParams
======================
*/
void I_UpdateSoundParams( int handle, int vol, int sep, int pitch) {
}
/*
======================
I_ShutdownSound
======================
*/
void I_ShutdownSound(void) {
int done = 0;
int i;
if ( S_initialized ) {
// Stop all sounds, but don't destroy the XAudio2 buffers.
for ( i = 0; i < NUM_SOUNDBUFFERS; ++i ) {
activeSound_t * sound = &activeSounds[i];
if ( sound == NULL ) {
continue;
}
I_StopSound( sound->id, 0 );
if ( sound->m_pSourceVoice ) {
sound->m_pSourceVoice->FlushSourceBuffers();
}
}
for (i=1 ; i<NUMSFX ; i++) {
if ( S_sfx[i].data && !(S_sfx[i].link) ) {
//Z_Free( S_sfx[i].data );
free( S_sfx[i].data );
}
}
}
I_StopSong( 0 );
S_initialized = 0;
// Done.
return;
}
/*
======================
I_InitSoundHardware
Called from the tech4x initialization code. Sets up Doom classic's
sound channels.
======================
*/
void I_InitSoundHardware( int numOutputChannels_, int channelMask ) {
::numOutputChannels = numOutputChannels_;
// Initialize the X3DAudio
// Speaker geometry configuration on the final mix, specifies assignment of channels
// to speaker positions, defined as per WAVEFORMATEXTENSIBLE.dwChannelMask
// SpeedOfSound - not used by doomclassic
X3DAudioInitialize( channelMask, 340.29f, X3DAudioInstance );
for ( int i = 0; i < NUM_SOUNDBUFFERS; ++i ) {
// Initialize source voices
I_InitSoundChannel( i, numOutputChannels );
}
I_InitMusic();
soundHardwareInitialized = true;
}
/*
======================
I_ShutdownitSoundHardware
Called from the tech4x shutdown code. Tears down Doom classic's
sound channels.
======================
*/
void I_ShutdownSoundHardware() {
soundHardwareInitialized = false;
I_ShutdownMusic();
for ( int i = 0; i < NUM_SOUNDBUFFERS; ++i ) {
activeSound_t * sound = &activeSounds[i];
if ( sound == NULL ) {
continue;
}
if ( sound->m_pSourceVoice ) {
sound->m_pSourceVoice->Stop();
sound->m_pSourceVoice->FlushSourceBuffers();
sound->m_pSourceVoice->DestroyVoice();
sound->m_pSourceVoice = NULL;
}
if ( sound->m_DSPSettings.pMatrixCoefficients ) {
delete [] sound->m_DSPSettings.pMatrixCoefficients;
sound->m_DSPSettings.pMatrixCoefficients = NULL;
}
}
}
/*
======================
I_InitSoundChannel
======================
*/
void I_InitSoundChannel( int channel, int numOutputChannels_ ) {
activeSound_t *soundchannel = &activeSounds[ channel ];
// RB: fixed non-aggregates cannot be initialized with initializer list
#if defined(USE_WINRT) //(_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
X3DAUDIO_VECTOR ZeroVector( 0.0f, 0.0f, 0.0f );
#else
X3DAUDIO_VECTOR ZeroVector = { 0.0f, 0.0f, 0.0f };
#endif
// RB end
// Set up emitter parameters
soundchannel->m_Emitter.OrientFront.x = 0.0f;
soundchannel->m_Emitter.OrientFront.y = 0.0f;
soundchannel->m_Emitter.OrientFront.z = 1.0f;
soundchannel->m_Emitter.OrientTop.x = 0.0f;
soundchannel->m_Emitter.OrientTop.y = 1.0f;
soundchannel->m_Emitter.OrientTop.z = 0.0f;
soundchannel->m_Emitter.Position = ZeroVector;
soundchannel->m_Emitter.Velocity = ZeroVector;
soundchannel->m_Emitter.pCone = &(soundchannel->m_Cone);
soundchannel->m_Emitter.pCone->InnerAngle = 0.0f; // Setting the inner cone angles to X3DAUDIO_2PI and
// outer cone other than 0 causes
// the emitter to act like a point emitter using the
// INNER cone settings only.
soundchannel->m_Emitter.pCone->OuterAngle = 0.0f; // Setting the outer cone angles to zero causes
// the emitter to act like a point emitter using the
// OUTER cone settings only.
soundchannel->m_Emitter.pCone->InnerVolume = 0.0f;
soundchannel->m_Emitter.pCone->OuterVolume = 1.0f;
soundchannel->m_Emitter.pCone->InnerLPF = 0.0f;
soundchannel->m_Emitter.pCone->OuterLPF = 1.0f;
soundchannel->m_Emitter.pCone->InnerReverb = 0.0f;
soundchannel->m_Emitter.pCone->OuterReverb = 1.0f;
soundchannel->m_Emitter.ChannelCount = 1;
soundchannel->m_Emitter.ChannelRadius = 0.0f;
soundchannel->m_Emitter.pVolumeCurve = NULL;
soundchannel->m_Emitter.pLFECurve = NULL;
soundchannel->m_Emitter.pLPFDirectCurve = NULL;
soundchannel->m_Emitter.pLPFReverbCurve = NULL;
soundchannel->m_Emitter.pReverbCurve = NULL;
soundchannel->m_Emitter.CurveDistanceScaler = 1200.0f;
soundchannel->m_Emitter.DopplerScaler = 1.0f;
soundchannel->m_Emitter.pChannelAzimuths = g_EmitterAzimuths;
soundchannel->m_DSPSettings.SrcChannelCount = 1;
soundchannel->m_DSPSettings.DstChannelCount = numOutputChannels_;
soundchannel->m_DSPSettings.pMatrixCoefficients = new FLOAT[ numOutputChannels_ ];
// Create Source voice
WAVEFORMATEX voiceFormat = {0};
voiceFormat.wFormatTag = WAVE_FORMAT_PCM;
voiceFormat.nChannels = 1;
voiceFormat.nSamplesPerSec = 11025;
voiceFormat.nAvgBytesPerSec = 11025;
voiceFormat.nBlockAlign = 1;
voiceFormat.wBitsPerSample = 8;
voiceFormat.cbSize = 0;
soundSystemLocal.hardware.GetIXAudio2()->CreateSourceVoice( &soundchannel->m_pSourceVoice, (WAVEFORMATEX *)&voiceFormat );
}
/*
======================
I_InitSound
======================
*/
void I_InitSound() {
if (S_initialized == 0) {
int i;
// RB: non-aggregates cannot be initialized with initializer list
#if defined(USE_WINRT) // (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
X3DAUDIO_VECTOR ZeroVector( 0.0f, 0.0f, 0.0f );
#else
X3DAUDIO_VECTOR ZeroVector = { 0.0f, 0.0f, 0.0f };
#endif
// RB end
// Set up listener parameters
doom_Listener.OrientFront.x = 0.0f;
doom_Listener.OrientFront.y = 0.0f;
doom_Listener.OrientFront.z = 1.0f;
doom_Listener.OrientTop.x = 0.0f;
doom_Listener.OrientTop.y = 1.0f;
doom_Listener.OrientTop.z = 0.0f;
doom_Listener.Position = ZeroVector;
doom_Listener.Velocity = ZeroVector;
for (i=1 ; i<NUMSFX ; i++)
{
// Alias? Example is the chaingun sound linked to pistol.
if (!S_sfx[i].link)
{
// Load data from WAD file.
S_sfx[i].data = getsfx( S_sfx[i].name, &lengths[i] );
}
else
{
// Previously loaded already?
S_sfx[i].data = S_sfx[i].link->data;
lengths[i] = lengths[(S_sfx[i].link - S_sfx)/sizeof(sfxinfo_t)];
}
}
S_initialized = 1;
}
}
/*
======================
I_SubmitSound
======================
*/
void I_SubmitSound(void)
{
// Only do this for player 0, it will still handle positioning
// for other players, but it can't be outside the game
// frame like the soundEvents are.
if ( DoomLib::GetPlayer() == 0 ) {
// Do 3D positioning of sounds
I_UpdateSound();
// Check for XMP notifications
I_UpdateMusic();
}
}
// =========================================================
// =========================================================
// Background Music
// =========================================================
// =========================================================
/*
======================
I_SetMusicVolume
======================
*/
void I_SetMusicVolume(int volume)
{
x_MusicVolume = (float)volume / 15.f;
}
/*
======================
I_InitMusic
======================
*/
void I_InitMusic(void)
{
if ( !Music_initialized ) {
// Initialize Timidity
Timidity_Init( MIDI_RATE, MIDI_FORMAT, MIDI_CHANNELS, MIDI_RATE, "classicmusic/gravis.cfg" );
hMusicThread = NULL;
musicBuffer = NULL;
totalBufferSize = 0;
waitingForMusic = false;
musicReady = false;
// Create Source voice
WAVEFORMATEX voiceFormat = {0};
voiceFormat.wFormatTag = WAVE_FORMAT_PCM;
voiceFormat.nChannels = 2;
voiceFormat.nSamplesPerSec = MIDI_RATE;
voiceFormat.nAvgBytesPerSec = MIDI_RATE * MIDI_FORMAT_BYTES * 2;
voiceFormat.nBlockAlign = MIDI_FORMAT_BYTES * 2;
voiceFormat.wBitsPerSample = MIDI_FORMAT_BYTES * 8;
voiceFormat.cbSize = 0;
// RB: XAUDIO2_VOICE_MUSIC not available on Windows 8 SDK
soundSystemLocal.hardware.GetIXAudio2()->CreateSourceVoice( &pMusicSourceVoice, (WAVEFORMATEX *)&voiceFormat/*, XAUDIO2_VOICE_MUSIC*/ );
// RB end
Music_initialized = true;
}
}
/*
======================
I_ShutdownMusic
======================
*/
void I_ShutdownMusic(void)
{
I_StopSong( 0 );
if ( Music_initialized ) {
if ( pMusicSourceVoice ) {
pMusicSourceVoice->Stop();
pMusicSourceVoice->FlushSourceBuffers();
pMusicSourceVoice->DestroyVoice();
pMusicSourceVoice = NULL;
}
if ( hMusicThread ) {
DWORD rc;
do {
GetExitCodeThread( hMusicThread, &rc );
if ( rc == STILL_ACTIVE ) {
Sleep( 1 );
}
} while( rc == STILL_ACTIVE );
CloseHandle( hMusicThread );
}
if ( musicBuffer ) {
free( musicBuffer );
}
Timidity_Shutdown();
}
pMusicSourceVoice = NULL;
hMusicThread = NULL;
musicBuffer = NULL;
totalBufferSize = 0;
waitingForMusic = false;
musicReady = false;
Music_initialized = false;
}
int Mus2Midi(unsigned char* bytes, unsigned char* out, int* len);
namespace {
const int MaxMidiConversionSize = 1024 * 1024;
unsigned char midiConversionBuffer[MaxMidiConversionSize];
}
/*
======================
I_LoadSong
======================
*/
DWORD WINAPI I_LoadSong( LPVOID songname ) {
idStr lumpName = "d_";
lumpName += static_cast< const char * >( songname );
unsigned char * musFile = static_cast< unsigned char * >( W_CacheLumpName( lumpName.c_str(), PU_STATIC_SHARED ) );
int length = 0;
Mus2Midi( musFile, midiConversionBuffer, &length );
doomMusic = Timidity_LoadSongMem( midiConversionBuffer, length );
if ( doomMusic ) {
musicBuffer = (byte *)malloc( MIDI_CHANNELS * MIDI_FORMAT_BYTES * doomMusic->samples );
totalBufferSize = doomMusic->samples * MIDI_CHANNELS * MIDI_FORMAT_BYTES;
Timidity_Start( doomMusic );
int rc = RC_NO_RETURN_VALUE;
int num_bytes = 0;
int offset = 0;
do {
rc = Timidity_PlaySome( musicBuffer + offset, MIDI_RATE, &num_bytes );
offset += num_bytes;
} while ( rc != RC_TUNE_END );
Timidity_Stop();
Timidity_FreeSong( doomMusic );
}
musicReady = true;
return ERROR_SUCCESS;
}
/*
======================
I_PlaySong
======================
*/
void I_PlaySong( const char *songname, int looping)
{
if ( !Music_initialized ) {
return;
}
if ( pMusicSourceVoice != NULL ) {
// Stop the voice and flush packets before freeing the musicBuffer
pMusicSourceVoice->Stop();
pMusicSourceVoice->FlushSourceBuffers();
}
// Make sure voice is stopped before we free the buffer
bool isStopped = false;
int d = 0;
while ( !isStopped ) {
XAUDIO2_VOICE_STATE test = {};
if ( pMusicSourceVoice != NULL ) {
pMusicSourceVoice->GetState( &test );
}
if ( test.pCurrentBufferContext == NULL && test.BuffersQueued == 0 ) {
isStopped = true;
}
//I_Printf( "waiting to stop (%d)\n", d++ );
}
// Clear old state
if ( musicBuffer != NULL ) {
free( musicBuffer );
musicBuffer = NULL;
}
musicReady = false;
I_LoadSong( (LPVOID)songname );
waitingForMusic = true;
if ( DoomLib::GetPlayer() >= 0 ) {
::g->mus_looping = looping;
}
}
/*
======================
I_UpdateMusic
======================
*/
void I_UpdateMusic() {
if ( !Music_initialized ) {
return;
}
if ( waitingForMusic ) {
if ( musicReady && pMusicSourceVoice != NULL ) {
if ( musicBuffer ) {
// Set up packet