@@ -34,6 +34,12 @@ void AudioCodec::Start() {
3434 output_volume_ = 10 ;
3535 }
3636
37+ // 保存原始输出采样率
38+ if (original_output_sample_rate_ == 0 ) {
39+ original_output_sample_rate_ = output_sample_rate_;
40+ ESP_LOGI (TAG, " Saved original output sample rate: %d Hz" , original_output_sample_rate_);
41+ }
42+
3743 if (tx_handle_ != nullptr ) {
3844 ESP_ERROR_CHECK (i2s_channel_enable (tx_handle_));
3945 }
@@ -75,3 +81,74 @@ void AudioCodec::EnableOutput(bool enable) {
7581 output_enabled_ = enable;
7682 ESP_LOGI (TAG, " Set output enable to %s" , enable ? " true" : " false" );
7783}
84+
85+ bool AudioCodec::SetOutputSampleRate (int sample_rate) {
86+ // 特殊处理:如果传入 -1,表示重置到原始采样率
87+ if (sample_rate == -1 ) {
88+ if (original_output_sample_rate_ > 0 ) {
89+ sample_rate = original_output_sample_rate_;
90+ ESP_LOGI (TAG, " Resetting to original output sample rate: %d Hz" , sample_rate);
91+ } else {
92+ ESP_LOGW (TAG, " Original sample rate not available, cannot reset" );
93+ return false ;
94+ }
95+ }
96+
97+ if (sample_rate <= 0 || sample_rate > 192000 ) {
98+ ESP_LOGE (TAG, " Invalid sample rate: %d" , sample_rate);
99+ return false ;
100+ }
101+
102+ if (output_sample_rate_ == sample_rate) {
103+ ESP_LOGI (TAG, " Sample rate already set to %d Hz" , sample_rate);
104+ return true ;
105+ }
106+
107+ if (tx_handle_ == nullptr ) {
108+ ESP_LOGW (TAG, " TX handle is null, only updating sample rate variable" );
109+ output_sample_rate_ = sample_rate;
110+ return true ;
111+ }
112+
113+ ESP_LOGI (TAG, " Changing output sample rate from %d to %d Hz" , output_sample_rate_, sample_rate);
114+
115+ // 先尝试禁用 I2S 通道(如果已启用的话)
116+ esp_err_t disable_ret = i2s_channel_disable (tx_handle_);
117+ if (disable_ret == ESP_OK) {
118+ ESP_LOGI (TAG, " Disabled I2S TX channel for reconfiguration" );
119+ } else if (disable_ret == ESP_ERR_INVALID_STATE) {
120+ // 通道可能已经是禁用状态,这是正常的
121+ ESP_LOGI (TAG, " I2S TX channel was already disabled" );
122+ } else {
123+ ESP_LOGW (TAG, " Failed to disable I2S TX channel: %s" , esp_err_to_name (disable_ret));
124+ }
125+
126+ // 重新配置 I2S 时钟
127+ i2s_std_clk_config_t clk_cfg = {
128+ .sample_rate_hz = (uint32_t )sample_rate,
129+ .clk_src = I2S_CLK_SRC_DEFAULT,
130+ .mclk_multiple = I2S_MCLK_MULTIPLE_256,
131+ #ifdef I2S_HW_VERSION_2
132+ .ext_clk_freq_hz = 0 ,
133+ #endif
134+ };
135+
136+ esp_err_t ret = i2s_channel_reconfig_std_clock (tx_handle_, &clk_cfg);
137+
138+ // 重新启用通道(无论之前是什么状态,现在都需要启用以便播放音频)
139+ esp_err_t enable_ret = i2s_channel_enable (tx_handle_);
140+ if (enable_ret != ESP_OK) {
141+ ESP_LOGE (TAG, " Failed to enable I2S TX channel: %s" , esp_err_to_name (enable_ret));
142+ } else {
143+ ESP_LOGI (TAG, " Enabled I2S TX channel" );
144+ }
145+
146+ if (ret == ESP_OK) {
147+ output_sample_rate_ = sample_rate;
148+ ESP_LOGI (TAG, " Successfully changed output sample rate to %d Hz" , sample_rate);
149+ return true ;
150+ } else {
151+ ESP_LOGE (TAG, " Failed to change sample rate to %d Hz: %s" , sample_rate, esp_err_to_name (ret));
152+ return false ;
153+ }
154+ }
0 commit comments