Skip to content

Commit 6ecdfd4

Browse files
author
Dave Plummer
committed
Redo smoothing
1 parent c062423 commit 6ecdfd4

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

include/soundanalyzer.h

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static constexpr AudioInputParams kParamsMesmerizer{
101101
0.90f, // energyEnvDecay
102102
0.000001f, // energyMinEnv
103103
0.25f, // bandCompLow
104-
6.5f, // bandCompHigh
104+
10.0f, // bandCompHigh
105105
0.00f, // frameSilenceGate
106106
0.00f, // normNoiseGate
107107
3.0f, // envFloorFromNoise (cap auto-gain at ~1/4 of pure-noise)
@@ -310,7 +310,7 @@ class SoundAnalyzer : public ISoundAnalyzer
310310
// I'm old enough I can only hear up to about 12000Hz, but feel free to adjust. Remember from
311311
// school that you need to sample at double the frequency you want to process, so 24000 samples is 12000Hz
312312

313-
static constexpr size_t SAMPLING_FREQUENCY = 24000;
313+
static constexpr size_t SAMPLING_FREQUENCY = 20000;
314314
static constexpr size_t LOWEST_FREQ = 100;
315315
static constexpr size_t HIGHEST_FREQ = SAMPLING_FREQUENCY / 2;
316316

@@ -449,8 +449,8 @@ class SoundAnalyzer : public ISoundAnalyzer
449449

450450
void ComputeBandLayout()
451451
{
452-
const float fMin = 50.0f;
453-
const float fMax = std::min<float>(12000.0f, SAMPLING_FREQUENCY / 2.0f);
452+
const float fMin = LOWEST_FREQ;
453+
const float fMax = std::min<float>(HIGHEST_FREQ, SAMPLING_FREQUENCY / 2.0f);
454454
const float binWidth = (float)SAMPLING_FREQUENCY / (MAX_SAMPLES / 2.0f);
455455
int prevBin = 2;
456456
#if SPECTRUM_BAND_SCALE_MEL
@@ -614,6 +614,32 @@ class SoundAnalyzer : public ISoundAnalyzer
614614
_Peaks._Level[b] = v;
615615
sumNorm += v;
616616
}
617+
618+
#if ENABLE_AUDIO_SMOOTHING
619+
// Spatially smooth live spectrum peaks by blending with neighbors.
620+
// This affects displayed bars (and VU after we recompute below).
621+
{
622+
float tmp[NUM_BANDS];
623+
for (int b = 0; b < NUM_BANDS; ++b)
624+
{
625+
float v = _Peaks._Level[b];
626+
float l = (b > 0) ? _Peaks._Level[b - 1] : v;
627+
float r = (b < NUM_BANDS - 1) ? _Peaks._Level[b + 1] : v;
628+
tmp[b] = (v * 2.0f + l + r) * 0.25f; // (2*center + left + right) / 4
629+
}
630+
for (int b = 0; b < NUM_BANDS; ++b)
631+
{
632+
float v = tmp[b];
633+
if (v > 1.0f) v = 1.0f;
634+
_vPeaks[b] = v;
635+
_Peaks._Level[b] = v;
636+
}
637+
// Recompute VU from smoothed values
638+
sumNorm = 0.0f;
639+
for (int b = 0; b < NUM_BANDS; ++b)
640+
sumNorm += _Peaks._Level[b];
641+
}
642+
#endif
617643
UpdateVU((float)(sumNorm / NUM_BANDS));
618644
return _Peaks;
619645
}
@@ -912,15 +938,7 @@ class SoundAnalyzer : public ISoundAnalyzer
912938
_peak2Decay[iBand] -= min(decayAmount2, _peak2Decay[iBand]);
913939
}
914940

915-
// Manual smoothing if desired
916-
917-
#if ENABLE_AUDIO_SMOOTHING
918-
for (int iBand = 1; iBand < NUM_BANDS - 1; iBand += 2)
919-
{
920-
_peak1Decay[iBand] = (_peak1Decay[iBand] * 2 + _peak1Decay[iBand - 1] + _peak1Decay[iBand + 1]) / 4;
921-
_peak2Decay[iBand] = (_peak2Decay[iBand] * 2 + _peak2Decay[iBand - 1] + _peak2Decay[iBand + 1]) / 4;
922-
}
923-
#endif
941+
// Removed old smoothing of decay overlays; smoothing now applies to live peaks in ProcessPeaksEnergy()
924942
}
925943

926944
// Update the per-band decay overlays from the latest peaks.

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ build_flags = -DM5DEMO=1
381381
[env:m5stackdemo]
382382
extends = dev_m5stack
383383
build_flags = -DM5DEMO=1
384-
-DNUM_BANDS=32
384+
-DNUM_BANDS=160
385385
${dev_m5stack.build_flags}
386386

387387
; Now for the Heltec, with WiFi and webserver enabled

src/audio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ void IRAM_ATTR AudioSamplerTaskEntry(void *)
9191
debugV("VURatio: %f\n", g_Analyzer.VURatio());
9292
debugV("VURatioFade: %f\n", g_Analyzer.VURatioFade());
9393

94-
// Delay enough time to yield 60fps max
94+
// Delay enough time to yield 45fps max
9595
// We wait a minimum even if busy so we don't Bogart the CPU
9696

97-
constexpr auto kMaxFPS = 60;
97+
constexpr auto kMaxFPS = 45;
9898
const auto targetDelay = PERIOD_FROM_FREQ(kMaxFPS) * MILLIS_PER_SECOND / MICROS_PER_SECOND;
9999
delay(max(1.0, targetDelay - (millis() - lastFrame)));
100100

0 commit comments

Comments
 (0)