Skip to content

Commit

Permalink
Allow microphone channel to be specified in config (esphome#4871)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesserockz authored May 22, 2023
1 parent 40d110f commit f7b5c63
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion esphome/components/i2s_audio/microphone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import esphome.codegen as cg

from esphome import pins
from esphome.const import CONF_ID, CONF_NUMBER
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_NUMBER
from esphome.components import microphone, esp32
from esphome.components.adc import ESP32_VARIANT_ADC1_PIN_TO_CHANNEL, validate_adc_pin

Expand All @@ -25,6 +25,12 @@
"I2SAudioMicrophone", I2SAudioIn, microphone.Microphone, cg.Component
)

i2s_channel_fmt_t = cg.global_ns.enum("i2s_channel_fmt_t")
CHANNELS = {
"left": i2s_channel_fmt_t.I2S_CHANNEL_FMT_ONLY_LEFT,
"right": i2s_channel_fmt_t.I2S_CHANNEL_FMT_ONLY_RIGHT,
}

INTERNAL_ADC_VARIANTS = [esp32.const.VARIANT_ESP32]
PDM_VARIANTS = [esp32.const.VARIANT_ESP32, esp32.const.VARIANT_ESP32S3]

Expand All @@ -47,6 +53,7 @@ def validate_esp32_variant(config):
{
cv.GenerateID(): cv.declare_id(I2SAudioMicrophone),
cv.GenerateID(CONF_I2S_AUDIO_ID): cv.use_id(I2SAudioComponent),
cv.Optional(CONF_CHANNEL, default="right"): cv.enum(CHANNELS),
}
).extend(cv.COMPONENT_SCHEMA)

Expand Down Expand Up @@ -86,4 +93,6 @@ async def to_code(config):
cg.add(var.set_din_pin(config[CONF_I2S_DIN_PIN]))
cg.add(var.set_pdm(config[CONF_PDM]))

cg.add(var.set_channel(CHANNELS[config[CONF_CHANNEL]]))

await microphone.register_microphone(var, config)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void I2SAudioMicrophone::start_() {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 16000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.channel_format = this->channel_,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub
}
#endif

void set_channel(i2s_channel_fmt_t channel) { this->channel_ = channel; }

protected:
void start_();
void stop_();
Expand All @@ -40,6 +42,7 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub
#endif
bool pdm_{false};
std::vector<uint8_t> buffer_;
i2s_channel_fmt_t channel_;

HighFrequencyLoopRequester high_freq_;
};
Expand Down

0 comments on commit f7b5c63

Please sign in to comment.