tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DelayBuffer.cpp (8789B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "DelayBuffer.h"
      8 
      9 #include "AudioChannelFormat.h"
     10 #include "AudioNodeEngine.h"
     11 #include "mozilla/PodOperations.h"
     12 
     13 namespace mozilla {
     14 
     15 size_t DelayBuffer::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
     16  size_t amount = 0;
     17  amount += mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
     18  for (size_t i = 0; i < mChunks.Length(); i++) {
     19    amount += mChunks[i].SizeOfExcludingThis(aMallocSizeOf, false);
     20  }
     21 
     22  amount += mUpmixChannels.ShallowSizeOfExcludingThis(aMallocSizeOf);
     23  return amount;
     24 }
     25 
     26 void DelayBuffer::Write(const AudioBlock& aInputChunk) {
     27  // We must have a reference to the buffer if there are channels
     28  MOZ_ASSERT(aInputChunk.IsNull() == !aInputChunk.ChannelCount());
     29 #ifdef DEBUG
     30  MOZ_ASSERT(!mHaveWrittenBlock);
     31  mHaveWrittenBlock = true;
     32 #endif
     33 
     34  if (!EnsureBuffer()) {
     35    return;
     36  }
     37 
     38  if (mCurrentChunk == mLastReadChunk) {
     39    mLastReadChunk = -1;  // invalidate cache
     40  }
     41  mChunks[mCurrentChunk] = aInputChunk.AsAudioChunk();
     42 }
     43 
     44 void DelayBuffer::Read(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
     45                       AudioBlock* aOutputChunk,
     46                       ChannelInterpretation aChannelInterpretation) {
     47  int chunkCount = mChunks.Length();
     48  if (!chunkCount) {
     49    aOutputChunk->SetNull(WEBAUDIO_BLOCK_SIZE);
     50    return;
     51  }
     52 
     53  // Find the maximum number of contributing channels to determine the output
     54  // channel count that retains all signal information.  Buffered blocks will
     55  // be upmixed if necessary.
     56  //
     57  // First find the range of "delay" offsets backwards from the current
     58  // position.  Note that these may be negative for frames that are after the
     59  // current position (including i).
     60  float minDelay = aPerFrameDelays[0];
     61  float maxDelay = minDelay;
     62  for (unsigned i = 1; i < WEBAUDIO_BLOCK_SIZE; ++i) {
     63    minDelay = std::min(minDelay, aPerFrameDelays[i] - i);
     64    maxDelay = std::max(maxDelay, aPerFrameDelays[i] - i);
     65  }
     66 
     67  // Now find the chunks touched by this range and check their channel counts.
     68  int oldestChunk = ChunkForDelay(std::ceil(maxDelay));
     69  int youngestChunk = ChunkForDelay(std::floor(minDelay));
     70 
     71  uint32_t channelCount = 0;
     72  for (int i = oldestChunk; true; i = (i + 1) % chunkCount) {
     73    channelCount =
     74        GetAudioChannelsSuperset(channelCount, mChunks[i].ChannelCount());
     75    if (i == youngestChunk) {
     76      break;
     77    }
     78  }
     79 
     80  if (channelCount) {
     81    aOutputChunk->AllocateChannels(channelCount);
     82    ReadChannels(aPerFrameDelays, aOutputChunk, 0, channelCount,
     83                 aChannelInterpretation);
     84  } else {
     85    aOutputChunk->SetNull(WEBAUDIO_BLOCK_SIZE);
     86  }
     87 }
     88 
     89 void DelayBuffer::ReadChannel(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
     90                              AudioBlock* aOutputChunk, uint32_t aChannel,
     91                              ChannelInterpretation aChannelInterpretation) {
     92  if (!mChunks.Length()) {
     93    float* outputChannel = aOutputChunk->ChannelFloatsForWrite(aChannel);
     94    PodZero(outputChannel, WEBAUDIO_BLOCK_SIZE);
     95    return;
     96  }
     97 
     98  ReadChannels(aPerFrameDelays, aOutputChunk, aChannel, 1,
     99               aChannelInterpretation);
    100 }
    101 
    102 void DelayBuffer::ReadChannels(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
    103                               AudioBlock* aOutputChunk, uint32_t aFirstChannel,
    104                               uint32_t aNumChannelsToRead,
    105                               ChannelInterpretation aChannelInterpretation) {
    106  uint32_t totalChannelCount = aOutputChunk->ChannelCount();
    107  uint32_t readChannelsEnd = aFirstChannel + aNumChannelsToRead;
    108  MOZ_ASSERT(readChannelsEnd <= totalChannelCount);
    109 
    110  if (mUpmixChannels.Length() != totalChannelCount) {
    111    mLastReadChunk = -1;  // invalidate cache
    112  }
    113 
    114  for (uint32_t channel = aFirstChannel; channel < readChannelsEnd; ++channel) {
    115    PodZero(aOutputChunk->ChannelFloatsForWrite(channel), WEBAUDIO_BLOCK_SIZE);
    116  }
    117 
    118  for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
    119    float currentDelay = aPerFrameDelays[i];
    120    MOZ_ASSERT(currentDelay >= 0.0f);
    121    MOZ_ASSERT(currentDelay <= (mChunks.Length() - 1) * WEBAUDIO_BLOCK_SIZE);
    122 
    123    // Interpolate two input frames in case the read position does not match
    124    // an integer index.
    125    // Use the larger delay, for the older frame, first, as this is more
    126    // likely to use the cached upmixed channel arrays.
    127    int floorDelay = int(currentDelay);
    128    float interpolationFactor = currentDelay - floorDelay;
    129    int positions[2];
    130    positions[1] = PositionForDelay(floorDelay) + i;
    131    positions[0] = positions[1] - 1;
    132 
    133    for (unsigned tick = 0; tick < std::size(positions); ++tick) {
    134      int readChunk = ChunkForPosition(positions[tick]);
    135      // The zero check on interpolationFactor is important because, when
    136      // currentDelay is integer, positions[0] may be outside the range
    137      // considered for determining totalChannelCount.
    138      // mVolume is not set on default initialized chunks so also handle null
    139      // chunks specially.
    140      if (interpolationFactor != 0.0f && !mChunks[readChunk].IsNull()) {
    141        int readOffset = OffsetForPosition(positions[tick]);
    142        UpdateUpmixChannels(readChunk, totalChannelCount,
    143                            aChannelInterpretation);
    144        float multiplier = interpolationFactor * mChunks[readChunk].mVolume;
    145        for (uint32_t channel = aFirstChannel; channel < readChannelsEnd;
    146             ++channel) {
    147          aOutputChunk->ChannelFloatsForWrite(channel)[i] +=
    148              multiplier * mUpmixChannels[channel][readOffset];
    149        }
    150      }
    151 
    152      interpolationFactor = 1.0f - interpolationFactor;
    153    }
    154  }
    155 }
    156 
    157 void DelayBuffer::Read(float aDelayTicks, AudioBlock* aOutputChunk,
    158                       ChannelInterpretation aChannelInterpretation) {
    159  float computedDelay[WEBAUDIO_BLOCK_SIZE];
    160 
    161  for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
    162    computedDelay[i] = aDelayTicks;
    163  }
    164 
    165  Read(computedDelay, aOutputChunk, aChannelInterpretation);
    166 }
    167 
    168 bool DelayBuffer::EnsureBuffer() {
    169  if (mChunks.Length() == 0) {
    170    // The length of the buffer is at least one block greater than the maximum
    171    // delay so that writing an input block does not overwrite the block that
    172    // would subsequently be read at maximum delay.  Also round up to the next
    173    // block size, so that no block of writes will need to wrap.
    174    const int chunkCount = (mMaxDelayTicks + 2 * WEBAUDIO_BLOCK_SIZE - 1) >>
    175                           WEBAUDIO_BLOCK_SIZE_BITS;
    176    if (!mChunks.SetLength(chunkCount, fallible)) {
    177      return false;
    178    }
    179 
    180    mLastReadChunk = -1;
    181  }
    182  return true;
    183 }
    184 
    185 int DelayBuffer::PositionForDelay(int aDelay) {
    186  // Adding mChunks.Length() keeps integers positive for defined and
    187  // appropriate bitshift, remainder, and bitwise operations.
    188  return ((mCurrentChunk + mChunks.Length()) * WEBAUDIO_BLOCK_SIZE) - aDelay;
    189 }
    190 
    191 int DelayBuffer::ChunkForPosition(int aPosition) {
    192  MOZ_ASSERT(aPosition >= 0);
    193  return (aPosition >> WEBAUDIO_BLOCK_SIZE_BITS) % mChunks.Length();
    194 }
    195 
    196 int DelayBuffer::OffsetForPosition(int aPosition) {
    197  MOZ_ASSERT(aPosition >= 0);
    198  return aPosition & (WEBAUDIO_BLOCK_SIZE - 1);
    199 }
    200 
    201 int DelayBuffer::ChunkForDelay(int aDelay) {
    202  return ChunkForPosition(PositionForDelay(aDelay));
    203 }
    204 
    205 void DelayBuffer::UpdateUpmixChannels(
    206    int aNewReadChunk, uint32_t aChannelCount,
    207    ChannelInterpretation aChannelInterpretation) {
    208  if (aNewReadChunk == mLastReadChunk) {
    209    MOZ_ASSERT(mUpmixChannels.Length() == aChannelCount);
    210    return;
    211  }
    212 
    213  NS_WARNING_ASSERTION(mHaveWrittenBlock || aNewReadChunk != mCurrentChunk,
    214                       "Smoothing is making feedback delay too small.");
    215 
    216  mLastReadChunk = aNewReadChunk;
    217  mUpmixChannels.ClearAndRetainStorage();
    218  mUpmixChannels.AppendElements(mChunks[aNewReadChunk].ChannelData<float>());
    219  MOZ_ASSERT(mUpmixChannels.Length() <= aChannelCount);
    220  if (mUpmixChannels.Length() < aChannelCount) {
    221    if (aChannelInterpretation == ChannelInterpretation::Speakers) {
    222      AudioChannelsUpMix(&mUpmixChannels, aChannelCount,
    223                         SilentChannel::ZeroChannel<float>());
    224      MOZ_ASSERT(mUpmixChannels.Length() == aChannelCount,
    225                 "We called GetAudioChannelsSuperset to avoid this");
    226    } else {
    227      // Fill up the remaining channels with zeros
    228      for (uint32_t channel = mUpmixChannels.Length(); channel < aChannelCount;
    229           ++channel) {
    230        mUpmixChannels.AppendElement(SilentChannel::ZeroChannel<float>());
    231      }
    232    }
    233  }
    234 }
    235 
    236 }  // namespace mozilla