From 25dae87647c05307c7bc21a5ad37704b13e9139b Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 25 Apr 2025 09:08:43 +0200 Subject: [PATCH] Adjusted range --- src/main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8b230eb..f1397a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,10 +13,13 @@ #define I2S_MIC_SERIAL_DATA 10 // Add sample history tracking for range limiting -#define HISTORY_DURATION_MS 3000 // 3 seconds history +#define HISTORY_DURATION_MS 1000 // 1 seconds history #define SAMPLES_PER_MS (SAMPLE_RATE / 1000) #define HISTORY_SIZE (HISTORY_DURATION_MS * SAMPLES_PER_MS) +#define MAX_RANGE_LIMIT 150000000 // Maximum allowed range limit +#define DEFAULT_RANGE_LIMIT 20000 // Default range limit when no history + class AudioLevelTracker { public: AudioLevelTracker() { @@ -32,8 +35,9 @@ public: sampleHistory.pop_front(); } - // Add new sample + // Add new sample, but cap it at MAX_RANGE_LIMIT int32_t absValue = abs(sample); + absValue = min(absValue, MAX_RANGE_LIMIT); // Cap the value SamplePoint newPoint = {currentTime, absValue}; sampleHistory.push_back(newPoint); @@ -106,7 +110,7 @@ void loop() // Calculate dynamic range limit based on max level from last 10 seconds int32_t currentMaxLevel = audioLevelTracker.getMaxLevel(); - int32_t rangelimit = currentMaxLevel > 0 ? currentMaxLevel : 20000; // fallback to default if no history + int32_t rangelimit = currentMaxLevel > 0 ? currentMaxLevel : DEFAULT_RANGE_LIMIT; // fallback to default if no history // dump the samples out to the serial channel for (int i = 0; i < samples_read; i++)