-
Notifications
You must be signed in to change notification settings - Fork 0
/
XAudio2BasicSound.cpp
executable file
·401 lines (348 loc) · 12.6 KB
/
XAudio2BasicSound.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
//--------------------------------------------------------------------------------------
// File: XAudio2BasicSound.cpp
//
// Simple playback of a .WAV file using XAudio2
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN£k
#include <windows.h>
#include <stdio.h>
#include "WAVFileReader.h"
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
#include <xaudio2.h>
#pragma comment(lib,"xaudio2.lib")
#else
#include <C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include\comdecl.h>
#include <C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Include\xaudio2.h>
#endif
//--------------------------------------------------------------------------------------
// Helper macros
//--------------------------------------------------------------------------------------
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=nullptr; } }
#endif
//--------------------------------------------------------------------------------------
// Forward declaration
//--------------------------------------------------------------------------------------
HRESULT PlayWave( _In_ IXAudio2* pXaudio2, _In_z_ LPCWSTR szFilename );
HRESULT FindMediaFileCch( _Out_writes_(cchDest) WCHAR* strDestPath, _In_ int cchDest, _In_z_ LPCWSTR strFilename );
//--------------------------------------------------------------------------------------
// Entry point to the program
//--------------------------------------------------------------------------------------
int main()
{
//
// Initialize XAudio2
//
CoInitializeEx( nullptr, COINIT_MULTITHREADED );
IXAudio2* pXAudio2 = nullptr;
UINT32 flags = 0;
#if (_WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/) && defined(_DEBUG)
flags |= XAUDIO2_DEBUG_ENGINE;
#endif
HRESULT hr = XAudio2Create( &pXAudio2, flags );
if( FAILED( hr ) )
{
wprintf( L"Failed to init XAudio2 engine: %#X\n", hr );
CoUninitialize();
return 0;
}
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) && defined(_DEBUG)
// To see the trace output, you need to view ETW logs for this application:
// Go to Control Panel, Administrative Tools, Event Viewer.
// View->Show Analytic and Debug Logs.
// Applications and Services Logs / Microsoft / Windows / XAudio2.
// Right click on Microsoft Windows XAudio2 debug logging, Properties, then Enable Logging, and hit OK
XAUDIO2_DEBUG_CONFIGURATION debug ={0};
debug.TraceMask = XAUDIO2_LOG_ERRORS | XAUDIO2_LOG_WARNINGS;
debug.BreakMask = XAUDIO2_LOG_ERRORS;
pXAudio2->SetDebugConfiguration( &debug, 0 );
#endif
//
// Create a mastering voice
//
IXAudio2MasteringVoice* pMasteringVoice = nullptr;
if( FAILED( hr = pXAudio2->CreateMasteringVoice( &pMasteringVoice ) ) )
{
wprintf( L"Failed creating mastering voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
wprintf(L"Playing mono WAV PCM file...\n");
if (FAILED(hr = PlayWave(pXAudio2, L"a.wav")))
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
#if (0)
//
// Play a PCM wave file
//
wprintf( L"Playing mono WAV PCM file..." );
if( FAILED( hr = PlayWave( pXAudio2, L"Media\\Wavs\\MusicMono.wav" ) ) )
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
//
// Play an ADPCM wave file
//
wprintf( L"\nPlaying mono WAV ADPCM file (loops twice)..." );
if( FAILED( hr = PlayWave( pXAudio2, L"Media\\Wavs\\MusicMono_adpcm.wav" ) ) )
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
//
// Play a 5.1 PCM wave extensible file
//
wprintf( L"\nPlaying 5.1 WAV PCM file..." );
if( FAILED( hr = PlayWave( pXAudio2, L"Media\\Wavs\\MusicSurround.wav" ) ) )
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
#if (_WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
//
// Play a mono xWMA wave file
//
wprintf( L"\nPlaying mono xWMA file..." );
if( FAILED( hr = PlayWave( pXAudio2, L"Media\\Wavs\\MusicMono_xwma.wav" ) ) )
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
//
// Play a 5.1 xWMA wave file
//
wprintf( L"\nPlaying 5.1 xWMA file..." );
if( FAILED( hr = PlayWave( pXAudio2, L"Media\\Wavs\\MusicSurround_xwma.wav" ) ) )
{
wprintf( L"Failed creating source voice: %#X\n", hr );
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
return 0;
}
#endif
#endif
//
// Cleanup XAudio2
//
wprintf( L"\nFinished playing\n" );
// All XAudio2 interfaces are released when the engine is destroyed, but being tidy
pMasteringVoice->DestroyVoice();
SAFE_RELEASE( pXAudio2 );
CoUninitialize();
}
//--------------------------------------------------------------------------------------
// Name: PlayWave
// Desc: Plays a wave and blocks until the wave finishes playing
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT PlayWave( IXAudio2* pXaudio2, LPCWSTR szFilename )
{
//
// Locate the wave file
//
WCHAR strFilePath[MAX_PATH];
HRESULT hr = FindMediaFileCch( strFilePath, MAX_PATH, szFilename );
if( FAILED( hr ) )
{
wprintf( L"Failed to find media file: %s\n", szFilename );
return hr;
}
//
// Read in the wave file
//
std::unique_ptr<uint8_t[]> waveFile;
DirectX::WAVData waveData;
if ( FAILED( hr = DirectX::LoadWAVAudioFromFileEx( strFilePath, waveFile, waveData ) ) )
{
wprintf( L"Failed reading WAV file: %#X (%s)\n", hr, strFilePath );
return hr;
}
//
// Play the wave using a XAudio2SourceVoice
//
// Create the source voice
IXAudio2SourceVoice* pSourceVoice;
if( FAILED( hr = pXaudio2->CreateSourceVoice( &pSourceVoice, waveData.wfx ) ) )
{
wprintf( L"Error %#X creating source voice\n", hr );
return hr;
}
// Submit the wave sample data using an XAUDIO2_BUFFER structure
XAUDIO2_BUFFER buffer = {0};
buffer.pAudioData = waveData.startAudio;
buffer.Flags = XAUDIO2_END_OF_STREAM; // tell the source voice not to expect any data after this buffer
buffer.AudioBytes = waveData.audioBytes;
if ( waveData.loopLength > 0 )
{
buffer.LoopBegin = waveData.loopStart;
buffer.LoopLength = waveData.loopLength;
buffer.LoopCount = 1; // We'll just assume we play the loop twice
}
#if (_WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
if ( waveData.seek )
{
XAUDIO2_BUFFER_WMA xwmaBuffer = {0};
xwmaBuffer.pDecodedPacketCumulativeBytes = waveData.seek;
xwmaBuffer.PacketCount = waveData.seekCount;
if( FAILED( hr = pSourceVoice->SubmitSourceBuffer( &buffer, &xwmaBuffer ) ) )
{
wprintf( L"Error %#X submitting source buffer (xWMA)\n", hr );
pSourceVoice->DestroyVoice();
return hr;
}
}
#else
if ( waveData.seek )
{
wprintf( L"This platform does not support xWMA or XMA2\n" );
pSourceVoice->DestroyVoice();
return hr;
}
#endif
else if( FAILED( hr = pSourceVoice->SubmitSourceBuffer( &buffer ) ) )
{
wprintf( L"Error %#X submitting source buffer\n", hr );
pSourceVoice->DestroyVoice();
return hr;
}
hr = pSourceVoice->Start( 0 );
hr = pSourceVoice->SetFrequencyRatio(1);
float vol=0, refvol;
//Sleep(2000);
pSourceVoice->SetChannelVolumes(1, &vol);
pSourceVoice->GetChannelVolumes(1 , &refvol );
//wprintf(L"%f\n", refvol);
vol = 0.1;
// Let the sound play
BOOL isRunning = TRUE;
BOOL zoom = FALSE;
while( SUCCEEDED( hr ) && isRunning )
{
XAUDIO2_VOICE_STATE state;
pSourceVoice->GetState( &state );
isRunning = ( state.BuffersQueued > 0 ) != 0;
if (zoom == FALSE){
vol = vol*1.01;
if (vol > 1){
zoom = TRUE;
vol = 1;
}
hr = pSourceVoice->SetChannelVolumes(1, &vol);
}
pSourceVoice->GetChannelVolumes(1, &refvol);
//wprintf(L"%f %d\n", refvol, *(state.pCurrentBufferContext).PlayLength);
// Wait till the escape key is pressed
if (GetAsyncKeyState(VK_LEFT)){
float sp;
pSourceVoice->GetFrequencyRatio(&sp);
pSourceVoice->SetFrequencyRatio(sp - 0.1);
wprintf(L"Now speed ratio is %f\n", sp - 0.1);
//break;
}
if (GetAsyncKeyState(VK_RIGHT)){
float sp;
pSourceVoice->GetFrequencyRatio(&sp);
pSourceVoice->SetFrequencyRatio(sp + 0.1);
wprintf(L"Now speed ratio is %f\n", sp + 0.1);
//break;
}
if (GetAsyncKeyState(VK_UP) && zoom){
vol = vol + 0.1;
pSourceVoice->SetChannelVolumes(1, &vol);
wprintf(L"Now volume is %f\n", vol );
//break;
}
if (GetAsyncKeyState(VK_DOWN) && zoom){
vol = vol - 0.1;
pSourceVoice->SetChannelVolumes(1, &vol);
wprintf(L"Now volume is %f\n", vol);
//break;
}
Sleep( 10 );
}
// Wait till the escape key is released
while (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_DOWN))
Sleep( 10 );
pSourceVoice->DestroyVoice();
return hr;
}
//--------------------------------------------------------------------------------------
// Helper function to try to find the location of a media file
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT FindMediaFileCch( WCHAR* strDestPath, int cchDest, LPCWSTR strFilename )
{
bool bFound = false;
if( !strFilename || strFilename[0] == 0 || !strDestPath || cchDest < 10 )
return E_INVALIDARG;
// Get the exe name, and exe path
WCHAR strExePath[MAX_PATH] = {0};
WCHAR strExeName[MAX_PATH] = {0};
WCHAR* strLastSlash = nullptr;
GetModuleFileName( nullptr, strExePath, MAX_PATH );
strExePath[MAX_PATH - 1] = 0;
strLastSlash = wcsrchr( strExePath, TEXT( '\\' ) );
if( strLastSlash )
{
wcscpy_s( strExeName, MAX_PATH, &strLastSlash[1] );
// Chop the exe name from the exe path
*strLastSlash = 0;
// Chop the .exe from the exe name
strLastSlash = wcsrchr( strExeName, TEXT( '.' ) );
if( strLastSlash )
*strLastSlash = 0;
}
wcscpy_s( strDestPath, cchDest, strFilename );
if( GetFileAttributes( strDestPath ) != 0xFFFFFFFF )
return S_OK;
// Search all parent directories starting at .\ and using strFilename as the leaf name
WCHAR strLeafName[MAX_PATH] = {0};
wcscpy_s( strLeafName, MAX_PATH, strFilename );
WCHAR strFullPath[MAX_PATH] = {0};
WCHAR strFullFileName[MAX_PATH] = {0};
WCHAR strSearch[MAX_PATH] = {0};
WCHAR* strFilePart = nullptr;
GetFullPathName( L".", MAX_PATH, strFullPath, &strFilePart );
if( !strFilePart )
return E_FAIL;
while( strFilePart && *strFilePart != '\0' )
{
swprintf_s( strFullFileName, MAX_PATH, L"%s\\%s", strFullPath, strLeafName );
if( GetFileAttributes( strFullFileName ) != 0xFFFFFFFF )
{
wcscpy_s( strDestPath, cchDest, strFullFileName );
bFound = true;
break;
}
swprintf_s( strFullFileName, MAX_PATH, L"%s\\%s\\%s", strFullPath, strExeName, strLeafName );
if( GetFileAttributes( strFullFileName ) != 0xFFFFFFFF )
{
wcscpy_s( strDestPath, cchDest, strFullFileName );
bFound = true;
break;
}
swprintf_s( strSearch, MAX_PATH, L"%s\\..", strFullPath );
GetFullPathName( strSearch, MAX_PATH, strFullPath, &strFilePart );
}
if( bFound )
return S_OK;
// On failure, return the file as the path but also return an error code
wcscpy_s( strDestPath, cchDest, strFilename );
return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
}