XAudio2Create function fail..

Hello guys.. I'm using XAudio2 library as my sound for the project. And compiles without errors:

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);
}


I skipped other code .. just showing sound initialization. If I run it I get the InitializeSoundEngine failed ! from the main function.
I just run to cursor and I put a Breakpoint to line where start's with XAudio2Create function... and says the XAudio->lpVtbl is nullptr // <Unable to read the memory>..
What am I doing wrong.. ? I just follow the MSDN how to create sound with XAudio2 and following the instruction.. seems I'm not doing something..
Any Help.. please ?
If any other code needed I can share..
Your InitializeSoundEngine function is doing a lot of internal error checking. Narrow it down so that you know at which exact call Result becomes a value other than S_OK.
If I run it I get the InitializeSoundEngine failed ! from the main function.
Maybe it failed because a required .dll (like Windows.Media.Audio.dll) could not be found. Or maybe your sound card does not support Direct Sound.

See:
https://support.microsoft.com/en-us/topic/error-message-error-initializing-direct-sound-occurs-when-starting-directx-c892d63a-ac48-51c4-8c25-3653448703ed
I'm using XAudio2 library as my sound for the project.


For your project... any reason not to use MF? (Microsoft Media Foundation)

https://docs.microsoft.com/en-us/windows/win32/medfound/microsoft-media-foundation-sdk

XAudio is low level API, and MF is higher level.
Topic archived. No new replies allowed.