refactor ♻️: Updated I2S sample rate from 8 kHz to 4 kHz, reduced DMA buffer count and length, adjusted gain for 16-bit range, increased task stack size and priority, and decreased debug interval
- Updated `AudioLevelTracker.h` to scale the maximum range limit by a factor of 16. - Updated the I2S sample rate from 8 kHz to 4 kHz, reduced DMA buffer count and length, adjusted gain for 16-bit range, increased task stack size and priority, and decreased debug interval. - The `initI2S` function has been modified to use a smaller buffer size and adjust the data format to 16-bit, ensuring compatibility with existing code while reducing memory usage.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <deque>
|
||||
#include "Config.h"
|
||||
|
||||
class AudioLevelTracker {
|
||||
public:
|
||||
@@ -19,5 +20,5 @@ private:
|
||||
int32_t maxLevel;
|
||||
|
||||
static const uint32_t HISTORY_DURATION_MS = 3000; // 3 seconds history
|
||||
static const int32_t MAX_RANGE_LIMIT = 200000000; // Maximum allowed range limit
|
||||
static const int32_t MAX_RANGE_LIMIT = Config::DEFAULT_RANGE_LIMIT << 16; // Scale up 16-bit limit
|
||||
};
|
||||
@@ -12,30 +12,30 @@ namespace Config {
|
||||
constexpr int I2S_MIC_SERIAL_DATA = 10; // SD
|
||||
|
||||
// I2S Configuration
|
||||
constexpr int SAMPLE_RATE = 8000; // Hz
|
||||
constexpr int BITS_PER_SAMPLE = 32; // Bits
|
||||
constexpr int SAMPLE_RATE = 8000; // Hz - good for frequencies up to 4kHz
|
||||
constexpr int BITS_PER_SAMPLE = 16; // Changed to 16-bit
|
||||
constexpr int CHANNELS = 1; // Mono input
|
||||
constexpr int SAMPLE_BUFFER_SIZE = 512; // Samples per buffer
|
||||
constexpr int SAMPLE_BUFFER_SIZE = 1024; // 128ms window at 8kHz - good for low frequency resolution
|
||||
|
||||
// DMA Configuration
|
||||
constexpr int DMA_BUFFER_COUNT = 8; // Number of DMA buffers
|
||||
constexpr int DMA_BUFFER_LEN = 1024; // Length of each DMA buffer
|
||||
constexpr int DMA_BUFFER_COUNT = 4; // Reduced for lower latency
|
||||
constexpr int DMA_BUFFER_LEN = 512; // Smaller chunks for faster processing
|
||||
|
||||
// Audio Processing
|
||||
constexpr float DC_OFFSET = 0.0f; // DC offset correction
|
||||
constexpr float GAIN = 1.0f; // Audio gain multiplier
|
||||
constexpr int32_t NOISE_THRESHOLD = 1000; // Ignore audio below this level
|
||||
constexpr int32_t DEFAULT_RANGE_LIMIT = 200000000; // Default range limit for plotting
|
||||
constexpr float DECAY_FACTOR = 0.95f; // Level decay rate
|
||||
constexpr float GAIN = 1.5f; // Adjusted gain for 16-bit range
|
||||
constexpr int16_t NOISE_THRESHOLD = 1000; // Adjusted for 16-bit range
|
||||
constexpr int16_t DEFAULT_RANGE_LIMIT = 32767; // Max value for 16-bit
|
||||
constexpr float DECAY_FACTOR = 0.80f; // Faster decay for quicker note changes
|
||||
|
||||
// Timing and Debug
|
||||
constexpr uint32_t LEVEL_UPDATE_INTERVAL_MS = 100; // Level update interval
|
||||
constexpr uint32_t LEVEL_UPDATE_INTERVAL_MS = 50; // Faster updates for better responsiveness
|
||||
constexpr bool ENABLE_DEBUG = true; // Enable debug output
|
||||
constexpr int DEBUG_INTERVAL_MS = 1000; // Debug print interval
|
||||
|
||||
// System Configuration
|
||||
constexpr uint32_t TASK_STACK_SIZE = 4096; // Audio task stack size
|
||||
constexpr uint8_t TASK_PRIORITY = 1; // Audio task priority (0-24)
|
||||
constexpr uint8_t TASK_CORE = 0; // Core to run audio task (0 or 1)
|
||||
constexpr uint8_t TASK_PRIORITY = 2; // Increased priority for more consistent timing
|
||||
constexpr uint8_t TASK_CORE = 1; // Run on second core to avoid interference
|
||||
|
||||
}
|
||||
@@ -5,12 +5,12 @@ void initI2S() {
|
||||
i2s_config_t i2s_config = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
|
||||
.sample_rate = Config::SAMPLE_RATE,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
|
||||
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
.dma_buf_count = 4,
|
||||
.dma_buf_len = 1024,
|
||||
.dma_buf_count = Config::DMA_BUFFER_COUNT,
|
||||
.dma_buf_len = Config::DMA_BUFFER_LEN,
|
||||
.use_apll = false,
|
||||
.tx_desc_auto_clear = false,
|
||||
.fixed_mclk = 0
|
||||
@@ -30,5 +30,12 @@ void initI2S() {
|
||||
}
|
||||
|
||||
void readI2SSamples(int32_t* samples, size_t* bytesRead) {
|
||||
i2s_read(I2S_NUM_0, samples, Config::SAMPLE_BUFFER_SIZE * sizeof(int32_t), bytesRead, portMAX_DELAY);
|
||||
// Read data as 16-bit samples
|
||||
int16_t temp_buffer[Config::SAMPLE_BUFFER_SIZE];
|
||||
i2s_read(I2S_NUM_0, temp_buffer, Config::SAMPLE_BUFFER_SIZE * sizeof(int16_t), bytesRead, portMAX_DELAY);
|
||||
|
||||
// Convert 16-bit samples to 32-bit for compatibility with existing code
|
||||
for (int i = 0; i < Config::SAMPLE_BUFFER_SIZE; i++) {
|
||||
samples[i] = temp_buffer[i] << 16; // Scale up to match previous 32-bit range
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user