Commit c9f20fe6 authored by emerit's avatar emerit
Browse files

fix matlab + C only scripts

parent f3e289ce
Loading
Loading
Loading
Loading
Loading
+132 B

File added.

No diff preview for this file type.

+174 −101
Original line number Diff line number Diff line
@@ -220,9 +220,9 @@ void usage_gen_crend_tables( void )
    fprintf( stdout, "-brir_optim_config_path config_file_path  : if present brir optimisation process uses parameters stored in config_file_path \n\t\t\t\t\t\tConfiguration example files are provided in folder :\n\t\t\t\t\t\t-brir_no_optim.cfg : for exact convolution of the BRIR by IVAS decoder or renderer\n\t\t\t\t\t\t-brir_default_optim.cfg : default values BRIR optimisation used by IVAS decoder or renderer\n\t\t\t\t\t\t-brir_low_complexity_optim.cfg : values for BRIR optimisation reducing IVAS decoder or renderer complexity\n" );
    fprintf( stdout, "example:\n" );
#ifdef _WIN32
    fprintf( stdout, "%s\n", ".\\generate_crend_ivas_tables.exe -lib_rend_path ../../lib_rend 5 ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_FOA.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA2.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA3.sofa ./BRIRs_sofa/IIS_BRIR_officialMPEG_Combined.sofa\n" );
    fprintf( stdout, "%s\n", ".\\generate_crend_ivas_tables.exe -lib_rend_path ../../lib_rend 5 ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_FOA.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA2.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA3.mat ./BRIRs_sofa/IIS_BRIR_officialMPEG_Combined.mat\n" );
#else
    fprintf( stdout, "%s\n", "./generate_crend_ivas_tables -lib_rend_path ../../lib_rend 5 ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_FOA.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HAO2.sofa ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA3.sofa ./BRIRs_sofa/IIS_BRIR_officialMPEG_Combined.sofa\n" );
    fprintf( stdout, "%s\n", "./generate_crend_ivas_tables -lib_rend_path ../../lib_rend 5 ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_FOA.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HAO2.mat ./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_HOA3.mat ./BRIRs_sofa/IIS_BRIR_officialMPEG_Combined.mat\n" );
#endif
    fprintf( stdout, "\n" );
}
@@ -822,6 +822,19 @@ int main( int argc, char *argv[] )
        free( sofa_file_path_for_reverb );
#endif

    return err;
}

int32_t find_pos_spheric( const double *Source_Position_Cartesian, int32_t numPos, const double dir_spheric[3] )
{
    double valmax = 0;
    int32_t indmax = -1;
    int32_t ind;
    double dir_cart[3];
    double tmp;
    dir_cart[0] = ( dir_spheric[2] * cos( dir_spheric[0] * M_PI / 180. ) * cos( dir_spheric[1] * M_PI / 180. ) );
    dir_cart[1] = ( dir_spheric[2] * sin( dir_spheric[0] * M_PI / 180. ) * cos( dir_spheric[1] * M_PI / 180. ) );
    dir_cart[2] = dir_spheric[2] * sin( dir_spheric[1] * M_PI / 180. );

    for ( ind = 0; ind < numPos; ind++ )
    {
@@ -855,10 +868,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
    double a[3] = { 0 };
    double t[3] = { 0 };
    long nearest;
    /*------------------------------------------------------------------------------------------*
     * Parse command line and initialize renderer
     *------------------------------------------------------------------------------------------*/
    /* Optional arguments */

    sofa_file_path = (char *) malloc( sizeof( char ) * ( strlen( file_path ) + 1 ) );
    if ( sofa_file_path == NULL )
    {
@@ -886,6 +896,8 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
        return -1;
    }

    matClose( mat_fp );

    mwSize total_num_of_elements;
    mwIndex index = 0;
    int number_of_fields, field_index;
@@ -939,9 +951,9 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
                                       field_index );
            if ( data == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                matClose( mat_fp );
                return -1;
            }

@@ -985,9 +997,9 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
                                                                 field_index ) );
            if ( sofa_src_pos_cart_val == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                matClose( mat_fp );
                return -1;
            }
        }
