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 | internal void Win32InitXAudio2(int32 BufferSize, int32 SamplesPerSeconds, XAUDIO2_BUFFER *AudioBuffer) { bool32 GotError = 0; // NOTE Load Library HMODULE XAudio2Library = LoadLibraryA("xaudio2_8.dll"); //must call this for COM, sais the Internetz ?!?!1 //CoInitializeEx( NULL, COINIT_MULTITHREADED ); if(XAudio2Library) { // NOTE Create XAudio2 object IXAudio2 *XAudio2 = NULL; HRESULT hr; x_audio2_create *XAudio2Create = (x_audio2_create *)GetProcAddress(XAudio2Library, "XAudio2Create"); if (XAudio2Create && SUCCEEDED(XAudio2Create(&XAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR))) { // NOTE Create the master voice IXAudio2MasteringVoice* MasterVoice = NULL; if (SUCCEEDED(XAudio2->CreateMasteringVoice(&MasterVoice))) { // NOTE Create the WAVEFORMAT WAVEFORMATEX WaveFormat = {}; WaveFormat.wFormatTag = WAVE_FORMAT_PCM; WaveFormat.nChannels = 2; WaveFormat.nSamplesPerSec = SamplesPerSeconds; WaveFormat.wBitsPerSample = 16; WaveFormat.nBlockAlign = (WaveFormat.nChannels*WaveFormat.wBitsPerSample) / 8; WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec*WaveFormat.nBlockAlign; WaveFormat.cbSize = 0; // NOTE Create the Souce voice IXAudio2SourceVoice *SourceVoice; if(SUCCEEDED(XAudio2->CreateSourceVoice( &SourceVoice, &WaveFormat))) { // NOTE Allocate buffer. The buffer is owned by the program and not the XAudio2 //AudioBuffer = {} AudioBuffer->Flags = 0; AudioBuffer->AudioBytes = BufferSize; AudioBuffer->pAudioData = (const BYTE*)VirtualAlloc(0, BufferSize, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); AudioBuffer->PlayBegin = 0; AudioBuffer->PlayLength = 0; AudioBuffer->LoopBegin = 0; AudioBuffer->LoopLength = 0; AudioBuffer->LoopCount = XAUDIO2_LOOP_INFINITE; AudioBuffer->pContext = 0; // NOTE Pass XAUDIO2_BUFFER to the source voice if(SUCCEEDED(SourceVoice->SubmitSourceBuffer(AudioBuffer))) { // NOTE Start playing if (SUCCEEDED(SourceVoice->Start(0))) { //YEAH! } else { // TODO Diagnostics GotError = 32; } } else { // TODO Diagnostics GotError = 16; } } else { // TODO Diagnostics GotError = 8; } } else { // TODO Diagnostics GotError = 4; } } else { // TODO Diagnostics GotError = 2; } } else { // TODO Diagnostics GotError = 1; } if(GotError) { //CoUninitialize(); // Free mem? } } |