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
|
//Audio
...
...
#include <xaudio2.h>
#pragma comment(lib, "XAudio2.lib")
...
IXAudio2SourceVoice* XAudioSFXSourceVoice[NUMBER_OF_SFX_SOURCE_VOICES] = {0};
IXAudio2SourceVoice* XAudioMusicSourceVoice = NULL;
float SFXVolume = 0.5f;
float MusicVolume = 0.5f;
IXAudio2* XAudio;
IXAudio2MasteringVoice* XAudioMasteringVoice;
uint8_t gSFXSourceVoiceSelector;
int __stdcall WinMain(_In_ HINSTANCE Instance, _In_opt_ HINSTANCE PrevInstance, _In_ LPSTR CommandLine, _In_ int CmdShow)
{
.....
// Somewhere in the main function called
if (InitializeSoundEngine() != S_OK)
{
MessageBoxA(NULL, "InitializeSoundEngine failed !", "Error", MB_ICONERROR | MB_OK);
goto Exit;
}
....
Exit:
return(0);
}
// XAudio2
HRESULT InitializeSoundEngine(void)
{
HRESULT Result = S_OK;
WAVEFORMATEX SfxWaveFormat = {0};
WAVEFORMATEX MusicWaveFormat = {0};
Result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (Result != S_OK)
{
LogMessageA(LL_ERROR, "[%s] CoInitializeEx failed with code 0x%08lx !", __FUNCTION__, Result);
goto Exit;
}
Result = XAudio2Create(&XAudio, 0, XAUDIO2_ANY_PROCESSOR);
if (FAILED(Result))
{
LogMessageA(LL_ERROR, "[%s] XAudio2Create failed with code 0x%08lx !", __FUNCTION__, Result);
goto Exit;
}
Result = XAudio->lpVtbl->CreateMasteringVoice(XAudio, &XAudioMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE,
0, 0, NULL, 0);
if (FAILED(Result))
{
LogMessageA(LL_ERROR, "[%s] CreatingMasteringVoice failed with code 0x%08lx !", __FUNCTION__, Result);
goto Exit;
}
SfxWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
SfxWaveFormat.nChannels = 1; // Mono
SfxWaveFormat.nSamplesPerSec = 44100;
SfxWaveFormat.nAvgBytesPerSec = SfxWaveFormat.nSamplesPerSec * SfxWaveFormat.nChannels * 2;
SfxWaveFormat.nBlockAlign = SfxWaveFormat.nChannels * 2;
SfxWaveFormat.wBitsPerSample = 16;
SfxWaveFormat.cbSize = 0x6164;
for (uint8_t counter = 0; counter < NUMBER_OF_SFX_SOURCE_VOICES; counter++)
{
Result = XAudio->lpVtbl->CreateSourceVoice(XAudio, &XAudioSFXSourceVoice[counter], &SfxWaveFormat, 0, XAUDIO2_DEFAULT_FREQ_RATIO,
NULL, NULL, NULL);
if (Result != S_OK)
{
LogMessageA(LL_ERROR, "[%s] CreatingSourceVoice failed with code 0x%08lx !", __FUNCTION__, Result);
goto Exit;
}
XAudioSFXSourceVoice[counter]->lpVtbl->SetVolume(XAudioSFXSourceVoice[counter], SFXVolume, XAUDIO2_COMMIT_NOW);
}
MusicWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
MusicWaveFormat.nChannels = 2; // Stereo
MusicWaveFormat.nSamplesPerSec = 44100;
MusicWaveFormat.nAvgBytesPerSec = MusicWaveFormat.nSamplesPerSec * MusicWaveFormat.nChannels * 2;
MusicWaveFormat.nBlockAlign = MusicWaveFormat.nChannels * 2;
MusicWaveFormat.wBitsPerSample = 16;
MusicWaveFormat.cbSize = 0;
Result = XAudio->lpVtbl->CreateSourceVoice(XAudio, &XAudioMusicSourceVoice, &MusicWaveFormat, 0, XAUDIO2_DEFAULT_FREQ_RATIO,
NULL, NULL, NULL);
if (Result != S_OK)
{
LogMessageA(LL_ERROR, "[%s] CreatingSourceVoice failed with code 0x%08lx !", __FUNCTION__, Result);
goto Exit;
}
XAudioMusicSourceVoice->lpVtbl->SetVolume(XAudioMusicSourceVoice, MusicVolume, XAUDIO2_COMMIT_NOW);
Exit:
return(Result);
}
| |