Skip to content

Commit 20aebf2

Browse files
committed
improved support for PDM microphones (SPM1423)
seems that the "swapped channel" workaround is not needed for PDM.
1 parent 9fdc176 commit 20aebf2

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

usermods/audioreactive/audio_reactive.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ class AudioReactive : public Usermod {
11491149
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3)
11501150
case 5:
11511151
DEBUGSR_PRINT(F("AR: I2S PDM Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT));
1152-
audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE);
1152+
audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE, true, 1.0f/4.0f);
11531153
delay(100);
11541154
if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin);
11551155
break;

usermods/audioreactive/audio_source.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,20 @@ class I2SSource : public AudioSource {
201201
return;
202202
}
203203
} else {
204+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)
205+
#if !defined(SOC_I2S_SUPPORTS_PDM_RX)
206+
#warning this MCU does not support PDM microphones
207+
#endif
208+
#endif
204209
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3)
210+
// example from espressif: https://github.com/espressif/esp-idf/blob/release/v4.4/examples/peripherals/i2s/i2s_audio_recorder_sdcard/main/i2s_recorder_main.c
205211
// This is an I2S PDM microphone, these microphones only use a clock and
206212
// data line, to make it simpler to debug, use the WS pin as CLK and SD
207213
// pin as DATA
208214
_config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM); // Change mode to pdm if clock pin not provided. PDM is not supported on ESP32-S2. PDM RX not supported on ESP32-C3
215+
_config.channel_format =I2S_CHANNEL_FMT_ONLY_LEFT; // seems that PDM mono mode always uses left channel.
216+
//_config.use_apll = true; // experimental - use aPLL clock source to improve sampling quality
217+
//_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; // not needed
209218
#endif
210219
}
211220

@@ -249,9 +258,11 @@ class I2SSource : public AudioSource {
249258

250259
DEBUGSR_PRINTF("AR: I2S#0 driver %s aPLL; fixed_mclk=%d.\n", _config.use_apll? "uses":"without", _config.fixed_mclk);
251260
DEBUGSR_PRINTF("AR: Sample scaling factor = %6.4f\n", _sampleScale);
252-
if(_config.mode & I2S_MODE_MASTER)
253-
DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in MASTER mode."));
254-
else
261+
if(_config.mode & I2S_MODE_MASTER) {
262+
if (_config.mode & I2S_MODE_PDM)
263+
DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in PDM MASTER mode."));
264+
else DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in MASTER mode."));
265+
} else
255266
DEBUGSR_PRINTLN(F("AR: I2S#0 driver installed in SLAVE mode."));
256267

257268
err = i2s_set_pin(I2S_NUM_0, &_pinConfig);

0 commit comments

Comments
 (0)