stas-sl / esphome-sound-level-meter

74 stars 12 forks source link

Updating to i2s_audio component #5

Open mikenorgate opened 1 year ago

mikenorgate commented 1 year ago

Now that ESPHome has an official i2s_audio component https://esphome.io/components/i2s_audio.html do you have any plans to update the sound level meter to use that implementation?

stas-sl commented 1 year ago

Yeah, I know, it would make sense of course. But unfortunately it lacks a few features for now that I consider rather important. First one is probably quite easy to fix/implement. Currently sample rate/bits per sample are hardcoded there as 16bits@16kHz. Sound meter will of course work with it, but you might need to recalculate all IIR coefficients and probably loose some precision. I don't know how much the difference will be, but I'd prefer rather more precision than less :) I hope, ability to specify audio quality via config will be implemented in the official i2s_audio component at some point.

https://github.com/esphome/esphome/blob/59d6b3afa09c51dc92f036fb8ee746d84489a3ea/esphome/components/i2s_audio/microphone/i2s_audio_microphone.cpp#L27-L41

  i2s_driver_config_t config = {
      .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
      .sample_rate = 16000,
      .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
      .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
      .communication_format = I2S_COMM_FORMAT_STAND_I2S,
      .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
      .dma_buf_count = 4,
      .dma_buf_len = 256,
      .use_apll = false,
      .tx_desc_auto_clear = false,
      .fixed_mclk = 0,
      .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT,
      .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
  };

Second feature is probably more complicated. Currently I'm reading audio data in a separate FreeRTOS task, which means that it can read/process audio even if there are other long running tasks in parallel (like HTTP/MQTT requests). Depending on your config/setup you might have or have not them, but I do have and I'd like not to loose audio data.

Considering these 2 issues, I'd say I don't plan to use official i2s_audio for now, but if it will change in future, I can change my mind :)

wojciechczyz commented 1 year ago

Hi Stas,

bits_per_sample was added recently. Hopefully, sample rate would come soon too: https://github.com/esphome/feature-requests/issues/2289

Cheers!

karrui commented 3 months ago

For those tracking this, sample_rate seems to have been added!

@stas-sl would this be good enough for i2s_audio support?