summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/DelayBuffer.cpp
blob: cab2920080fad75b2c58210f8fc318e5d091d088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- 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/. */

#include "DelayBuffer.h"

#include "mozilla/PodOperations.h"
#include "AudioChannelFormat.h"
#include "AudioNodeEngine.h"

namespace mozilla {

size_t DelayBuffer::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
  size_t amount = 0;
  amount += mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
  for (size_t i = 0; i < mChunks.Length(); i++) {
    amount += mChunks[i].SizeOfExcludingThis(aMallocSizeOf, false);
  }

  amount += mUpmixChannels.ShallowSizeOfExcludingThis(aMallocSizeOf);
  return amount;
}

void DelayBuffer::Write(const AudioBlock& aInputChunk) {
  // We must have a reference to the buffer if there are channels
  MOZ_ASSERT(aInputChunk.IsNull() == !aInputChunk.ChannelCount());
#ifdef DEBUG
  MOZ_ASSERT(!mHaveWrittenBlock);
  mHaveWrittenBlock = true;
#endif

  if (!EnsureBuffer()) {
    return;
  }

  if (mCurrentChunk == mLastReadChunk) {
    mLastReadChunk = -1;  // invalidate cache
  }
  mChunks[mCurrentChunk] = aInputChunk.AsAudioChunk();
}

void DelayBuffer::Read(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
                       AudioBlock* aOutputChunk,
                       ChannelInterpretation aChannelInterpretation) {
  int chunkCount = mChunks.Length();
  if (!chunkCount) {
    aOutputChunk->SetNull(WEBAUDIO_BLOCK_SIZE);
    return;
  }

  // Find the maximum number of contributing channels to determine the output
  // channel count that retains all signal information.  Buffered blocks will
  // be upmixed if necessary.
  //
  // First find the range of "delay" offsets backwards from the current
  // position.  Note that these may be negative for frames that are after the
  // current position (including i).
  float minDelay = aPerFrameDelays[0];
  float maxDelay = minDelay;
  for (unsigned i = 1; i < WEBAUDIO_BLOCK_SIZE; ++i) {
    minDelay = std::min(minDelay, aPerFrameDelays[i] - i);
    maxDelay = std::max(maxDelay, aPerFrameDelays[i] - i);
  }

  // Now find the chunks touched by this range and check their channel counts.
  int oldestChunk = ChunkForDelay(std::ceil(maxDelay));
  int youngestChunk = ChunkForDelay(std::floor(minDelay));

  uint32_t channelCount = 0;
  for (int i = oldestChunk; true; i = (i + 1) % chunkCount) {
    channelCount =
        GetAudioChannelsSuperset(channelCount, mChunks[i].ChannelCount());
    if (i == youngestChunk) {
      break;
    }
  }

  if (channelCount) {
    aOutputChunk->AllocateChannels(channelCount);
    ReadChannels(aPerFrameDelays, aOutputChunk, 0, channelCount,
                 aChannelInterpretation);
  } else {
    aOutputChunk->SetNull(WEBAUDIO_BLOCK_SIZE);
  }
}

void DelayBuffer::ReadChannel(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
                              AudioBlock* aOutputChunk, uint32_t aChannel,
                              ChannelInterpretation aChannelInterpretation) {
  if (!mChunks.Length()) {
    float* outputChannel = aOutputChunk->ChannelFloatsForWrite(aChannel);
    PodZero(outputChannel, WEBAUDIO_BLOCK_SIZE);
    return;
  }

  ReadChannels(aPerFrameDelays, aOutputChunk, aChannel, 1,
               aChannelInterpretation);
}

