Skip to content

Commit

Permalink
add FFTAnalyzerManager::getFFTDataFromScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Buisson committed Mar 7, 2024
1 parent 3248732 commit 72f8a10
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
46 changes: 43 additions & 3 deletions Source/Module/modules/audio/analysis/FFTAnalyzerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ FFTAnalyzerManager::FFTAnalyzerManager() :
maxDB = addFloatParameter("Max DB", "", 0, -100, 20);

selectItemWhenCreated = false;

//script
scriptObject.getDynamicObject()->setMethod("getFFTData", &FFTAnalyzerManager::getFFTDataFromScript);
}

FFTAnalyzerManager::~FFTAnalyzerManager()
Expand All @@ -45,20 +48,35 @@ void FFTAnalyzerManager::process(const float* samples, int numSamples)
auto mindB = minDB->floatValue();
auto maxdB = jmax<float>(maxDB->floatValue(), mindB);

float tmpScopeData[scopeSize];
for (int i = 0; i < scopeSize; ++i) // [3]
{
auto skewedProportionX = 1.0f - std::exp(std::log(1.0f - i / (float)scopeSize) * 0.2f);
auto fftDataIndex = jlimit(0, fftSize / 2, (int)(skewedProportionX * fftSize / 2));
auto level = jmap(jlimit(mindB, maxdB, Decibels::gainToDecibels(fftData[fftDataIndex]) - Decibels::gainToDecibels((float)fftSize)), mindB, maxdB, 0.0f, 1.0f);
scopeData[i] = level; // [4]
tmpScopeData[i] = level; // [4]
}

for (auto& i : items) i->process(scopeData, scopeSize);
for (auto& i : items)
{
i->process(tmpScopeData, scopeSize);
}

{
const ScopedLock lock(scopeDataMutex);
memcpy(scopeData, tmpScopeData, sizeof(scopeData));
}

nextFFTBlockReady = false;
}
}

void FFTAnalyzerManager::copyScopeData(float* scopeData, int maxSize) const
{
const ScopedLock lock(scopeDataMutex);
memcpy(scopeData, this->scopeData, std::min(maxSize * sizeof(*scopeData), sizeof(this->scopeData)));
}

void FFTAnalyzerManager::pushNextSampleIntoFifo(float sample)
{
if (fifoIndex == fftSize) // [11]
Expand All @@ -77,4 +95,26 @@ void FFTAnalyzerManager::pushNextSampleIntoFifo(float sample)
InspectableEditor* FFTAnalyzerManager::getEditorInternal(bool isRoot, Array<Inspectable*> inspectables)
{
return new FFTAnalyzerManagerEditor(this, isRoot);
}
}

var FFTAnalyzerManager::getFFTDataFromScript(const var::NativeFunctionArgs& a)
{
FFTAnalyzerManager* manager = getObjectFromJS<FFTAnalyzerManager>(a);
var result;
if (a.numArguments > 0 && a.arguments[0].isArray())
{
result = a.arguments[0];
}
else
{
result = Array<var>{};
}

Array<var>& resultArray = *result.getArray();
resultArray.clearQuick();

float tmpScopeData[scopeSize];
manager->copyScopeData(tmpScopeData);
resultArray.addArray(tmpScopeData, scopeSize);
return result;
}
11 changes: 9 additions & 2 deletions Source/Module/modules/audio/analysis/FFTAnalyzerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ class FFTAnalyzerManager :
scopeSize = 256 // [3]
};

void process(const float* samples, int numSamples);
void copyScopeData(float* scopeData, int maxSize = scopeSize) const;

private:
dsp::FFT forwardFFT; // [4]
dsp::WindowingFunction<float> window; // [5]
float fifo[fftSize]; // [6]
float fftData[2 * fftSize]; // [7]
int fifoIndex = 0; // [8]
bool nextFFTBlockReady = false; // [9]
float scopeData[scopeSize]; // [10]
CriticalSection scopeDataMutex;

void process(const float* samples, int numSamples);
void pushNextSampleIntoFifo(float sample);

InspectableEditor* getEditorInternal(bool isRoot, Array<Inspectable*> inspectables = Array<Inspectable*>()) override;
};

//SCRIPT
static var getFFTDataFromScript(const var::NativeFunctionArgs& a);
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ void FFTAnalyzerManagerEditor::FFTViz::paint(Graphics& g)
Path fftPath;
fftPath.startNewSubPath(r.getRelativePoint(0.f, 1.0f).toFloat());

float scopeData[analyzerManager->scopeSize];
analyzerManager->copyScopeData(scopeData);
for (int i = 0; i < analyzerManager->scopeSize; ++i)
{
fftPath.lineTo(r.getRelativePoint(i * 1.0f / analyzerManager->scopeSize, 1 - analyzerManager->scopeData[i]).toFloat());
fftPath.lineTo(r.getRelativePoint(i * 1.0f / analyzerManager->scopeSize, 1 - scopeData[i]).toFloat());
}

fftPath.lineTo(r.getRelativePoint(1.0f, 1.0f).toFloat());
Expand Down

0 comments on commit 72f8a10

Please sign in to comment.