[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / Modules / webaudio / PeriodicWave.cpp
index a37d388..ec9a17c 100644 (file)
@@ -33,7 +33,6 @@
 #include "PeriodicWave.h"
 
 #include "FFTFrame.h"
-#include "OscillatorNode.h"
 #include "VectorMath.h"
 #include <algorithm>
 
@@ -45,44 +44,40 @@ namespace WebCore {
     
 using namespace VectorMath;
 
-PassRefPtr<PeriodicWave> PeriodicWave::create(float sampleRate, Float32Array* real, Float32Array* imag)
+Ref<PeriodicWave> PeriodicWave::create(float sampleRate, Float32Array& real, Float32Array& imaginary)
 {
-    bool isGood = real && imag && real->length() == imag->length();
-    ASSERT(isGood);
-    if (isGood) {
-        RefPtr<PeriodicWave> waveTable = adoptRef(new PeriodicWave(sampleRate));
-        size_t numberOfComponents = real->length();
-        waveTable->createBandLimitedTables(real->data(), imag->data(), numberOfComponents);
-        return waveTable;
-    }
-    return nullptr;
+    ASSERT(real.length() == imaginary.length());
+
+    auto waveTable = adoptRef(*new PeriodicWave(sampleRate));
+    waveTable->createBandLimitedTables(real.data(), imaginary.data(), real.length());
+    return waveTable;
 }
 
-PassRefPtr<PeriodicWave> PeriodicWave::createSine(float sampleRate)
+Ref<PeriodicWave> PeriodicWave::createSine(float sampleRate)
 {
-    RefPtr<PeriodicWave> waveTable = adoptRef(new PeriodicWave(sampleRate));
-    waveTable->generateBasicWaveform(OscillatorNode::SINE);
+    Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
+    waveTable->generateBasicWaveform(Type::Sine);
     return waveTable;
 }
 
-PassRefPtr<PeriodicWave> PeriodicWave::createSquare(float sampleRate)
+Ref<PeriodicWave> PeriodicWave::createSquare(float sampleRate)
 {
-    RefPtr<PeriodicWave> waveTable = adoptRef(new PeriodicWave(sampleRate));
-    waveTable->generateBasicWaveform(OscillatorNode::SQUARE);
+    Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
+    waveTable->generateBasicWaveform(Type::Square);
     return waveTable;
 }
 
-PassRefPtr<PeriodicWave> PeriodicWave::createSawtooth(float sampleRate)
+Ref<PeriodicWave> PeriodicWave::createSawtooth(float sampleRate)
 {
-    RefPtr<PeriodicWave> waveTable = adoptRef(new PeriodicWave(sampleRate));
-    waveTable->generateBasicWaveform(OscillatorNode::SAWTOOTH);
+    Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
+    waveTable->generateBasicWaveform(Type::Sawtooth);
     return waveTable;
 }
 
-PassRefPtr<PeriodicWave> PeriodicWave::createTriangle(float sampleRate)
+Ref<PeriodicWave> PeriodicWave::createTriangle(float sampleRate)
 {
-    RefPtr<PeriodicWave> waveTable = adoptRef(new PeriodicWave(sampleRate));
-    waveTable->generateBasicWaveform(OscillatorNode::TRIANGLE);
+    Ref<PeriodicWave> waveTable = adoptRef(*new PeriodicWave(sampleRate));
+    waveTable->generateBasicWaveform(Type::Triangle);
     return waveTable;
 }
 
@@ -197,7 +192,7 @@ void PeriodicWave::createBandLimitedTables(const float* realData, const float* i
         realP[0] = 0;
 
         // Create the band-limited table.
-        m_bandLimitedTables.append(std::make_unique<AudioFloatArray>(m_periodicWaveSize));
+        m_bandLimitedTables.append(makeUnique<AudioFloatArray>(m_periodicWaveSize));
 
         // Apply an inverse FFT to generate the time-domain table data.
         float* data = m_bandLimitedTables[rangeIndex]->data();
@@ -217,7 +212,7 @@ void PeriodicWave::createBandLimitedTables(const float* realData, const float* i
     }
 }
 
-void PeriodicWave::generateBasicWaveform(int shape)
+void PeriodicWave::generateBasicWaveform(Type shape)
 {
     unsigned fftSize = periodicWaveSize();
     unsigned halfSize = fftSize / 2;
@@ -242,31 +237,26 @@ void PeriodicWave::generateBasicWaveform(int shape)
         // Calculate Fourier coefficients depending on the shape.
         // Note that the overall scaling (magnitude) of the waveforms is normalized in createBandLimitedTables().
         switch (shape) {
-        case OscillatorNode::SINE:
+        case Type::Sine:
             // Standard sine wave function.
             a = 0;
             b = (n == 1) ? 1 : 0;
             break;
-        case OscillatorNode::SQUARE:
+        case Type::Square:
             // Square-shaped waveform with the first half its maximum value and the second half its minimum value.
             a = 0;
             b = invOmega * ((n & 1) ? 2 : 0);
             break;
-        case OscillatorNode::SAWTOOTH:
+        case Type::Sawtooth:
             // Sawtooth-shaped waveform with the first half ramping from zero to maximum and the second half from minimum to zero.
             a = 0;
             b = -invOmega * cos(0.5 * omega);
             break;
-        case OscillatorNode::TRIANGLE:
+        case Type::Triangle:
             // Triangle-shaped waveform going from its maximum value to its minimum value then back to the maximum value.
             a = (4 - 4 * cos(0.5 * omega)) / (n * n * piFloat * piFloat);
             b = 0;
             break;
-        default:
-            ASSERT_NOT_REACHED();
-            a = 0;
-            b = 0;
-            break;
         }
 
         realP[n] = a;