Commit 712123e4 authored by emerit's avatar emerit
Browse files

remove call option to python

parent 27408bd9
Loading
Loading
Loading
Loading
+23 −67
Original line number Diff line number Diff line
@@ -11,10 +11,6 @@ set(IVAS_TRUNK_ENC_PATH ${IVAS_TRUNK_PATH}/lib_enc)
set(IVAS_TRUNK_COM_PATH ${IVAS_TRUNK_PATH}/lib_com)
set(IVAS_TRUNK_DEBUG_PATH ${IVAS_TRUNK_PATH}/lib_debug)

option(USE_PYTHON "Use SOFA python reader" OFF)
option(USE_MATLAB "Use SOFA matlab reader" ON)

if(USE_MATLAB)
option(USE_MATLAB_ENG "Use matlab engine" OFF) # allows to use sofa file as input to the exe, but on windows requires to register matlab as a com server type in matlab "comserver('register')"" 
find_package(Matlab REQUIRED)
message("Matlab_VERSION = ${Matlab_VERSION}")
@@ -31,42 +27,11 @@ if(USE_MATLAB)
message("Matlab_MEX_LIBRARY = ${Matlab_MEX_LIBRARY}")
message("Matlab_ENGINE_LIBRARY = ${Matlab_ENGINE_LIBRARY}")
message("Matlab_DATAARRAY_LIBRARY = ${Matlab_DATAARRAY_LIBRARY}")
    add_definitions(-DUSE_MATLAB)
include_directories(${Matlab_INCLUDE_DIRS} ${IVAS_TRUNK_UTIL_PATH} ${IVAS_TRUNK_ENC_PATH} ${IVAS_TRUNK_DEC_PATH} ${IVAS_TRUNK_REND_PATH} ${IVAS_TRUNK_COM_PATH} ${IVAS_TRUNK_DEBUG_PATH})
if(USE_MATLAB_ENG)
    string(REPLACE "mex" "eng" Matlab_ENG_LIBRARY ${Matlab_MEX_LIBRARY})
    add_definitions(-DUSE_MATLAB_ENG)
endif()
endif()

if(USE_PYTHON)
    find_package(Python REQUIRED COMPONENTS Development Interpreter NumPy)

    message("Python_EXECUTABLE = ${Python_EXECUTABLE}")
    message("Python_SITELIB = ${Python_SITELIB}")
    message("Python_SITEARCH = ${Python_SITEARCH}")
    message("Python_LIBRARIES = ${Python_LIBRARIES}")
    message("Python_LIBRARY_DIRS = ${Python_LIBRARY_DIRS}")
    message("Python_INCLUDE_DIRS = ${Python_INCLUDE_DIRS}")
    message("Python_NumPy_INCLUDE_DIRS = ${Python_NumPy_INCLUDE_DIRS}")

    string(REPLACE "/numpy/core/include" "" Python_SitePackages ${Python_NumPy_INCLUDE_DIRS})
    string(REPLACE "/site-packages/numpy/core/include" "" Python_User_Folder ${Python_NumPy_INCLUDE_DIRS})
    set(Python_User_Script "${Python_User_Folder}/Scripts")
    message("Python_User_Script = ${Python_User_Script}")
    message("Python_SitePackages = ${Python_SitePackages}")

    if(DEFINED ENV{PYTHONPATH})
        message("PYTHONPATH = $ENV{PYTHONPATH}")
    else()
        set(ENV{PYTHONPATH} "${Python_SitePackages};${IVAS_TRUNK_SCRIPTS_PATH}")
        message("set PYTHONPATH to $ENV{PYTHONPATH}")
    endif()

    add_definitions(-DNPY_NO_DEPRECATED_API)

    include_directories(${Python_INCLUDE_DIRS} ${Python_NumPy_INCLUDE_DIRS} ${IVAS_TRUNK_UTIL_PATH} ${IVAS_TRUNK_ENC_PATH} ${IVAS_TRUNK_DEC_PATH} ${IVAS_TRUNK_REND_PATH} ${IVAS_TRUNK_COM_PATH} ${IVAS_TRUNK_DEBUG_PATH})
endif()

set(SOURCE_FILES_C
    ${PROJECT_SOURCE_DIR}/ivas_crend_binaural_filter_design.c
@@ -107,7 +72,6 @@ add_library(${PROJECT_NAME}_lib STATIC ${SOURCE_FILES_C} ${SOURCE_FILES_H})

add_executable(${PROJECT_NAME} generate_crend_ivas_tables_from_sofa.c)

if(USE_MATLAB)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_lib ${Matlab_MAT_LIBRARY} ${Matlab_MX_LIBRARY})
if(UNIX AND NOT APPLE)
    target_link_libraries(${PROJECT_NAME} libstdc++.so.6 -lm -ldl)
@@ -115,14 +79,6 @@ if(USE_MATLAB)
if(USE_MATLAB_ENG)
    target_link_libraries(${PROJECT_NAME} ${Matlab_ENG_LIBRARY})
endif()
endif()

