feat ✨: Updated src/main.cpp to utilize FormatConverterStream for converting 32-bit I2S input to 16-bit output and configured an AudioFFTBase with the correct bits per sample
- Modified `src/main.cpp` to use `FormatConverterStream` for converting 32-bit I2S input to 16-bit output, then configured an `AudioFFTBase` with the appropriate bits per sample.
This commit is contained in:
21
src/main.cpp
21
src/main.cpp
@@ -4,11 +4,16 @@
|
|||||||
|
|
||||||
I2SStream i2sStream; // I2S input stream for INMP441
|
I2SStream i2sStream; // I2S input stream for INMP441
|
||||||
AudioRealFFT fft; // FFT analyzer
|
AudioRealFFT fft; // FFT analyzer
|
||||||
StreamCopy copier(fft, i2sStream); // copy I2S mic to FFT
|
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 channels = 1; // INMP441 is mono
|
||||||
int samples_per_second = 11025;
|
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) {
|
const char* solfegeName(uint8_t midiNote) {
|
||||||
static const char* solfegeNames[] = {
|
static const char* solfegeNames[] = {
|
||||||
@@ -30,7 +35,7 @@ void fftResult(AudioFFTBase &fft) {
|
|||||||
const char* solfege = solfegeName(midiNote);
|
const char* solfege = solfegeName(midiNote);
|
||||||
int octave = (midiNote / 12) - 1;
|
int octave = (midiNote / 12) - 1;
|
||||||
|
|
||||||
Serial.print(freq, 2);
|
Serial.print(freq, 0);
|
||||||
Serial.print(" Hz | ");
|
Serial.print(" Hz | ");
|
||||||
|
|
||||||
Serial.print("MIDI ");
|
Serial.print("MIDI ");
|
||||||
@@ -50,6 +55,9 @@ void fftResult(AudioFFTBase &fft) {
|
|||||||
|
|
||||||
Serial.print(" | Diff: ");
|
Serial.print(" | Diff: ");
|
||||||
Serial.println(diff, 2);
|
Serial.println(diff, 2);
|
||||||
|
|
||||||
|
Serial.print(" | Amp: ");
|
||||||
|
Serial.println(result.magnitude, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +69,7 @@ void setup() {
|
|||||||
// Configure I2SStream for INMP441
|
// Configure I2SStream for INMP441
|
||||||
auto cfg = i2sStream.defaultConfig(RX_MODE);
|
auto cfg = i2sStream.defaultConfig(RX_MODE);
|
||||||
cfg.i2s_format = I2S_STD_FORMAT;
|
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.channels = channels;
|
||||||
cfg.sample_rate = samples_per_second;
|
cfg.sample_rate = samples_per_second;
|
||||||
cfg.is_master = true;
|
cfg.is_master = true;
|
||||||
@@ -70,12 +78,15 @@ void setup() {
|
|||||||
cfg.pin_data = 10; // SD
|
cfg.pin_data = 10; // SD
|
||||||
i2sStream.begin(cfg);
|
i2sStream.begin(cfg);
|
||||||
|
|
||||||
|
// Configure FormatConverterStream to convert 32-bit to 16-bit
|
||||||
|
converter.begin(from, to); // Convert to 16-bit
|
||||||
|
|
||||||
// Configure FFT
|
// Configure FFT
|
||||||
auto tcfg = fft.defaultConfig();
|
auto tcfg = fft.defaultConfig();
|
||||||
tcfg.length = 8192; // 186ms @ 11kHz minimun C2 theoretical
|
tcfg.length = 8192; // 186ms @ 11kHz minimun C2 theoretical
|
||||||
tcfg.channels = channels;
|
tcfg.channels = channels;
|
||||||
tcfg.sample_rate = samples_per_second;
|
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;
|
tcfg.callback = &fftResult;
|
||||||
fft.begin(tcfg);
|
fft.begin(tcfg);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user