#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "AudioPullFIFO.h"
namespace WebCore {
AudioPullFIFO::AudioPullFIFO(AudioSourceProvider& audioProvider, unsigned numberOfChannels, size_t fifoLength, size_t providerSize)
: m_provider(audioProvider)
, m_fifoAudioBus(numberOfChannels, fifoLength)
, m_fifoLength(fifoLength)
, m_framesInFifo(0)
, m_readIndex(0)
, m_writeIndex(0)
, m_providerSize(providerSize)
, m_tempBus(numberOfChannels, providerSize)
{
}
void AudioPullFIFO::consume(AudioBus* destination, size_t framesToConsume)
{
bool isGood = destination && (framesToConsume <= m_fifoLength);
ASSERT(isGood);
if (!isGood)
return;
if (framesToConsume > m_framesInFifo) {
fillBuffer(framesToConsume - m_framesInFifo);
}
size_t part1Length;
size_t part2Length;
findWrapLengths(m_readIndex, framesToConsume, part1Length, part2Length);
size_t numberOfChannels = m_fifoAudioBus.numberOfChannels();
for (size_t channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
float* destinationData = destination->channel(channelIndex)->mutableData();
const float* sourceData = m_fifoAudioBus.channel(channelIndex)->data();
bool isCopyGood = ((m_readIndex < m_fifoLength)
&& (m_readIndex + part1Length) <= m_fifoLength
&& (part1Length <= destination->length())
&& (part1Length + part2Length) <= destination->length());
ASSERT(isCopyGood);
if (!isCopyGood)
return;
memcpy(destinationData, sourceData + m_readIndex, part1Length * sizeof(*sourceData));
if (part2Length > 0)
memcpy(destinationData + part1Length, sourceData, part2Length * sizeof(*sourceData));
}
m_readIndex = updateIndex(m_readIndex, framesToConsume);
m_framesInFifo -= framesToConsume;
ASSERT(m_framesInFifo >= 0);
}
void AudioPullFIFO::findWrapLengths(size_t index, size_t size, size_t& part1Length, size_t& part2Length)
{
ASSERT(index < m_fifoLength && size <= m_fifoLength);
if (index < m_fifoLength && size <= m_fifoLength) {
if (index + size > m_fifoLength) {
part1Length = m_fifoLength - index;
part2Length = size - part1Length;
} else {
part1Length = size;
part2Length = 0;
}
} else {
part1Length = 0;
part2Length = 0;
}
}
void AudioPullFIFO::fillBuffer(size_t numberOfFrames)
{
size_t framesProvided = 0;
while (framesProvided < numberOfFrames) {
m_provider.provideInput(&m_tempBus, m_providerSize);
size_t part1Length;
size_t part2Length;
findWrapLengths(m_writeIndex, m_providerSize, part1Length, part2Length);
size_t numberOfChannels = m_fifoAudioBus.numberOfChannels();
for (size_t channelIndex = 0; channelIndex < numberOfChannels; ++channelIndex) {
float* destination = m_fifoAudioBus.channel(channelIndex)->mutableData();
const float* source = m_tempBus.channel(channelIndex)->data();
bool isCopyGood = (part1Length <= m_providerSize
&& (part1Length + part2Length) <= m_providerSize
&& (m_writeIndex < m_fifoLength)
&& (m_writeIndex + part1Length) <= m_fifoLength
&& part2Length < m_fifoLength);
ASSERT(isCopyGood);
if (!isCopyGood)
return;
memcpy(destination + m_writeIndex, source, part1Length * sizeof(*destination));
if (part2Length > 0)
memcpy(destination, source + part1Length, part2Length * sizeof(*destination));
}
m_framesInFifo += m_providerSize;
ASSERT(m_framesInFifo <= m_fifoLength);
m_writeIndex = updateIndex(m_writeIndex, m_providerSize);
framesProvided += m_providerSize;
}
}
}
#endif // ENABLE(WEB_AUDIO)