Commit ce311bf4 authored by Devansh Kandpal's avatar Devansh Kandpal
Browse files

Minor code cleanup after porting input_fc_fx calculations

parent f4e31a1e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1096,7 +1096,7 @@ void ivas_reverb_calc_color_levels_fx(
ivas_error ivas_reverb_prepare_cldfb_params(
    const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pInput_params,
    const HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    const int32_t output_Fs,
    const Word32 output_Fs,
    Word32 *pOutput_t60,
    Word32 *pOutput_ene );

+0 −7
Original line number Diff line number Diff line
@@ -2439,17 +2439,10 @@ ivas_error ivas_binaural_reverb_init(

    if ( ( roomAcoustics != NULL ) && roomAcoustics->override )
    {
        /*float t60_temp[CLDFB_NO_CHANNELS_MAX];
        float ene_temp[CLDFB_NO_CHANNELS_MAX];*/

        revTimes = t60;
        revEne = ene;

        /*if ( ( error = ivas_reverb_prepare_cldfb_params( roomAcoustics, hHrtfStatistics, sampling_rate, t60_temp, ene_temp ) ) != IVAS_ERR_OK )
        {
            return error;
        }*/

        if ( ( error = ivas_reverb_prepare_cldfb_params( roomAcoustics, hHrtfStatistics, sampling_rate, t60, ene ) ) != IVAS_ERR_OK )
        {
            return error;
+19 −85
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ typedef struct cldfb_convolver_state
    float filter_states_im[BINAURAL_CONVBANDS][CLDFB_CONVOLVER_NTAPS_MAX];
} cldfb_convolver_state;

static void ivas_reverb_set_energies( const Word32 *avg_pwr_l, const Word32 *avg_pwr_r, const int32_t sampling_rate, Word32 *avg_pwr_l_out, Word32 *avg_pwr_r_out );
static void ivas_reverb_set_energies( const Word32 *avg_pwr_l, const Word32 *avg_pwr_r, const Word32 sampling_rate, Word32 *avg_pwr_l_out, Word32 *avg_pwr_r_out );

/*-----------------------------------------------------------------------------------------*
 * Function ivas_reverb_prepare_cldfb_params()
@@ -72,19 +72,15 @@ static void ivas_reverb_set_energies( const Word32 *avg_pwr_l, const Word32 *avg
 * Prepares reverb parameters for CLDFB-based reverberator
 *-----------------------------------------------------------------------------------------*/


ivas_error ivas_reverb_prepare_cldfb_params(
    const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pInput_params,
    const HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    const int32_t output_Fs,
    const Word32 output_Fs,
    Word32 *pOutput_t60,
    Word32 *pOutput_ene )
{
    int16_t idx;
    Word16 idx;

    #define CLDFB_BAND_WIDTH (400) // TODO: Fixme!
    //const Word16 cldfb_freq_halfstep = MAX_SAMPLING_RATE / ( 4 * CLDFB_NO_CHANNELS_MAX );
    //const Word16 halfstep = 13107200; //Q16
    Word32 output_fc_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 output_t60_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 output_ene_fx[CLDFB_NO_CHANNELS_MAX];
@@ -100,58 +96,14 @@ ivas_error ivas_reverb_prepare_cldfb_params(

    FOR ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
//        output_fc_fx[idx] = (Word32) ( ( idx + 0.5f ) * ( MAX_SAMPLING_RATE / ( 2 * CLDFB_NO_CHANNELS_MAX ) ) ) * ONE_IN_Q16;

//        output_fc_fx[idx] = ( ( idx << 1 ) + 1 ) * cldfb_freq_halfstep * ONE_IN_Q16;

//        output_fc_fx[idx] = mult( add( ( mult(idx, ONE_IN_Q16) << 1 ), ONE_IN_Q16 ), halfstep );//Debug

        output_fc_fx[idx] = L_add( L_shr( cldfb_band_width, 1 ), L_shl(Mult_32_16( cldfb_band_width, idx ), 15 ) );
 //       int a = 1; // for debugging
    }

    // TODO: Replace all these dedicated interpolate functions with just one taking a single input vector and map it to a single output vector. The following function and the function call should be removed from code
    //ivas_reverb_interpolate_acoustic_data_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, pInput_params->pAcoustic_dsr_fx,
                                              //CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx, output_ene_fx );

    /* ---------- Debugging Block Starts-------------------*/
        //write to csv 
    FILE *filewriter = fopen("output.csv", "w");
    if (filewriter == NULL)
    {
        perror("Failed to open file");
    }
    fprintf( filewriter, "T60(old function),DSR (old fucntion)\n" );

    FOR( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        //Print t60 and DSR
         //fprintf( filewriter, "%f,%f\n", (float) output_t60_fx[idx] / ( 1 << 26 ), (float) output_ene_fx[idx] / ( 1 << 30 ) );
    }
    
     //fprintf( filewriter, "\n" );

     /* --------- Debugging Block Ends--------------------*/

    //T60 - Q26
    ivas_reverb_interp_on_freq_grid_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx );
    //DSR - Q30
    ivas_reverb_interp_on_freq_grid_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_dsr_fx, CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_ene_fx );

        /* ---------- Debugging Block Starts-------------------*/
    // write to csv 
    FOR( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        // Print T60 and DSR
        // fprintf( filewriter, "%f,%f\n", (float) output_t60_fx[idx] / ( 1 << 26 ), (float) output_ene_fx[idx] / ( 1 << 30 ) );
        // Print input and output center frequency
        fprintf( filewriter, "%f,%f\n", (float) pInput_params->pFc_input_fx[idx] / ( 1 << 16 ), (float) output_fc_fx[idx] / ( 1 << 16 ) );
    }
    fprintf( filewriter, "\n" );
    //Close file
    fclose( filewriter );
    /* --------- Debugging Block Ends--------------------*/

    /* adjust DSR for the delay difference */
    delay_diff_fx = L_sub( pInput_params->inputPreDelay_fx, pInput_params->acousticPreDelay_fx ); //Q27

@@ -161,7 +113,7 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    {
        L_tmp = Mpy_32_32( output_t60_fx[idx], ln_1e6_inverted_fx );// L_tmp in Q26

        exp_argument_fx = BASOP_Util_Divide3232_Scale_newton( delay_diff_fx, L_tmp, &tmp_exp );//exp_argument_L_fx in Q30 (not alwys, because tmp_exp values are not constant) //Scale() returns values in the highest possible precision
        exp_argument_fx = BASOP_Util_Divide3232_Scale_newton( delay_diff_fx, L_tmp, &tmp_exp );//exp_argument_L_fx in Q30 (not alwys, because tmp_exp values are not constant)
        exp_argument_fx = L_shr_sat( exp_argument_fx, sub(6,tmp_exp));//exp_argument_L_fx in Q26

        /* Limit exponent to approx +/-100 dB in case of incoherent value of delay_diff, to prevent overflow */
@@ -186,10 +138,8 @@ ivas_error ivas_reverb_prepare_cldfb_params(

        //Assign t60 to output pointer
        pOutput_t60[idx] = output_t60_fx[idx]; //Q26

    }

    //This function can be condensed and merged within prepare_cldfb_params(). It is only called once, at this location. 
    ivas_reverb_set_energies( hHrtfStatistics->average_energy_l, hHrtfStatistics->average_energy_r, output_Fs, avg_pwr_left_fx, avg_pwr_right_fx ); //This function returns avg powers for left and right in Q28 -> why?? how??

    for ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
@@ -217,56 +167,41 @@ ivas_error ivas_reverb_prepare_cldfb_params(
static void ivas_reverb_set_energies(
    const Word32 *avg_pwr_l,
    const Word32 *avg_pwr_r,
    const int32_t sampling_rate,
    const Word32 sampling_rate,
    Word32 *avg_pwr_left,
    Word32 *avg_pwr_right )
{
    int16_t freq_idx;
    float input_fc[FFT_SPECTRUM_SIZE];
    Word16 freq_idx;
#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
    Word32 input_fc_fx[FFT_SPECTRUM_SIZE];
    Word32 output_fc_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 avg_pwr_left_fx[CLDFB_NO_CHANNELS_MAX];  // Q28
    Word32 avg_pwr_right_fx[CLDFB_NO_CHANNELS_MAX]; // Q28
    const Word16 cldfb_freq_halfstep = MAX_SAMPLING_RATE / ( 4 * CLDFB_NO_CHANNELS_MAX );
    const Word32 cldfb_band_width = 26214400;       // 400 in Q16
    Word16 s;
    Word16 temp;
#endif

    float input_fc[FFT_SPECTRUM_SIZE];

    const int16_t avg_pwr_len = sampling_rate == 16000 ? LR_IAC_LENGTH_NR_FC_16KHZ : LR_IAC_LENGTH_NR_FC;
    const Word16 avg_pwr_len = sampling_rate == 16000 ? LR_IAC_LENGTH_NR_FC_16KHZ : LR_IAC_LENGTH_NR_FC;
    temp = BASOP_Util_Divide3216_Scale( sampling_rate, sub( avg_pwr_len, 1 ), &s );

    for ( freq_idx = 0; freq_idx < avg_pwr_len; freq_idx++ )
    FOR( freq_idx = 0; freq_idx < CLDFB_NO_CHANNELS_MAX; freq_idx++ )
    {
        input_fc[freq_idx] = freq_idx * ( 0.5f * sampling_rate / (float) ( avg_pwr_len - 1 ) );
#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
        input_fc_fx[freq_idx] = (Word32) input_fc[freq_idx] * ONE_IN_Q16;
#endif
        input_fc_fx[freq_idx] = L_shl( L_mult( temp, freq_idx ), add( 15, s ) );

       /* input_fc[freq_idx] = freq_idx * ( 0.5f * sampling_rate / (float) ( avg_pwr_len - 1 ) );
        input_fc_fx[freq_idx] = input_fc[freq_idx] * ONE_IN_Q16;*/
    }

#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
    for ( freq_idx = 0; freq_idx < CLDFB_NO_CHANNELS_MAX; freq_idx++ )
    {
        output_fc_fx[freq_idx] = ( ( freq_idx << 1 ) + 1 ) * cldfb_freq_halfstep * ONE_IN_Q16;
        output_fc_fx[freq_idx] = L_add( L_shr( cldfb_band_width, 1 ), L_shl( Mult_32_16( cldfb_band_width, freq_idx ), 15 ) );
    }
#endif

            /* ---------- Debugging Block Starts-------------------*/
    FILE *filewriter = fopen( "output2.csv", "w" );
    if ( filewriter == NULL )
    {
        perror( "Failed to open file" );
    }
    // write to csv
    FOR( freq_idx = 0; freq_idx < CLDFB_NO_CHANNELS_MAX; freq_idx++ )
    {
        // Print T60 and DSR
        // fprintf( filewriter, "%f,%f\n", (float) output_t60_fx[idx] / ( 1 << 26 ), (float) output_ene_fx[idx] / ( 1 << 30 ) );
        // Print input and output center frequency
        fprintf( filewriter, "%f,%f\n", (float) input_fc_fx[freq_idx] / ( 1 << 16 ), (float) output_fc_fx[freq_idx] / ( 1 << 16 ) );
    }
    // Close file
     fclose( filewriter );
    /* --------- Debugging Block Ends--------------------*/

#ifndef FIX_1741_REVERB_TIMES_Q_FORMAT

    Word32 *input_fc_fx = (Word32 *) malloc( 60 * sizeof( Word32 ) );
@@ -291,8 +226,7 @@ static void ivas_reverb_set_energies(
#endif

#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
    //ivas_reverb_interpolate_energies_fx( avg_pwr_len, input_fc_fx, avg_pwr_l, avg_pwr_r,
      //                                   CLDFB_NO_CHANNELS_MAX, output_fc_fx, avg_pwr_left_fx, avg_pwr_right_fx ); //Retains Q-Format

    //Avg Energy Left
    ivas_reverb_interp_on_freq_grid_fx( avg_pwr_len, input_fc_fx, avg_pwr_l, CLDFB_NO_CHANNELS_MAX, output_fc_fx, avg_pwr_left_fx );
    //Avg Energy Right