From 58184a5a74ab3ec3e5ff1d25a9f28e4c0743d39e Mon Sep 17 00:00:00 2001 From: Troy <5659019+troyhacks@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:45:32 -0400 Subject: [PATCH] Moved calcs into usermod except zero crossings --- usermods/audioreactive/audio_reactive.h | 26 ++----------------- .../usermod_v2_auto_playlist.h | 16 +++++++++--- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index 959ab54c..8a5610be 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -141,9 +141,7 @@ static uint8_t fftResult[NUM_GEQ_CHANNELS]= {0}; // Our calculated freq. chann static float fftCalc[NUM_GEQ_CHANNELS] = {0.0f}; // Try and normalize fftBin values to a max of 4096, so that 4096/16 = 256. (also used by dynamics limiter) static float fftAvg[NUM_GEQ_CHANNELS] = {0.0f}; // Calculated frequency channel results, with smoothing (used if dynamics limiter is ON) -uint_fast32_t zeroCrossingCount = 0; -uint_fast32_t energy = 0; -uint_fast32_t lowFreqencyContent = 0; +uint_fast16_t zeroCrossingCount = 0; // TODO: probably best not used by receive nodes static float agcSensitivity = 128; // AGC sensitivity estimation, based on agc gain (multAgc). calculated by getSensitivity(). range 0..255 @@ -787,22 +785,6 @@ void FFTcode(void * parameter) 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; // scale down so we get 0 sometimes - lowFreqencyContent = fftResult[0]; - - // WLED-MM/TroyHacks: Ideally these numbers are roughly in the same ratios - // ...but most importantly all values need to be hitting zero 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; #if !defined(I2S_GRAB_ADC1_COMPLETELY) @@ -1778,11 +1760,7 @@ class AudioReactive : public Usermod { um_data->u_data[10] = &agcSensitivity; // used (New) um_data->u_type[10] = UMT_FLOAT; um_data->u_data[11] = &zeroCrossingCount; - um_data->u_type[11] = UMT_UINT32; - um_data->u_data[12] = &energy; - um_data->u_type[12] = UMT_UINT32; - um_data->u_data[13] = &lowFreqencyContent; - um_data->u_type[13] = UMT_UINT32; + um_data->u_type[11] = UMT_UINT16; #else // ESP8266 // See https://github.com/MoonModules/WLED/pull/60#issuecomment-1666972133 for explanation of these alternative sources of data 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 5a1ec6d9..b07d1178 100644 --- a/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h +++ b/usermods/usermod_v2_auto_playlist/usermod_v2_auto_playlist.h @@ -15,6 +15,8 @@ class AutoPlaylistUsermod : public Usermod { byte lastAutoPlaylist = 0; int change_timer = millis(); + uint_fast32_t energy = 0; + uint_fast32_t avg_long_energy = 250; uint_fast32_t avg_long_lfc = 1000; uint_fast32_t avg_long_zcr = 500; @@ -71,9 +73,17 @@ class AutoPlaylistUsermod : public Usermod { void change(um_data_t *um_data) { - uint_fast32_t zcr = *(uint_fast32_t*)um_data->u_data[11]; - uint_fast32_t energy = *(uint_fast32_t*)um_data->u_data[12]; - uint_fast32_t lfc = *(uint_fast32_t*)um_data->u_data[13]; + uint8_t *fftResult = (uint8_t*)um_data->u_data[2]; + + for (int i=0; i < NUM_GEQ_CHANNELS; i++) { + energy += fftResult[i]; + } + + energy *= energy; + energy /= 10000; // scale down so we get 0 sometimes + + uint8_t lfc = fftResult[0]; + uint_fast16_t zcr = *(uint_fast16_t*)um_data->u_data[11]; // WLED-MM/TroyHacks: Calculate the long- and short-running averages // and the individual vector distances.