import struct
import os
import math
#pro = 'denoise'
pro = 'aec'
# 假定的枚举值
if pro == 'denoise':
    start_magic = 0x12345678
    param_version = 15
    function = 0
    mode = 3#A2D D2A
    enable = 1
    level = 4
    input_option = 0 
    output_option = 0
    mic_gain = 5
    hp_volume = 41
    dac_output_inverse = 0
    iis_smplerate = 16000
    iis_bitwidth = 16
    iis_rx_function = 1
    iis_tx_function = 1
    yuliu5_a = 0
    yuliu6_a = 0
    yuliu7_a = 0
    yuliu8_a = 0
    yuliu9_a = 0
    yuliu10_a = 0
    stop_magic = 0x87654321

    # 打包数据为大端格式的二进制
    #22
    denoise_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIIIII',
                            start_magic,
                            param_version,
                            function,
                            mode,
                            enable,
                            level,
                            input_option,
                            output_option,
                            mic_gain,
                            hp_volume,
                            dac_output_inverse,
                            iis_smplerate,
                            iis_bitwidth,
                            iis_rx_function,
                            iis_tx_function,
                            yuliu5_a,
                            yuliu6_a,
                            yuliu7_a,
                            yuliu8_a,
                            yuliu9_a,
                            yuliu10_a,
                            stop_magic,
                            )
# 打包数据为大端格式的二进制
elif pro == 'aec':
    print('*********************aec************************')
    aec_start_magic = 0x12345678
    aec_param_version = 15
    aec_function = 2
    aec_mode = 3#A2D D2A
    aec_enable = 1
    aec_level = 4
    aec_main_mic_option = 0
    aec_main_mic_gain = 5
    aec_sub_mic_option = 1
    aec_sub_mic_gain = 5
    aec_output_option = 0
    aec_hp_volume = 41
    aec_output_inverse = 0
    iis_smplerate = 16000
    iis_bitwidth = 16
    iis_rx_function = 1
    iis_tx_function = 1
    aec_yuliu7_a = 0
    aec_yuliu8_a = 0
    aec_yuliu9_a = 0
    aec_yuliu10_a = 0
    aec_stop_magic = 0x87654321

    # 打包数据为大端格式的二进制
    #22
    denoise_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIIIII',
                            aec_start_magic,
                            aec_param_version,
                            aec_function,
                            aec_mode,
                            aec_enable,
                            aec_level,
                            aec_main_mic_option,
                            aec_main_mic_gain,
                            aec_sub_mic_option,
                            aec_sub_mic_gain,
                            aec_output_option,
                            aec_hp_volume,
                            aec_output_inverse,
                            iis_smplerate,
                            iis_bitwidth,
                            iis_rx_function,
                            iis_tx_function,
                            aec_yuliu7_a,
                            aec_yuliu8_a,
                            aec_yuliu9_a,
                            aec_yuliu10_a,
                            aec_stop_magic,
                            )
# 打包数据为大端格式的二进制
    
agc_start_magic = 0x12345678
agc_param_type = 0x02
agc_param_enable = 1
AGC_enable = 1
noise_gain_enable = 0
Hysteresis = 0
Min_gain = 15
Max_gain = 15
noise_level = 59
Target_level = 20
Peak_Attack_time = 2950760
Peak_Release_time = 147634
Gain_Attack_time = 2950760
Gain_Release_time = 147634

Peak_Attack_time_Ts = 62500
Peak_Attack_time_ta = 1000000

Peak_Release_time_Ts = 62500
Peak_Release_time_tr = 200000000

Gain_Attack_time_Ts = 62500
Gain_Attack_time_ta = 1000000

Gain_Release_time_Ts = 62500
Gain_Release_time_tr = 200000000

agc_stop_magic = 0x87654321

agc_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIIIIII',
                          agc_start_magic,
                          agc_param_type,
                          agc_param_enable,
                          AGC_enable,
                          noise_gain_enable,
                          Hysteresis,
                          Min_gain,
                          Max_gain,
                          noise_level,
                          Target_level,
                          Peak_Attack_time,
                          Peak_Release_time,
                          Gain_Attack_time,
                          Gain_Release_time,
                          
                          Peak_Attack_time_Ts,
                          Peak_Attack_time_ta,

                          Peak_Release_time_Ts,
                          Peak_Release_time_tr,

                          Gain_Attack_time_Ts,
                          Gain_Attack_time_ta,
                          
                          Gain_Release_time_Ts,
                          Gain_Release_time_tr,

                          agc_stop_magic,
                          )


