#define DEBUG_PRINT_ENABLE 0
#include "eq_coefficient_calculator.h"
#include "biquad_standalone.h"
#include <math.h>

#if 0 // DEBUG_PRINT_ENABLE
#define DPRINTF(...) printf(__VA_ARGS__)
#else
#define DPRINTF(...)
#endif


// 系数计算函数
int eq_calculate_coefficients_from_params(filter_params_t *params, 
                                        uint32_t sample_rate,
                                        q2_30 coeffs[5],
                                        int *bshift) {
    
    if (!params || !coeffs || !bshift) {
        return -1;
    }

    // 根据滤波器类型调用相应的biquad计算函数
    switch (params->type) {
        case FILTER_TYPE_BYPASS:
            eq_biquad_bypass(coeffs);
            *bshift = 0;
            break;
            
        case FILTER_TYPE_LOWPASS:
            eq_biquad_lowpass(coeffs, 
                                     params->fc, 
                                     (float)sample_rate, 
                                     params->q);
            *bshift = 0;
            break;
            
        case FILTER_TYPE_HIGHPASS:
            eq_biquad_highpass(coeffs, 
                                      params->fc, 
                                      (float)sample_rate, 
                                      params->q);
            *bshift = 0;
            break;
            
        case FILTER_TYPE_BANDPASS:
        case FILTER_TYPE_BANDSTOP:
        case FILTER_TYPE_NOTCH:
        case FILTER_TYPE_CONST_Q:
            // These filter types have been removed to save memory.
            // Fall through to bypass behavior.
            eq_biquad_bypass(coeffs);
            *bshift = 0;
            break;
            
        case FILTER_TYPE_ALLPASS:
            eq_biquad_allpass(coeffs, 
                                     params->fc, 
                                     (float)sample_rate, 
                                     params->q);
            *bshift = 0;
            break;
            
        case FILTER_TYPE_PEAKING:
            *bshift = eq_biquad_peaking(coeffs, 
                                               params->fc, 
                                               (float)sample_rate, 
                                               params->q,
                                               params->gain);
            break;
            
        case FILTER_TYPE_LOWSHELF:
            *bshift = eq_biquad_lowshelf(coeffs, 
                                                 params->fc, 
                                                 (float)sample_rate, 
                                                 params->q,
                                                 params->gain);
            break;
            
        case FILTER_TYPE_HIGHSHELF:
            *bshift = eq_biquad_highshelf(coeffs, 
                                                  params->fc, 
                                                  (float)sample_rate, 
                                                  params->q,
                                                  params->gain);
            break;
            
        default:
            // 默认使用bypass
            eq_biquad_bypass(coeffs);
            *bshift = 0;
            break;
    }

    #if 1 //DEBUG_PRINT_ENABLE == 1
    DPRINTF("--------------------------------\n");
    for(int i = 0; i < 5; i++) {
        if (coeffs[i] < 0) {
            DPRINTF("coeffs[%d]: -0x%08x\n", i, (unsigned int)(-coeffs[i]));
        } else {
            DPRINTF("coeffs[%d]: 0x%08x\n", i, (unsigned int)coeffs[i]);
        }
    }
    DPRINTF("--------------------------------\n");
    #endif
    
    return 0;
}

// 批量计算所有滤波器的系数
int eq_calculate_all_coefficients(eq_mode_data_t *eq_data, 
                                 uint32_t sample_rate) {
    
    if (!eq_data) {
        return -1;
    }
    
    int total_bshift = 0;
    
    // 计算每个滤波器的系数
    for (int i = 0; i < MAX_EQ_BANDS; i++) {
        q2_30 coeffs[5];
        int bshift;
        
        if (eq_calculate_coefficients_from_params(&eq_data->bands[i], 
                                                sample_rate, coeffs, &bshift) == 0) {
            // 存储计算出的系数
            for (int j = 0; j < 5; j++) {
                eq_data->filter.coef[j][i] = coeffs[j];
            }
            total_bshift += bshift;
        }
    }
    
    // 更新总bshift
    eq_data->total_bshift = total_bshift;

    #if DEBUG_PRINT_ENABLE == 1
    DPRINTF("total_bshift: %d\n", total_bshift);
    #endif
    
    return 0;
}
