significantly improve visual performance

This commit is contained in:
Boof2015
2026-03-28 16:42:43 -04:00
parent a972d4646b
commit 9b22400747
15 changed files with 778 additions and 20 deletions
+21
View File
@@ -139,6 +139,25 @@ Napi::Value SpectrumSetSmoothing(const Napi::CallbackInfo& info) {
return env.Undefined();
}
Napi::Value SpectrumPushSamples(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsTypedArray()) {
Napi::TypeError::New(env, "Expected Float32Array").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Float32Array audioData = info[0].As<Napi::Float32Array>();
spectrum.pushSamples(audioData.Data(), audioData.ElementLength());
return env.Undefined();
}
Napi::Value SpectrumGetMagnitudes(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
const auto& magnitudes = spectrum.getMagnitudes();
Napi::Float32Array result = Napi::Float32Array::New(env, magnitudes.size());
memcpy(result.Data(), magnitudes.data(), magnitudes.size() * sizeof(float));
return result;
}
Napi::Value SpectrumProcess(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsTypedArray()) {
@@ -280,6 +299,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
specExports.Set("getFFTSize", Napi::Function::New(env, SpectrumGetFFTSize));
specExports.Set("setSampleRate", Napi::Function::New(env, SpectrumSetSampleRate));
specExports.Set("setSmoothing", Napi::Function::New(env, SpectrumSetSmoothing));
specExports.Set("pushSamples", Napi::Function::New(env, SpectrumPushSamples));
specExports.Set("getMagnitudes", Napi::Function::New(env, SpectrumGetMagnitudes));
specExports.Set("process", Napi::Function::New(env, SpectrumProcess));
specExports.Set("binToFrequency", Napi::Function::New(env, SpectrumBinToFrequency));
specExports.Set("reset", Napi::Function::New(env, SpectrumReset));
+18 -7
View File
@@ -55,7 +55,7 @@ void Spectrum::applyWindow(const float* input, float* output, size_t length) {
}
}
void Spectrum::pushSamples(const float* input, size_t length) {
void Spectrum::pushHistory(const float* input, size_t length) {
if (length == 0 || fftSize_ == 0) {
return;
}
@@ -73,13 +73,9 @@ void Spectrum::pushSamples(const float* input, size_t length) {
bufferedSamples_ = std::min(fftSize_, bufferedSamples_ + length);
}
const std::vector<float>& Spectrum::process(const float* audioData, size_t length) {
if (audioData != nullptr && length > 0) {
pushSamples(audioData, length);
}
void Spectrum::updateMagnitudes() {
if (historyBuffer_.empty() || magnitudes_.empty()) {
return smoothedMagnitudes_;
return;
}
// Always analyze a full FFT frame from the rolling buffer.
@@ -115,6 +111,21 @@ const std::vector<float>& Spectrum::process(const float* audioData, size_t lengt
smoothedMagnitudes_[i] = -100.0f;
}
}
}
void Spectrum::pushSamples(const float* input, size_t length) {
if (input != nullptr && length > 0) {
pushHistory(input, length);
}
updateMagnitudes();
}
const std::vector<float>& Spectrum::process(const float* audioData, size_t length) {
if (audioData != nullptr && length > 0) {
pushHistory(audioData, length);
}
updateMagnitudes();
return smoothedMagnitudes_;
}
+8 -1
View File
@@ -16,6 +16,12 @@ public:
void setSampleRate(float sampleRate);
void setSmoothing(float smoothing); // 0.0 - 1.0
// Feed new samples into the rolling history and update the latest magnitudes.
void pushSamples(const float* input, size_t length);
// Read the latest smoothed magnitudes without mutating analyzer state.
const std::vector<float>& getMagnitudes() const { return smoothedMagnitudes_; }
// Process audio and get spectrum data
// Returns magnitude data (size = fftSize / 2)
const std::vector<float>& process(const float* audioData, size_t length);
@@ -39,7 +45,8 @@ private:
size_t bufferedSamples_;
void applyWindow(const float* input, float* output, size_t length);
void pushSamples(const float* input, size_t length);
void pushHistory(const float* input, size_t length);
void updateMagnitudes();
};
} // namespace Visualizer