Commit 1619b2ce authored by bratell's avatar bratell Committed by Commit bot

Report PeriodicWave memory usage to v8 so GC can be properly scheduled

A PeriodicWave object can use half a MB and v8 needs to know about that
or it will not schedule garbage collects when memory usage increases.

BUG=578351

Review URL: https://codereview.chromium.org/1632753002

Cr-Commit-Position: refs/heads/master@{#371777}
parent 27521495
......@@ -87,7 +87,8 @@ PeriodicWave* PeriodicWave::createTriangle(float sampleRate)
}
PeriodicWave::PeriodicWave(float sampleRate)
: m_sampleRate(sampleRate)
: m_v8ExternalMemory(0)
, m_sampleRate(sampleRate)
, m_centsPerRange(CentsPerRange)
{
float nyquist = 0.5 * m_sampleRate;
......@@ -98,6 +99,11 @@ PeriodicWave::PeriodicWave(float sampleRate)
m_numberOfRanges = 0.5 + kNumberOfOctaveBands * log2f(periodicWaveSize());
}
PeriodicWave::~PeriodicWave()
{
adjustV8ExternalMemory(-static_cast<int64_t>(m_v8ExternalMemory));
}
unsigned PeriodicWave::periodicWaveSize() const
{
// Choose an appropriate wave size for the given sample rate. This allows us to use shorter
......@@ -162,6 +168,13 @@ unsigned PeriodicWave::numberOfPartialsForRange(unsigned rangeIndex) const
return numberOfPartials;
}
// Tell V8 about the memory we're using so it can properly schedule garbage collects.
void PeriodicWave::adjustV8ExternalMemory(int delta)
{
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(delta);
m_v8ExternalMemory += delta;
}
// Convert into time-domain wave buffers.
// 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.
......@@ -208,7 +221,9 @@ void PeriodicWave::createBandLimitedTables(const float* realData, const float* i
imagP[0] = 0;
// Create the band-limited table.
OwnPtr<AudioFloatArray> table = adoptPtr(new AudioFloatArray(periodicWaveSize()));
unsigned waveSize = periodicWaveSize();
OwnPtr<AudioFloatArray> table = adoptPtr(new AudioFloatArray(waveSize));
adjustV8ExternalMemory(waveSize * sizeof(float));
m_bandLimitedTables.append(table.release());
// Apply an inverse FFT to generate the time-domain table data.
......
......@@ -48,6 +48,8 @@ public:
// Creates an arbitrary periodic wave given the frequency components (Fourier coefficients).
static PeriodicWave* create(float sampleRate, DOMFloat32Array* real, DOMFloat32Array* imag, bool normalize);
virtual ~PeriodicWave();
// Returns pointers to the lower and higher wave data for the pitch range containing
// the given fundamental frequency. These two tables are in adjacent "pitch" ranges
// where the higher table will have the maximum number of partials which won't alias when played back
......@@ -72,6 +74,8 @@ private:
void generateBasicWaveform(int);
size_t m_v8ExternalMemory;
float m_sampleRate;
unsigned m_numberOfRanges;
float m_centsPerRange;
......@@ -88,6 +92,8 @@ private:
unsigned numberOfPartialsForRange(unsigned rangeIndex) const;
void adjustV8ExternalMemory(int delta);
// Creates tables based on numberOfComponents Fourier coefficients.
void createBandLimitedTables(const float* real, const float* imag, unsigned numberOfComponents, bool disableNormalization);
Vector<OwnPtr<AudioFloatArray>> m_bandLimitedTables;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment