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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "PeriodicWave.h"
#include <JavaScriptCore/TypedArrays.h>
#include "BaseAudioContext.h"
#include "FFTFrame.h"
#include "VectorMath.h"
#include <algorithm>
#include <wtf/StdLibExtras.h>
// The number of bands per octave. Each octave will have this many entries in the wave tables.
constexpr unsigned NumberOfOctaveBands = 3;
// The max length of a periodic wave. This must be a power of two greater than
// or equal to 2048 and must be supported by the FFT routines.
constexpr unsigned MaxPeriodicWaveSize = 16384;
constexpr float CentsPerRange = 1200 / NumberOfOctaveBands;
namespace WebCore {
Ref<PeriodicWave> PeriodicWave::create(float sampleRate, Float32Array& real, Float32Array& imaginary)
{
ASSERT(real.length() == imaginary.length());
auto waveTable = adoptRef(*new PeriodicWave(sampleRate));
waveTable->createBandLimitedTables(real.typedSpan(), imaginary.typedSpan());
return waveTable;
}
ExceptionOr<Ref<PeriodicWave>> PeriodicWave::create(BaseAudioContext& context, PeriodicWaveOptions&& options)
{
Vector<float> real;
Vector<float> imag;
if (options.real && options.imag) {
if (options.real->size() != options.imag->size())
return Exception { ExceptionCode::IndexSizeError, "real and imag have different lengths"_s };
if (options.real->size() < 2)
return Exception { ExceptionCode::IndexSizeError, "real's length cannot be less than 2"_s };
if (options.imag->size() < 2)
return Exception { ExceptionCode::IndexSizeError, "imag's length cannot be less than 2"_s };
real = WTFMove(*options.real);
imag = WTFMove(*options.imag);
} else if (options.real) {
if (options.real->size() < 2)
return Exception { ExceptionCode::IndexSizeError, "real's length cannot be less than 2"_s };
real = WTFMove(*options.real);
imag.fill(0, real.size());
} else if (options.imag) {
if (options.imag->size() < 2)
return Exception { ExceptionCode::IndexSizeError, "imag's length cannot be less than 2"_s };
imag = WTFMove(*options.imag);
real.fill(0, imag.size());
} else {
real.fill(0, 2);
imag.fill(0, 2);
imag[1] = 1;
}
real[0] = 0;
imag[0] = 0;
auto waveTable = adoptRef(*new PeriodicWave(context.sampleRate()));
waveTable->createBandLimitedTables(real, imag, options.disableNormalization ? ShouldDisableNormalization::Yes : ShouldDisableNormalization::No);
return waveTable;
}
Ref<PeriodicWave> PeriodicWave::createSine(float sampleRate)
{
Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
waveTable->generateBasicWaveform(Type::Sine);
return waveTable;
}
Ref<PeriodicWave> PeriodicWave::createSquare(float sampleRate)
{
Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
waveTable->generateBasicWaveform(Type::Square);
return waveTable;
}
Ref<PeriodicWave> PeriodicWave::createSawtooth(float sampleRate)
{
Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
waveTable->generateBasicWaveform(Type::Sawtooth);
return waveTable;
}
Ref<PeriodicWave> PeriodicWave::createTriangle(float sampleRate)
{
Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
waveTable->generateBasicWaveform(Type::Triangle);
return waveTable;
}
PeriodicWave::PeriodicWave(float sampleRate)
: m_sampleRate(sampleRate)
, m_numberOfRanges(0.5 + NumberOfOctaveBands * log2f(periodicWaveSize()))
{
float nyquist = 0.5 * m_sampleRate;
m_lowestFundamentalFrequency = nyquist / maxNumberOfPartials();
m_rateScale = periodicWaveSize() / m_sampleRate;
}
void PeriodicWave::waveDataForFundamentalFrequency(float fundamentalFrequency, std::span<float>& lowerWaveData, std::span<float>& higherWaveData, float& tableInterpolationFactor)
{
// Negative frequencies are allowed, in which case we alias to the positive frequency.
fundamentalFrequency = std::abs(fundamentalFrequency);
// Calculate the pitch range.
float ratio = fundamentalFrequency > 0 ? fundamentalFrequency / m_lowestFundamentalFrequency : 0.5;
float centsAboveLowestFrequency = log2f(ratio) * 1200;
// Add one to round-up to the next range just in time to truncate partials before aliasing occurs.
float pitchRange = 1 + centsAboveLowestFrequency / CentsPerRange;
pitchRange = std::max(pitchRange, 0.0f);
pitchRange = std::min(pitchRange, static_cast<float>(m_numberOfRanges - 1));
// The words "lower" and "higher" refer to the table data having the lower and higher numbers of partials.
// It's a little confusing since the range index gets larger the more partials we cull out.
// So the lower table data will have a larger range index.
unsigned rangeIndex1 = static_cast<unsigned>(pitchRange);
unsigned rangeIndex2 = rangeIndex1 < m_numberOfRanges - 1 ? rangeIndex1 + 1 : rangeIndex1;
lowerWaveData = m_bandLimitedTables[rangeIndex2]->span();
higherWaveData = m_bandLimitedTables[rangeIndex1]->span();
// Ranges from 0 -> 1 to interpolate between lower -> higher.
tableInterpolationFactor = pitchRange - rangeIndex1;
}
unsigned PeriodicWave::maxNumberOfPartials() const
{
return periodicWaveSize() / 2;
}
unsigned PeriodicWave::numberOfPartialsForRange(unsigned rangeIndex) const
{
// Number of cents below nyquist where we cull partials.
float centsToCull = rangeIndex * CentsPerRange;
// A value from 0 -> 1 representing what fraction of the partials to keep.
float cullingScale = pow(2, -centsToCull / 1200);
// The very top range will have all the partials culled.
unsigned numberOfPartials = cullingScale * maxNumberOfPartials();
return numberOfPartials;
}
// Convert into time-domain wave tables.
// One table is created for each range for non-aliasing playback at different playback rates.
// Thus, higher ranges have more high-frequency partials culled out.
void PeriodicWave::createBandLimitedTables(std::span<const float> realData, std::span<const float> imagData, ShouldDisableNormalization disableNormalization)
{
float normalizationScale = 0.5;
size_t fftSize = periodicWaveSize();
size_t halfSize = fftSize / 2;
size_t numberOfComponents = realData.size();
numberOfComponents = std::min(numberOfComponents, halfSize);
m_bandLimitedTables.reserveCapacity(m_numberOfRanges);
for (unsigned rangeIndex = 0; rangeIndex < m_numberOfRanges; ++rangeIndex) {
// This FFTFrame is used to cull partials (represented by frequency bins).
FFTFrame frame(fftSize);
auto& realP = frame.realData();
auto& imagP = frame.imagData();
RELEASE_ASSERT(realP.size() >= numberOfComponents);
RELEASE_ASSERT(imagP.size() >= numberOfComponents);
// Copy from loaded frequency data and scale.
VectorMath::multiplyByScalar(realData.first(numberOfComponents), fftSize, realP.span());
VectorMath::multiplyByScalar(imagData.first(numberOfComponents), -static_cast<float>(fftSize), imagP.span());
// Find the starting bin where we should start culling.
// We need to clear out the highest frequencies to band-limit the waveform.
size_t numberOfPartials = numberOfPartialsForRange(rangeIndex);
// If fewer components were provided than 1/2 FFT size, then clear the
// remaining bins. We also need to cull the aliasing partials for this
// pitch range.
size_t clampedNumberOfComponents = std::min(numberOfComponents, numberOfPartials + 1);
if (clampedNumberOfComponents < halfSize) {
size_t numValues = halfSize - clampedNumberOfComponents;
zeroSpan(realP.span().subspan(clampedNumberOfComponents, numValues));
zeroSpan(imagP.span().subspan(clampedNumberOfComponents, numValues));
}
// Clear packed-nyquist and any DC-offset.
realP[0] = 0;
imagP[0] = 0;
// Create the band-limited table.
m_bandLimitedTables.append(makeUnique<AudioFloatArray>(fftSize));
// Apply an inverse FFT to generate the time-domain table data.
auto data = m_bandLimitedTables[rangeIndex]->span();
frame.doInverseFFT(data);
// For the first range (which has the highest power), calculate its peak value then compute normalization scale.
if (disableNormalization == ShouldDisableNormalization::No) {
if (!rangeIndex) {
float maxValue = VectorMath::maximumMagnitude(data);
if (maxValue)
normalizationScale = 1.0f / maxValue;
}
}
// Apply normalization scale.
VectorMath::multiplyByScalar(data, normalizationScale, data);
}
}
void PeriodicWave::generateBasicWaveform(Type shape)
{
unsigned fftSize = periodicWaveSize();
unsigned halfSize = fftSize / 2;
AudioFloatArray real(halfSize);
AudioFloatArray imag(halfSize);
auto realP = real.span();
auto imagP = imag.span();
// Clear DC and Nyquist.
realP[0] = 0;
imagP[0] = 0;
for (unsigned n = 1; n < halfSize; ++n) {
float piFactor = 2 / (n * piFloat);
// All waveforms are odd functions with a positive slope at time 0. Hence
// the coefficients for cos() are always 0.
// Fourier coefficients according to standard definition:
// b = 1/pi*integrate(f(x)*sin(n*x), x, -pi, pi)
// = 2/pi*integrate(f(x)*sin(n*x), x, 0, pi)
// since f(x) is an odd function.
float b; // Coefficient for sin().
// Calculate Fourier coefficients depending on the shape.
// Note that the overall scaling (magnitude) of the waveforms is normalized in createBandLimitedTables().
switch (shape) {
case Type::Sine:
// Standard sine wave function.
b = (n == 1) ? 1 : 0;
break;
case Type::Square:
// Square-shaped waveform with the first half its maximum value and the
// second half its minimum value.
//
// See http://mathworld.wolfram.com/FourierSeriesSquareWave.html
//
// b[n] = 2/n/pi*(1-(-1)^n)
// = 4/n/pi for n odd and 0 otherwise.
// = 2*(2/(n*pi)) for n odd
b = (n & 1) ? 2 * piFactor : 0;
break;
case Type::Sawtooth:
// Sawtooth-shaped waveform with the first half ramping from zero to
// maximum and the second half from minimum to zero.
//
// b[n] = -2*(-1)^n/pi/n
// = (2/(n*pi))*(-1)^(n+1)
b = piFactor * ((n & 1) ? 1 : -1);
break;
case Type::Triangle:
// Triangle-shaped waveform going from 0 at time 0 to 1 at time pi/2 and
// back to 0 at time pi.
//
// See http://mathworld.wolfram.com/FourierSeriesTriangleWave.html
//
// b[n] = 8*sin(pi*k/2)/(pi*k)^2
// = 8/pi^2/n^2*(-1)^((n-1)/2) for n odd and 0 otherwise
// = 2*(2/(n*pi))^2 * (-1)^((n-1)/2)
if (n & 1)
b = 2 * (piFactor * piFactor) * ((((n - 1) >> 1) & 1) ? -1 : 1);
else
b = 0;
break;
}
realP[n] = 0;
imagP[n] = b;
}
createBandLimitedTables(realP, imagP);
}
unsigned PeriodicWave::periodicWaveSize() const
{
// Choose an appropriate wave size for the given sample rate. This allows us
// to use shorter FFTs when possible to limit the complexity. The breakpoints
// here are somewhat arbitrary, but we want sample rates around 44.1 kHz or so
// to have a size of 4096 to preserve backward compatibility.
if (m_sampleRate <= 24000)
return 2048;
if (m_sampleRate <= 88200)
return 4096;
return MaxPeriodicWaveSize;
}
} // namespace WebCore
#endif // ENABLE(WEB_AUDIO)
|