if(USE_PYTHON)
    target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_lib ${Python_LIBRARIES})
    if(UNIX AND NOT APPLE)
        target_link_libraries(${PROJECT_NAME} -lm -ldl)
    endif()
endif()

if(WIN32)
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
+5 −268
Original line number Diff line number Diff line
@@ -59,17 +59,10 @@
#undef M
#endif

#ifdef USE_MATLAB
#include "mat.h"
#ifdef USE_MATLAB_ENG
#include "engine.h"
#endif
#else
#include <Python.h>
#include "numpy/utils.h"       // Include any other Numpy headers, UFuncs for example.
#include "numpy/numpyconfig.h" // Include any other Numpy headers, UFuncs for example.
#include "numpy/arrayobject.h" // Include any other Numpy headers, UFuncs for example.
#endif

/*------------------------------------------------------------------------------------------*
 * Constants
@@ -232,38 +225,6 @@ char *h_file_path = NULL;
char *rom_file_name = NULL;
uint16_t frame_len_ms = 5;

#ifndef USE_MATLAB
PyObject *sofa_load_func = NULL;
PyObject *sofa_find_dir_func = NULL;

#define PYVERIFY( exp )                                    \
    if ( ( exp ) == 0 )                                    \
    {                                                      \
        fprintf( stderr, "%s[%d]: ", __FILE__, __LINE__ ); \
        PyErr_Print();                                     \
        exit( 1 );                                         \
    }

PyObject *import_name( const char *modname, const char *symbol )
{
    PyObject *u_name, *module;
    u_name = PyUnicode_FromString( modname );
    module = PyImport_Import( u_name );
    Py_DECREF( u_name );

    if ( module == NULL )
        return NULL;

    return PyObject_GetAttrString( module, symbol );
}

int numpy_import_array( int ret )
{
    import_array1( ret );
    return 0;
}

#endif
/*------------------------------------------------------------------------------------------*
 * Standalone Renderer program
 *
@@ -651,120 +612,6 @@ int main( int argc, char *argv[] )
#endif
    tmpstr[0] = '\0';

#ifndef USE_MATLAB

    Py_Initialize();

    PyObject *sysPath = PySys_GetObject( (char *) "path" );
    PyList_Append( sysPath, ( PyUnicode_FromString( current_path ) ) );
    PyList_Append( sysPath, ( PyUnicode_FromString( ivas_path ) ) );

    if ( numpy_import_array( -1 ) == -1 )
    {
        Py_Finalize();
        fprintf( stderr, "Cannot load numpy!\n\n" );
        usage_gen_crend_tables();
        if ( h_file_path )
            free( h_file_path );
        if ( c_file_path )
            free( c_file_path );
        if ( lib_rend_path )
            free( lib_rend_path );
        if ( binary_path )
            free( binary_path );
        if ( binary_common_file_name )
            free( binary_common_file_name );

        return -1;
    }

    sofa_load_func = import_name( "SofaReader", "load" );

    if ( sofa_load_func == NULL )
    {
        Py_Finalize();
        fprintf( stderr, "SofaReader.py!\n\n" );
        usage_gen_crend_tables();
        if ( h_file_path )
            free( h_file_path );
        if ( c_file_path )
            free( c_file_path );
        if ( lib_rend_path )
            free( lib_rend_path );
        if ( binary_path )
            free( binary_path );
        if ( binary_common_file_name )
            free( binary_common_file_name );

        return -1;
    }

    int ret = PyCallable_Check( sofa_load_func );
    if ( ret == 0 )
    {
        Py_DECREF( sofa_load_func );

        Py_Finalize();
        fprintf( stderr, "error reading SofaReader.py!\n\n" );
        usage_gen_crend_tables();
        if ( h_file_path )
            free( h_file_path );
        if ( c_file_path )
            free( c_file_path );
        if ( lib_rend_path )
            free( lib_rend_path );
        if ( binary_path )
            free( binary_path );
        if ( binary_common_file_name )
            free( binary_common_file_name );

        return -1;
    }

    sofa_find_dir_func = import_name( "SofaReader", "find_pos_spheric" );
    if ( sofa_find_dir_func == NULL )
    {
        Py_DECREF( sofa_load_func );

        Py_Finalize();
        fprintf( stderr, "error reading SofaReader.py!\n\n" );
        usage_gen_crend_tables();
        if ( h_file_path )
            free( h_file_path );
        if ( c_file_path )
            free( c_file_path );
        if ( lib_rend_path )
            free( lib_rend_path );
        if ( binary_path )
            free( binary_path );
        if ( binary_common_file_name )
            free( binary_common_file_name );

        return -1;
    }

    ret = PyCallable_Check( sofa_find_dir_func );
    if ( ret == 0 )
    {
        Py_DECREF( sofa_find_dir_func );
        Py_DECREF( sofa_load_func );

        Py_Finalize();
        fprintf( stderr, "SofaReader.py!\n\n" );
        usage_gen_crend_tables();
        if ( h_file_path )
            free( h_file_path );
        if ( c_file_path )
            free( c_file_path );
        if ( lib_rend_path )
            free( lib_rend_path );
        if ( binary_path )
            free( binary_path );
        if ( binary_common_file_name )
            free( binary_common_file_name );
        return -1;
    }
#endif
    int err = 0;
    for ( ; i < argc; i++ )
    {
@@ -891,7 +738,6 @@ int main( int argc, char *argv[] )
    if ( rom_file_name )
        free( rom_file_name );


    if ( binary_path )
        free( binary_path );
    if ( binary_common_file_name )
@@ -899,13 +745,6 @@ int main( int argc, char *argv[] )
    if ( binary_file_path )
        free( binary_file_path );

#ifndef USE_MATLAB
    Py_DECREF( sofa_find_dir_func );
    Py_DECREF( sofa_load_func );

    Py_Finalize();
#endif

    return err;
}

@@ -952,17 +791,6 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
    double a[3] = { 0 };
    double t[3] = { 0 };
    long nearest;
#ifndef USE_MATLAB
    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;
    PyArrayObject *sofa_src_pos_cart = NULL;
    PyArrayObject *dir_t = NULL;
#endif
    /*------------------------------------------------------------------------------------------*
     * Parse command line and initialize renderer
     *------------------------------------------------------------------------------------------*/
