diff --git a/include/AudioLevelTracker.h b/include/AudioLevelTracker.h index 2fa5eb8..0f9742f 100644 --- a/include/AudioLevelTracker.h +++ b/include/AudioLevelTracker.h @@ -2,6 +2,7 @@ #include #include +#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 }; \ No newline at end of file diff --git a/include/Config.h b/include/Config.h index e99cfb4..8fa0da4 100644 --- a/include/Config.h +++ b/include/Config.h @@ -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 } \ No newline at end of file diff --git a/src/I2SConfig.cpp b/src/I2SConfig.cpp index c80d03b..24d9f26 100644 --- a/src/I2SConfig.cpp +++ b/src/I2SConfig.cpp @@ -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 + } } \ No newline at end of file