diff --git a/src/main.cpp b/src/main.cpp index afd15cf..cbfb4cc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,17 @@ #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 +AudioRealFFT fft; // FFT analyzer +FormatConverterStream converter(i2sStream); // Convert 32-bit input to 16-bit output +StreamCopy copier(fft, converter); // copy converted data 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 +int input_bits_per_sample = 32; // INMP441 sends 24-bit data in 32-bit words +int fft_bits_per_sample = 16; + +AudioInfo from(samples_per_second, channels, input_bits_per_sample); +AudioInfo to(samples_per_second, channels, fft_bits_per_sample); const char* solfegeName(uint8_t midiNote) { static const char* solfegeNames[] = { @@ -30,7 +35,7 @@ void fftResult(AudioFFTBase &fft) { const char* solfege = solfegeName(midiNote); int octave = (midiNote / 12) - 1; - Serial.print(freq, 2); + Serial.print(freq, 0); Serial.print(" Hz | "); Serial.print("MIDI "); @@ -50,6 +55,9 @@ void fftResult(AudioFFTBase &fft) { Serial.print(" | Diff: "); Serial.println(diff, 2); + + Serial.print(" | Amp: "); + Serial.println(result.magnitude, 0); } } @@ -61,7 +69,7 @@ void setup() { // Configure I2SStream for INMP441 auto cfg = i2sStream.defaultConfig(RX_MODE); cfg.i2s_format = I2S_STD_FORMAT; - cfg.bits_per_sample = bits_per_sample; + cfg.bits_per_sample = input_bits_per_sample; cfg.channels = channels; cfg.sample_rate = samples_per_second; cfg.is_master = true; @@ -70,12 +78,15 @@ void setup() { cfg.pin_data = 10; // SD i2sStream.begin(cfg); + // Configure FormatConverterStream to convert 32-bit to 16-bit + converter.begin(from, to); // Convert to 16-bit + // 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.bits_per_sample = fft_bits_per_sample; // FFT expects 16-bit data after conversion tcfg.callback = &fftResult; fft.begin(tcfg);