-
Notifications
You must be signed in to change notification settings - Fork 411
/
Audio.h
830 lines (629 loc) · 29.6 KB
/
Audio.h
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
//--------------------------------------------------------------------------------------
// File: Audio.h
//
// DirectXTK for Audio header
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkId=248929
// http://go.microsoft.com/fwlink/?LinkID=615561
//--------------------------------------------------------------------------------------
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <objbase.h>
#include <mmreg.h>
#include <Audioclient.h>
#if (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX)
#include <xma2defs.h>
#ifdef _MSC_VER
#pragma comment(lib,"acphal.lib")
#endif
#endif
#ifndef XAUDIO2_HELPER_FUNCTIONS
#define XAUDIO2_HELPER_FUNCTIONS
#endif
#if defined(USING_XAUDIO2_REDIST) || (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/) || defined(_XBOX_ONE)
#define USING_XAUDIO2_9
#elif (_WIN32_WINNT >= 0x0603 /*_WIN32_WINNT_WINBLUE*/)
#define USING_XAUDIO2_8
#else
#error DirectX Tool Kit for Audio not supported on this platform
#endif
#include <xaudio2.h>
#include <xaudio2fx.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4619 4616 5246)
#endif
#include <x3daudio.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <xapofx.h>
#if !defined(USING_XAUDIO2_REDIST) && defined(_MSC_VER)
#if defined(USING_XAUDIO2_8) && defined(NTDDI_WIN10) && !defined(_M_IX86)
// The xaudio2_8.lib in the Windows 10 SDK for x86 is incorrectly annotated as __cdecl instead of __stdcall, so avoid using it in this case.
#pragma comment(lib,"xaudio2_8.lib")
#else
#pragma comment(lib,"xaudio2.lib")
#endif
#endif
#include <DirectXMath.h>
namespace DirectX
{
class SoundEffectInstance;
class SoundStreamInstance;
//----------------------------------------------------------------------------------
struct AudioStatistics
{
size_t playingOneShots; // Number of one-shot sounds currently playing
size_t playingInstances; // Number of sound effect instances currently playing
size_t allocatedInstances; // Number of SoundEffectInstance allocated
size_t allocatedVoices; // Number of XAudio2 voices allocated (standard, 3D, one-shots, and idle one-shots)
size_t allocatedVoices3d; // Number of XAudio2 voices allocated for 3D
size_t allocatedVoicesOneShot; // Number of XAudio2 voices allocated for one-shot sounds
size_t allocatedVoicesIdle; // Number of XAudio2 voices allocated for one-shot sounds but not currently in use
size_t audioBytes; // Total wave data (in bytes) in SoundEffects and in-memory WaveBanks
#if (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX)
size_t xmaAudioBytes; // Total wave data (in bytes) in SoundEffects and in-memory WaveBanks allocated with ApuAlloc
#endif
size_t streamingBytes; // Total size of streaming buffers (in bytes) in streaming WaveBanks
};
//----------------------------------------------------------------------------------
class IVoiceNotify
{
public:
virtual ~IVoiceNotify() = default;
IVoiceNotify(const IVoiceNotify&) = delete;
IVoiceNotify& operator=(const IVoiceNotify&) = delete;
IVoiceNotify(IVoiceNotify&&) = default;
IVoiceNotify& operator=(IVoiceNotify&&) = default;
virtual void __cdecl OnBufferEnd() = 0;
// Notfication that a voice buffer has finished
// Note this is called from XAudio2's worker thread, so it should perform very minimal and thread-safe operations
virtual void __cdecl OnCriticalError() = 0;
// Notification that the audio engine encountered a critical error
virtual void __cdecl OnReset() = 0;
// Notification of an audio engine reset
virtual void __cdecl OnUpdate() = 0;
// Notification of an audio engine per-frame update (opt-in)
virtual void __cdecl OnDestroyEngine() noexcept = 0;
// Notification that the audio engine is being destroyed
virtual void __cdecl OnTrim() = 0;
// Notification of a request to trim the voice pool
virtual void __cdecl GatherStatistics(AudioStatistics& stats) const = 0;
// Contribute to statistics request
virtual void __cdecl OnDestroyParent() noexcept = 0;
// Optional notification used by some objects
protected:
IVoiceNotify() = default;
};
//----------------------------------------------------------------------------------
enum AUDIO_ENGINE_FLAGS : uint32_t
{
AudioEngine_Default = 0x0,
AudioEngine_EnvironmentalReverb = 0x1,
AudioEngine_ReverbUseFilters = 0x2,
AudioEngine_UseMasteringLimiter = 0x4,
AudioEngine_Debug = 0x10000,
AudioEngine_ThrowOnNoAudioHW = 0x20000,
AudioEngine_DisableVoiceReuse = 0x40000,
};
enum SOUND_EFFECT_INSTANCE_FLAGS : uint32_t
{
SoundEffectInstance_Default = 0x0,
SoundEffectInstance_Use3D = 0x1,
SoundEffectInstance_ReverbUseFilters = 0x2,
SoundEffectInstance_NoSetPitch = 0x4,
SoundEffectInstance_UseRedirectLFE = 0x10000,
};
enum AUDIO_ENGINE_REVERB : uint32_t
{
Reverb_Off,
Reverb_Default,
Reverb_Generic,
Reverb_Forest,
Reverb_PaddedCell,
Reverb_Room,
Reverb_Bathroom,
Reverb_LivingRoom,
Reverb_StoneRoom,
Reverb_Auditorium,
Reverb_ConcertHall,
Reverb_Cave,
Reverb_Arena,
Reverb_Hangar,
Reverb_CarpetedHallway,
Reverb_Hallway,
Reverb_StoneCorridor,
Reverb_Alley,
Reverb_City,
Reverb_Mountains,
Reverb_Quarry,
Reverb_Plain,
Reverb_ParkingLot,
Reverb_SewerPipe,
Reverb_Underwater,
Reverb_SmallRoom,
Reverb_MediumRoom,
Reverb_LargeRoom,
Reverb_MediumHall,
Reverb_LargeHall,
Reverb_Plate,
Reverb_MAX
};
enum SoundState : uint32_t
{
STOPPED = 0,
PLAYING,
PAUSED
};
//----------------------------------------------------------------------------------
class AudioEngine
{
public:
explicit AudioEngine(
AUDIO_ENGINE_FLAGS flags = AudioEngine_Default,
_In_opt_ const WAVEFORMATEX* wfx = nullptr,
_In_opt_z_ const wchar_t* deviceId = nullptr,
AUDIO_STREAM_CATEGORY category = AudioCategory_GameEffects) noexcept(false);
AudioEngine(AudioEngine&&) noexcept;
AudioEngine& operator= (AudioEngine&&) noexcept;
AudioEngine(AudioEngine const&) = delete;
AudioEngine& operator= (AudioEngine const&) = delete;
virtual ~AudioEngine();
bool __cdecl Update();
// Performs per-frame processing for the audio engine, returns false if in 'silent mode'
bool __cdecl Reset(_In_opt_ const WAVEFORMATEX* wfx = nullptr, _In_opt_z_ const wchar_t* deviceId = nullptr);
// Reset audio engine from critical error/silent mode using a new device; can also 'migrate' the graph
// Returns true if succesfully reset, false if in 'silent mode' due to no default device
// Note: One shots are lost, all SoundEffectInstances are in the STOPPED state after successful reset
void __cdecl Suspend() noexcept;
void __cdecl Resume();
// Suspend/resumes audio processing (i.e. global pause/resume)
float __cdecl GetMasterVolume() const noexcept;
void __cdecl SetMasterVolume(float volume);
// Master volume property for all sounds
void __cdecl SetReverb(AUDIO_ENGINE_REVERB reverb);
void __cdecl SetReverb(_In_opt_ const XAUDIO2FX_REVERB_PARAMETERS* native);
// Sets environmental reverb for 3D positional audio (if active)
void __cdecl SetMasteringLimit(int release, int loudness);
// Sets the mastering volume limiter properties (if active)
AudioStatistics __cdecl GetStatistics() const;
// Gathers audio engine statistics
WAVEFORMATEXTENSIBLE __cdecl GetOutputFormat() const noexcept;
// Returns the format of the audio output device associated with the mastering voice.
uint32_t __cdecl GetChannelMask() const noexcept;
// Returns the output channel mask
int __cdecl GetOutputSampleRate() const noexcept;
// Returns the sample rate going into the mastering voice
unsigned int __cdecl GetOutputChannels() const noexcept;
// Returns the number of channels going into the mastering voice
bool __cdecl IsAudioDevicePresent() const noexcept;
// Returns true if the audio graph is operating normally, false if in 'silent mode'
bool __cdecl IsCriticalError() const noexcept;
// Returns true if the audio graph is halted due to a critical error (which also places the engine into 'silent mode')
// Voice pool management.
void __cdecl SetDefaultSampleRate(int sampleRate);
// Sample rate for voices in the reuse pool (defaults to 44100)
void __cdecl SetMaxVoicePool(size_t maxOneShots, size_t maxInstances);
// Maximum number of voices to allocate for one-shots and instances
// Note: one-shots over this limit are ignored; too many instance voices throws an exception
void __cdecl TrimVoicePool();
// Releases any currently unused voices
// Internal-use functions
void __cdecl AllocateVoice(_In_ const WAVEFORMATEX* wfx,
SOUND_EFFECT_INSTANCE_FLAGS flags, bool oneshot, _Outptr_result_maybenull_ IXAudio2SourceVoice** voice);
void __cdecl DestroyVoice(_In_ IXAudio2SourceVoice* voice) noexcept;
// Should only be called for instance voices, not one-shots
void __cdecl RegisterNotify(_In_ IVoiceNotify* notify, bool usesUpdate);
void __cdecl UnregisterNotify(_In_ IVoiceNotify* notify, bool usesOneShots, bool usesUpdate);
// XAudio2 interface access
IXAudio2* __cdecl GetInterface() const noexcept;
IXAudio2MasteringVoice* __cdecl GetMasterVoice() const noexcept;
IXAudio2SubmixVoice* __cdecl GetReverbVoice() const noexcept;
X3DAUDIO_HANDLE& __cdecl Get3DHandle() const noexcept;
// Static functions
struct RendererDetail
{
std::wstring deviceId;
std::wstring description;
};
static std::vector<RendererDetail> __cdecl GetRendererDetails();
// Returns a list of valid audio endpoint devices
#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
explicit AudioEngine(
AUDIO_ENGINE_FLAGS flags = AudioEngine_Default,
_In_opt_ const WAVEFORMATEX* wfx = nullptr,
_In_opt_z_ const __wchar_t* deviceId = nullptr,
AUDIO_STREAM_CATEGORY category = AudioCategory_GameEffects) noexcept(false);
bool __cdecl Reset(_In_opt_ const WAVEFORMATEX* wfx = nullptr, _In_opt_z_ const __wchar_t* deviceId = nullptr);
#endif
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
};
//----------------------------------------------------------------------------------
class WaveBank
{
public:
WaveBank(_In_ AudioEngine* engine, _In_z_ const wchar_t* wbFileName);
WaveBank(WaveBank&&) noexcept;
WaveBank& operator= (WaveBank&&) noexcept;
WaveBank(WaveBank const&) = delete;
WaveBank& operator= (WaveBank const&) = delete;
virtual ~WaveBank();
void __cdecl Play(unsigned int index);
void __cdecl Play(unsigned int index, float volume, float pitch, float pan);
void __cdecl Play(_In_z_ const char* name);
void __cdecl Play(_In_z_ const char* name, float volume, float pitch, float pan);
std::unique_ptr<SoundEffectInstance> __cdecl CreateInstance(unsigned int index,
SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
std::unique_ptr<SoundEffectInstance> __cdecl CreateInstance(_In_z_ const char* name,
SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
std::unique_ptr<SoundStreamInstance> __cdecl CreateStreamInstance(unsigned int index,
SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
std::unique_ptr<SoundStreamInstance> __cdecl CreateStreamInstance(_In_z_ const char* name,
SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
bool __cdecl IsPrepared() const noexcept;
bool __cdecl IsInUse() const noexcept;
bool __cdecl IsStreamingBank() const noexcept;
bool __cdecl IsAdvancedFormat() const noexcept;
size_t __cdecl GetSampleSizeInBytes(unsigned int index) const noexcept;
// Returns size of wave audio data
size_t __cdecl GetSampleDuration(unsigned int index) const noexcept;
// Returns the duration in samples
size_t __cdecl GetSampleDurationMS(unsigned int index) const noexcept;
// Returns the duration in milliseconds
const WAVEFORMATEX* __cdecl GetFormat(unsigned int index, _Out_writes_bytes_(maxsize) WAVEFORMATEX* wfx, size_t maxsize) const noexcept;
int __cdecl Find(_In_z_ const char* name) const;
#ifdef USING_XAUDIO2_9
bool __cdecl FillSubmitBuffer(unsigned int index, _Out_ XAUDIO2_BUFFER& buffer, _Out_ XAUDIO2_BUFFER_WMA& wmaBuffer) const;
#else
void __cdecl FillSubmitBuffer(unsigned int index, _Out_ XAUDIO2_BUFFER& buffer) const;
#endif
void __cdecl UnregisterInstance(_In_ IVoiceNotify* instance);
HANDLE __cdecl GetAsyncHandle() const noexcept;
bool __cdecl GetPrivateData(unsigned int index, _Out_writes_bytes_(datasize) void* data, size_t datasize);
#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
WaveBank(_In_ AudioEngine* engine, _In_z_ const __wchar_t* wbFileName);
#endif
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
};
//----------------------------------------------------------------------------------
class SoundEffect
{
public:
SoundEffect(_In_ AudioEngine* engine, _In_z_ const wchar_t* waveFileName);
SoundEffect(_In_ AudioEngine* engine, _Inout_ std::unique_ptr<uint8_t[]>& wavData,
_In_ const WAVEFORMATEX* wfx, _In_reads_bytes_(audioBytes) const uint8_t* startAudio, size_t audioBytes);
SoundEffect(_In_ AudioEngine* engine, _Inout_ std::unique_ptr<uint8_t[]>& wavData,
_In_ const WAVEFORMATEX* wfx, _In_reads_bytes_(audioBytes) const uint8_t* startAudio, size_t audioBytes,
uint32_t loopStart, uint32_t loopLength);
#ifdef USING_XAUDIO2_9
SoundEffect(_In_ AudioEngine* engine, _Inout_ std::unique_ptr<uint8_t[]>& wavData,
_In_ const WAVEFORMATEX* wfx, _In_reads_bytes_(audioBytes) const uint8_t* startAudio, size_t audioBytes,
_In_reads_(seekCount) const uint32_t* seekTable, size_t seekCount);
#endif
SoundEffect(SoundEffect&&) noexcept;
SoundEffect& operator= (SoundEffect&&) noexcept;
SoundEffect(SoundEffect const&) = delete;
SoundEffect& operator= (SoundEffect const&) = delete;
virtual ~SoundEffect();
void __cdecl Play();
void __cdecl Play(float volume, float pitch, float pan);
std::unique_ptr<SoundEffectInstance> __cdecl CreateInstance(SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
bool __cdecl IsInUse() const noexcept;
size_t __cdecl GetSampleSizeInBytes() const noexcept;
// Returns size of wave audio data
size_t __cdecl GetSampleDuration() const noexcept;
// Returns the duration in samples
size_t __cdecl GetSampleDurationMS() const noexcept;
// Returns the duration in milliseconds
const WAVEFORMATEX* __cdecl GetFormat() const noexcept;
#ifdef USING_XAUDIO2_9
bool __cdecl FillSubmitBuffer(_Out_ XAUDIO2_BUFFER& buffer, _Out_ XAUDIO2_BUFFER_WMA& wmaBuffer) const;
#else
void __cdecl FillSubmitBuffer(_Out_ XAUDIO2_BUFFER& buffer) const;
#endif
void __cdecl UnregisterInstance(_In_ IVoiceNotify* instance);
#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)
SoundEffect(_In_ AudioEngine* engine, _In_z_ const __wchar_t* waveFileName);
#endif
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
};
//----------------------------------------------------------------------------------
struct AudioListener : public X3DAUDIO_LISTENER
{
X3DAUDIO_CONE ListenerCone;
AudioListener() noexcept :
X3DAUDIO_LISTENER{},
ListenerCone{}
{
OrientFront.z = -1.f;
OrientTop.y = 1.f;
}
void XM_CALLCONV SetPosition(FXMVECTOR v) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Position), v);
}
void __cdecl SetPosition(const XMFLOAT3& pos) noexcept
{
Position.x = pos.x;
Position.y = pos.y;
Position.z = pos.z;
}
void XM_CALLCONV SetVelocity(FXMVECTOR v) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Velocity), v);
}
void __cdecl SetVelocity(const XMFLOAT3& vel) noexcept
{
Velocity.x = vel.x;
Velocity.y = vel.y;
Velocity.z = vel.z;
}
void XM_CALLCONV SetOrientation(FXMVECTOR forward, FXMVECTOR up) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), forward);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), up);
}
void __cdecl SetOrientation(const XMFLOAT3& forward, const XMFLOAT3& up) noexcept
{
OrientFront.x = forward.x; OrientTop.x = up.x;
OrientFront.y = forward.y; OrientTop.y = up.y;
OrientFront.z = forward.z; OrientTop.z = up.z;
}
void XM_CALLCONV SetOrientationFromQuaternion(FXMVECTOR quat) noexcept
{
const XMVECTOR forward = XMVector3Rotate(g_XMIdentityR2, quat);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), forward);
const XMVECTOR up = XMVector3Rotate(g_XMIdentityR1, quat);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), up);
}
// Updates velocity and orientation by tracking changes in position over time.
void XM_CALLCONV Update(FXMVECTOR newPos, XMVECTOR upDir, float dt) noexcept
{
if (dt > 0.f)
{
const XMVECTOR lastPos = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(&Position));
XMVECTOR vDelta = XMVectorSubtract(newPos, lastPos);
const XMVECTOR vt = XMVectorReplicate(dt);
XMVECTOR v = XMVectorDivide(vDelta, vt);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Velocity), v);
vDelta = XMVector3Normalize(vDelta);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), vDelta);
v = XMVector3Cross(upDir, vDelta);
v = XMVector3Normalize(v);
v = XMVector3Cross(vDelta, v);
v = XMVector3Normalize(v);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), v);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Position), newPos);
}
}
void __cdecl SetOmnidirectional() noexcept
{
pCone = nullptr;
}
void __cdecl SetCone(const X3DAUDIO_CONE& listenerCone);
bool __cdecl IsValid() const;
};
//----------------------------------------------------------------------------------
struct AudioEmitter : public X3DAUDIO_EMITTER
{
X3DAUDIO_CONE EmitterCone;
float EmitterAzimuths[XAUDIO2_MAX_AUDIO_CHANNELS];
AudioEmitter() noexcept :
X3DAUDIO_EMITTER{},
EmitterCone{},
EmitterAzimuths{}
{
OrientFront.z = -1.f;
OrientTop.y =
ChannelRadius =
CurveDistanceScaler =
DopplerScaler = 1.f;
ChannelCount = 1;
pChannelAzimuths = EmitterAzimuths;
InnerRadiusAngle = X3DAUDIO_PI / 4.0f;
}
void XM_CALLCONV SetPosition(FXMVECTOR v) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Position), v);
}
void __cdecl SetPosition(const XMFLOAT3& pos) noexcept
{
Position.x = pos.x;
Position.y = pos.y;
Position.z = pos.z;
}
void XM_CALLCONV SetVelocity(FXMVECTOR v) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Velocity), v);
}
void __cdecl SetVelocity(const XMFLOAT3& vel) noexcept
{
Velocity.x = vel.x;
Velocity.y = vel.y;
Velocity.z = vel.z;
}
void XM_CALLCONV SetOrientation(FXMVECTOR forward, FXMVECTOR up) noexcept
{
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), forward);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), up);
}
void __cdecl SetOrientation(const XMFLOAT3& forward, const XMFLOAT3& up) noexcept
{
OrientFront.x = forward.x; OrientTop.x = up.x;
OrientFront.y = forward.y; OrientTop.y = up.y;
OrientFront.z = forward.z; OrientTop.z = up.z;
}
void XM_CALLCONV SetOrientationFromQuaternion(FXMVECTOR quat) noexcept
{
const XMVECTOR forward = XMVector3Rotate(g_XMIdentityR2, quat);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), forward);
const XMVECTOR up = XMVector3Rotate(g_XMIdentityR1, quat);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), up);
}
// Updates velocity and orientation by tracking changes in position over time.
void XM_CALLCONV Update(FXMVECTOR newPos, XMVECTOR upDir, float dt) noexcept
{
if (dt > 0.f)
{
const XMVECTOR lastPos = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(&Position));
XMVECTOR vDelta = XMVectorSubtract(newPos, lastPos);
const XMVECTOR vt = XMVectorReplicate(dt);
XMVECTOR v = XMVectorDivide(vDelta, vt);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Velocity), v);
vDelta = XMVector3Normalize(vDelta);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientFront), vDelta);
v = XMVector3Cross(upDir, vDelta);
v = XMVector3Normalize(v);
v = XMVector3Cross(vDelta, v);
v = XMVector3Normalize(v);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&OrientTop), v);
XMStoreFloat3(reinterpret_cast<XMFLOAT3*>(&Position), newPos);
}
}
void __cdecl SetOmnidirectional() noexcept
{
pCone = nullptr;
}
// Only used for single-channel emitters.
void __cdecl SetCone(const X3DAUDIO_CONE& emitterCone);
// Set multi-channel emitter azimuths based on speaker configuration geometry.
void __cdecl EnableDefaultMultiChannel(unsigned int channels, float radius = 1.f);
// Set default volume, LFE, LPF, and reverb curves.
void __cdecl EnableDefaultCurves() noexcept;
void __cdecl EnableLinearCurves() noexcept;
void __cdecl EnableInverseSquareCurves() noexcept
{
pVolumeCurve = nullptr;
pLFECurve = nullptr;
pLPFDirectCurve = nullptr;
pLPFReverbCurve = nullptr;
pReverbCurve = nullptr;
}
bool __cdecl IsValid() const;
};
//----------------------------------------------------------------------------------
class SoundEffectInstance
{
public:
SoundEffectInstance(SoundEffectInstance&&) noexcept;
SoundEffectInstance& operator= (SoundEffectInstance&&) noexcept;
SoundEffectInstance(SoundEffectInstance const&) = delete;
SoundEffectInstance& operator= (SoundEffectInstance const&) = delete;
virtual ~SoundEffectInstance();
void __cdecl Play(bool loop = false);
void __cdecl Stop(bool immediate = true) noexcept;
void __cdecl Pause() noexcept;
void __cdecl Resume();
void __cdecl SetVolume(float volume);
void __cdecl SetPitch(float pitch);
void __cdecl SetPan(float pan);
void __cdecl Apply3D(const X3DAUDIO_LISTENER& listener, const X3DAUDIO_EMITTER& emitter, bool rhcoords = true);
bool __cdecl IsLooped() const noexcept;
SoundState __cdecl GetState() noexcept;
unsigned int __cdecl GetChannelCount() const noexcept;
IVoiceNotify* __cdecl GetVoiceNotify() const noexcept;
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
// Private constructors
SoundEffectInstance(_In_ AudioEngine* engine, _In_ SoundEffect* effect, SOUND_EFFECT_INSTANCE_FLAGS flags);
SoundEffectInstance(_In_ AudioEngine* engine, _In_ WaveBank* effect, unsigned int index, SOUND_EFFECT_INSTANCE_FLAGS flags);
friend std::unique_ptr<SoundEffectInstance> __cdecl SoundEffect::CreateInstance(SOUND_EFFECT_INSTANCE_FLAGS);
friend std::unique_ptr<SoundEffectInstance> __cdecl WaveBank::CreateInstance(unsigned int, SOUND_EFFECT_INSTANCE_FLAGS);
};
//----------------------------------------------------------------------------------
class SoundStreamInstance
{
public:
SoundStreamInstance(SoundStreamInstance&&) noexcept;
SoundStreamInstance& operator= (SoundStreamInstance&&) noexcept;
SoundStreamInstance(SoundStreamInstance const&) = delete;
SoundStreamInstance& operator= (SoundStreamInstance const&) = delete;
virtual ~SoundStreamInstance();
void __cdecl Play(bool loop = false);
void __cdecl Stop(bool immediate = true) noexcept;
void __cdecl Pause() noexcept;
void __cdecl Resume();
void __cdecl SetVolume(float volume);
void __cdecl SetPitch(float pitch);
void __cdecl SetPan(float pan);
void __cdecl Apply3D(const X3DAUDIO_LISTENER& listener, const X3DAUDIO_EMITTER& emitter, bool rhcoords = true);
bool __cdecl IsLooped() const noexcept;
SoundState __cdecl GetState() noexcept;
unsigned int __cdecl GetChannelCount() const noexcept;
IVoiceNotify* __cdecl GetVoiceNotify() const noexcept;
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
// Private constructors
SoundStreamInstance(_In_ AudioEngine* engine, _In_ WaveBank* effect, unsigned int index, SOUND_EFFECT_INSTANCE_FLAGS flags);
friend std::unique_ptr<SoundStreamInstance> __cdecl WaveBank::CreateStreamInstance(unsigned int, SOUND_EFFECT_INSTANCE_FLAGS);
};
//----------------------------------------------------------------------------------
class DynamicSoundEffectInstance
{
public:
DynamicSoundEffectInstance(_In_ AudioEngine* engine,
_In_ std::function<void __cdecl(DynamicSoundEffectInstance*)> bufferNeeded,
int sampleRate, int channels, int sampleBits = 16,
SOUND_EFFECT_INSTANCE_FLAGS flags = SoundEffectInstance_Default);
DynamicSoundEffectInstance(DynamicSoundEffectInstance&&) noexcept;
DynamicSoundEffectInstance& operator= (DynamicSoundEffectInstance&&) noexcept;
DynamicSoundEffectInstance(DynamicSoundEffectInstance const&) = delete;
DynamicSoundEffectInstance& operator= (DynamicSoundEffectInstance const&) = delete;
virtual ~DynamicSoundEffectInstance();
void __cdecl Play();
void __cdecl Stop(bool immediate = true) noexcept;
void __cdecl Pause() noexcept;
void __cdecl Resume();
void __cdecl SetVolume(float volume);
void __cdecl SetPitch(float pitch);
void __cdecl SetPan(float pan);
void __cdecl Apply3D(const X3DAUDIO_LISTENER& listener, const X3DAUDIO_EMITTER& emitter, bool rhcoords = true);
void __cdecl SubmitBuffer(_In_reads_bytes_(audioBytes) const uint8_t* pAudioData, size_t audioBytes);
void __cdecl SubmitBuffer(_In_reads_bytes_(audioBytes) const uint8_t* pAudioData, uint32_t offset, size_t audioBytes);
SoundState __cdecl GetState() noexcept;
size_t __cdecl GetSampleDuration(size_t bytes) const noexcept;
// Returns duration in samples of a buffer of a given size
size_t __cdecl GetSampleDurationMS(size_t bytes) const noexcept;
// Returns duration in milliseconds of a buffer of a given size
size_t __cdecl GetSampleSizeInBytes(uint64_t duration) const noexcept;
// Returns size of a buffer for a duration given in milliseconds
int __cdecl GetPendingBufferCount() const noexcept;
const WAVEFORMATEX* __cdecl GetFormat() const noexcept;
unsigned int __cdecl GetChannelCount() const noexcept;
private:
// Private implementation.
class Impl;
std::unique_ptr<Impl> pImpl;
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#endif
DEFINE_ENUM_FLAG_OPERATORS(AUDIO_ENGINE_FLAGS)
DEFINE_ENUM_FLAG_OPERATORS(SOUND_EFFECT_INSTANCE_FLAGS)
#ifdef __clang__
#pragma clang diagnostic pop
#endif
}