Compare commits
3 Commits
test_bits_
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 25dae87647 | |||
| e9205c88fa | |||
| 4654bea268 |
@@ -33,4 +33,6 @@ monitor_filters = esp32_exception_decoder
|
||||
lib_deps =
|
||||
https://github.com/pschatzmann/arduino-audio-tools.git
|
||||
https://github.com/pschatzmann/arduino-audio-driver.git
|
||||
|
||||
https://github.com/tzapu/WiFiManager.git
|
||||
me-no-dev/AsyncTCP
|
||||
https://github.com/me-no-dev/ESPAsyncWebServer.git
|
||||
|
||||
196
src/main.cpp
196
src/main.cpp
@@ -1,88 +1,130 @@
|
||||
#include "AudioTools.h"
|
||||
// #include "AudioTools/AudioLibs/AudioI2SStream.h"
|
||||
#include "AudioTools/AudioLibs/AudioRealFFT.h" // or AudioKissFFT
|
||||
#include <Arduino.h>
|
||||
#include <driver/i2s.h>
|
||||
#include <deque>
|
||||
|
||||
I2SStream i2sStream; // I2S input stream for INMP441
|
||||
AudioRealFFT fft; // FFT analyzer
|
||||
StreamCopy copier(fft, i2sStream); // copy I2S mic to FFT
|
||||
// you shouldn't need to change these settings
|
||||
#define SAMPLE_BUFFER_SIZE 512
|
||||
#define SAMPLE_RATE 8000
|
||||
// most microphones will probably default to left channel but you may need to tie the L/R pin low
|
||||
#define I2S_MIC_CHANNEL I2S_CHANNEL_FMT_ONLY_LEFT
|
||||
// either wire your microphone to the same pins or change these to match your wiring
|
||||
#define I2S_MIC_SERIAL_CLOCK 8
|
||||
#define I2S_MIC_LEFT_RIGHT_CLOCK 9
|
||||
#define I2S_MIC_SERIAL_DATA 10
|
||||
|
||||
int channels = 1; // INMP441 is mono
|
||||
int samples_per_second = 11025;
|
||||
int bits_per_sample = 32; // INMP441 sends 24-bit data in 32-bit words
|
||||
// Add sample history tracking for range limiting
|
||||
#define HISTORY_DURATION_MS 1000 // 1 seconds history
|
||||
#define SAMPLES_PER_MS (SAMPLE_RATE / 1000)
|
||||
#define HISTORY_SIZE (HISTORY_DURATION_MS * SAMPLES_PER_MS)
|
||||
|
||||
const char* solfegeName(uint8_t midiNote) {
|
||||
static const char* solfegeNames[] = {
|
||||
"Do", "Do#", "Re", "Re#", "Mi", "Fa", "Fa#", "Sol", "Sol#", "La", "La#", "Si"
|
||||
};
|
||||
return solfegeNames[midiNote % 12];
|
||||
}
|
||||
#define MAX_RANGE_LIMIT 150000000 // Maximum allowed range limit
|
||||
#define DEFAULT_RANGE_LIMIT 20000 // Default range limit when no history
|
||||
|
||||
void fftResult(AudioFFTBase &fft) {
|
||||
float diff;
|
||||
auto result = fft.result();
|
||||
|
||||
if (result.magnitude > 100) { // avoid noise floor
|
||||
float magnitude_dB = 20.0 * log10(result.magnitude);
|
||||
float freq = result.frequency;
|
||||
|
||||
// MIDI note number
|
||||
int midiNote = round(69 + 12.0 * log2(freq / 440.0));
|
||||
const char* solfege = solfegeName(midiNote);
|
||||
int octave = (midiNote / 12) - 1;
|
||||
|
||||
Serial.print(freq, 2);
|
||||
Serial.print(" Hz | ");
|
||||
|
||||
Serial.print("MIDI ");
|
||||
Serial.print(midiNote);
|
||||
Serial.print(" | ");
|
||||
|
||||
Serial.print("Note: ");
|
||||
Serial.print(result.frequencyAsNote(diff));
|
||||
Serial.print(" | ");
|
||||
|
||||
Serial.print("Solfège: ");
|
||||
Serial.print(solfege);
|
||||
Serial.print(octave);
|
||||
|
||||
Serial.print(" | dB: ");
|
||||
Serial.print(magnitude_dB, 2);
|
||||
|
||||
Serial.print(" | Diff: ");
|
||||
Serial.println(diff, 2);
|
||||
class AudioLevelTracker {
|
||||
public:
|
||||
AudioLevelTracker() {
|
||||
resetMaxLevel();
|
||||
}
|
||||
}
|
||||
|
||||
void updateMaxLevel(int32_t sample) {
|
||||
uint32_t currentTime = millis();
|
||||
|
||||
void setup() {
|
||||
// Remove old samples (older than 10 seconds)
|
||||
while (!sampleHistory.empty() &&
|
||||
(currentTime - sampleHistory.front().timestamp) > HISTORY_DURATION_MS) {
|
||||
sampleHistory.pop_front();
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// Update maximum
|
||||
maxLevel = 0;
|
||||
for (const auto& point : sampleHistory) {
|
||||
if (point.value > maxLevel) {
|
||||
maxLevel = point.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t getMaxLevel() const {
|
||||
return maxLevel;
|
||||
}
|
||||
|
||||
void resetMaxLevel() {
|
||||
maxLevel = 0;
|
||||
sampleHistory.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
struct SamplePoint {
|
||||
uint32_t timestamp;
|
||||
int32_t value;
|
||||
};
|
||||
std::deque<SamplePoint> sampleHistory;
|
||||
int32_t maxLevel;
|
||||
};
|
||||
|
||||
AudioLevelTracker audioLevelTracker;
|
||||
|
||||
// don't mess around with this
|
||||
i2s_config_t i2s_config = {
|
||||
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
|
||||
.sample_rate = SAMPLE_RATE,
|
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
|
||||
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
|
||||
.communication_format = I2S_COMM_FORMAT_STAND_I2S, // Updated from I2S_COMM_FORMAT_I2S
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
.dma_buf_count = 4,
|
||||
.dma_buf_len = 1024,
|
||||
.use_apll = false,
|
||||
.tx_desc_auto_clear = false,
|
||||
.fixed_mclk = 0};
|
||||
|
||||
// and don't mess around with this
|
||||
i2s_pin_config_t i2s_mic_pins = {
|
||||
.bck_io_num = I2S_MIC_SERIAL_CLOCK,
|
||||
.ws_io_num = I2S_MIC_LEFT_RIGHT_CLOCK,
|
||||
.data_out_num = I2S_PIN_NO_CHANGE,
|
||||
.data_in_num = I2S_MIC_SERIAL_DATA};
|
||||
|
||||
void setup()
|
||||
{
|
||||
// we need serial output for the plotter
|
||||
Serial.begin(115200);
|
||||
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
|
||||
|
||||
// Configure I2SStream for INMP441
|
||||
auto cfg = i2sStream.defaultConfig(RX_MODE);
|
||||
cfg.i2s_format = I2S_STD_FORMAT;
|
||||
cfg.bits_per_sample = bits_per_sample;
|
||||
cfg.channels = channels;
|
||||
cfg.sample_rate = samples_per_second;
|
||||
cfg.is_master = true;
|
||||
cfg.pin_bck = 8; // SCK
|
||||
cfg.pin_ws = 9; // WS
|
||||
cfg.pin_data = 10; // SD
|
||||
i2sStream.begin(cfg);
|
||||
|
||||
// Configure FFT
|
||||
auto tcfg = fft.defaultConfig();
|
||||
tcfg.length = 8192; // 186ms @ 11kHz minimun C2 theoretical
|
||||
tcfg.channels = channels;
|
||||
tcfg.sample_rate = samples_per_second;
|
||||
tcfg.bits_per_sample = bits_per_sample;
|
||||
tcfg.callback = &fftResult;
|
||||
fft.begin(tcfg);
|
||||
|
||||
Serial.println("Setup complete. Listening...");
|
||||
|
||||
// start up the I2S peripheral
|
||||
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
|
||||
i2s_set_pin(I2S_NUM_0, &i2s_mic_pins);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
copier.copy(); // Stream mic data into FFT processor
|
||||
int32_t raw_samples[SAMPLE_BUFFER_SIZE];
|
||||
void loop()
|
||||
{
|
||||
// read from the I2S device
|
||||
size_t bytes_read = 0;
|
||||
i2s_read(I2S_NUM_0, raw_samples, sizeof(int32_t) * SAMPLE_BUFFER_SIZE, &bytes_read, portMAX_DELAY);
|
||||
int samples_read = bytes_read / sizeof(int32_t);
|
||||
|
||||
// Calculate dynamic range limit based on max level from last 10 seconds
|
||||
int32_t currentMaxLevel = audioLevelTracker.getMaxLevel();
|
||||
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++)
|
||||
{
|
||||
// Update the max level tracker with current sample
|
||||
audioLevelTracker.updateMaxLevel(raw_samples[i]);
|
||||
|
||||
// Print range limits for plotter
|
||||
Serial.print(rangelimit * -1);
|
||||
Serial.print(" ");
|
||||
Serial.print(rangelimit);
|
||||
Serial.print(" ");
|
||||
|
||||
// Print the actual sample
|
||||
Serial.printf("%ld\n", raw_samples[i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user