EQ_start_magic = 0x12345678
EQ_param_type = 0x03
EQ_param_enable = 1
EQ_enable = 1
EQ_type = 0
data11 = 0
data12 = 0
data13 = 0
data14 = 0
data15 = 0
data21 = 0
data22 = 0
data23 = 0
data24 = 0
data25 = 0
data31 = 0
data32 = 0
data33 = 0
data34 = 0
data35 = 0
data41 = 0
data42 = 0
data43 = 0
data44 = 0
data45 = 0
data51 = 0
data52 = 0
data53 = 0
data54 = 0
data55 = 0
data61 = 0
data62 = 0
data63 = 0
data64 = 0
data65 = 0
data71 = 0
data72 = 0
data73 = 0
data74 = 0
data75 = 0
data81 = 0
data82 = 0
data83 = 0
data84 = 0
data85 = 0
EQ_stop_magic = 0x87654321
eq_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII',
                        EQ_start_magic,
                        EQ_param_type,
                        EQ_param_enable,
                        EQ_enable,
                        EQ_type,
                        data11,
                        data12,
                        data13,
                        data14,
                        data15,
                        data21,
                        data22,
                        data23,
                        data24,
                        data25,
                        data31,
                        data32,
                        data33,
                        data34,
                        data35,
                        data41,
                        data42,
                        data43,
                        data44,
                        data45,
                        data51,
                        data52,
                        data53,
                        data54,
                        data55,
                        data61,
                        data62,
                        data63,
                        data64,
                        data65,
                        data71,
                        data72,
                        data73,
                        data74,
                        data75,
                        data81,
                        data82,
                        data83,
                        data84,
                        data85,
                        EQ_stop_magic,
                          )

RDC_start_magic = 0x12345678
RDC_param_type = 0x04
RDC_param_enable = 1
RDC_enable = 1
DRC_lmth_data_dB = 5
DRC_lmth = int(math.pow(10, ((0 - DRC_lmth_data_dB) / 20.0)) * math.pow(2, 30))
DRC_SLOP_data = 1
DRC_SLOP = int(DRC_SLOP_data*math.pow(2,30)/100)

DRC_PEAK_AT = 0
DRC_PEAK_RT = 0
DRC_GAIN_AT = 0
DRC_GAIN_RT = 0

DRC_Peak_Attack_time_Ts = 62500
DRC_Peak_Attack_time_ta = 1000000

DRC_Peak_Release_time_Ts = 62500
DRC_Peak_Release_time_tr = 200000000

DRC_Gain_Attack_time_Ts = 62500
DRC_Gain_Attack_time_ta = 1000000

DRC_Gain_Release_time_Ts = 62500
DRC_Gain_Release_time_tr = 200000000



DRC_stop_magic = 0x12345678

DRC_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIIII',
                        RDC_start_magic,
                        RDC_param_type,
                        RDC_param_enable,
                        RDC_enable,
                        DRC_lmth,
                        DRC_SLOP,
                        DRC_PEAK_AT,
                        DRC_PEAK_RT,
                        DRC_GAIN_AT,
                        DRC_GAIN_RT,
                        DRC_Peak_Attack_time_Ts,
                        DRC_Peak_Attack_time_ta,
                        DRC_Peak_Release_time_Ts,
                        DRC_Peak_Release_time_tr,
                        DRC_Gain_Attack_time_Ts,
                        DRC_Gain_Attack_time_ta,
                        DRC_Gain_Release_time_Ts,
                        DRC_Gain_Release_time_tr,
                        DRC_lmth_data_dB,
                        DRC_SLOP_data,
                        DRC_stop_magic,
                          )


hardware_start_magic = 0x12345678
hardware_param_type = 0x05
hardware_param_enable = 1
function_switch_pin = 5
mute_switch_pin = 4
denoise_led_pin = 2
mute_led_pin = 1
power_contrl = 0
codec_bus = 0
hardware_yuliu0 = 0
hardware_yuliu1 = 0
hardware_yuliu2 = 0
hardware_yuliu3 = 0
hardware_yuliu4 = 0
hardware_yuliu5 = 0
hardware_yuliu6 = 0
hardware_yuliu7 = 0
hardware_yuliu8 = 0
hardware_yuliu9 = 0
hardware_stop_magic = 0x87654321;

hardware_packed_data = struct.pack('<IIIIIIIIIIIIIIIIIIII',
                          hardware_start_magic,
                          hardware_param_type,
                          hardware_param_enable,
                          function_switch_pin,
                          mute_switch_pin,
                          denoise_led_pin,
                          mute_led_pin,    
                          power_contrl,
                          codec_bus,
                          hardware_yuliu0,
                          hardware_yuliu1,
                          hardware_yuliu2,
                          hardware_yuliu3,
                          hardware_yuliu4,
                          hardware_yuliu5,
                          hardware_yuliu6,
                          hardware_yuliu7,
                          hardware_yuliu8,
                          hardware_yuliu9,
                          hardware_stop_magic,
                          )

# 获取脚本当前执行的目录
current_directory = os.path.dirname(os.path.abspath(__file__))

file_path = os.path.join(current_directory, 'ext.a')

# 将数据写入文件并对齐到4K
with open(file_path, 'wb') as f:
    f.write(denoise_packed_data)
    f.write(agc_packed_data)
    f.write(eq_packed_data)
    f.write(DRC_packed_data)
    f.write(hardware_packed_data)

    # 计算填充需求
    padding_required = 4096 - (f.tell() % 4096)
    if padding_required != 4096:  # 如果刚好是4K的倍数，则不添加填充
        f.write(b'\0' * padding_required)  # 添加填充

print(f"文件已生成在: {file_path}")