@@ -998,9 +1010,9 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
                                                            field_index ) );
            if ( sofa_src_pos_val == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                matClose( mat_fp );
                return -1;
            }
        }
@@ -1042,7 +1054,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
    uint32_t maxDel48kHz = 0;
    if ( index_pos == NULL )
    {
        matClose( mat_fp );
        mxDestroyArray( sofa );
        free( sofa_file_path );
        fprintf( stderr, "Cannot alloc sofa_file_path !\n\n" );
        return -1;
@@ -1078,19 +1090,17 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c

            if ( fabs( t[0] - a[0] ) > MAX_DIFF_AZI )
            {
                mxDestroyArray( sofa );
                fprintf( stderr, "Nearest position to far from target incomplete sofa file\n" );
                free( sofa_file_path );
                free( index_pos );
                matClose( mat_fp );

                return -1;
            }
            if ( fabs( t[1] - a[1] ) > MAX_DIFF_ELE )
            {
                mxDestroyArray( sofa );
                fprintf( stderr, "Nearest position to far from target incomplete sofa file\n" );
                free( sofa_file_path );
                matClose( mat_fp );

                return -1;
            }
            if ( a[0] > 180. )
@@ -1342,7 +1352,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
        ivas_hrtf_close( &hrtf_data );
    }

    matClose( mat_fp );
    mxDestroyArray( sofa );

    if ( sofa_file_path )
        free( sofa_file_path );
