i2s INMP441 MEMS microphone streamed via ESP32 PICO-D4 to MAX98357A earphone output #1817
-
I would appreciate help with the below code. At best, I get low volume voice (albeit full volume chord) streaming to the MAX98375A earphones from the INMP441 microphone. I have tried virtually all combinations of channels, bits/channel, sampleRate... Assume the wiring is correct since the chord plays beautifully. Any suggestions, comments... welcomed and much appreciated in advance. /*
# Simple program to stream audio captured
# from:
# left channel INMP441 MEMS microphone
# via:
# ESP32 PICO-D4
# to:
# left Channel MAX98357A earphone output
*/
#include <AudioTools.h>
AudioInfo info(22050, 1, 16);
I2SStream i2s_in; // in from I2S INMP441 Mems Microphone Board
I2SStream i2s_out; // out to I2S MAX98357A Board
VolumeStream volume(i2s_in);
ConverterFillLeftAndRight<int32_t> filler(RightIsEmpty); // fill both channels - or change to RightIsEmpt
StreamCopy copier(i2s_out, volume); // copies the converted in channel (conv) to i2s_out
// const float sampleRate = 22050; //44100; // Sample rate in Hz
int16_t amplitude = 10000;
bool hasRun = false;
void beep(int duration_ms, float freq) {
unsigned long startTime = millis();
const float frequency = freq;
const float sampleRate = 22050; // Sample rate in Hz
float phase = 0.0;
while (millis() - startTime < duration_ms) {
int16_t sample = (int16_t)(amplitude * sin(phase));
phase += 2 * PI * frequency / sampleRate;
if (phase >= 2 * PI) {
phase -= 2 * PI;
}
i2s_out.write((uint8_t *)&sample, sizeof(sample));
}
}
void setup() {
pinMode(23, OUTPUT); // Used for SD pin on MAX98357A Board output. High = Left, Low=Off
digitalWrite(23, HIGH); // Set the pin HIGH
// Configure i2s_in with the current combination
auto cfg_in = i2s_in.defaultConfig(RX_MODE);
cfg_in.copyFrom(info);
cfg_in.i2s_format = I2S_STD_FORMAT;
cfg_in.is_master = true;
cfg_in.port_no = 0;
//
cfg_in.pin_bck = 26;
cfg_in.pin_ws = 25;
cfg_in.pin_data = 27;
//
// cfg_in.channel_format = audio_tools::I2SChannelSelect::Left; //Left; // I2S_CHANNEL_FMT_ALL_RIGHT;
i2s_in.begin(cfg_in);
volume.begin(cfg_in); // we need to provide the bits_per_sample and channels same as input mems microphone
volume.setVolume(0.95);
// Configure i2s_out with the current combination
auto cfg_out = i2s_out.defaultConfig(TX_MODE);
cfg_out.copyFrom(info);
cfg_out.i2s_format = I2S_STD_FORMAT;
cfg_out.is_master = true;
cfg_out.port_no = 1;
// cfg_out.channel_format = audio_tools::I2SChannelSelect::Left;
cfg_out.pin_bck = 10;
cfg_out.pin_ws = 5;
cfg_out.pin_data = 9;
i2s_out.begin(cfg_out);
}
void playChord() {
// Tome (beep) sequence confirms I2S output operating properly
beep(50, 261.63); // C note - Root
delay(5);
beep(50, 329.63); // E note - Major third
delay(5);
beep(90, 493.88); // B note - Major 7th
}
void loop() {
if (!hasRun) {
playChord();
hasRun = true;
}
copier.copy(filler);
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Just read the Wiki about Volume Control: you might want to amplify the signal! |
Beta Was this translation helpful? Give feedback.
-
according to its datasheet the mic always delivers 32bit frames but with just 24bit used for samples. If you use 16bit samples I think with „normal“ volume control you end up with full amplitudes but only 12 bits used. |
Beta Was this translation helpful? Give feedback.
Just read the Wiki about Volume Control: you might want to amplify the signal!