Bug 1240054 - Only rebuild BandLimitedTables if more partials are required r=padenot

We currently rebuild the BandLimitedTables whenever we encounter a lower
fundamental frequency but it is only necessary to rebuild the tables if we
can fit more partials below the Nyquist frequency. Rebuilding the tables
unnecessarily can cause performance problems, particularly in the case where
the frequency is continually lowered.
This commit is contained in:
Dan Minor 2016-01-18 08:51:29 -05:00
parent 53250aff04
commit 8e008f9729
2 changed files with 10 additions and 4 deletions

View File

@ -109,7 +109,7 @@ PeriodicWave::createTriangle(float sampleRate)
PeriodicWave::PeriodicWave(float sampleRate, size_t numberOfComponents)
: m_sampleRate(sampleRate)
, m_centsPerRange(CentsPerRange)
, m_lowestRequestedFundamentalFrequency(std::numeric_limits<float>::max())
, m_maxPartialsInBandLimitedTable(0)
, m_normalizationScale(1.0f)
{
float nyquist = 0.5 * m_sampleRate;
@ -148,7 +148,13 @@ void PeriodicWave::waveDataForFundamentalFrequency(float fundamentalFrequency, f
// to the positive frequency.
fundamentalFrequency = fabsf(fundamentalFrequency);
if (fundamentalFrequency < m_lowestRequestedFundamentalFrequency) {
// We only need to rebuild to the tables if the new fundamental
// frequency is low enough to allow for more partials below the
// Nyquist frequency.
unsigned numberOfPartials = numberOfPartialsForRange(0);
float nyquist = 0.5 * m_sampleRate;
numberOfPartials = std::min(numberOfPartials, (unsigned)(nyquist / fundamentalFrequency));
if (numberOfPartials > m_maxPartialsInBandLimitedTable) {
for (unsigned rangeIndex = 0; rangeIndex < m_numberOfRanges; ++rangeIndex) {
m_bandLimitedTables[rangeIndex] = 0;
}
@ -156,7 +162,7 @@ void PeriodicWave::waveDataForFundamentalFrequency(float fundamentalFrequency, f
// We need to create the first table to determine the normalization
// constant.
createBandLimitedTables(fundamentalFrequency, 0);
m_lowestRequestedFundamentalFrequency = fundamentalFrequency;
m_maxPartialsInBandLimitedTable = numberOfPartials;
}
// Calculate the pitch range.

View File

@ -106,7 +106,7 @@ private:
// Creates table for specified index based on fundamental frequency.
void createBandLimitedTables(float fundamentalFrequency, unsigned rangeIndex);
float m_lowestRequestedFundamentalFrequency;
unsigned m_maxPartialsInBandLimitedTable;
float m_normalizationScale;
nsTArray<nsAutoPtr<AlignedAudioFloatArray> > m_bandLimitedTables;
};