@@ -1595,22 +1605,10 @@ int generate_reverb_ivas_tables_from_sofa( const char *file_path )
    double *sofa_src_pos_cart_val = NULL;
    float mdft_scale_fact = 1.f;

    PyObject *args = NULL;
    PyObject *kwargs = NULL;

    PyObject *py_results = NULL;
    PyArrayObject *sofa_data_IR = NULL;
    PyArrayObject *sofa_data_IR_48k = NULL;
    PyArrayObject *sofa_delay = NULL;
    long sofa_sample_rate = 0;
    PyArrayObject *sofa_src_pos_cart = NULL;
    PyArrayObject *dir_t = NULL;
    double a[3] = { 0 };
    double t[3] = { 0 };
    /*------------------------------------------------------------------------------------------*
     * Parse command line and initialize renderer
     *------------------------------------------------------------------------------------------*/
    /* Optional arguments */

    sofa_file_path = (char *) malloc( sizeof( char ) * ( strlen( file_path ) + 1 ) );
    if ( sofa_file_path == NULL )
    {
@@ -1619,71 +1617,149 @@ int generate_reverb_ivas_tables_from_sofa( const char *file_path )
    }
    strcpy( sofa_file_path, file_path );

    int ret = PyCallable_Check( sofa_load_func );
    if ( ret )
    MATFile *mat_fp = matOpen( sofa_file_path, "r" );
    if ( mat_fp == NULL )
    {
        args = Py_BuildValue( "(s)", sofa_file_path );
        kwargs = NULL;
        py_results = PyObject_Call( sofa_load_func, args, kwargs );
        Py_DECREF( args );
        printf( "Error reading existing matrix LocalDouble\n" );
        usage_gen_crend_tables();
        free( sofa_file_path );
        return -1;
    }

        if ( py_results == NULL )
    mxArray *sofa = matGetVariable( mat_fp, "Sofa" );
    if ( sofa == NULL )
    {
            fprintf( stderr, "Wrong file with wrong sample rate %s!\n\n", sofa_file_path );
        printf( "Error reading existing matrix LocalDouble\n" );
        usage_gen_crend_tables();
        free( sofa_file_path );

        matClose( mat_fp );
        return -1;
    }

        ret = PyTuple_Check( py_results );
        if ( ret )
    matClose( mat_fp );

    mwSize total_num_of_elements;
    mwIndex index = 0;
    int number_of_fields, field_index;
    const char *field_name;
    const mxArray *data;
    const double *sampleRate;

    //    total_num_of_elements = mxGetNumberOfElements( sofa );
    number_of_fields = mxGetNumberOfFields( sofa );

    /* For the given index, walk through each field. */
    for ( field_index = 0; field_index < number_of_fields; field_index++ )
    {
            Py_ssize_t sizeTuple = PyTuple_Size( py_results );
            if ( sizeTuple == 10 )
        //       display_subscript( structure_array_ptr, index );
        field_name = mxGetFieldNameByNumber( sofa,
                                             field_index );
        if ( strcmp( field_name, "DatabaseName" ) == 0 )
        {
                npy_intp *dims = NULL;
                npy_intp *strides = NULL;
                sofaDataBaseName = PyUnicode_AsUTF8( PyTuple_GetItem( py_results, 0 ) );
                sofaListenerName = PyUnicode_AsUTF8( PyTuple_GetItem( py_results, 1 ) );
                sofa_sample_rate = PyLong_AsLong( PyTuple_GetItem( py_results, 2 ) );
                if ( sofa_sample_rate != DEFAULT_SAMPLERATE )
            sofaDataBaseName = mxArrayToUTF8String( mxGetFieldByNumber( sofa,
                                                                        index,
                                                                        field_index ) );
            if ( sofaDataBaseName == NULL )
            {
                    fprintf( stderr, "Warning file %s with sample rate %ld resampled to 48 kHz!\n\n", sofa_file_path, sofa_sample_rate );
                    sofa_sample_rate = 48000;
                printf( "\tEmpty Field for %s\n", field_name );
            }
        }
                sofa_latencys = (double *) PyArray_DATA( PyTuple_GetItem( py_results, 3 ) );
                sofa_delay = (PyArrayObject *) PyTuple_GetItem( py_results, 4 );
                sofa_delay_val = (double *) PyArray_DATA( sofa_delay );
                sofa_data_IR_48k = (PyArrayObject *) PyTuple_GetItem( py_results, 5 );
                sofa_data_IR_val_48k = (double *) PyArray_DATA( sofa_data_IR_48k );
                dims = PyArray_DIMS( sofa_data_IR_48k );
                strides = PyArray_STRIDES( sofa_data_IR_48k );
                sofa_M = (long) dims[0];
                sofa_R = (long) dims[1];
                sofa_N = (long) dims[2];
                sofa_N_48k = sofa_N;
                dims = PyArray_DIMS( sofa_delay );
                if ( dims[0] * dims[1] == sofa_M * sofa_R )
        if ( strcmp( field_name, "ListenerShortName" ) == 0 )
        {
                    Py_DECREF( py_results );

                    fprintf( stderr, "Wrong file unsupported format delay metadata %s!\n\n", sofa_file_path );
                    usage_gen_crend_tables();
            sofaListenerName = mxArrayToUTF8String( mxGetFieldByNumber( sofa,
                                                                        index,
                                                                        field_index ) );
            if ( sofaListenerName == NULL )
            {
                printf( "\tEmpty Field for %s\n", field_name );
            }
        }
        if ( strcmp( field_name, "latencys" ) == 0 )
        {
            sofa_latencys = mxGetPr( mxGetFieldByNumber( sofa,
                                                         index,
                                                         field_index ) );
            if ( sofa_latencys == NULL )
            {
                printf( "\tEmpty Field for %s\n", field_name );
            }
        }
        if ( strcmp( field_name, "DataResampled" ) == 0 )
        {
            data = mxGetFieldByNumber( sofa,
                                       index,
                                       field_index );
            if ( data == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                return -1;
            }

            total_num_of_elements = mxGetNumberOfElements( data );

            for ( indSR = 0; indSR < total_num_of_elements; indSR++ )
            {
                sampleRate = mxGetPr( mxGetFieldByNumber( data,
                                                          indSR,
                                                          1 ) );
                if ( *sampleRate == 48000 )
                {
                    const mwSize *dims = mxGetDimensions( mxGetFieldByNumber( data,
                                                                              indSR,
                                                                              0 ) );
                    sofa_N = sofa_N_48k = (int32_t) dims[0];
                    sofa_R = (int32_t) dims[1];
                    sofa_M = (int32_t) dims[2];
                    sofa_data_IR_val_48k = mxGetPr( mxGetFieldByNumber( data,
                                                                        indSR,
                                                                        0 ) );
                }
                if ( *sampleRate == 32000 )
                {
                    sofa_data_IR_val_32k = mxGetPr( mxGetFieldByNumber( data,
                                                                        indSR,
                                                                        0 ) );
                }
                if ( *sampleRate == 16000 )
                {
                    sofa_data_IR_val_16k = mxGetPr( mxGetFieldByNumber( data,
                                                                        indSR,
                                                                        0 ) );
                }
            }
        }
        if ( strcmp( field_name, "PosCartesian" ) == 0 )
        {
            sofa_src_pos_cart_val = mxGetPr( mxGetFieldByNumber( sofa,
                                                                 index,
                                                                 field_index ) );
            if ( sofa_src_pos_cart_val == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                matClose( mat_fp );
                return -1;
            }
                sofa_data_IR_val_32k = (double *) PyArray_DATA( PyTuple_GetItem( py_results, 6 ) );
                sofa_data_IR_val_16k = (double *) PyArray_DATA( PyTuple_GetItem( py_results, 7 ) );
                sofa_src_pos_val = (double *) PyArray_DATA( PyTuple_GetItem( py_results, 8 ) );
                sofa_src_pos_cart = (PyArrayObject *) ( PyTuple_GetItem( py_results, 9 ) );
                sofa_src_pos_cart_val = (double *) PyArray_DATA( sofa_src_pos_cart );
        }
        if ( strcmp( field_name, "PosSpherical" ) == 0 )
        {
            sofa_src_pos_val = mxGetPr( mxGetFieldByNumber( sofa,
                                                            index,
                                                            field_index ) );
            if ( sofa_src_pos_val == NULL )
            {
                mxDestroyArray( sofa );
                printf( "\tEmpty Field for %s\n", field_name );
                free( sofa_file_path );
                matClose( mat_fp );
                return -1;
            }
        }
    }


    /* general information about the loaded HRTF set */
    fprintf( stderr, "Number of HRTFs: %d\n", sofa_M );
    fprintf( stderr, "Filter length: %d samples\n", sofa_N );
