Code sanitation.

Default analog pin -1
This commit is contained in:
Blaz Kristan 2022-08-21 19:15:42 +02:00
parent d053bc562f
commit 720fae8720

View File

@ -173,7 +173,6 @@ void FFTcode(void * parameter)
// see https://www.freertos.org/vtaskdelayuntil.html // see https://www.freertos.org/vtaskdelayuntil.html
const TickType_t xFrequency = FFT_MIN_CYCLE * portTICK_PERIOD_MS; const TickType_t xFrequency = FFT_MIN_CYCLE * portTICK_PERIOD_MS;
//const TickType_t xFrequency_2 = (FFT_MIN_CYCLE * portTICK_PERIOD_MS) / 2;
for(;;) { for(;;) {
TickType_t xLastWakeTime = xTaskGetTickCount(); TickType_t xLastWakeTime = xTaskGetTickCount();
@ -208,8 +207,8 @@ void FFTcode(void * parameter)
if ((vReal[i] <= (INT16_MAX - 1024)) && (vReal[i] >= (INT16_MIN + 1024))) //skip extreme values - normally these are artefacts if ((vReal[i] <= (INT16_MAX - 1024)) && (vReal[i] >= (INT16_MIN + 1024))) //skip extreme values - normally these are artefacts
if (fabsf((float)vReal[i]) > maxSample) maxSample = fabsf((float)vReal[i]); if (fabsf((float)vReal[i]) > maxSample) maxSample = fabsf((float)vReal[i]);
} }
// release sample to volume reactive effects // release sample to volume reactive effects (not really necessary as float FFT calculation takes only 2ms)
micDataReal = maxSample; // doing this early allows filters (getSample() and agcAvg()) to run on latest values - we'll have up-to-date gain and noise gate values when FFT is done micDataReal = maxSample;
// run FFT // run FFT
#ifdef UM_AUDIOREACTIVE_USE_NEW_FFT #ifdef UM_AUDIOREACTIVE_USE_NEW_FFT
@ -228,10 +227,6 @@ void FFTcode(void * parameter)
FFT.Compute( FFT_FORWARD ); // Compute FFT FFT.Compute( FFT_FORWARD ); // Compute FFT
FFT.ComplexToMagnitude(); // Compute magnitudes FFT.ComplexToMagnitude(); // Compute magnitudes
#endif #endif
//
// vReal[3 .. 255] contain useful data, each a 20Hz interval (60Hz - 5120Hz).
// There could be interesting data at bins 0 to 2, but there are too many artifacts.
//
#ifdef UM_AUDIOREACTIVE_USE_NEW_FFT #ifdef UM_AUDIOREACTIVE_USE_NEW_FFT
FFT.majorPeak(FFT_MajorPeak, FFT_Magnitude); // let the effects know which freq was most dominant FFT.majorPeak(FFT_MajorPeak, FFT_Magnitude); // let the effects know which freq was most dominant
@ -294,7 +289,6 @@ void FFTcode(void * parameter)
fftCalc[15] = fftAddAvg(165,215) * 0.70f; // 50 7106 - 9259 high -- with some damping fftCalc[15] = fftAddAvg(165,215) * 0.70f; // 50 7106 - 9259 high -- with some damping
// don't use the last bins from 216 to 255. They are usually contaminated by aliasing (aka noise) // don't use the last bins from 216 to 255. They are usually contaminated by aliasing (aka noise)
#endif #endif
} else { // noise gate closed } else { // noise gate closed
for (int i=0; i < 16; i++) { for (int i=0; i < 16; i++) {
//fftCalc[i] *= 0.82f; // decay to zero //fftCalc[i] *= 0.82f; // decay to zero
@ -338,8 +332,7 @@ void FFTcode(void * parameter)
// Logarithmic scaling // Logarithmic scaling
currentResult *= 0.42; // 42 is the answer ;-) currentResult *= 0.42; // 42 is the answer ;-)
currentResult -= 8.0; // this skips the lowest row, giving some room for peaks currentResult -= 8.0; // this skips the lowest row, giving some room for peaks
if (currentResult > 1.0) if (currentResult > 1.0) currentResult = logf(currentResult); // log to base "e", which is the fastest log() function
currentResult = logf(currentResult); // log to base "e", which is the fastest log() function
else currentResult = 0.0; // special handling, because log(1) = 0; log(0) = undefined else currentResult = 0.0; // special handling, because log(1) = 0; log(0) = undefined
currentResult *= 0.85f + (float(i)/18.0f); // extra up-scaling for high frequencies currentResult *= 0.85f + (float(i)/18.0f); // extra up-scaling for high frequencies
currentResult = mapf(currentResult, 0, LOG_256, 0, 255); // map [log(1) ... log(255)] to [0 ... 255] currentResult = mapf(currentResult, 0, LOG_256, 0, 255); // map [log(1) ... log(255)] to [0 ... 255]
@ -355,8 +348,7 @@ void FFTcode(void * parameter)
// square root scaling // square root scaling
currentResult *= 0.38f; currentResult *= 0.38f;
currentResult -= 6.0f; currentResult -= 6.0f;
if (currentResult > 1.0) if (currentResult > 1.0) currentResult = sqrtf(currentResult);
currentResult = sqrtf(currentResult);
else currentResult = 0.0; // special handling, because sqrt(0) = undefined else currentResult = 0.0; // special handling, because sqrt(0) = undefined
currentResult *= 0.85f + (float(i)/4.5f); // extra up-scaling for high frequencies currentResult *= 0.85f + (float(i)/4.5f); // extra up-scaling for high frequencies
currentResult = mapf(currentResult, 0.0, 16.0, 0.0, 255.0); // map [sqrt(1) ... sqrt(256)] to [0 ... 255] currentResult = mapf(currentResult, 0.0, 16.0, 0.0, 255.0); // map [sqrt(1) ... sqrt(256)] to [0 ... 255]
@ -394,7 +386,7 @@ class AudioReactive : public Usermod {
private: private:
#ifndef AUDIOPIN #ifndef AUDIOPIN
int8_t audioPin = 36; int8_t audioPin = -1;
#else #else
int8_t audioPin = AUDIOPIN; int8_t audioPin = AUDIOPIN;
#endif #endif
@ -615,7 +607,6 @@ class AudioReactive : public Usermod {
if((fabs(sampleReal) < 2.0f) || (sampleMax < 1.0f)) { if((fabs(sampleReal) < 2.0f) || (sampleMax < 1.0f)) {
// MIC signal is "squelched" - deliver silence // MIC signal is "squelched" - deliver silence
//multAgcTemp = multAgc; // keep old control value (no change)
tmpAgc = 0; tmpAgc = 0;
// we need to "spin down" the intgrated error buffer // we need to "spin down" the intgrated error buffer
if (fabs(control_integrated) < 0.01) control_integrated = 0.0; if (fabs(control_integrated) < 0.01) control_integrated = 0.0;
@ -628,7 +619,6 @@ class AudioReactive : public Usermod {
multAgcTemp = agcTarget1[AGC_preset] / sampleMax; // Make the multiplier so that sampleMax * multiplier = second setpoint multAgcTemp = agcTarget1[AGC_preset] / sampleMax; // Make the multiplier so that sampleMax * multiplier = second setpoint
} }
// limit amplification // limit amplification
//multAgcTemp = constrain(multAgcTemp, 0.015625f, 32.0f); // 1/64 < multAgcTemp < 32
if (multAgcTemp > 32.0f) multAgcTemp = 32.0f; if (multAgcTemp > 32.0f) multAgcTemp = 32.0f;
if (multAgcTemp < 1.0f/64.0f) multAgcTemp = 1.0f/64.0f; if (multAgcTemp < 1.0f/64.0f) multAgcTemp = 1.0f/64.0f;
@ -673,9 +663,6 @@ class AudioReactive : public Usermod {
else else
sampleAgc += agcSampleSmooth[AGC_preset] * (tmpAgc - sampleAgc); // smooth path sampleAgc += agcSampleSmooth[AGC_preset] * (tmpAgc - sampleAgc); // smooth path
//userVar0 = sampleAvg * 4;
//if (userVar0 > 255) userVar0 = 255;
last_soundAgc = soundAgc; last_soundAgc = soundAgc;
} // agcAvg() } // agcAvg()
@ -751,13 +738,12 @@ class AudioReactive : public Usermod {
//if (userVar1 == 0) samplePeak = 0; //if (userVar1 == 0) samplePeak = 0;
// Poor man's beat detection by seeing if sample > Average + some value. // Poor man's beat detection by seeing if sample > Average + some value.
// if (sample > (sampleAvg + maxVol) && millis() > (timeOfPeak + 200)) { if ((maxVol > 0) && (binNum > 1) && (fftBin[binNum] > maxVol) && (millis() > (timeOfPeak + 100))) {
if ((maxVol > 0) && (binNum > 1) && (fftBin[binNum] > maxVol) && (millis() > (timeOfPeak + 100))) { // This goes through ALL of the 255 bins - but ignores stupid settings // This goes through ALL of the 255 bins - but ignores stupid settings
// Then we got a peak, else we don't. The peak has to time out on its own in order to support UDP sound sync. // Then we got a peak, else we don't. The peak has to time out on its own in order to support UDP sound sync.
samplePeak = true; samplePeak = true;
timeOfPeak = millis(); timeOfPeak = millis();
udpSamplePeak = true; udpSamplePeak = true;
//userVar1 = samplePeak;
} }
} // getSample() } // getSample()
@ -771,7 +757,7 @@ class AudioReactive : public Usermod {
static unsigned long last_time = 0; static unsigned long last_time = 0;
static float last_volumeSmth = 0.0f; static float last_volumeSmth = 0.0f;
if(limiterOn == false) return; if (limiterOn == false) return;
long delta_time = millis() - last_time; long delta_time = millis() - last_time;
delta_time = constrain(delta_time , 1, 1000); // below 1ms -> 1ms; above 1sec -> sily lil hick-up delta_time = constrain(delta_time , 1, 1000); // below 1ms -> 1ms; above 1sec -> sily lil hick-up
@ -1043,7 +1029,7 @@ class AudioReactive : public Usermod {
if (audioSyncEnabled & 0x02) disableSoundProcessing = true; // make sure everything is disabled IF in audio Receive mode if (audioSyncEnabled & 0x02) disableSoundProcessing = true; // make sure everything is disabled IF in audio Receive mode
if (audioSyncEnabled & 0x01) disableSoundProcessing = false; // keep running audio IF we're in audio Transmit mode if (audioSyncEnabled & 0x01) disableSoundProcessing = false; // keep running audio IF we're in audio Transmit mode
if(!audioSource->isInitialized()) disableSoundProcessing = true; // no audio source if (!audioSource->isInitialized()) disableSoundProcessing = true; // no audio source
// Only run the sampling code IF we're not in Receive mode or realtime mode // Only run the sampling code IF we're not in Receive mode or realtime mode
@ -1239,61 +1225,62 @@ class AudioReactive : public Usermod {
uiDomString += F(" /><div class=\"sliderdisplay\"></div></div></div>"); //<output class=\"sliderbubble\"></output> uiDomString += F(" /><div class=\"sliderdisplay\"></div></div></div>"); //<output class=\"sliderbubble\"></output>
infoArr.add(uiDomString); infoArr.add(uiDomString);
} }
//else infoArr.add("<br/> <div>&nbsp;</div>"); // no processing - add empty line
// The following can be used for troubleshooting user errors and is so not enclosed in #ifdef WLED_DEBUG
// current Audio input // current Audio input
infoArr = user.createNestedArray(F("Audio Source")); infoArr = user.createNestedArray(F("Audio Source"));
if (audioSyncEnabled & 0x02) { if (audioSyncEnabled & 0x02) {
// UDP sound sync - receive mode // UDP sound sync - receive mode
infoArr.add("UDP sound sync"); infoArr.add(F("UDP sound sync"));
if (udpSyncConnected) { if (udpSyncConnected) {
if (millis() - last_UDPTime < 2500) if (millis() - last_UDPTime < 2500)
infoArr.add(" - receiving"); infoArr.add(F(" - receiving"));
else else
infoArr.add(" - idle"); infoArr.add(F(" - idle"));
} else { } else {
infoArr.add(" - no connection"); infoArr.add(F(" - no connection"));
} }
} else { } else {
// Analog or I2S digital input // Analog or I2S digital input
if (audioSource && (audioSource->isInitialized())) { if (audioSource && (audioSource->isInitialized())) {
// audio source sucessfully configured // audio source sucessfully configured
if(audioSource->getType() == AudioSource::Type_I2SAdc) { if (audioSource->getType() == AudioSource::Type_I2SAdc) {
infoArr.add("ADC analog"); infoArr.add(F("ADC analog"));
} else { } else {
infoArr.add("I2S digital"); infoArr.add(F("I2S digital"));
} }
// input level or "silence" // input level or "silence"
if (maxSample5sec > 1.0) { if (maxSample5sec > 1.0) {
float my_usage = 100.0f * (maxSample5sec / 255.0f); float my_usage = 100.0f * (maxSample5sec / 255.0f);
snprintf(myStringBuffer, 15, " - peak %3d%%", int(my_usage)); snprintf_P(myStringBuffer, 15, PSTR(" - peak %3d%%"), int(my_usage));
infoArr.add(myStringBuffer); infoArr.add(myStringBuffer);
} else { } else {
infoArr.add(" - quiet"); infoArr.add(F(" - quiet"));
} }
} else { } else {
// error during audio source setup // error during audio source setup
infoArr.add("not initialized"); infoArr.add(F("not initialized"));
infoArr.add(" - check GPIO config"); infoArr.add(F(" - check GPIO config"));
} }
} }
// Sound processing (FFT and input filters) // Sound processing (FFT and input filters)
infoArr = user.createNestedArray(F("Sound Processing")); infoArr = user.createNestedArray(F("Sound Processing"));
if (audioSource && (disableSoundProcessing == false)) { if (audioSource && (disableSoundProcessing == false)) {
infoArr.add("running"); infoArr.add(F("running"));
} else { } else {
infoArr.add("suspended"); infoArr.add(F("suspended"));
} }
// AGC or manual Gain // AGC or manual Gain
if((soundAgc==0) && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) { if ((soundAgc==0) && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) {
infoArr = user.createNestedArray(F("Manual Gain")); infoArr = user.createNestedArray(F("Manual Gain"));
float myGain = ((float)sampleGain/40.0f * (float)inputLevel/128.0f) + 1.0f/16.0f; // non-AGC gain from presets float myGain = ((float)sampleGain/40.0f * (float)inputLevel/128.0f) + 1.0f/16.0f; // non-AGC gain from presets
infoArr.add(roundf(myGain*100.0f) / 100.0f); infoArr.add(roundf(myGain*100.0f) / 100.0f);
infoArr.add("x"); infoArr.add("x");
} }
if(soundAgc && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) { if (soundAgc && (disableSoundProcessing == false) && !(audioSyncEnabled & 0x02)) {
infoArr = user.createNestedArray(F("AGC Gain")); infoArr = user.createNestedArray(F("AGC Gain"));
infoArr.add(roundf(multAgc*100.0f) / 100.0f); infoArr.add(roundf(multAgc*100.0f) / 100.0f);
infoArr.add("x"); infoArr.add("x");
@ -1303,14 +1290,13 @@ class AudioReactive : public Usermod {
infoArr = user.createNestedArray(F("UDP Sound Sync")); infoArr = user.createNestedArray(F("UDP Sound Sync"));
if (audioSyncEnabled) { if (audioSyncEnabled) {
if (audioSyncEnabled & 0x01) { if (audioSyncEnabled & 0x01) {
infoArr.add("send mode"); infoArr.add(F("send mode"));
} else if (audioSyncEnabled & 0x02) { } else if (audioSyncEnabled & 0x02) {
infoArr.add("receive mode"); infoArr.add(F("receive mode"));
} }
} else } else
infoArr.add("off"); infoArr.add("off");
if (audioSyncEnabled && !udpSyncConnected) infoArr.add(" <i>(unconnected)</i>"); if (audioSyncEnabled && !udpSyncConnected) infoArr.add(" <i>(unconnected)</i>");
//if (!udpSyncConnected) infoArr.add(" <i>(unconnected)</i>");
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
infoArr = user.createNestedArray(F("Sampling time")); infoArr = user.createNestedArray(F("Sampling time"));