Skip to content

Fix redundantly setting i2s_format#18

Merged
chmorgan merged 1 commit intochmorgan:mainfrom
robert-alfaro:bugfix/fix-i2s-format
May 17, 2025
Merged

Fix redundantly setting i2s_format#18
chmorgan merged 1 commit intochmorgan:mainfrom
robert-alfaro:bugfix/fix-i2s-format

Conversation

@robert-alfaro
Copy link
Contributor

The i2s format configuration was being set upon every call to play audio regardless of an actual format change because the variable being used to track and compare against was always new.

By moving the tracking variable to the audio instance context, we can safely compare and set when necessary.

@chmorgan chmorgan requested a review from Copilot May 17, 2025 02:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR prevents unnecessary reconfiguration of the I2S format on each audio playback by storing the last-used format in the audio_instance_t context instead of a temporary variable.

  • Added i2s_format field to audio_instance_t
  • Removed local i2s_format and replaced its uses with the context field
  • Zero-initialized instance.i2s_format in audio_player_new
Comments suppressed due to low confidence (1)

audio_player.cpp:365

  • The format specifiers %lu may not match the type of bits_per_sample and channels. Verify their types and use %d or %zu accordingly to avoid logging mismatches.
LOGI_1("format change: sr=%d, bit=%lu, ch=%lu",

Comment on lines +361 to +371
if ((instance.i2s_format.sample_rate != i->output.fmt.sample_rate) ||
(instance.i2s_format.channels != i->output.fmt.channels) ||
(instance.i2s_format.bits_per_sample != i->output.fmt.bits_per_sample)) {
instance.i2s_format = i->output.fmt;
LOGI_1("format change: sr=%d, bit=%lu, ch=%lu",
instance.i2s_format.sample_rate,
instance.i2s_format.bits_per_sample,
instance.i2s_format.channels);
i2s_slot_mode_t channel_setting = (instance.i2s_format.channels == 1) ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO;
ret = i->config.clk_set_fn(instance.i2s_format.sample_rate,
instance.i2s_format.bits_per_sample,
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

[nitpick] For consistency and future support of multiple instances, consider using i->i2s_format instead of the global instance inside this function.

Suggested change
if ((instance.i2s_format.sample_rate != i->output.fmt.sample_rate) ||
(instance.i2s_format.channels != i->output.fmt.channels) ||
(instance.i2s_format.bits_per_sample != i->output.fmt.bits_per_sample)) {
instance.i2s_format = i->output.fmt;
LOGI_1("format change: sr=%d, bit=%lu, ch=%lu",
instance.i2s_format.sample_rate,
instance.i2s_format.bits_per_sample,
instance.i2s_format.channels);
i2s_slot_mode_t channel_setting = (instance.i2s_format.channels == 1) ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO;
ret = i->config.clk_set_fn(instance.i2s_format.sample_rate,
instance.i2s_format.bits_per_sample,
if ((i->i2s_format.sample_rate != i->output.fmt.sample_rate) ||
(i->i2s_format.channels != i->output.fmt.channels) ||
(i->i2s_format.bits_per_sample != i->output.fmt.bits_per_sample)) {
i->i2s_format = i->output.fmt;
LOGI_1("format change: sr=%d, bit=%lu, ch=%lu",
i->i2s_format.sample_rate,
i->i2s_format.bits_per_sample,
i->i2s_format.channels);
i2s_slot_mode_t channel_setting = (i->i2s_format.channels == 1) ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO;
ret = i->config.clk_set_fn(i->i2s_format.sample_rate,
i->i2s_format.bits_per_sample,

Copilot uses AI. Check for mistakes.
instance.i2s_format.channels);
i2s_slot_mode_t channel_setting = (instance.i2s_format.channels == 1) ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO;
ret = i->config.clk_set_fn(instance.i2s_format.sample_rate,
instance.i2s_format.bits_per_sample,
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

This still references the removed local i2s_format. Update it to instance.i2s_format.bits_per_sample (or i->i2s_format.bits_per_sample if following suggestion above) so the correct field is used.

Copilot uses AI. Check for mistakes.
@chmorgan chmorgan merged commit 699b4dc into chmorgan:main May 17, 2025
1 check passed
@chmorgan
Copy link
Owner

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants