feat ✨: Added .gitignore, README files, partitions, build options, dependencies, and configuration for ESP32-S3 development board using Arduino.
- Added `.gitignore` file to exclude unnecessary build and system files. - Summary: Added a README file for project headers, improving organization and management of header files in the source codebase. - Added a README file in the lib directory for project-specific libraries. - Added new partitions to `partitions.csv`. - The `git diff` output updates the `platformio.ini` file to include new build and upload options, dependencies, and configuration for an ESP32-S3 development board using Arduino. - The user has added a new `main.cpp` file, added I2S and FFT functionality to it, and defined a function to calculate the frequency and convert it to a MIDI note number. - Added a new README file in the `test` directory for PlatformIO Unit Testing.
This commit is contained in:
88
src/main.cpp
Normal file
88
src/main.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "AudioTools.h"
|
||||
// #include "AudioTools/AudioLibs/AudioI2SStream.h"
|
||||
#include "AudioTools/AudioLibs/AudioRealFFT.h" // or AudioKissFFT
|
||||
|
||||
I2SStream i2sStream; // I2S input stream for INMP441
|
||||
AudioRealFFT fft; // FFT analyzer
|
||||
StreamCopy copier(fft, i2sStream); // copy I2S mic to FFT
|
||||
|
||||
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
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
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...");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
copier.copy(); // Stream mic data into FFT processor
|
||||
}
|
||||
Reference in New Issue
Block a user