Re: Re: alcCaptureSamples crashes on stereo capturing

Chris Robinson <[email protected]> Fri, 10 Aug 2012 02:06:33 -0700
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
On 08/10/2012 01:35 AM, Philipp Kraus wrote:
> Yes, my code shows exactly;

 > if (l_samplesread > 0)
 > {
 >     l_samplesread = std::min(static_cast<std::size_t>(l_samplesread),
 >                              l_buffer.size()-i);
 >     alcCaptureSamples(m_device, (ALCvoid*)(&l_buffer(i)),
 >                       l_samplesread);
 >     i += l_samplesread;
 > }

The problem here is that the count you're getting from and passing to 
OpenAL is in sample frames. 1 sample frame for 8-bit stereo is 2 bytes. 
So when you ask for n samples, you need a buffer n*2 bytes big. 
l_buffer.size()-i gives you the number of bytes the buffer can hold, but 
telling OpenAL to store that many samples which needs twice as much. The 
size of the buffer is correct, but you're telling OpenAL to write too 
much into it.

The fix would be to do it like this:

l_samplesread = std::min(static_cast<std::size_t>(l_samplesread),
                          (l_buffer.size()-i)/2);
alcCaptureSamples(m_device, (ALCvoid*)(&l_buffer(i)), l_samplesread);
i += l_samplesread*2;


Also, concerning this bit:
if(!l_samplesread && alcIsExtensionPresent(m_device, "ALC_EXT_disconnect"))
{
     stop capturing
}

you'll want to make sure the device is actually disconnected before 
stopping. It's possible OpenAL hasn't received any audio since the last 
time you got some and can tell you 0 temporarily, but start giving you 
audio again when it receives the next chunk. Do it like this:

if(!l_samplesread && alcIsExtensionPresent(m_device, "ALC_EXT_disconnect"))
{
     ALCint connected = 0;
     alcGetIntegerv(m_device, ALC_CONNECTED, 1, &connected);
     if(!connected)
     {
         stop capturing
     }
}

_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal