-1

I'm getting a read access violation somewhere I say there is something pointing to null I am currently using XAudio2.9 and Libvorbis 1.3.7. I edited this to be more informative this is all of it I hope you guys can find the issue with this I'm new to messing with sound in C++ and been trying to learn how to use this for months now I have some trouble understanding things like this.

#include <xaudio2redist/xaudio2.h>
#include <fileapi.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

#define STREAMING_BUFFER_SIZE 4096
#define MAX_BUFFER_COUNT 4                                             

class SoundDevice
{
public:
bool audio_init();
bool audio_load(const char* filename);
bool play();
bool audio_clean();
private:
IXAudio2* Audio = nullptr;
IXAudio2MasteringVoice* Masteringvoice = nullptr;
IXAudio2SourceVoice* Sourcevoice = nullptr;
WAVEFORMATEX formatex;
XAUDIO2_BUFFER buffer = { 0 };
XAUDIO2_VOICE_STATE state;
HRESULT hr;

// LibVorbis
HANDLE vorbishandle;
OggVorbis_File* vorbisfile;
vorbis_info* vorbisinfo;

char buffers[MAX_BUFFER_COUNT][STREAMING_BUFFER_SIZE];
int sec = 0; // section
DWORD pos = 0; // position
int ret = 0; // return
DWORD currentDiskReadBuffer = 0;
};

bool SoundDevice::audio_init()
{
hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);

if (FAILED(hr))
{
    MessageBoxA(0, "Failed to CoInitialize", "Error:", hr);
    return false;
}

if (FAILED(hr = XAudio2Create(&Audio, 0, XAUDIO2_DEFAULT_PROCESSOR)))
{
    MessageBoxA(0, "Failed to create XAudio2", "Error:", hr);
    return false;
}

if (FAILED(hr = Audio->CreateMasteringVoice(&Masteringvoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, 0)))
{
    MessageBoxA(0, "Failed to create Mastering Voice", "Error:", hr);
    return false;
}

vorbisinfo = ov_info(vorbisfile, -1);

ZeroMemory(&formatex, sizeof(WAVEFORMATEX));
formatex.wFormatTag = 1;
formatex.nChannels = 2;
formatex.nSamplesPerSec = 44100;
formatex.nAvgBytesPerSec = formatex.nChannels * formatex.nSamplesPerSec * 2;
formatex.nBlockAlign = formatex.nChannels * 2;
formatex.wBitsPerSample = 16;
formatex.cbSize = sizeof(formatex);

ZeroMemory(&buffers[currentDiskReadBuffer], sizeof(DWORD));

buffer.pAudioData = reinterpret_cast<BYTE*>(&buffers[currentDiskReadBuffer]);
buffer.AudioBytes = STREAMING_BUFFER_SIZE;
buffer.Flags = XAUDIO2_END_OF_STREAM;

if (FAILED(hr = Audio->CreateSourceVoice(&Sourcevoice, &formatex, 0, 2.0f, 0, 0, 0)))
{
    MessageBoxA(0, "Failed to create Source Voice", "Error:", hr);
    return hr;
}

return 0;
}

bool SoundDevice::audio_load(const char* filename)
{
vorbishandle = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, nullptr, FILE_SHARE_READ, FILE_ATTRIBUTE_NORMAL, vorbishandle);

if (ov_open_callbacks(vorbishandle, vorbisfile, nullptr, 0, OV_CALLBACKS_DEFAULT))
{
    CloseHandle(vorbishandle);
    return false;
}

while (ret && pos < STREAMING_BUFFER_SIZE)
{
    ret = ov_read(vorbisfile, buffers[currentDiskReadBuffer] + pos, STREAMING_BUFFER_SIZE - pos, 0, 2, 1, &sec);
    pos += ret;
}

return hr;
}

bool SoundDevice::play()
{
hr = Sourcevoice->SubmitSourceBuffer(&buffer, 0);
hr = Sourcevoice->Start();
return hr;
}

bool SoundDevice::audio_clean()
{
Sourcevoice->Stop(0);
Sourcevoice->FlushSourceBuffers();
Sourcevoice->DestroyVoice();
return hr;
}
  • 1
    Just because this is where the program crashes or reports an error doesn't mean this is where the problem is. C++ does not work this way. The problem can be anywhere in your code, but after the bug occurs the program keeps running for a little bit before it finally crashes here. This is why stackoverflow.com's [help] requires you to show a [mre] that everyone else can cut/paste ***exactly as shown***, then compile, run, and reproduce your problem. See [ask] for more information. Until you do that, it is unlikely that anyone will be able to figure out your problem. – Sam Varshavchik Apr 03 '22 at 00:29
  • Any suggestions? –  Apr 03 '22 at 01:34
  • The first suggestion would be to use your text editor to reformat the shown code so that it's logically indented. Unindented code, like that, is difficult to read or understand. The best way to improve your chances of getting help on Stackoverflow is to make it as easy as possible for everyone else to understand the shown code. – Sam Varshavchik Apr 03 '22 at 01:38
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 03 '22 at 04:54
  • I've just been having trouble loading in audio with xaudio2 can someone explain how to do it I tried so many times. –  Apr 03 '22 at 14:22

1 Answers1

1

I can't seem to find the problem for a few reasons, mainly because I can't see your error message. But also, a tip I can give you is to indent your code properly, for example you could indent everything inside of the class by a tab or a few spaces, it doesn't really matter which as long as it is consistent. Speaking from experience, I found it easier to read my code when I started to indent the code inside definitions like public or private and protected. Just a simple piece of advice to help debugging in general. (Keep in mind, this is my first answer ever so please be nice but don't be afraid to give tips.)