This source file includes following definitions.
- m_writeIndex
- consume
- push
- findWrapLengths
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "platform/audio/AudioFIFO.h"
namespace WebCore {
AudioFIFO::AudioFIFO(unsigned numberOfChannels, size_t fifoLength)
: m_fifoAudioBus(AudioBus::create(numberOfChannels, fifoLength))
, m_fifoLength(fifoLength)
, m_framesInFifo(0)
, m_readIndex(0)
, m_writeIndex(0)
{
}
void AudioFIFO::consume(AudioBus* destination, size_t framesToConsume)
{
bool isGood = destination && (framesToConsume <= m_fifoLength) && (framesToConsume <= m_framesInFifo) && (destination->length() >= framesToConsume);
ASSERT(isGood);
if (!isGood)
return;
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)
memcpy(destinationData + part1Length, sourceData, part2Length * sizeof(*sourceData));
}
m_readIndex = updateIndex(m_readIndex, framesToConsume);
ASSERT(m_framesInFifo >= framesToConsume);
m_framesInFifo -= framesToConsume;
}
void AudioFIFO::push(const AudioBus* sourceBus)
{
bool isGood = sourceBus && (m_framesInFifo + sourceBus->length() <= m_fifoLength);
if (!isGood)
return;
size_t sourceLength = sourceBus->length();
size_t part1Length;
size_t part2Length;
findWrapLengths(m_writeIndex, sourceLength, 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 = sourceBus->channel(channelIndex)->data();
bool isCopyGood = ((m_writeIndex < m_fifoLength)
&& (m_writeIndex + part1Length) <= m_fifoLength
&& part2Length < m_fifoLength
&& part1Length + part2Length <= sourceLength);
ASSERT(isCopyGood);
if (!isCopyGood)
return;
memcpy(destination + m_writeIndex, source, part1Length * sizeof(*destination));
if (part2Length)
memcpy(destination, source + part1Length, part2Length * sizeof(*destination));
}
m_framesInFifo += sourceLength;
ASSERT(m_framesInFifo <= m_fifoLength);
m_writeIndex = updateIndex(m_writeIndex, sourceLength);
}
void AudioFIFO::findWrapLengths(size_t index, size_t size, size_t& part1Length, size_t& part2Length)
{
ASSERT_WITH_SECURITY_IMPLICATION(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;
}
}
}
#endif