-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound_manager.cpp
More file actions
666 lines (582 loc) · 15.6 KB
/
Copy pathsound_manager.cpp
File metadata and controls
666 lines (582 loc) · 15.6 KB
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
#include "winner.h"
#include "sound_manager.h"
#include "assertions.h"
#include "utils.h"
#include "os_utils.h"
#if defined _DEBUG && !defined NDEBUG
# include <iostream>
#endif // _DEBUG
#pragma comment( lib, "xaudio2_8.lib" )
namespace mwrl = Microsoft::WRL;
namespace wav
{
// wav properties - all sounds must have the same format
static constexpr WORD nChannelsPerSound = 2u;
static constexpr DWORD nSamplesPerSec = 48000u; // valid: 44100u, 48000u, 96000u
static constexpr WORD nBitsPerSample = 16u;
}// wav
SoundManager::SoundManager( WAVEFORMATEXTENSIBLE* format )
{
// keep a pointer to the format
m_pFormat = format;
HRESULT hres;
hres = XAudio2Create( &m_pXAudio2,
0u,
XAUDIO2_DEFAULT_PROCESSOR );
ASSERT_HRES_IF_FAILED;
hres = m_pXAudio2->CreateMasteringVoice( &m_pMasterVoice );
ASSERT_HRES_IF_FAILED;
// create the channels
m_idleChannels.reserve( m_nMaxAudioChannels );
for ( size_t i = 0; i < m_nMaxAudioChannels; ++i )
{
m_idleChannels.emplace_back( std::make_unique<Channel>() );
}
m_submixes.reserve( m_nMaxSubmixes );
for ( size_t i = 0; i < m_nMaxSubmixes; ++i )
{
m_submixes.emplace_back( std::make_unique<Submix>() );
}
}
SoundManager::~SoundManager() noexcept
{
std::unique_lock<std::mutex> ul{m_mu};
m_occupiedChannels.clear();
m_idleChannels.clear();
m_submixes.clear();
m_pMasterVoice->DestroyVoice();
m_pMasterVoice = nullptr;
}
SoundManager::Channel::~Channel() noexcept
{
if ( m_pSourceVoice || m_pSound )
{
m_pSourceVoice->DestroyVoice();
m_pSourceVoice = nullptr;
m_pSound = nullptr;
}
}
SoundManager::Channel::Channel( Channel&& rhs ) cond_noex
:
m_pSourceVoice{rhs.m_pSourceVoice},
m_pSound{rhs.m_pSound}
{
rhs.m_pSourceVoice = nullptr;
rhs.m_pSound = nullptr;
}
auto SoundManager::Channel::operator=( Channel &&rhs ) cond_noex -> Channel&
{
Channel tmp{std::move( rhs )};
std::swap( *this,
tmp );
return *this;
}
bool SoundManager::Channel::setupChannel( SoundManager& soundManager,
Sound& sound )
{
m_pSound = &sound;
class VoiceCallback
: public IXAudio2VoiceCallback
{
public:
// Called when the voice is about to start processing a new audio buffer.
void STDMETHODCALLTYPE OnBufferStart( void* pBufferContext ) override
{
pass_;
}
// Called when the voice finishes processing a buffer.
void STDMETHODCALLTYPE OnBufferEnd( void* pBufferContext ) override
{
Channel& channel = *reinterpret_cast<Channel*>( pBufferContext );
channel.stopSound();
{
//std::unique_lock<std::mutex> lg{channel.m_pSound->m_mu};
// clear Sound's channel if it exists
channel.m_pSound->m_busyChannels.erase(
std::find( channel.m_pSound->m_busyChannels.begin(),
channel.m_pSound->m_busyChannels.end(),
&channel ) );
// notify any thread that might be waiting for activeChannels
// to become zero (i.e. thread calling destructor)
channel.m_pSound->m_condVar.notify_all();
}
SoundManager::getInstance().deactivateChannel( channel );
}
// Called when the voice reaches the end position of a loop.
void STDMETHODCALLTYPE OnLoopEnd( void* pBufferContext ) override
{
pass_;
}
// Called when the voice has just finished playing a contiguous audio stream.
void STDMETHODCALLTYPE OnStreamEnd() override
{
pass_;
}
// Called when a critical error occurs during voice processing.
void STDMETHODCALLTYPE OnVoiceError( void* pBufferContext,
HRESULT Error ) override
{
pass_;
}
// Called just after the processing pass for the voice ends.
void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() override
{
pass_;
}
// Called during each processing pass for each voice, just
void STDMETHODCALLTYPE OnVoiceProcessingPassStart( UINT32 bytesRequired ) override
{
pass_;
}
};
static VoiceCallback voiceCallback;
m_pSound->m_pXaudioBuffer->pContext = this;
HRESULT hres;
UINT32 sourceVoiceCreationFlags = 0u;
auto& waveFormat = m_pSound->m_pWaveFormat;
// 5. optional - specify an output (submix) voice for this source voice
const auto& soundSubmixName = sound.getSubmixName();
if ( soundSubmixName != "" )
{
//std::unique_lock<std::mutex> lg{soundManager.m_mu, std::try_to_lock}; // already locked!
// grab a submix from the bag and fill it up
auto& newSubmix = soundManager.m_submixes.back();
newSubmix->setName( soundSubmixName );
soundManager.m_pXAudio2->CreateSubmixVoice( &newSubmix->m_pSubmixVoice,
waveFormat->Format.nChannels,
waveFormat->Format.nSamplesPerSec,
0u,
0u,
nullptr,
nullptr );
// create the voice sends structure
newSubmix->m_outputVoiceSendDesc = {0, newSubmix->m_pSubmixVoice};
newSubmix->m_outputVoiceSends = {1, &newSubmix->m_outputVoiceSendDesc};
// 6. Create the source voice
hres = soundManager.m_pXAudio2->CreateSourceVoice( &m_pSourceVoice,
(WAVEFORMATEX*)waveFormat.get(),
sourceVoiceCreationFlags,
XAUDIO2_DEFAULT_FREQ_RATIO,
&voiceCallback,
&newSubmix->m_outputVoiceSends,
nullptr );
}
else
{
// 6. Create the source voice
hres = soundManager.m_pXAudio2->CreateSourceVoice( &m_pSourceVoice,
(WAVEFORMATEX*)waveFormat.get(),
sourceVoiceCreationFlags,
XAUDIO2_DEFAULT_FREQ_RATIO,
&voiceCallback,
nullptr,
nullptr );
}
ASSERT_HRES_IF_FAILED;
// set steady sample rate
if ( waveFormat->Format.nSamplesPerSec != wav::nSamplesPerSec )
{
waveFormat->Format.nSamplesPerSec = wav::nSamplesPerSec;
hres = m_pSourceVoice->SetSourceSampleRate( wav::nSamplesPerSec );
ASSERT_HRES_IF_FAILED;
}
// 6. submit the XAUDIO2_BUFFER to the source voice
hres = m_pSourceVoice->SubmitSourceBuffer( m_pSound->m_pXaudioBuffer.get() );
ASSERT_HRES_IF_FAILED;
return true;
}
void SoundManager::Channel::playSound( Sound* sound,
float volume )
{
ASSERT( m_pSound, "Null Sound!" );
ASSERT( m_pSourceVoice, "Null Voice!" );
sound->m_busyChannels.emplace_back( this );
HRESULT hres = m_pSourceVoice->SetVolume( volume );
ASSERT_HRES_IF_FAILED;
hres = m_pSourceVoice->Start( 0u );
ASSERT_HRES_IF_FAILED;
}
void SoundManager::Channel::stopSound() cond_noex
{
ASSERT( m_pSound, "Sound was not initialized!" );
ASSERT( m_pSourceVoice, "Voice was not set!" );
m_pSourceVoice->Stop();
m_pSourceVoice->FlushSourceBuffers();
}
void SoundManager::Channel::rechannel( const Sound* pOldSound,
Sound* pNewSound )
{
ASSERT( pOldSound == pNewSound, "Channel mismatch!" );
m_pSound = pNewSound;
}
Sound* SoundManager::Channel::getSound() const cond_noex
{
ASSERT( m_pSound, "Sound is null!" );
return m_pSound;
}
SoundManager& SoundManager::getInstance( WAVEFORMATEXTENSIBLE* format )
{
static SoundManager soundManager{format};
return soundManager;
}
void SoundManager::setMasterVolume( float volume )
{
m_pMasterVoice->SetVolume( volume );
}
void SoundManager::playChannelSound( class Sound* sound,
float volume )
{
std::unique_lock<std::mutex> ul{m_mu};
if ( !m_idleChannels.empty() && m_occupiedChannels.size() < m_nMaxAudioChannels )
{
auto& channel = m_idleChannels.back();
channel->setupChannel( *this,
*sound );
m_occupiedChannels.emplace_back( std::move( channel ) );
m_idleChannels.pop_back();
m_occupiedChannels.back()->playSound( sound,
volume );
}
}
void SoundManager::deactivateChannel( Channel& channel )
{
std::lock_guard<std::mutex> lg{m_mu};
// convert ptr/ref to container iterator
auto it = std::find_if( m_occupiedChannels.begin(),
m_occupiedChannels.end(),
[&channel] ( const std::unique_ptr<Channel>& cha )
{
return &channel == cha.get();
});
ASSERT( &it != nullptr, "Channel was already absent!" );
m_idleChannels.emplace_back( std::move( *it ) );
m_occupiedChannels.erase( it );
}
#pragma region LibrarySoundReading
#ifdef _XBOX // big endian
#define fourccRIFF 'RIFF'
#define fourccDATA 'data'
#define fourccFMT 'fmt '
#define fourccWAVE 'WAVE'
#define fourccXWMA 'XWMA'
#define fourccDPDS 'dpds'
#else // little endian
#define fourccRIFF 'FFIR'
#define fourccDATA 'atad'
#define fourccFMT ' tmf'
#define fourccWAVE 'EVAW'
#define fourccXWMA 'AMWX'
#define fourccDPDS 'sdpd'
#endif
HRESULT Sound::findChunk( HANDLE file,
DWORD fourcc,
DWORD& chunkSize,
DWORD& chunkDataPosition )
{
HRESULT hr = S_OK;
if ( SetFilePointer( file,
0,
nullptr,
FILE_BEGIN ) == INVALID_SET_FILE_POINTER )
return HRESULT_FROM_WIN32( GetLastError() );
DWORD chunkType;
DWORD chunkDataSize;
DWORD RIFFDataSize = 0;
DWORD fileType;
DWORD bytesRead = 0;
DWORD fileOffset = 0;
while ( hr == S_OK )
{
DWORD bytesRead;
if ( ReadFile( file,
&chunkType,
sizeof( DWORD ),
&bytesRead,
nullptr ) == 0 )
hr = HRESULT_FROM_WIN32( GetLastError() );
if ( ReadFile( file,
&chunkDataSize,
sizeof( DWORD ),
&bytesRead,
nullptr ) == 0 )
hr = HRESULT_FROM_WIN32( GetLastError() );
switch ( chunkType )
{
case fourccRIFF:
RIFFDataSize = chunkDataSize;
chunkDataSize = 4;
if ( 0 == ReadFile( file,
&fileType,
sizeof( DWORD ),
&bytesRead,
nullptr ) )
hr = HRESULT_FROM_WIN32( GetLastError() );
break;
default:
if ( SetFilePointer( file, //-V303
chunkDataSize,
nullptr,
FILE_CURRENT ) == INVALID_SET_FILE_POINTER )
return HRESULT_FROM_WIN32( GetLastError() );
}
fileOffset += sizeof( DWORD ) * 2;
if ( chunkType == fourcc )
{
chunkSize = chunkDataSize;
chunkDataPosition = fileOffset;
return S_OK;
}
fileOffset += chunkDataSize;
if ( bytesRead >= RIFFDataSize )
return S_FALSE;
}
return S_OK;
}
HRESULT Sound::readChunkData( HANDLE file,
void* buffer,
DWORD buffersize,
DWORD bufferoffset )
{
HRESULT hr = S_OK;
if ( SetFilePointer( file, //-V303
bufferoffset,
nullptr,
FILE_BEGIN ) == INVALID_SET_FILE_POINTER )
{
return HRESULT_FROM_WIN32( GetLastError() );
}
DWORD bytesRead;
if ( ReadFile( file,
buffer,
buffersize,
&bytesRead,
nullptr ) == 0 )
{
hr = HRESULT_FROM_WIN32( GetLastError() );
}
return hr;
}
#pragma endregion
Sound::Sound( const char* zsFilename,
const std::string& defaultName,
const std::string& defaultSubmixName )
:
m_name{defaultName},
m_submixName{defaultSubmixName},
// Initialize wave format and audio buffer
m_pWaveFormat{std::make_unique<WAVEFORMATEXTENSIBLE>()},
m_pXaudioBuffer{std::make_unique<XAUDIO2_BUFFER>()}
{
HANDLE file = CreateFileW( util::s2ws( zsFilename ).data(),
GENERIC_READ,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
0,
nullptr );
if ( file == INVALID_HANDLE_VALUE )
{
ASSERT_IF_FAILED( HRESULT_FROM_WIN32( GetLastError() ) );
}
HRESULT hres = SetFilePointer( file, //-V303
0,
nullptr,
FILE_BEGIN );
if ( hres == INVALID_SET_FILE_POINTER )
{
ASSERT_HRES_IF_FAILED( HRESULT_FROM_WIN32( GetLastError() ) );
}
// 1. locate the 'RIFF' chunk in the audio file and check the file type
DWORD chunkSize;
DWORD chunkPosition;
hres = findChunk( file,
fourccRIFF,
chunkSize,
chunkPosition );
ASSERT_HRES_IF_FAILED;
// check file type should be fourccWAVE = 'XWMA'
DWORD fileType;
hres = readChunkData( file,
&fileType,
sizeof DWORD,
chunkPosition );
ASSERT_HRES_IF_FAILED;
if ( fileType != fourccWAVE )
{
std::cout << "Unsupported Filetype '"
<< fileType
<< "' discovered:\n";
#if defined _DEBUG && !defined NDEBUG
__debugbreak();
#endif // _DEBUG
}
// 2. locate the 'fmt' chunk and copy its contents into a WAVEFORMATEXTENSIBLE structure
hres = findChunk( file,
fourccFMT,
chunkSize,
chunkPosition );
ASSERT_HRES_IF_FAILED;
hres = readChunkData( file,
m_pWaveFormat.get(),
chunkSize,
chunkPosition );
ASSERT_HRES_IF_FAILED;
ASSERT( m_pWaveFormat->Format.nChannels == wav::nChannelsPerSound,
"Wrong amount of channels per sound!" );
ASSERT( m_pWaveFormat->Format.wFormatTag == WAVE_FORMAT_PCM,
"Only XPCM format allowed!" );
ASSERT( m_pWaveFormat->Format.wBitsPerSample == wav::nBitsPerSample,
"Wrong bits per sample!" );
//ASSERT( m_pWaveFormat->Format.nSamplesPerSec == wav::nSamplesPerSec,
// "Wrong number of samples per second!" );
ASSERT( m_pWaveFormat->Format.cbSize == 0,
"No extra Format information allowed" );
m_pWaveFormat->Format.nChannels = wav::nChannelsPerSound;
m_pWaveFormat->Format.nSamplesPerSec = wav::nSamplesPerSec;
m_pWaveFormat->Format.wBitsPerSample = wav::nBitsPerSample;
m_pWaveFormat->Format.nBlockAlign = (wav::nBitsPerSample / 8) * wav::nChannelsPerSound;
m_pWaveFormat->Format.nAvgBytesPerSec = m_pWaveFormat->Format.nBlockAlign * wav::nSamplesPerSec;
m_pWaveFormat->Format.cbSize = 0;
m_pWaveFormat->Format.wFormatTag = WAVE_FORMAT_PCM;
// 3. locate the 'data' of the chunk and copy its contents into a buffer
hres = findChunk( file,
fourccDATA,
chunkSize,
chunkPosition );
ASSERT_HRES_IF_FAILED;
//std::cout << chunkSize << '\n';
m_pAudioData = std::make_unique<BYTE[]>( static_cast<std::size_t>( chunkSize ) );
hres = readChunkData( file,
m_pAudioData.get(),
chunkSize,
chunkPosition );
ASSERT_HRES_IF_FAILED;
// 4. populate the XAUDIO2_BUFFER structure
m_pXaudioBuffer->AudioBytes = chunkSize; // size of the audio buffer in Bytes
m_pXaudioBuffer->pAudioData = m_pAudioData.get(); // buffer containing audio data
m_pXaudioBuffer->Flags = XAUDIO2_END_OF_STREAM;
}
Sound::Sound( Sound&& rhs ) cond_noex
:
m_name{std::move( rhs.m_name )},
m_submixName{std::move( rhs.m_submixName )}
{
// lock the rhs mutex before we copy/move to guard from
std::unique_lock<std::mutex> ulr{rhs.m_mu, std::defer_lock};
std::unique_lock<std::mutex> ull{m_mu, std::defer_lock};
std::lock( ull, ulr );
m_pAudioData = std::move( rhs.m_pAudioData );
m_busyChannels = std::move( rhs.m_busyChannels );
for ( auto& channel : m_busyChannels )
{
channel->rechannel( &rhs,
this );
}
rhs.m_condVar.notify_all();
}
Sound& Sound::operator=( Sound &&rhs ) cond_noex
{
Sound tmp{std::move( rhs )};
std::swap( *this,
tmp );
return *this;
}
Sound::~Sound() noexcept
{
if ( !m_busyChannels.empty() )
{
std::unique_lock<std::mutex> ul{m_mu};
for ( const auto& i : m_busyChannels )
{
i->stopSound();
}
// wait for those channels to finish playing
m_condVar.wait( ul,
[this] () { return m_busyChannels.size() == 0; } );
}
}
std::string Sound::getName() const cond_noex
{
return m_name;
}
std::string Sound::getSubmixName() const cond_noex
{
return m_submixName;
}
void Sound::play( float volume )
{
SoundManager::getInstance( m_pWaveFormat.get() )
.playChannelSound( this,
volume );
}
void Sound::stop()
{
std::lock_guard<std::mutex> lg{m_mu};
if ( !m_busyChannels.empty() )
{
for ( const auto& channel : m_busyChannels )
{
channel->stopSound();
}
}
//if ( m_submixName != "" )
//{
//
//}
}
SoundManager::Submix::Submix( const std::string& name )
:
m_name{name},
m_outputVoiceSendDesc{0},
m_outputVoiceSends{0}
{
}
SoundManager::Submix::~Submix() noexcept
{
if ( m_pSubmixVoice )
{
m_pSubmixVoice->DestroyVoice();
m_pSubmixVoice = nullptr;
}
}
std::string SoundManager::Submix::getName() const cond_noex
{
return m_name;
}
void SoundManager::Submix::setName( const std::string& name ) cond_noex
{
m_name = name;
}
void SoundManager::setSubmixVolume( const Submix& submix,
float volume ) cond_noex
{
std::lock_guard<std::mutex> lg{m_mu};
for ( const auto& s : m_submixes )
{
if ( s->getName() == submix.getName() )
{
s->setVolume( volume );
}
}
}
void SoundManager::Submix::setVolume( float volume ) cond_noex
{
m_pSubmixVoice->SetVolume( volume );
}
SoundManager::Submix::Submix( Submix&& rhs ) cond_noex
:
m_name{std::move( rhs.m_name )},
m_outputVoiceSendDesc{std::move( rhs.m_outputVoiceSendDesc )},
m_outputVoiceSends{std::move( rhs.m_outputVoiceSends )},
m_pSubmixVoice{std::move( rhs.m_pSubmixVoice )}
{
rhs.m_name = "";
rhs.m_pSubmixVoice = nullptr;
}
SoundManager::Submix& SoundManager::Submix::operator=( Submix &&rhs ) cond_noex
{
Submix tmp{std::move( rhs )};
std::swap( *this,
tmp );
return *this;
}