Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[i2s_audio] Add more options to speakers and microphones #7306

Merged
merged 9 commits into from
Sep 11, 2024

Conversation

pyos
Copy link
Contributor

@pyos pyos commented Aug 18, 2024

What does this implement/fix?

Now I2S speakers also have the following options that already exist for microphones:

  • i2s_mode (primary/secondary; default: primary)
  • use_apll (true/false, default: false)
  • channel (left/right/mono/stereo, default: mono)
  • sample_rate (default: 16000)
  • bits_per_sample (default: 16bit)

Both speakers and microphones also have:

  • bits_per_channel (default: equal to bits_per_sample)

channel has new values:

  • stereo: input/output data is interpreted as a sequence of (right, left) pairs (this option works for both speakers and microphones);
  • mono: for speakers, this is the old behavior - each sample from the input is sent to both left and right channels.

Also, speakers have an extra option:

  • timeout (default: 100ms) - how long the background tasks should wait before releasing the bus if the buffer is empty.

The mode option of speakers was removed, because it didn't actually do anything.

The pdm option for microphones is now optional and defaults to false.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#4166

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

i2s_audio:
- i2s_lrclk_pin: 26
  i2s_bclk_pin: 27
  id: i2so
- i2s_lrclk_pin: 33 # WS
  i2s_bclk_pin: 32 # SCK
  id: i2si

microphone:
- platform: i2s_audio
  id: sound_in
  adc_type: external
  i2s_din_pin: 25 # SD
  i2s_audio_id: i2si
  sample_rate: 24000
  bits_per_sample: 32bit
  channel: right
  on_data:
    lambda: |-
      id(sound_out)->play((const uint8_t*)x.data(), x.size() * sizeof(x[0]));

speaker:
- platform: i2s_audio
  id: sound_out
  dac_type: external
  i2s_dout_pin: 14
  i2s_audio_id: i2so
  sample_rate: 24000
  bits_per_sample: 32bit
  channel: mono
  timeout: 500ms

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@probot-esphome
Copy link

Hey there @jesserockz, mind taking a look at this pull request as it has been labeled with an integration (i2s_audio) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@codecov-commenter
Copy link

codecov-commenter commented Aug 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.96%. Comparing base (4d8b5ed) to head (e43a33e).
Report is 1293 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #7306      +/-   ##
==========================================
+ Coverage   53.70%   53.96%   +0.25%     
==========================================
  Files          50       50              
  Lines        9408     9687     +279     
  Branches     1654     1711      +57     
==========================================
+ Hits         5053     5228     +175     
- Misses       4056     4128      +72     
- Partials      299      331      +32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pyos pyos force-pushed the i2s-options branch 2 times, most recently from 764a85c to 5e33b1a Compare August 18, 2024 13:17
Copy link
Contributor

@nielsnl68 nielsnl68 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we had almost the same ideas, already.
see:
#7183
#7137

esphome/components/i2s_audio/__init__.py Show resolved Hide resolved
esphome/components/i2s_audio/__init__.py Outdated Show resolved Hide resolved
Comment on lines 91 to 67
cv.Required(CONF_PDM): cv.boolean,
cv.Optional(CONF_PDM, default=False): cv.boolean,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my honest opinion this should be an option for the ADC_TYPE enum like:
internal, standard, PDM

BASE_SCHEMA = i2s_component_schema(
speaker.SPEAKER_SCHEMA,
I2SAudioSpeaker,
default_channel=CONF_MONO,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to use "CONF_RIGHT" here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONF_MONO is equivalent to the current behavior

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but still i do think it is not necessary

esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp Outdated Show resolved Hide resolved
esphome/components/i2s_audio/speaker/i2s_audio_speaker.h Outdated Show resolved Hide resolved
esphome/components/i2s_audio/speaker/i2s_audio_speaker.h Outdated Show resolved Hide resolved
@esphome
Copy link

esphome bot commented Aug 19, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

Now I2S speakers also have the following options that already
exist for microphones:

 * `i2s_mode` (primary/secondary; default: primary)
 * `use_apll` (true/false, default: false)
 * `channel` (left/right/mono/stereo, default: mono)
 * `sample_rate` (default: 16000)
 * `bits_per_sample` (default: 16bit)

Both speakers and microphones also have:

 * `bits_per_channel` (default: equal to `bits_per_sample`)

`channel` has new values:

 * stereo: input/output data is interpreted as a sequence of
   (right, left) pairs (this option works for both speakers
   and microphones);
 * mono: for speakers, this is the old behavior - each sample
   from the input is sent to both left and right channels.

Also, speakers have an extra option:

 * `timeout` (default: 100ms) - how long the background tasks
   should wait before releasing the bus if the buffer is empty.
At high sample rates in stereo mode they overwhelm the queue...
@jesserockz
Copy link
Member

I have rebased this PR after merging #7183. The tests compile but I have not actually looked at the code or reviewed or tested yet.

@jesserockz jesserockz merged commit 04248b6 into esphome:dev Sep 11, 2024
28 checks passed
@jesserockz jesserockz mentioned this pull request Sep 11, 2024
dcoghlan pushed a commit to dcoghlan/esphome that referenced this pull request Sep 12, 2024
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants