From 5d6cd95ed9733fdfe3a45fbb2d36bf7e78089719 Mon Sep 17 00:00:00 2001 From: Steven Dan Date: Thu, 26 Mar 2026 17:17:41 +0800 Subject: [PATCH] pop noise fix --- .../src/extensions/audiohw.xc | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc index 24d2531..1397704 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc +++ b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/audiohw.xc @@ -470,7 +470,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } debug_printf("HTR3236 LED driver wake success 9\n"); - + // 设置PWM频率为22kHz (避免可闻噪声) //htr3236_set_frequency(i2c, HTR3236_FREQ_22KHZ); unsafe @@ -481,7 +481,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } debug_printf("HTR3236 set frequency success 9\n"); - + // 初始化LED控制 unsafe{led_control_init(&led_ctx, &htr3236_dev, (client interface i2c_master_if)i_i2c_client);} @@ -804,7 +804,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, p_button_music_mode :> button_music_mode; // 1A p_button_game_mode :> button_game_mode; // 1L p_button_ai71_onoff :> button_ai71_onoff; // 1M - + p_mic_gain_encoder1 :> mic_encoder1; // 4F2 p_mic_gain_encoder2 :> mic_encoder2; // 4E2 @@ -1095,8 +1095,8 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, led_on(&led_ctx, led_l_physical_map[i]); } } - - + + // mic_gain = 0; @@ -1125,14 +1125,14 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, uint8_t rotating = (mic_encoder2 & 0b0100) == 0; // 4E2 uint8_t encode_input1 = ((mic_encoder1 & 0b0100) != 0); // 4F2 - + if(rotating) { if(!rotation_active) { // 旋转开始 prev_encode_input1 = encode_input1; sample_count = 1; rotation_active = 1; - + // 根据上次方向预判 if(fast_rotation_mode) { // 快速模式只需要2次采样 @@ -1140,14 +1140,14 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } else { sample_count++; - + // 动态调整采样次数:快速模式用2次,普通模式用3次 uint8_t required_samples = fast_rotation_mode ? 2 : 3; - + if(sample_count >= required_samples) { if(encode_input1 != prev_encode_input1) { uint8_t current_dir = encode_input1 ? 1 : 2; - + // 执行音量调节 if(current_dir == 1) { // 顺时针 if((codec_adc_pga_gain_reg_value < NAU88L21_PGA_GAIN_REG_MAX_VALUE) && (flag_mic_mute == 0)) { @@ -1176,9 +1176,9 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, g_mic_volume_level = (codec_adc_pga_gain_reg_value <= 37) ? codec_adc_pga_gain_reg_value : 37; debug_printf("volume down %d\n", codec_adc_pga_gain_reg_value); save_value(mic_vol_path, codec_adc_pga_gain_reg_value); - + uint8_t new_led_count = mic_gain_to_led[codec_adc_pga_gain_reg_value]; - + // 如果LED数减少,关闭减少的那个LED if(new_led_count < old_led_count) { // 关闭的LED索引 = old_led_count - 1 @@ -1187,7 +1187,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } } - + // 检测是否快速旋转 if(current_dir == last_valid_direction) { consecutive_same_dir++; @@ -1198,7 +1198,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, consecutive_same_dir = 0; fast_rotation_mode = 0; // 方向变化,退出快速模式 } - + last_valid_direction = current_dir; rotation_active = 0; sample_count = 0; @@ -1224,14 +1224,14 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, uint8_t hp_rotating = ((hp_encoder & 0b00100000) == 0); // 8D5 uint8_t hp_encode_input1 = ((hp_encoder & 0b00010000) != 0); // 8D4 - + if(hp_rotating) { if(!hp_rotation_active) { // 旋转开始 hp_prev_encode_input1 = hp_encode_input1; hp_sample_count = 1; hp_rotation_active = 1; - + // 根据上次方向预判 if(hp_fast_rotation_mode) { // 快速模式只需要2次采样 @@ -1239,14 +1239,14 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } else { hp_sample_count++; - + // 动态调整采样次数:快速模式用2次,普通模式用3次 uint8_t hp_required_samples = hp_fast_rotation_mode ? 2 : 3; - + if(hp_sample_count >= hp_required_samples) { if(hp_encode_input1 != hp_prev_encode_input1) { uint8_t hp_current_dir = hp_encode_input1 ? 1 : 2; - + // 执行音量调节 if(hp_current_dir == 1) { // 顺时针 if(dac_level < DAC_LEVEL_MAX && flag_hp_mute == 0) { @@ -1290,7 +1290,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, } } } - + // 检测是否快速旋转 if(hp_current_dir == hp_last_valid_direction) { hp_consecutive_same_dir++; @@ -1301,7 +1301,7 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, hp_consecutive_same_dir = 0; hp_fast_rotation_mode = 0; // 方向变化,退出快速模式 } - + hp_last_valid_direction = hp_current_dir; hp_rotation_active = 0; hp_sample_count = 0; @@ -1617,13 +1617,22 @@ void AudioHwInit() /* Configures the external audio hardware for the required sample frequency */ void AudioHwConfig(unsigned samFreq, unsigned mClk, unsigned dsdMode, unsigned sampRes_DAC, unsigned sampRes_ADC) { + static unsigned count = 0; g_samfreq = samFreq; { sw_pll_fixed_clock(mClk); } - p_ctl_mute <: 1; // 通知tile0 (button_task) 采样率已变化,用于配置NAU88C22寄存器 unsafe {uc_audiohw <: samFreq; } unsafe {uc_audiohw <: dsdMode; } -} + if (count++ > 0) + { + p_ctl_mute <: 1; + } + else + { + debug_printf("don't unmute at boot\n"); + } + +}