From b0a6c0a2ed6548178d9a5e6532e71922a6f72d88 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 9 Mar 2022 19:37:49 +0100 Subject: [PATCH 1/3] improved AGC function * doesn't amplify signals below squelch * gain factor is smoothed out (a bit), so it will not jump from 1 to MAX instantly * does not "de-amplify" loud samples by more than 50% --- wled00/audio_reactive.h | 36 +++++++++++++++++++++++++++--------- wled00/usermod.cpp | 4 +++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/wled00/audio_reactive.h b/wled00/audio_reactive.h index 403cddaef6..d204fe6801 100644 --- a/wled00/audio_reactive.h +++ b/wled00/audio_reactive.h @@ -36,7 +36,7 @@ AudioSource *audioSource; #define DEBUGSR_PRINTF(x...) #endif -// #define MIC_LOGGER +#define MIC_LOGGER // #define MIC_SAMPLING_LOG // #define FFT_SAMPLING_LOG @@ -199,10 +199,26 @@ void getSample() { /* * A simple averaging multiplier to automatically adjust sound sensitivity. */ +/* + * A simple, but hairy, averaging multiplier to automatically adjust sound sensitivity. + * not sure if not sure "sample" or "sampleAvg" is the correct input signal for AGC + */ void agcAvg() { - multAgc = (sampleAvg < 1) ? targetAgc : targetAgc / sampleAvg; // Make the multiplier so that sampleAvg * multiplier = setpoint - int tmpAgc = sample * multAgc; + float lastMultAgc = multAgc; + float tmpAgc; + if(fabs(sampleAvg) < 2.0) { + tmpAgc = sampleAvg; // signal below squelch -> deliver silence + multAgc = multAgc * 0.95; // slightly decrease gain multiplier + } else { + multAgc = (sampleAvg < 1) ? targetAgc : targetAgc / sampleAvg; // Make the multiplier so that sampleAvg * multiplier = setpoint + } + + if (multAgc < 0.5) multAgc = 0.5; // signal higher than 2*setpoint -> don't reduce it further + multAgc = (lastMultAgc*127.0 +multAgc) / 128.0; //apply some filtering to gain multiplier -> smoother transitions + tmpAgc = (float)sample * multAgc; // apply gain to signal + if (tmpAgc <= (soundSquelch*1.2)) tmpAgc = sample; // check against squelch threshold - increased by 20% to avoid artefacts (ripples) + if (tmpAgc > 255) tmpAgc = 255; sampleAgc = tmpAgc; // ONLY update sampleAgc ONCE because it's used elsewhere asynchronously!!!! userVar0 = sampleAvg * 4; @@ -453,12 +469,14 @@ void logAudio() { #ifdef MIC_LOGGER -// Serial.print(micIn); Serial.print(" "); -// Serial.print(sample); Serial.print(" "); -// Serial.print(sampleAvg); Serial.print(" "); -// Serial.print(sampleAgc); Serial.print(" "); -// Serial.print(micData); Serial.print(" "); -// Serial.print(micDataSm); Serial.print(" "); + //Serial.print("micData:"); Serial.print(micData); Serial.print("\t"); + //Serial.print("micDataSm:"); Serial.print(micDataSm); Serial.print("\t"); + //Serial.print("micIn:"); Serial.print(micIn); Serial.print("\t"); + //Serial.print("micLev:"); Serial.print(micLev); Serial.print("\t"); + Serial.print("sample:"); Serial.print(sample); Serial.print("\t"); + //Serial.print("sampleAvg:"); Serial.print(sampleAvg); Serial.print("\t"); + //Serial.print("multAgc:"); Serial.print(multAgc); Serial.print("\t"); + Serial.print("sampleAgc:"); Serial.print(sampleAgc); Serial.print("\t"); Serial.println(" "); #endif diff --git a/wled00/usermod.cpp b/wled00/usermod.cpp index 80afa46bc5..f054e3cb75 100644 --- a/wled00/usermod.cpp +++ b/wled00/usermod.cpp @@ -83,7 +83,9 @@ void userLoop() { getSample(); // Sample the microphone agcAvg(); // Calculated the PI adjusted value as sampleAvg myVals[millis()%32] = sampleAgc; - logAudio(); + EVERY_N_MILLIS(20) { + logAudio(); + } } if (audioSyncEnabled & (1 << 0)) { // Only run the transmit code IF we're in Transmit mode //Serial.println("Transmitting UDP Mic Packet"); From 1f12df142457dddaac3f0ce44384183674d16848 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 9 Mar 2022 19:41:22 +0100 Subject: [PATCH 2/3] disable mic_logger --- wled00/audio_reactive.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/audio_reactive.h b/wled00/audio_reactive.h index d204fe6801..21062d8d65 100644 --- a/wled00/audio_reactive.h +++ b/wled00/audio_reactive.h @@ -36,7 +36,7 @@ AudioSource *audioSource; #define DEBUGSR_PRINTF(x...) #endif -#define MIC_LOGGER +// #define MIC_LOGGER // #define MIC_SAMPLING_LOG // #define FFT_SAMPLING_LOG From 858f03a793f17fa050d5874c02c4fba796a450fb Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 9 Mar 2022 20:59:04 +0100 Subject: [PATCH 3/3] skip call to logAudio when not needed --- wled00/usermod.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wled00/usermod.cpp b/wled00/usermod.cpp index f054e3cb75..22fde5f953 100644 --- a/wled00/usermod.cpp +++ b/wled00/usermod.cpp @@ -83,9 +83,12 @@ void userLoop() { getSample(); // Sample the microphone agcAvg(); // Calculated the PI adjusted value as sampleAvg myVals[millis()%32] = sampleAgc; +#if defined(MIC_LOGGER) || defined(MIC_SAMPLING_LOG) || defined(FFT_SAMPLING_LOG) EVERY_N_MILLIS(20) { logAudio(); } +#endif + } if (audioSyncEnabled & (1 << 0)) { // Only run the transmit code IF we're in Transmit mode //Serial.println("Transmitting UDP Mic Packet");