Adjusted range
This commit is contained in:
10
src/main.cpp
10
src/main.cpp
@@ -13,10 +13,13 @@
|
|||||||
#define I2S_MIC_SERIAL_DATA 10
|
#define I2S_MIC_SERIAL_DATA 10
|
||||||
|
|
||||||
// Add sample history tracking for range limiting
|
// 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 SAMPLES_PER_MS (SAMPLE_RATE / 1000)
|
||||||
#define HISTORY_SIZE (HISTORY_DURATION_MS * SAMPLES_PER_MS)
|
#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 {
|
class AudioLevelTracker {
|
||||||
public:
|
public:
|
||||||
AudioLevelTracker() {
|
AudioLevelTracker() {
|
||||||
@@ -32,8 +35,9 @@ public:
|
|||||||
sampleHistory.pop_front();
|
sampleHistory.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new sample
|
// Add new sample, but cap it at MAX_RANGE_LIMIT
|
||||||
int32_t absValue = abs(sample);
|
int32_t absValue = abs(sample);
|
||||||
|
absValue = min(absValue, MAX_RANGE_LIMIT); // Cap the value
|
||||||
SamplePoint newPoint = {currentTime, absValue};
|
SamplePoint newPoint = {currentTime, absValue};
|
||||||
sampleHistory.push_back(newPoint);
|
sampleHistory.push_back(newPoint);
|
||||||
|
|
||||||
@@ -106,7 +110,7 @@ void loop()
|
|||||||
|
|
||||||
// Calculate dynamic range limit based on max level from last 10 seconds
|
// Calculate dynamic range limit based on max level from last 10 seconds
|
||||||
int32_t currentMaxLevel = audioLevelTracker.getMaxLevel();
|
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
|
// dump the samples out to the serial channel
|
||||||
for (int i = 0; i < samples_read; i++)
|
for (int i = 0; i < samples_read; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user