@@ -975,7 +803,6 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
    }
    strcpy( sofa_file_path, file_path );

#ifdef USE_MATLAB
    MATFile *mat_fp = matOpen( sofa_file_path, "r" );
    if ( mat_fp == NULL )
    {
@@ -1114,73 +941,6 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
            }
        }
    }
    //    }

#else
    int ret = PyCallable_Check( sofa_load_func );
    if ( ret )
    {
        args = Py_BuildValue( "(s)", sofa_file_path );
        kwargs = NULL;
        py_results = PyObject_Call( sofa_load_func, args, kwargs );
        Py_DECREF( args );

        if ( py_results == NULL )
        {
            fprintf( stderr, "Wrong file with wrong sample rate %s!\n\n", sofa_file_path );
            usage_gen_crend_tables();
            free( sofa_file_path );

            return -1;
        }

        ret = PyTuple_Check( py_results );
        if ( ret )
        {
            Py_ssize_t sizeTuple = PyTuple_Size( py_results );
            if ( sizeTuple == 10 )
            {
                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 )
                {
                    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;
                }
                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 )
                {
                    Py_DECREF( py_results );

                    fprintf( stderr, "Wrong file unsupported format delay metadata %s!\n\n", sofa_file_path );
                    usage_gen_crend_tables();
                    free( sofa_file_path );

                    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 );
            }
        }
    }
#endif

    struct ivas_layout_config lscfg;

@@ -1212,30 +972,13 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
    if ( lscfg.isloudspeaker && ( lscfg.nb_channel > 11 ) )
    {
        long nearest = 0;
        // int nd = 1;
        // int64_t n = 3;
#ifndef USE_MATLAB
        npy_intp dim[1] = { 3 };

        dir_t = (PyArrayObject *) PyArray_SimpleNewFromData( 1, dim, NPY_DOUBLE, t );
        if ( dir_t == NULL )
        {
            fprintf( stderr, "Cannot load numpy!\n\n" );
            usage_gen_crend_tables();
            free( sofa_file_path );

            return -1;
        }
#endif
    }
    /* get index to a position given in t (in spherical) */
    int *index_pos = (int *) malloc( sizeof( int ) * lscfg.nb_channel );
    uint32_t maxDel48kHz = 0;
    if ( index_pos == NULL )
    {
#ifndef USE_MATLAB
        Py_DECREF( py_results );
#endif
        matClose( mat_fp );
        free( sofa_file_path );
        fprintf( stderr, "Cannot alloc sofa_file_path !\n\n" );
        return -1;
@@ -1257,12 +1000,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
                                        // tmp[2] = t[2];
                                        // fprintf( stderr, "Wanted Position: %lf, %lf, %lf\n", t[0], t[1], t[2] );
                                        // fprintf( stderr, "Wanted Position: %lf, %lf, %lf\n", tmp[0], tmp[1], tmp[2] );
#ifdef USE_MATLAB
            nearest = find_pos_spheric( sofa_src_pos_cart_val, sofa_M, t );
#else
            py_results = PyObject_CallFunction( sofa_find_dir_func, "OO", (PyObject *) sofa_src_pos_cart, (PyObject *) dir_t );
            nearest = PyLong_AsLong( py_results );
#endif
            if ( ( nearest >= 0 ) && ( nearest < sofa_M ) )
            {
                a[0] = sofa_src_pos_val[nearest * 3] * 180. / M_PI;
@@ -1279,12 +1017,16 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
                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 )
            {
                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. )
@@ -1536,12 +1278,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, ConfigReader *c
        ivas_hrtf_close( &hrtf_data );
    }

#ifdef USE_MATLAB
    matClose( mat_fp );
#else
    Py_DECREF( args );
    Py_DECREF( py_results );
#endif

    if ( sofa_file_path )
        free( sofa_file_path );