40 posts
Help with XAudio2
Edited by BernFeth on Reason: Initial post
Hey guys,

I am trying to get XAudio2 to work but I am having some trouble, I would appreciate if anyone can help me out.

Here is what I tried so far:

  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 typedef HRESULT X_Audio2Create(IXAudio2 **ppXAudio2, UINT32 Flags, XAUDIO2_PROCESSOR XAudio2Processor); global X_Audio2Create *xAudio2Create; [...] HMODULE XAudioLib = LoadLibrary("xaudio2_9.dll"); if (XAudioLib) { xAudio2Create = (X_Audio2Create *)GetProcAddress(XAudioLib, "XAudio2Create"); if (xAudio2Create) { IXAudio2 *XAudio = 0; if (xAudio2Create(&XAudio, 0, XAUDIO2_DEFAULT_PROCESSOR) == S_OK) { IXAudio2MasteringVoice *masteringVoice; if (XAudio->CreateMasteringVoice(&masteringVoice) == S_OK) { IXAudio2SourceVoice *sourceVoice; WAVEFORMATEX waveFormat; waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nChannels = 2; waveFormat.nSamplesPerSec = 11025; waveFormat.wBitsPerSample = 16; waveFormat.nBlockAlign = (waveFormat.nChannels * waveFormat.wBitsPerSample) / 8; HRESULT result = XAudio->CreateSourceVoice(&sourceVoice, &waveFormat, XAUDIO2_VOICE_NOSRC, XAUDIO2_MAX_FREQ_RATIO, &voiceCallback, 0, 0); if (result == S_OK) { int y = 3; } } } } } 

it fails the last if, the error returned is:

XAUDIO2_E_INVALID_CALL
0x88960001

Returned by XAudio2 for certain API usage errors (invalid calls and so on) that are hard to avoid completely and should be handled by a title at runtime. (API usage errors that are completely avoidable, such as invalid parameters, cause an ASSERT in debug builds and undefined behavior in retail builds, so no error code is defined for them.)

I really struggled with audio when I followed handmade hero for the first time.
Now I'm starting the series again and I decided to try Xaudio2 instead, but I don't know much about audio overall.

Thanks!
Mārtiņš Možeiko
2362 posts / 2 projects
Help with XAudio2
Edited by Mārtiņš Možeiko on
You have not set nAvgBytesPerSec and cbSize members of WAVEFORMATEX structure.
They probably contain garbage values. Typically windows api returns error "invalid call" or "invalid value" for these kind of cases - when incorrect values are passed in. So whenever you see this error returned, verify that every single value you are passing in is valid.
40 posts
Help with XAudio2
Ah... Thank you mmozeiko, you are absolutely right.

This is embarrasing but I have to say I hae no idea what to set the average bytes to. The other settings I used the values the doc recommended but it didn't say anything about average bytes. I know this is something I should know, sorry about that.

Should I rewatch handmade hero part where casey does it and use the values he used?

Can this setup even work the way directsound worked for casey?

Maybe I have got myself into a place I can't handle, perhaps I should try following casey's directsound implementation again, your thoughts mister?
Mārtiņš Možeiko
2362 posts / 2 projects
Help with XAudio2
It says in documentation to what value to set it: https://docs.microsoft.com/en-us/...api/mmeapi/ns-mmeapi-waveformatex
Required average data-transfer rate, in bytes per second, for the format tag. If wFormatTag is WAVE_FORMAT_PCM, nAvgBytesPerSec should be equal to the product of nSamplesPerSec and nBlockAlign.

I personally would not choose XAudio2 unless you need to run same code on Xbox. I would use WASAPI audio on Windows. Because every other audio API (DirectSound, XAudio2, WinMM) are just a wrappers over WASAPI. So WASAPI is the lowest level of audio API you can use on Windows.
40 posts
Help with XAudio2
Uh oh... Even more embarassing then...