Ejemplo n.º 1
0
void AudioFIFO::push(const AudioBus* sourceBus) {
  // Copy the sourceBus into the FIFO buffer.

  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));

    // Handle wrap around of the FIFO, if needed.
    if (part2Length)
      memcpy(destination, source + part1Length,
             part2Length * sizeof(*destination));
  }

  m_framesInFifo += sourceLength;
  ASSERT(m_framesInFifo <= m_fifoLength);
  m_writeIndex = updateIndex(m_writeIndex, sourceLength);
}
Ejemplo n.º 2
0
void AudioPullFIFO::consume(AudioBus* destination, size_t framesToConsume)
{
    bool isGood = destination && (framesToConsume <= m_fifoLength);
    ASSERT(isGood);
    if (!isGood)
        return;

    if (framesToConsume > m_framesInFifo) {
        // We don't have enough data in the FIFO to fulfill the request. Ask for more data.
        fillBuffer(framesToConsume - m_framesInFifo);
    }

    // We have enough data now. Copy the requested number of samples to the destination.

    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));
        // Handle wrap around of the FIFO, if needed.
        if (part2Length > 0)
            memcpy(destinationData + part1Length, sourceData, part2Length * sizeof(*sourceData));
    }
    m_readIndex = updateIndex(m_readIndex, framesToConsume);
    m_framesInFifo -= framesToConsume;
    ASSERT(m_framesInFifo >= 0);
}
Ejemplo n.º 3
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;

  // Copy the requested number of samples to the destination.

  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));
    // Handle wrap around of the FIFO, if needed.
    if (part2Length)
      memcpy(destinationData + part1Length, sourceData,
             part2Length * sizeof(*sourceData));
  }
  m_readIndex = updateIndex(m_readIndex, framesToConsume);
  ASSERT(m_framesInFifo >= framesToConsume);
  m_framesInFifo -= framesToConsume;
}
Ejemplo n.º 4
0
void AudioPullFIFO::fillBuffer(size_t numberOfFrames)
{
    // Keep asking the provider to give us data until we have received at least |numberOfFrames| of
    // data. Stuff the data into the FIFO.
    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));
            // Handle wrap around of the FIFO, if needed.
            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;
    }
}