Skip to content

Commit

Permalink
Merge pull request #182 from softhack007/agc_improvement
Browse files Browse the repository at this point in the history
AGC improvement
  • Loading branch information
atuline authored Mar 9, 2022
2 parents dd962a1 + 4aafb2f commit 2d4d6a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
34 changes: 26 additions & 8 deletions wled00/audio_reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion wled00/usermod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ void userLoop() {
getSample(); // Sample the microphone
agcAvg(); // Calculated the PI adjusted value as sampleAvg
myVals[millis()%32] = sampleAgc;
logAudio();
#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");
Expand Down

0 comments on commit 2d4d6a2

Please sign in to comment.