From d9e1ea710b62103c3ebe4ac7f28dde8d1856bada Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 8 Oct 2025 10:36:16 -0700 Subject: [PATCH] Don't set I2S rate unless it's different (#771) On the ESP32 it seems the i2s clock set will cause a hiccup, even if it's setting the exact same clock as before. Avoid it by only setting the I2S clock if it differs from the last set. Initialize it to 0hz to ensure the first setRate call always works. Supercedes #656 --- src/AudioOutputI2S.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/AudioOutputI2S.cpp b/src/AudioOutputI2S.cpp index ab582f4..cb7bf3c 100644 --- a/src/AudioOutputI2S.cpp +++ b/src/AudioOutputI2S.cpp @@ -44,7 +44,7 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int lsb_justified = false; bps = 16; channels = 2; - hertz = 44100; + hertz = 0; // Needs to be application-set bclkPin = 26; wclkPin = 25; doutPin = 22; @@ -126,7 +126,9 @@ bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout, int mclk) { } bool AudioOutputI2S::SetRate(int hz) { - // TODO - have a list of allowable rates from constructor, check them + if (this->hertz == hz) { + return true; + } this->hertz = hz; if (i2sOn) { #ifdef ESP32 @@ -306,7 +308,7 @@ bool AudioOutputI2S::begin(bool txDAC) { } #endif i2sOn = true; - SetRate(hertz); // Default + SetRate(hertz ? hertz : 44100); // Default return true; }