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

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

mercurial