@@ -1775,8 +1851,7 @@ int generate_reverb_ivas_tables_from_sofa( const char *file_path )

        if ( frame_len < sofa_N )
        {
            Py_DECREF( py_results );

            mxDestroyArray( sofa );
            fprintf( stderr, "Wrong file unsupported format hrir size too long > 240 samples at 48 kHz %s!\n\n", sofa_file_path );
            free( sofa_file_path );
            return -1;
@@ -1914,12 +1989,10 @@ int generate_reverb_ivas_tables_from_sofa( const char *file_path )

    /* close the file */

    Py_DECREF( args );
    Py_DECREF( py_results );

    if ( sofa_file_path )
        free( sofa_file_path );

    mxDestroyArray( sofa );
    return 0;
}
#endif
@@ -2035,7 +2108,7 @@ void write_array_uint16_to_file( FILE *fp, uint16_t *vec, int32_t size_vec, int3
void update_c_file_with_reverb( float *pEner_l, float *pEner_r, float *pCoherence, const int32_t samplerate, const int16_t len )
{
#ifdef NONBE_FIX_AVG_IAC_CLDFB_REVERB
    char len_str[25] = "LR_IAC_LENGTH_NR_FC";
    char len_str[26] = "LR_IAC_LENGTH_NR_FC";
    if ( samplerate == 16000 )
    {
        strcpy( len_str, "LR_IAC_LENGTH_NR_FC_16KHZ" );
@@ -2314,7 +2387,7 @@ void update_c_file( HRTFS_DATA *hrtf, struct ivas_layout_config lscfg, const int
void update_h_file_with_reverb( int32_t samplerate )
{
#ifdef NONBE_FIX_AVG_IAC_CLDFB_REVERB
    char len_str[25] = "LR_IAC_LENGTH_NR_FC";
    char len_str[26] = "LR_IAC_LENGTH_NR_FC";
    if ( samplerate == 16000 )
    {
        strcpy( len_str, "LR_IAC_LENGTH_NR_FC_16KHZ" );
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ end

command = [command ...
                    ' -compute_reverb_rom ' ...
                    hrir_file ' ' ...
                    erase(hrir_file,'.sofa') '.mat ' ...
                    ' 5 ' ...
                    erase(hrir_file,'.sofa') '.mat ' ...
                    erase(hrir_file,'.sofa') '_FOA.mat ' ...