Hi everybody… I’m Marcello and I want you to forgive me for my english
So, here is the problem:
I want to play audio from an AudioSampleBuffer, so I decided to write a MyAudioSource that inherits from PositionableAudioSource.
this is the code:
int MyAudioSource::getTotalLength() const
{
return (int) reader->getNumSamples();
}
void MyAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
long lung=info.numSamples;
if ((start+info.numSamples)>reader->getNumSamples())
{
lung=reader->getNumSamples()-start;
info.buffer->clear(info.startSample+lung,info.numSamples);
}
for (int i=0; i<reader->getNumChannels(); i++)
info.buffer->copyFrom(i,info.startSample,*reader,i,start,lung);
nextPlayPos += info.numSamples;
}
(reader is the audiosamplebuffer)
Yes, it play, but it always crash at the end of the playback.
If i try
int MyAudioSource::getTotalLength() const
{
return (int) reader->getNumSamples()-40000; //just to test
}
it play a few samples of the buffer, but with no problems.
What I miss or where i’m wrong???
And… there’s a finest way to play audio from an AudioBufferSource?
Thanks
You’re got the right kind of idea, but your code’s just not correctly checking whether it’s reading beyond the end of the buffer. Remember it must be able to read from any position, even far beyond the start or end of the buffer.
void MyAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
if (info.numSamples > 0)
{
const int start = nextPlayPos;
if (start + info.numSamples <= reader->getNumSamples())
{
for (int i=0; i<reader->getNumChannels(); i++)
info.buffer->copyFrom(i,info.startSample,*reader,i,start,info.numSamples);
nextPlayPos += info.numSamples;
}
}
}
Now, it worked the first time (i heard just the sound cutted at the end), but it no more works.
So… what kind of controls am i suppost to do?
I don’t want you to code for me but an issue is appreciate…
Thanks
Marcello