This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
SimpleSpatialPlaySoundXDK.cpp
579 lines (474 loc) · 17.8 KB
/
SimpleSpatialPlaySoundXDK.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
//--------------------------------------------------------------------------------------
// SimpleSpatialPlaySoundXDK.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleSpatialPlaySoundXDK.h"
#include "ATGColors.h"
#include "ControllerFont.h"
#include "WAVFileReader.h"
namespace
{
const LPCWSTR g_FileList[] =
{
L"Jungle_RainThunder_mix714.wav",
L"ChannelIDs714.wav",
};
const int numFiles = _countof(g_FileList);
}
extern void ExitSample() noexcept;
using namespace DirectX;
using Microsoft::WRL::ComPtr;
namespace
{
VOID CALLBACK SpatialWorkCallback(_Inout_ PTP_CALLBACK_INSTANCE Instance, _Inout_opt_ PVOID Context, _Inout_ PTP_WORK Work)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
Sample * Sink = (Sample *)Context;
Work;
Instance;
while (Sink->m_bThreadActive)
{
while (Sink->m_bPlayingSound && Sink->m_Renderer->IsActive())
{
// Wait for a signal from the audio-engine to start the next processing pass
if (WaitForSingleObject(Sink->m_Renderer->m_bufferCompletionEvent, 100) != WAIT_OBJECT_0)
{
//make a call to stream to see why we didn't get a signal after 100ms
hr = Sink->m_Renderer->m_SpatialAudioStream->Reset();
//if we have a stream error, set the renderer state to reset
if (hr != S_OK)
{
Sink->m_Renderer->Reset();
}
continue;
}
UINT32 frameCount;
UINT32 availableObjectCount;
// Begin the process of sending object data and metadata
// Get the number of active object that can be used to send object-data
// Get the number of frame count that each buffer be filled with
hr = Sink->m_Renderer->m_SpatialAudioStream->BeginUpdatingAudioObjects(
&availableObjectCount,
&frameCount);
//if we have a stream error, set the renderer state to reset
if (hr != S_OK)
{
Sink->m_Renderer->Reset();
}
Sink->m_availableObjects = availableObjectCount;
for (int chan = 0; chan < MAX_CHANNELS; chan++)
{
//Activate the object if not yet done
if (Sink->WavChannels[chan].object == nullptr)
{
// If this method called more than activeObjectCount times
// It will fail with this error HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)
hr = Sink->m_Renderer->m_SpatialAudioStream->ActivateSpatialAudioObject(
Sink->WavChannels[chan].objType,
&Sink->WavChannels[chan].object);
if (FAILED(hr))
{
continue;
}
}
//Get the object buffer
BYTE* buffer = nullptr;
UINT32 bytecount;
hr = Sink->WavChannels[chan].object->GetBuffer(&buffer, &bytecount);
if (FAILED(hr))
{
continue;
}
Sink->WavChannels[chan].object->SetVolume(Sink->WavChannels[chan].volume);
UINT32 readsize = bytecount;
for (UINT32 i = 0; i < readsize; i++)
{
UINT32 fileLoc = Sink->WavChannels[chan].curBufferLoc;
if (chan < Sink->m_numChannels)
{
buffer[i] = Sink->WavChannels[chan].wavBuffer[fileLoc];
}
else
{
buffer[i] = 0;
}
Sink->WavChannels[chan].curBufferLoc++;
if (Sink->WavChannels[chan].curBufferLoc == Sink->WavChannels[chan].buffersize)
{
Sink->WavChannels[chan].curBufferLoc = 0;
}
}
}
// Let the audio-engine know that the object data are available for processing now
hr = Sink->m_Renderer->m_SpatialAudioStream->EndUpdatingAudioObjects();
if (FAILED(hr))
{
Sink->m_Renderer->Reset();
continue;
}
}
}
}
}
Sample::Sample() :
m_numChannels(0),
WavChannels{},
m_bThreadActive(false),
m_bPlayingSound(false),
m_availableObjects(0),
m_frame(0),
m_fileLoaded(false),
m_curFile(0),
m_workThread(nullptr)
{
m_deviceResources = std::make_unique<DX::DeviceResources>();
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(IUnknown* window)
{
m_gamePad = std::make_unique<GamePad>();
m_deviceResources->SetWindow(window);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
InitializeSpatialStream();
SetChannelPosVolumes();
for (UINT32 i = 0; i < MAX_CHANNELS; i++)
{
WavChannels[i].buffersize = 0;
WavChannels[i].curBufferLoc = 0;
WavChannels[i].object = nullptr;
}
m_fileLoaded = LoadFile(g_FileList[m_curFile]);
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Frame %llu", m_frame);
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
PIXEndEvent();
m_frame++;
}
// Updates the world.
void Sample::Update(DX::StepTimer const&)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
//Are we resetting the renderer? This will happen if we get an invalid stream
// which can happen when render mode changes or device changes
if (m_Renderer->IsResetting())
{
//clear out renderer
m_Renderer.Reset();
// Create a new ISAC instance
m_Renderer = Microsoft::WRL::Make<ISACRenderer>();
// Initialize Default Audio Device
m_Renderer->InitializeAudioDeviceAsync();
//Reset all the Objects that were being used
for (int chan = 0; chan < MAX_CHANNELS; chan++)
{
WavChannels[chan].object = nullptr;
}
}
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
if (m_gamePadButtons.a == m_gamePadButtons.RELEASED)
{
//if we have an active renderer
if (m_Renderer && m_Renderer->IsActive())
{
//Start spatial worker thread
if (!m_bThreadActive)
{
//reload file to start again
m_fileLoaded = LoadFile(g_FileList[m_curFile]);
//startup spatial thread
m_bThreadActive = true;
m_bPlayingSound = true;
m_workThread = CreateThreadpoolWork(SpatialWorkCallback, this, nullptr);
SubmitThreadpoolWork(m_workThread);
}
else
{
//stop and shutdown spatial worker thread
m_bThreadActive = false;
m_bPlayingSound = false;
WaitForThreadpoolWorkCallbacks(m_workThread, FALSE);
CloseThreadpoolWork(m_workThread);
m_workThread = nullptr;
}
}
}
else if (m_gamePadButtons.b == m_gamePadButtons.RELEASED)
{
//if we have an active renderer
if (m_Renderer && m_Renderer->IsActive())
{
//if spatial thread active and playing, shutdown and start new file
if (m_bThreadActive && m_bPlayingSound)
{
m_bThreadActive = false;
m_bPlayingSound = false;
WaitForThreadpoolWorkCallbacks(m_workThread, FALSE);
CloseThreadpoolWork(m_workThread);
m_workThread = nullptr;
//load next file
m_curFile++;
if (m_curFile == numFiles) m_curFile = 0;
m_fileLoaded = LoadFile(g_FileList[m_curFile]);
//if successfully loaded, start up thread and playing new file
if (m_fileLoaded)
{
m_bThreadActive = true;
m_bPlayingSound = true;
m_workThread = CreateThreadpoolWork(SpatialWorkCallback, this, nullptr);
SubmitThreadpoolWork(m_workThread);
}
}
else
{
//if thread active but paused, shutdown thread to load new file
if (m_bThreadActive)
{
m_bThreadActive = false;
m_bPlayingSound = false;
WaitForThreadpoolWorkCallbacks(m_workThread, FALSE);
CloseThreadpoolWork(m_workThread);
m_workThread = nullptr;
}
//load next file
m_curFile++;
if (m_curFile == numFiles) m_curFile = 0;
m_fileLoaded = LoadFile(g_FileList[m_curFile]);
}
}
}
}
else
{
m_gamePadButtons.Reset();
}
PIXEndEvent();
}
#pragma endregion
#pragma region Frame Render
// Draws the scene.
void Sample::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
// Prepare the render target to render a new frame.
m_deviceResources->Prepare();
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
auto rect = m_deviceResources->GetOutputSize();
auto safeRect = SimpleMath::Viewport::ComputeTitleSafeArea(rect.right, rect.bottom);
XMFLOAT2 pos(float(safeRect.left), float(safeRect.top));
m_spriteBatch->Begin();
float spacing = m_font->GetLineSpacing();
m_font->DrawString(m_spriteBatch.get(), L"Simple Spatial Playback:", pos, ATG::Colors::White);
pos.y += spacing * 1.5f;
if (!m_Renderer->IsActive())
{
m_font->DrawString(m_spriteBatch.get(), L"Spatial Renderer Not Available", pos, ATG::Colors::Orange);
pos.y += spacing * 2.f;
}
else
{
wchar_t str[256] = {};
swprintf_s(str, L" File: %ls", g_FileList[m_curFile]);
m_font->DrawString(m_spriteBatch.get(), str, pos, ATG::Colors::White);
pos.y += spacing;
swprintf_s(str, L" State: %ls", ((m_bPlayingSound) ? L"Playing" : L"Stopped"));
m_font->DrawString(m_spriteBatch.get(), str, pos, ATG::Colors::White);
pos.y += spacing * 1.5f;
DX::DrawControllerString(m_spriteBatch.get(), m_font.get(), m_ctrlFont.get(), L"Use [A] to start/stop playback", pos, ATG::Colors::White);
pos.y += spacing;
DX::DrawControllerString(m_spriteBatch.get(), m_font.get(), m_ctrlFont.get(), L"Use [B] to change to next file", pos, ATG::Colors::White);
pos.y += spacing;
DX::DrawControllerString(m_spriteBatch.get(), m_font.get(), m_ctrlFont.get(), L"Use [View] to exit", pos, ATG::Colors::White);
}
m_spriteBatch->End();
PIXEndEvent(context);
// Show the new frame.
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit();
PIXEndEvent(context);
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto renderTarget = m_deviceResources->GetRenderTargetView();
auto depthStencil = m_deviceResources->GetDepthStencilView();
context->ClearRenderTargetView(renderTarget, ATG::Colors::Background);
context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
context->OMSetRenderTargets(1, &renderTarget, depthStencil);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Suspend(0);
}
void Sample::OnResuming()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Resume();
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto device = m_deviceResources->GetD3DDevice();
m_graphicsMemory = std::make_unique<GraphicsMemory>(device, m_deviceResources->GetBackBufferCount());
auto context = m_deviceResources->GetD3DDeviceContext();
m_spriteBatch = std::make_unique<SpriteBatch>(context);
m_font = std::make_unique<SpriteFont>(device, L"SegoeUI_18.spritefont");
m_ctrlFont = std::make_unique<SpriteFont>(device, L"XboxOneControllerSmall.spritefont");
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
}
#pragma endregion
HRESULT Sample::InitializeSpatialStream(void)
{
HRESULT hr = S_OK;
if (!m_Renderer)
{
// Create a new ISAC instance
m_Renderer = Microsoft::WRL::Make<ISACRenderer>();
// Selects the Default Audio Device
hr = m_Renderer->InitializeAudioDeviceAsync();
}
return hr;
}
bool Sample::LoadFile(LPCWSTR inFile)
{
//clear and reset wavchannels
for (UINT32 i = 0; i < MAX_CHANNELS; i++)
{
if (WavChannels[i].buffersize)
{
delete[] WavChannels[i].wavBuffer;
}
WavChannels[i].buffersize = 0;
WavChannels[i].curBufferLoc = 0;
WavChannels[i].object = nullptr;
}
HRESULT hr = S_OK;
std::unique_ptr<uint8_t[]> m_waveFile;
DX::WAVData WavData;
hr = DX::LoadWAVAudioFromFileEx(inFile, m_waveFile, WavData);
if (FAILED(hr))
{
return false;
}
if ((WavData.wfx->wFormatTag == 1 || WavData.wfx->wFormatTag == 65534) && WavData.wfx->nSamplesPerSec == 48000)
{
m_numChannels = WavData.wfx->nChannels;
int numSamples = WavData.audioBytes / (2 * m_numChannels);
for (int i = 0; i < m_numChannels; i++)
{
WavChannels[i].wavBuffer = new char[numSamples * 4];
WavChannels[i].buffersize = numSamples * 4;
}
float * tempnew;
short * tempdata = (short *)WavData.startAudio;
for (int i = 0; i < numSamples; i++)
{
for (int j = 0; j < m_numChannels; j++)
{
int chan = j;
tempnew = (float *)WavChannels[chan].wavBuffer;
int sample = (i * m_numChannels) + chan;
tempnew[i] = (float)tempdata[sample] / 32768;
}
}
}
else if ((WavData.wfx->wFormatTag == 3) && WavData.wfx->nSamplesPerSec == 48000)
{
m_numChannels = WavData.wfx->nChannels;
int numSamples = WavData.audioBytes / (4 * m_numChannels);
for (int i = 0; i < m_numChannels; i++)
{
WavChannels[i].wavBuffer = new char[numSamples * 4];
WavChannels[i].buffersize = numSamples * 4;
}
float * tempnew;
float * tempdata = (float *)WavData.startAudio;
for (int i = 0; i < numSamples; i++)
{
for (int j = 0; j < m_numChannels; j++)
{
int chan = j;
tempnew = (float *)WavChannels[chan].wavBuffer;
int sample = (i * m_numChannels) + chan;
tempnew[i] = (float)tempdata[sample];
}
}
}
else
{
return false;
}
return true;
}
void Sample::SetChannelPosVolumes(void)
{
WavChannels[0].volume = 1.f;
WavChannels[0].objType = AudioObjectType_FrontLeft;
WavChannels[1].volume = 1.f;
WavChannels[1].objType = AudioObjectType_FrontRight;
WavChannels[2].volume = 1.f;
WavChannels[2].objType = AudioObjectType_FrontCenter;
WavChannels[3].volume = 1.f;
WavChannels[3].objType = AudioObjectType_LowFrequency;
WavChannels[4].volume = 1.f;
WavChannels[4].objType = AudioObjectType_BackLeft;
WavChannels[5].volume = 1.f;
WavChannels[5].objType = AudioObjectType_BackRight;
WavChannels[6].volume = 1.f;
WavChannels[6].objType = AudioObjectType_SideLeft;
WavChannels[7].volume = 1.f;
WavChannels[7].objType = AudioObjectType_SideRight;
WavChannels[8].volume = 1.f;
WavChannels[8].objType = AudioObjectType_TopFrontLeft;
WavChannels[9].volume = 1.f;
WavChannels[9].objType = AudioObjectType_TopFrontRight;
WavChannels[10].volume = 1.f;
WavChannels[10].objType = AudioObjectType_TopBackLeft;
WavChannels[11].volume = 1.f;
WavChannels[11].objType = AudioObjectType_TopBackRight;
}