Hello.
So I've tried to make my own custom soundbuffer class, but I'm having trouble playing the audio I send to it. The program seems to work fine, the onGetData function and returns with no errors, but after a tiny bit I get an error:
Unhandled exception at 0x0F8E78E6 (openal32.dll) in cbsp-vs.exe: 0xC0000005: Access violation reading location 0x0B7DC040.
I'm running on Windows 10, Visual Studio 2015 and am using SFML 2.4.2 (Visual C++ 14 (2015) - 32-bit version from the website).
This is the code I've used in my MusicPlayer (custom audiostream) class:
void MusicPlayer::StartTrack(string track, int loopstart, int looplength) //loads music track data and initializes stream
{
curSample = 0;
loopStart = loopstart;
loopLength = looplength;
trackName = track;
sf::Int16* musicData = (sf::Int16*)BDP::ReadBDP(trackName, 0, 44); //load only the header
sampleCount = BitConv::FromInt32((uint8_t*)musicData, 40) / 2; //WARNING: Samples MUST be 16-bit!
short int channelCount = BitConv::FromInt16((uint8_t*)musicData, 22);
int sampleRate = BitConv::FromInt32((uint8_t*)musicData, 24);
initialize(channelCount, sampleRate);
free(musicData);
}
bool MusicPlayer::onGetData(Chunk& data)
{
sf::Int16* musicData;
if (curSample + BUFFER_LENGTH / 2 >= sampleCount)
{ //only add to buffer the remaining samples
int remainSamples = sampleCount - curSample;
musicData = (sf::Int16*)BDP::ReadBDP(trackName, 44 + curSample * 2, remainSamples * 2);
data.samples = (sf::Int16*)musicData;
data.sampleCount = remainSamples;
curSample = loopStart;
}
else
{ //add to buffer all available samples
musicData = (sf::Int16*)BDP::ReadBDP(trackName, 44 + curSample * 2, BUFFER_LENGTH);
data.samples = (sf::Int16*)musicData;
data.sampleCount = BUFFER_LENGTH / 2;
curSample += BUFFER_LENGTH / 2;
}
free(musicData);
return true;
}
And this is the code that I'm using to play it:
musicPlayer.StartTrack("snd/mus/menu.wav", 276706, 3251294);
musicPlayer.play();
(unsurprisingly it's just those 2 lines)
Thank you for your help.
EDIT: The ReadBDP function just reads a file (which is inside a bigger file) and it works perfectly, so pretend that I'm just reading a file (or part of it since I can specify offset and size to read from the file, which I did here in order to just read the sample data from a WAV).