2013-08-08 15:07:49 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
#ifndef DelayBuffer_h_
|
|
|
|
#define DelayBuffer_h_
|
2013-08-08 15:07:49 -07:00
|
|
|
|
|
|
|
#include "nsTArray.h"
|
2014-03-04 13:06:57 -08:00
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "mozilla/dom/AudioNodeBinding.h" // for ChannelInterpretation
|
2013-08-08 15:07:49 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
class DelayBuffer {
|
2014-03-04 13:06:57 -08:00
|
|
|
typedef dom::ChannelInterpretation ChannelInterpretation;
|
|
|
|
|
2013-08-08 15:07:49 -07:00
|
|
|
public:
|
|
|
|
// See WebAudioUtils::ComputeSmoothingRate() for frame to frame exponential
|
|
|
|
// |smoothingRate| multiplier.
|
2014-03-04 13:06:57 -08:00
|
|
|
DelayBuffer(int aMaxDelayTicks, double aSmoothingRate)
|
2013-08-08 15:07:49 -07:00
|
|
|
: mSmoothingRate(aSmoothingRate)
|
2014-03-04 13:06:57 -08:00
|
|
|
, mCurrentDelay(-1.0)
|
|
|
|
, mMaxDelayTicks(aMaxDelayTicks)
|
|
|
|
, mCurrentChunk(0)
|
|
|
|
// mLastReadChunk is initialized in EnsureBuffer
|
2013-08-08 15:07:49 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-04 13:06:57 -08:00
|
|
|
// Write a WEBAUDIO_BLOCK_SIZE block for aChannelCount channels.
|
|
|
|
void Write(const AudioChunk& aInputChunk);
|
|
|
|
|
|
|
|
// Read a block with an array of delays, in ticks, for each sample frame.
|
|
|
|
// Each delay must be > 0 and < MaxDelayTicks().
|
|
|
|
void Read(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
AudioChunk* aOutputChunk,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
|
|
|
// Read a block with a constant delay, which will be smoothed with the
|
|
|
|
// previous delay. The delay must be > 0 and < MaxDelayTicks().
|
|
|
|
void Read(double aDelayTicks, AudioChunk* aOutputChunk,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
|
|
|
|
|
|
|
// Read into one of the channels of aOutputChunk, given an array of
|
|
|
|
// delays in ticks. This is useful when delays are different on different
|
|
|
|
// channels. aOutputChunk must have already been allocated with at least as
|
|
|
|
// many channels as were in any of the blocks passed to Write().
|
|
|
|
void ReadChannel(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
const AudioChunk* aOutputChunk, uint32_t aChannel,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
2013-08-08 15:07:49 -07:00
|
|
|
|
2014-03-04 13:06:57 -08:00
|
|
|
// Advance the buffer pointer
|
|
|
|
void NextBlock()
|
|
|
|
{
|
|
|
|
mCurrentChunk = (mCurrentChunk + 1) % mChunks.Length();
|
|
|
|
}
|
2013-08-08 15:07:49 -07:00
|
|
|
|
2014-03-04 13:06:57 -08:00
|
|
|
void Reset() {
|
|
|
|
mChunks.Clear();
|
|
|
|
mCurrentDelay = -1.0;
|
|
|
|
};
|
2013-08-08 15:07:49 -07:00
|
|
|
|
2014-03-04 13:06:57 -08:00
|
|
|
int MaxDelayTicks() const { return mMaxDelayTicks; }
|
2013-08-08 15:07:49 -07:00
|
|
|
|
|
|
|
private:
|
2014-03-04 13:06:57 -08:00
|
|
|
void ReadChannels(const double aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
|
|
|
|
const AudioChunk* aOutputChunk,
|
|
|
|
uint32_t aFirstChannel, uint32_t aNumChannelsToRead,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
|
|
|
bool EnsureBuffer();
|
|
|
|
int PositionForDelay(int aDelay);
|
|
|
|
int ChunkForPosition(int aPosition);
|
|
|
|
int OffsetForPosition(int aPosition);
|
|
|
|
int ChunkForDelay(int aDelay);
|
|
|
|
void UpdateUpmixChannels(int aNewReadChunk, uint32_t channelCount,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
2013-08-08 15:07:49 -07:00
|
|
|
|
|
|
|
// Circular buffer for capturing delayed samples.
|
2014-03-04 13:06:57 -08:00
|
|
|
FallibleTArray<AudioChunk> mChunks;
|
|
|
|
// Cache upmixed channel arrays.
|
|
|
|
nsAutoTArray<const void*,GUESS_AUDIO_CHANNELS> mUpmixChannels;
|
2013-08-08 15:07:49 -07:00
|
|
|
double mSmoothingRate;
|
2014-03-04 13:06:57 -08:00
|
|
|
// Current delay, in fractional ticks
|
2013-08-08 15:07:49 -07:00
|
|
|
double mCurrentDelay;
|
2014-03-04 13:06:57 -08:00
|
|
|
// Maximum delay, in ticks
|
|
|
|
int mMaxDelayTicks;
|
|
|
|
// The current position in the circular buffer. The next write will be to
|
|
|
|
// this chunk, and the next read may begin before this chunk.
|
|
|
|
int mCurrentChunk;
|
|
|
|
// The chunk owning the pointers in mUpmixChannels
|
|
|
|
int mLastReadChunk;
|
2013-08-08 15:07:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // mozilla
|
|
|
|
|
2014-03-02 15:49:45 -08:00
|
|
|
#endif // DelayBuffer_h_
|