void DelayBuffer::ReadChannels(const float aPerFrameDelays[WEBAUDIO_BLOCK_SIZE],
                               AudioBlock* aOutputChunk, uint32_t aFirstChannel,
                               uint32_t aNumChannelsToRead,
                               ChannelInterpretation aChannelInterpretation) {
  uint32_t totalChannelCount = aOutputChunk->ChannelCount();
  uint32_t readChannelsEnd = aFirstChannel + aNumChannelsToRead;
  MOZ_ASSERT(readChannelsEnd <= totalChannelCount);

  if (mUpmixChannels.Length() != totalChannelCount) {
    mLastReadChunk = -1;  // invalidate cache
  }

  for (uint32_t channel = aFirstChannel; channel < readChannelsEnd; ++channel) {
    PodZero(aOutputChunk->ChannelFloatsForWrite(channel), WEBAUDIO_BLOCK_SIZE);
  }

  for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
    float currentDelay = aPerFrameDelays[i];
    MOZ_ASSERT(currentDelay >= 0.0f);
    MOZ_ASSERT(currentDelay <= (mChunks.Length() - 1) * WEBAUDIO_BLOCK_SIZE);

    // Interpolate two input frames in case the read position does not match
    // an integer index.
    // Use the larger delay, for the older frame, first, as this is more
    // likely to use the cached upmixed channel arrays.
    int floorDelay = int(currentDelay);
    float interpolationFactor = currentDelay - floorDelay;
    int positions[2];
    positions[1] = PositionForDelay(floorDelay) + i;
    positions[0] = positions[1] - 1;

    for (unsigned tick = 0; tick < ArrayLength(positions); ++tick) {
      int readChunk = ChunkForPosition(positions[tick]);
      // The zero check on interpolationFactor is important because, when
      // currentDelay is integer, positions[0] may be outside the range
      // considered for determining totalChannelCount.
      // mVolume is not set on default initialized chunks so also handle null
      // chunks specially.
      if (interpolationFactor != 0.0f && !mChunks[readChunk].IsNull()) {
        int readOffset = OffsetForPosition(positions[tick]);
        UpdateUpmixChannels(readChunk, totalChannelCount,
                            aChannelInterpretation);
        float multiplier = interpolationFactor * mChunks[readChunk].mVolume;
        for (uint32_t channel = aFirstChannel; channel < readChannelsEnd;
             ++channel) {
          aOutputChunk->ChannelFloatsForWrite(channel)[i] +=
              multiplier * mUpmixChannels[channel][readOffset];
        }
      }

      interpolationFactor = 1.0f - interpolationFactor;
    }
  }
}

void DelayBuffer::Read(float aDelayTicks, AudioBlock* aOutputChunk,
                       ChannelInterpretation aChannelInterpretation) {
  float computedDelay[WEBAUDIO_BLOCK_SIZE];

  for (unsigned i = 0; i < WEBAUDIO_BLOCK_SIZE; ++i) {
    computedDelay[i] = aDelayTicks;
  }

  Read(computedDelay, aOutputChunk, aChannelInterpretation);
}

bool DelayBuffer::EnsureBuffer() {
  if (mChunks.Length() == 0) {
    // The length of the buffer is at least one block greater than the maximum
    // delay so that writing an input block does not overwrite the block that
    // would subsequently be read at maximum delay.  Also round up to the next
    // block size, so that no block of writes will need to wrap.
    const int chunkCount = (mMaxDelayTicks + 2 * WEBAUDIO_BLOCK_SIZE - 1) >>
                           WEBAUDIO_BLOCK_SIZE_BITS;
    if (!mChunks.SetLength(chunkCount, fallible)) {
      return false;
    }

    mLastReadChunk = -1;
  }
  return true;
}

int DelayBuffer::PositionForDelay(int aDelay) {
  // Adding mChunks.Length() keeps integers positive for defined and
  // appropriate bitshift, remainder, and bitwise operations.
  return ((mCurrentChunk + mChunks.Length()) * WEBAUDIO_BLOCK_SIZE) - aDelay;
}

int DelayBuffer::ChunkForPosition(int aPosition) {
  MOZ_ASSERT(aPosition >= 0);
  return (aPosition >> WEBAUDIO_BLOCK_SIZE_BITS) % mChunks.Length();
}

int DelayBuffer::OffsetForPosition(int aPosition) {
  MOZ_ASSERT(aPosition >= 0);
  return aPosition & (WEBAUDIO_BLOCK_SIZE - 1);
}

int DelayBuffer::ChunkForDelay(int aDelay) {
  return ChunkForPosition(PositionForDelay(aDelay));
}

void DelayBuffer::UpdateUpmixChannels(
    int aNewReadChunk, uint32_t aChannelCount,
    ChannelInterpretation aChannelInterpretation) {
  if (aNewReadChunk == mLastReadChunk) {
    MOZ_ASSERT(mUpmixChannels.Length() == aChannelCount);
    return;
  }

  NS_WARNING_ASSERTION(mHaveWrittenBlock || aNewReadChunk != mCurrentChunk,
                       "Smoothing is making feedback delay too small.");

  mLastReadChunk = aNewReadChunk;
  mUpmixChannels = mChunks[aNewReadChunk].ChannelData<float>().Clone();
  MOZ_ASSERT(mUpmixChannels.Length() <= aChannelCount);
  if (mUpmixChannels.Length() < aChannelCount) {
    if (aChannelInterpretation == ChannelInterpretation::Speakers) {
      AudioChannelsUpMix(&mUpmixChannels, aChannelCount,
                         SilentChannel::ZeroChannel<float>());
      MOZ_ASSERT(mUpmixChannels.Length() == aChannelCount,
                 "We called GetAudioChannelsSuperset to avoid this");
    } else {
      // Fill up the remaining channels with zeros
      for (uint32_t channel = mUpmixChannels.Length(); channel < aChannelCount;
           ++channel) {
        mUpmixChannels.AppendElement(SilentChannel::ZeroChannel<float>());
      }
    }
  }
}

}  // namespace mozilla