File: OscillatorNode.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (454 lines) | stat: -rw-r--r-- 17,293 bytes parent folder | download | duplicates (2)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. 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 INC. 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 "OscillatorNode.h"

#include "AudioNodeOutput.h"
#include "AudioParam.h"
#include "AudioUtilities.h"
#include "PeriodicWave.h"
#include "VectorMath.h"
#include <wtf/IsoMallocInlines.h>

namespace WebCore {

WTF_MAKE_ISO_ALLOCATED_IMPL(OscillatorNode);

// Breakpoints where we deicde to do linear interoplation, 3-point interpolation or 5-point interpolation. See doInterpolation().
constexpr float interpolate2Point = 0.3;
constexpr float interpolate3Point = 0.16;

// Convert the detune value (in cents) to a frequency scale multiplier: 2^(d/1200).
static inline float detuneToFrequencyMultiplier(float detuneValue)
{
    return std::exp2(detuneValue / 1200);
}

// Clamp the frequency value to lie within Nyquist frequency. For NaN, arbitrarily clamp to +Nyquist.
static void clampFrequency(float* frequency, size_t framesToProcess, float nyquist)
{
    for (size_t k = 0; k < framesToProcess; ++k) {
        float f = frequency[k];
        frequency[k] = std::isnan(f) ? nyquist : clampTo(f, -nyquist, nyquist);
    }
}

ExceptionOr<Ref<OscillatorNode>> OscillatorNode::create(BaseAudioContext& context, const OscillatorOptions& options)
{
    if (options.type == OscillatorType::Custom && !options.periodicWave)
        return Exception { InvalidStateError, "Must provide periodicWave when using custom type."_s };
    
    auto oscillator = adoptRef(*new OscillatorNode(context, options));
    oscillator->suspendIfNeeded();

    auto result = oscillator->handleAudioNodeOptions(options, { 2, ChannelCountMode::Max, ChannelInterpretation::Speakers });
    if (result.hasException())
        return result.releaseException();
    
    if (options.periodicWave)
        oscillator->setPeriodicWave(*options.periodicWave);
    else {
        result = oscillator->setTypeForBindings(options.type);
        if (result.hasException())
            return result.releaseException();
    }

    return oscillator;
}

OscillatorNode::OscillatorNode(BaseAudioContext& context, const OscillatorOptions& options)
    : AudioScheduledSourceNode(context, NodeTypeOscillator)
    , m_frequency(AudioParam::create(context, "frequency"_s, options.frequency, -context.sampleRate() / 2, context.sampleRate() / 2, AutomationRate::ARate))
    , m_detune(AudioParam::create(context, "detune"_s, options.detune, -1200 * log2f(std::numeric_limits<float>::max()), 1200 * log2f(std::numeric_limits<float>::max()), AutomationRate::ARate))
    , m_phaseIncrements(AudioUtilities::renderQuantumSize)
    , m_detuneValues(AudioUtilities::renderQuantumSize)
{
    // An oscillator is always mono.
    addOutput(1);
    initialize();
}

OscillatorNode::~OscillatorNode()
{
    uninitialize();
}

ExceptionOr<void> OscillatorNode::setTypeForBindings(OscillatorType type)
{
    ALWAYS_LOG(LOGIDENTIFIER, type);
    ASSERT(isMainThread());

    if (type == OscillatorType::Custom) {
        if (m_type != OscillatorType::Custom)
            return Exception { InvalidStateError, "OscillatorNode.type cannot be changed to 'custom'"_s };
        return { };
    }

    setPeriodicWave(context().periodicWave(type));
    m_type = type;

    return { };
}

bool OscillatorNode::calculateSampleAccuratePhaseIncrements(size_t framesToProcess)
{
    bool isGood = framesToProcess <= m_phaseIncrements.size() && framesToProcess <= m_detuneValues.size();
    ASSERT(isGood);
    if (!isGood)
        return false;

    if (m_firstRender) {
        m_firstRender = false;
        m_frequency->resetSmoothedValue();
        m_detune->resetSmoothedValue();
    }

    bool hasSampleAccurateValues = false;
    bool hasFrequencyChanges = false;
    float* phaseIncrements = m_phaseIncrements.data();

    float finalScale = m_periodicWave->rateScale();

    if (m_frequency->hasSampleAccurateValues() && m_frequency->automationRate() == AutomationRate::ARate) {
        hasSampleAccurateValues = true;
        hasFrequencyChanges = true;

        // Get the sample-accurate frequency values and convert to phase increments.
        // They will be converted to phase increments below.
        m_frequency->calculateSampleAccurateValues(phaseIncrements, framesToProcess);
    } else {
        float frequency = m_frequency->finalValue();
        finalScale *= frequency;
    }

    if (m_detune->hasSampleAccurateValues() && m_detune->automationRate() == AutomationRate::ARate) {
        hasSampleAccurateValues = true;

        // Get the sample-accurate detune values.
        float* detuneValues = hasFrequencyChanges ? m_detuneValues.data() : phaseIncrements;
        m_detune->calculateSampleAccurateValues(detuneValues, framesToProcess);

        // Convert from cents to rate scalar.
        VectorMath::multiplyByScalar(detuneValues, 1.0 / 1200, detuneValues, framesToProcess);
        for (unsigned i = 0; i < framesToProcess; ++i)
            detuneValues[i] = std::exp2(detuneValues[i]);

        if (hasFrequencyChanges) {
            // Multiply frequencies by detune scalings.
            VectorMath::multiply(detuneValues, phaseIncrements, phaseIncrements, framesToProcess);
        }
    } else {
        float detune = m_detune->finalValue();
        float detuneScale = detuneToFrequencyMultiplier(detune);
        finalScale *= detuneScale;
    }

    if (hasSampleAccurateValues) {
        clampFrequency(phaseIncrements, framesToProcess, context().sampleRate() / 2);
        // Convert from frequency to wave increment.
        VectorMath::multiplyByScalar(phaseIncrements, finalScale, phaseIncrements, framesToProcess);
    }

    return hasSampleAccurateValues;
}

static float doInterpolation(double virtualReadIndex, float incr, unsigned readIndexMask, float tableInterpolationFactor, const float* lowerWaveData, const float* higherWaveData)
{
    ASSERT(incr >= 0);
    ASSERT(std::isfinite(virtualReadIndex));

    double sampleLower = 0;
    double sampleHigher = 0;

    unsigned readIndex0 = static_cast<unsigned>(virtualReadIndex);

    // Consider a typical sample rate of 44100 Hz and max periodic wave
    // size of 4096. The relationship between |incr| and the frequency
    // of the oscillator is |incr| = freq * 4096/44100. Or freq =
    // |incr|*44100/4096 = 10.8*|incr|.
    //
    // For the |incr| thresholds below, this means that we use linear
    // interpolation for all freq >= 3.2 Hz, 3-point Lagrange
    // for freq >= 1.7 Hz and 5-point Lagrange for every thing else.
    //
    // We use Lagrange interpolation because it's relatively simple to
    // implement and fairly inexpensive, and the interpolator always
    // passes through known points.
    if (incr >= interpolate2Point) {
        // Increment is fairly large, so we're doing no more than about 3
        // points between each wave table entry. Assume linear
        // interpolation between points is good enough.
        unsigned readIndex2 = readIndex0 + 1;

        // Contain within valid range.
        readIndex0 = readIndex0 & readIndexMask;
        readIndex2 = readIndex2 & readIndexMask;

        float sample1Lower = lowerWaveData[readIndex0];
        float sample2Lower = lowerWaveData[readIndex2];
        float sample1Higher = higherWaveData[readIndex0];
        float sample2Higher = higherWaveData[readIndex2];

        // Linearly interpolate within each table (lower and higher).
        double interpolationFactor = static_cast<float>(virtualReadIndex) - readIndex0;
        sampleHigher = (1 - interpolationFactor) * sample1Higher + interpolationFactor * sample2Higher;
        sampleLower = (1 - interpolationFactor) * sample1Lower + interpolationFactor * sample2Lower;

    } else if (incr >= interpolate3Point) {
        // We're doing about 6 interpolation values between each wave
        // table sample. Just use a 3-point Lagrange interpolator to get a
        // better estimate than just linear.
        //
        // See 3-point formula in http://dlmf.nist.gov/3.3#ii
        unsigned readIndex[3];

        for (int k = -1; k <= 1; ++k)
            readIndex[k + 1] = (readIndex0 + k) & readIndexMask;

        double a[3];
        double t = virtualReadIndex - readIndex0;

        a[0] = 0.5 * t * (t - 1);
        a[1] = 1 - t * t;
        a[2] = 0.5 * t * (t + 1);

        for (int k = 0; k < 3; ++k) {
            sampleLower += a[k] * lowerWaveData[readIndex[k]];
            sampleHigher += a[k] * higherWaveData[readIndex[k]];
        }
    } else {
        // For everything else (more than 6 points per entry), we'll do a
        // 5-point Lagrange interpolator. This is a trade-off between
        // quality and speed.
        //
        // See 5-point formula in http://dlmf.nist.gov/3.3#ii
        unsigned readIndex[5];
        for (int k = -2; k <= 2; ++k)
            readIndex[k + 2] = (readIndex0 + k) & readIndexMask;

        double a[5];
        double t = virtualReadIndex - readIndex0;
        double t2 = t * t;

        a[0] = t * (t2 - 1) * (t - 2) / 24;
        a[1] = -t * (t - 1) * (t2 - 4) / 6;
        a[2] = (t2 - 1) * (t2 - 4) / 4;
        a[3] = -t * (t + 1) * (t2 - 4) / 6;
        a[4] = t * (t2 - 1) * (t + 2) / 24;

        for (int k = 0; k < 5; ++k) {
            sampleLower += a[k] * lowerWaveData[readIndex[k]];
            sampleHigher += a[k] * higherWaveData[readIndex[k]];
        }
    }

    // Then interpolate between the two tables.
    float sample = (1 - tableInterpolationFactor) * sampleHigher + tableInterpolationFactor * sampleLower;
    return sample;
}

double OscillatorNode::processARate(int n, float* destP, double virtualReadIndex, float* phaseIncrements)
{
    float rateScale = m_periodicWave->rateScale();
    float invRateScale = 1 / rateScale;
    unsigned periodicWaveSize = m_periodicWave->periodicWaveSize();
    double invPeriodicWaveSize = 1.0 / periodicWaveSize;
    unsigned readIndexMask = periodicWaveSize - 1;

    float* higherWaveData = nullptr;
    float* lowerWaveData = nullptr;
    float tableInterpolationFactor = 0;

    for (int k = 0; k < n; ++k) {
        float incr = *phaseIncrements++;

        float frequency = invRateScale * incr;
        m_periodicWave->waveDataForFundamentalFrequency(frequency, lowerWaveData, higherWaveData, tableInterpolationFactor);

        float sample = doInterpolation(virtualReadIndex, std::abs(incr), readIndexMask, tableInterpolationFactor, lowerWaveData, higherWaveData);

        *destP++ = sample;

        // Increment virtual read index and wrap virtualReadIndex into the range
        // 0 -> periodicWaveSize.
        virtualReadIndex += incr;
        virtualReadIndex -= floor(virtualReadIndex * invPeriodicWaveSize) * periodicWaveSize;
    }

    return virtualReadIndex;
}

double OscillatorNode::processKRate(int n, float* destP, double virtualReadIndex)
{
    unsigned periodicWaveSize = m_periodicWave->periodicWaveSize();
    double invPeriodicWaveSize = 1.0 / periodicWaveSize;
    unsigned readIndexMask = periodicWaveSize - 1;

    float frequency = 0;
    float* higherWaveData = nullptr;
    float* lowerWaveData = nullptr;
    float tableInterpolationFactor = 0;

    frequency = m_frequency->finalValue();
    float detune = m_detune->finalValue();
    float detuneScale = detuneToFrequencyMultiplier(detune);
    frequency *= detuneScale;
    clampFrequency(&frequency, 1, context().sampleRate() / 2);
    m_periodicWave->waveDataForFundamentalFrequency(frequency, lowerWaveData, higherWaveData, tableInterpolationFactor);

    float rateScale = m_periodicWave->rateScale();
    float incr = frequency * rateScale;

    for (int k = 0; k < n; ++k) {
        float sample = doInterpolation(virtualReadIndex, std::abs(incr), readIndexMask, tableInterpolationFactor, lowerWaveData, higherWaveData);

        *destP++ = sample;

        // Increment virtual read index and wrap virtualReadIndex into the range
        // 0 -> periodicWaveSize.
        virtualReadIndex += incr;
        virtualReadIndex -= floor(virtualReadIndex * invPeriodicWaveSize) * periodicWaveSize;
    }

    return virtualReadIndex;
}

void OscillatorNode::process(size_t framesToProcess)
{
    auto& outputBus = *output(0)->bus();

    if (!isInitialized() || !outputBus.numberOfChannels()) {
        outputBus.zero();
        return;
    }

    ASSERT(framesToProcess <= m_phaseIncrements.size());
    if (framesToProcess > m_phaseIncrements.size())
        return;

    // The audio thread can't block on this lock, so we use tryLock() instead.
    if (!m_processLock.tryLock()) {
        // Too bad - tryLock() failed. We must be in the middle of changing wave-tables.
        outputBus.zero();
        return;
    }
    Locker locker { AdoptLock, m_processLock };

    // We must access m_periodicWave only inside the lock.
    if (!m_periodicWave.get()) {
        outputBus.zero();
        return;
    }

    size_t quantumFrameOffset = 0;
    size_t nonSilentFramesToProcess = 0;
    double startFrameOffset = 0;
    updateSchedulingInfo(framesToProcess, outputBus, quantumFrameOffset, nonSilentFramesToProcess, startFrameOffset);

    if (!nonSilentFramesToProcess) {
        outputBus.zero();
        return;
    }

    float* destP = outputBus.channel(0)->mutableData();

    ASSERT(quantumFrameOffset <= framesToProcess);

    // We keep virtualReadIndex double-precision since we're accumulating values.
    double virtualReadIndex = m_virtualReadIndex;

    float rateScale = m_periodicWave->rateScale();
    bool hasSampleAccurateValues = calculateSampleAccuratePhaseIncrements(framesToProcess);

    float frequency = 0;
    float* higherWaveData = nullptr;
    float* lowerWaveData = nullptr;
    float tableInterpolationFactor = 0;

    if (!hasSampleAccurateValues) {
        frequency = m_frequency->finalValue();
        float detune = m_detune->finalValue();
        float detuneScale = detuneToFrequencyMultiplier(detune);
        frequency *= detuneScale;
        clampFrequency(&frequency, 1, context().sampleRate() / 2);
        m_periodicWave->waveDataForFundamentalFrequency(frequency, lowerWaveData, higherWaveData, tableInterpolationFactor);
    }

    float* phaseIncrements = m_phaseIncrements.data();

    // Start rendering at the correct offset.
    destP += quantumFrameOffset;
    int n = nonSilentFramesToProcess;

    // If startFrameOffset is not 0, that means the oscillator doesn't actually
    // start at quantumFrameOffset, but just past that time. Adjust destP and n
    // to reflect that, and adjust virtualReadIndex to start the value at
    // startFrameOffset.
    if (startFrameOffset > 0) {
        ++destP;
        --n;
        virtualReadIndex += (1 - startFrameOffset) * frequency * rateScale;
        ASSERT(virtualReadIndex < m_periodicWave->periodicWaveSize());
    } else if (startFrameOffset < 0)
        virtualReadIndex = -startFrameOffset * frequency * rateScale;

    if (hasSampleAccurateValues)
        virtualReadIndex = processARate(n, destP, virtualReadIndex, phaseIncrements);
    else
        virtualReadIndex = processKRate(n, destP, virtualReadIndex);

    m_virtualReadIndex = virtualReadIndex;

    outputBus.clearSilentFlag();
}

void OscillatorNode::setPeriodicWave(PeriodicWave& periodicWave)
{
    ALWAYS_LOG(LOGIDENTIFIER, "sample rate = ", periodicWave.sampleRate(), ", wave size = ", periodicWave.periodicWaveSize(), ", rate scale = ", periodicWave.rateScale());
    ASSERT(isMainThread());
    
    // This synchronizes with process().
    Locker locker { m_processLock };
    m_periodicWave = &periodicWave;
    m_type = OscillatorType::Custom;
}

bool OscillatorNode::propagatesSilence() const
{
    ASSERT(context().isAudioThread());
    if (!isPlayingOrScheduled() || hasFinished())
        return true;
    if (!m_processLock.tryLock())
        return false; // Assume we have a periodic wave if we are unable to grab the lock.
    Locker locker { AdoptLock, m_processLock };
    return !m_periodicWave.get();
}

} // namespace WebCore

#endif // ENABLE(WEB_AUDIO)