Problems "playing" AudioSampleBuffer


#1

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


#2

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.


#3

thanks for the quick reply jules…
i wrote:

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


#4

There are a bunch of assertions in AudioSampleBuffer to warn you when you try to copy a dodgy area. Aren’t you running it in debug mode?


#5

Hi…
Problem solved.
It was a stupid error, dependent on the fact that the buffer didn’t exists anymore… (i hate pointers)
Thanks jules


#6

whuuuat ? could start a flame war here :slight_smile:


#7

Wait… I mean:
I always wrote java code… and it happens constantly to me that i forget one or two of them so…
I don’t want war… PEACE


#8

just joking… :slight_smile: peace to you too !


#9