content/media/webaudio/DelayBuffer.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     7 #ifndef DelayBuffer_h_
     8 #define DelayBuffer_h_
    10 #include "nsTArray.h"
    11 #include "AudioSegment.h"
    12 #include "mozilla/dom/AudioNodeBinding.h" // for ChannelInterpretation
    14 namespace mozilla {
    16 class DelayBuffer {
    17   typedef dom::ChannelInterpretation ChannelInterpretation;
    19 public:
    20   // See WebAudioUtils::ComputeSmoothingRate() for frame to frame exponential
    21   // |smoothingRate| multiplier.
    22   DelayBuffer(double aMaxDelayTicks, double aSmoothingRate)
    23     : mSmoothingRate(aSmoothingRate)
    24     , mCurrentDelay(-1.0)
    25     // Round the maximum delay up to the next tick.
    26     , mMaxDelayTicks(ceil(aMaxDelayTicks))
    27     , mCurrentChunk(0)
    28     // mLastReadChunk is initialized in EnsureBuffer
    29 #ifdef DEBUG
    30     , mHaveWrittenBlock(false)
    31 #endif
    32   {
    33     // The 180 second limit in AudioContext::CreateDelay() and the
    34     // 1 << MEDIA_TIME_FRAC_BITS limit on sample rate provide a limit on the
    35     // maximum delay.
    36     MOZ_ASSERT(aMaxDelayTicks <=
    37                std::numeric_limits<decltype(mMaxDelayTicks)>::max());
    38   }
    40   // Write a WEBAUDIO_BLOCK_SIZE block for aChannelCount channels.
    41   void Write(const AudioChunk& aInputChunk);
    43   // Read a block with an array of delays, in ticks, for each sample frame.
    44   // Each delay should be >= 0 and <= MaxDelayTicks().
    45   void Read(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
    46             AudioChunk* aOutputChunk,
    47             ChannelInterpretation aChannelInterpretation);
    48   // Read a block with a constant delay, which will be smoothed with the
    49   // previous delay.  The delay should be >= 0 and <= MaxDelayTicks().
    50   void Read(double aDelayTicks, AudioChunk* aOutputChunk,
    51             ChannelInterpretation aChannelInterpretation);
    53   // Read into one of the channels of aOutputChunk, given an array of
    54   // delays in ticks.  This is useful when delays are different on different
    55   // channels.  aOutputChunk must have already been allocated with at least as
    56   // many channels as were in any of the blocks passed to Write().
    57   void ReadChannel(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
    58                    const AudioChunk* aOutputChunk, uint32_t aChannel,
    59                    ChannelInterpretation aChannelInterpretation);
    61   // Advance the buffer pointer
    62   void NextBlock()
    63   {
    64     mCurrentChunk = (mCurrentChunk + 1) % mChunks.Length();
    65 #ifdef DEBUG
    66     MOZ_ASSERT(mHaveWrittenBlock);
    67     mHaveWrittenBlock = false;
    68 #endif
    69   }
    71   void Reset() {
    72     mChunks.Clear();
    73     mCurrentDelay = -1.0;
    74   };
    76   int MaxDelayTicks() const { return mMaxDelayTicks; }
    78   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
    80 private:
    81   void ReadChannels(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
    82                     const AudioChunk* aOutputChunk,
    83                     uint32_t aFirstChannel, uint32_t aNumChannelsToRead,
    84                     ChannelInterpretation aChannelInterpretation);
    85   bool EnsureBuffer();
    86   int PositionForDelay(int aDelay);
    87   int ChunkForPosition(int aPosition);
    88   int OffsetForPosition(int aPosition);
    89   int ChunkForDelay(int aDelay);
    90   void UpdateUpmixChannels(int aNewReadChunk, uint32_t channelCount,
    91                            ChannelInterpretation aChannelInterpretation);
    93   // Circular buffer for capturing delayed samples.
    94   FallibleTArray<AudioChunk> mChunks;
    95   // Cache upmixed channel arrays.
    96   nsAutoTArray<const void*,GUESS_AUDIO_CHANNELS> mUpmixChannels;
    97   double mSmoothingRate;
    98   // Current delay, in fractional ticks
    99   double mCurrentDelay;
   100   // Maximum delay, in ticks
   101   int mMaxDelayTicks;
   102   // The current position in the circular buffer.  The next write will be to
   103   // this chunk, and the next read may begin before this chunk.
   104   int mCurrentChunk;
   105   // The chunk owning the pointers in mUpmixChannels
   106   int mLastReadChunk;
   107 #ifdef DEBUG
   108   bool mHaveWrittenBlock;
   109 #endif
   110 };
   112 } // mozilla
   114 #endif // DelayBuffer_h_

mercurial