1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/PeriodicWave.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* 1.5 + * Copyright (C) 2012 Google Inc. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#ifndef PeriodicWave_h 1.33 +#define PeriodicWave_h 1.34 + 1.35 +#include "mozilla/dom/OscillatorNodeBinding.h" 1.36 +#include <nsAutoPtr.h> 1.37 +#include <nsTArray.h> 1.38 +#include "mozilla/MemoryReporting.h" 1.39 + 1.40 +namespace WebCore { 1.41 + 1.42 +typedef nsTArray<float> AudioFloatArray; 1.43 + 1.44 +class PeriodicWave { 1.45 +public: 1.46 + static PeriodicWave* createSine(float sampleRate); 1.47 + static PeriodicWave* createSquare(float sampleRate); 1.48 + static PeriodicWave* createSawtooth(float sampleRate); 1.49 + static PeriodicWave* createTriangle(float sampleRate); 1.50 + 1.51 + // Creates an arbitrary periodic wave given the frequency components 1.52 + // (Fourier coefficients). 1.53 + static PeriodicWave* create(float sampleRate, 1.54 + const float* real, 1.55 + const float* imag, 1.56 + size_t numberOfComponents); 1.57 + 1.58 + // Returns pointers to the lower and higher wave data for the pitch range 1.59 + // containing the given fundamental frequency. These two tables are in 1.60 + // adjacent "pitch" ranges where the higher table will have the maximum 1.61 + // number of partials which won't alias when played back at this 1.62 + // fundamental frequency. The lower wave is the next range containing fewer 1.63 + // partials than the higher wave. Interpolation between these two tables 1.64 + // can be made according to tableInterpolationFactor. Where values 1.65 + // from 0 -> 1 interpolate between lower -> higher. 1.66 + void waveDataForFundamentalFrequency(float, float* &lowerWaveData, float* &higherWaveData, float& tableInterpolationFactor); 1.67 + 1.68 + // Returns the scalar multiplier to the oscillator frequency to calculate 1.69 + // wave buffer phase increment. 1.70 + float rateScale() const { return m_rateScale; } 1.71 + 1.72 + unsigned periodicWaveSize() const { return m_periodicWaveSize; } 1.73 + float sampleRate() const { return m_sampleRate; } 1.74 + 1.75 + size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.76 + 1.77 +private: 1.78 + explicit PeriodicWave(float sampleRate); 1.79 + 1.80 + void generateBasicWaveform(mozilla::dom::OscillatorType); 1.81 + 1.82 + float m_sampleRate; 1.83 + unsigned m_periodicWaveSize; 1.84 + unsigned m_numberOfRanges; 1.85 + float m_centsPerRange; 1.86 + 1.87 + // The lowest frequency (in Hertz) where playback will include all of the 1.88 + // partials. Playing back lower than this frequency will gradually lose 1.89 + // more high-frequency information. 1.90 + // This frequency is quite low (~10Hz @ // 44.1KHz) 1.91 + float m_lowestFundamentalFrequency; 1.92 + 1.93 + float m_rateScale; 1.94 + 1.95 + unsigned numberOfRanges() const { return m_numberOfRanges; } 1.96 + 1.97 + // Maximum possible number of partials (before culling). 1.98 + unsigned maxNumberOfPartials() const; 1.99 + 1.100 + unsigned numberOfPartialsForRange(unsigned rangeIndex) const; 1.101 + 1.102 + // Creates tables based on numberOfComponents Fourier coefficients. 1.103 + void createBandLimitedTables(const float* real, const float* imag, unsigned numberOfComponents); 1.104 + nsTArray<nsAutoPtr<AudioFloatArray> > m_bandLimitedTables; 1.105 +}; 1.106 + 1.107 +} // namespace WebCore 1.108 + 1.109 +#endif // PeriodicWave_h