From 5ff44755d81a7f1431eb7c1aff2dfa47fb0487a5 Mon Sep 17 00:00:00 2001 From: Troy <5659019+troyhacks@users.noreply.github.com> Date: Mon, 25 Mar 2024 08:19:05 -0400 Subject: [PATCH] calculations based on normalized FFT results --- usermods/audioreactive/audio_reactive.h | 34 +++++++++---------- .../usermod_v2_auto_playlist.h | 19 ++++++----- wled00/FX_2Dfcn.cpp | 2 +- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index db6f5065..0d302db4 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -498,20 +498,16 @@ void FFTcode(void * parameter) // get a fresh batch of samples from I2S if (audioSource) audioSource->getSamples(vReal, samplesFFT); - + // WLED-MM/TroyHacks: Calculate zero crossings // zeroCrossingCount = 0; - energy = 0; - for (int i=0; i < samplesFFT; i++) { if (i < (samplesFFT)-2) { if((vReal[i] >= 0 && vReal[i+1] < 0) || (vReal[i+1] < 0 && vReal[i+1] >= 0)) { zeroCrossingCount++; } } - // WLED-MM/TroyHacks: Calculate energy - energy += (vReal[i] * vReal[i])/10000000; } #if defined(WLED_DEBUG) || defined(SR_DEBUG)|| defined(SR_STATS) @@ -645,17 +641,6 @@ void FFTcode(void * parameter) #endif FFT_MajorPeak = constrain(FFT_MajorPeak, 1.0f, 11025.0f); // restrict value to range expected by effects FFT_MajPeakSmth = FFT_MajPeakSmth + 0.42 * (FFT_MajorPeak - FFT_MajPeakSmth); // I like this "swooping peak" look - - // WLED-MM/TroyHacks: Calculate Low-Frequency Content - // - lowFreqencyContent = fftAddAvg(2,4)/1000; - - // USER_PRINT("ZCR = "); - // USER_PRINT(zeroCrossingCount); - // USER_PRINT(" Energy = "); - // USER_PRINT(" LFC = "); - // USER_PRINT(lowFreqencyContent); - // USER_PRINTLN(); } else { // skip second run --> clear fft results, keep peaks memset(vReal, 0, sizeof(vReal)); @@ -801,7 +786,22 @@ void FFTcode(void * parameter) // run peak detection autoResetPeak(); detectSamplePeak(); - + + // WLED-MM/TroyHacks: Calculate Energy & Low-Frequency Content + // + energy = 0; + for (int i=0; i < NUM_GEQ_CHANNELS; i++) { + energy += fftResult[i]; + } + energy *= energy; + energy /= 10000; + lowFreqencyContent = fftResult[0]; + + // WLED-MM/TroyHacks: Ideally these numbers are roughly in the same rations + // ...but more importantly hitting a zero point at a regular interval + // + // USER_PRINTF("ZCR: %3lu Energy: %5lu LFC: %4lu\n",(unsigned long)zeroCrossingCount,(unsigned long)energy,(unsigned long)lowFreqencyContent) + // we have new results - notify UDP sound send haveNewFFTResult = true; diff --git a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h index 39b14049..dc23135c 100644 --- a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h +++ b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h @@ -121,12 +121,15 @@ class AutoPlaylistUsermod : public Usermod { // the current music, especially after track changes or during // sparce intros and breakdowns. if (change_interval > ideal_change_min && distance_tracker < 1000) { - // change_threshold++; - change_threshold += distance_tracker/10; - USER_PRINTF("Increasing change_threshold to: %d\n",change_threshold); - USER_PRINT (" lowest recorded distance was: "); + + change_threshold += distance_tracker>10?distance_tracker/10:1; + + USER_PRINT ("The lowest recorded distance was: "); USER_PRINTLN(distance_tracker); + USER_PRINTF("Increasing change_threshold to: %d\n",change_threshold); + distance_tracker = UINT_FAST32_MAX; + } change_timer = millis(); } @@ -136,11 +139,9 @@ class AutoPlaylistUsermod : public Usermod { if (distance <= change_threshold && change_interval > change_lockout && volumeSmth > 0.1) { if (change_interval > ideal_change_max) { - // change_threshold += 1; - change_threshold += distance/10; + change_threshold += distance_tracker>10?distance_tracker/10:1; } else if (change_interval < ideal_change_min) { - // change_threshold -= 1; - change_threshold -= distance/10; + change_threshold -= distance_tracker>10?distance_tracker/10:1; } distance_tracker = UINT_FAST32_MAX; @@ -162,7 +163,7 @@ class AutoPlaylistUsermod : public Usermod { do { newpreset = autoChangeIds.at(random(0, autoChangeIds.size())); // random() is *exclusive* of the last value, so it's OK to use the full size. } - while (currentPreset == newpreset); + while (currentPreset == newpreset); // make sure we get a different random preset. applyPreset(newpreset); diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 999684b1..166536e1 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -64,7 +64,7 @@ void WS2812FX::setUpMatrix() { return; } - USER_PRINTF("setUpMatrix %d x %d\n", Segment::maxWidth, Segment::maxHeight); + // TroyHacks temp commented out, FIXME and put back: USER_PRINTF("setUpMatrix %d x %d\n", Segment::maxWidth, Segment::maxHeight); //WLEDMM recreate customMappingTable if more space needed if (Segment::maxWidth * Segment::maxHeight > customMappingTableSize) {