Commit 9f9d024f authored by emerit's avatar emerit
Browse files

add support of sofa files at sample rate different from 48kHz and/or source...

add support of sofa files at sample rate different from 48kHz and/or source position unit in radians
parent 23d9b857
Loading
Loading
Loading
Loading
+49 −16
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ def load(
) -> Tuple[
    str,
    str,
    np.ndarray,
    int,
    np.ndarray,
    np.ndarray,
    np.ndarray,
@@ -25,51 +25,84 @@ def load(
    if "latencys" in hrtf.variables.keys():
        latencys = np.array(hrtf.variables["latencys"])
    Sampling_Rate = np.array(hrtf.variables["Data.SamplingRate"])
    if Sampling_Rate.size > 1:
        return (
            "",
            "",
            0,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    Sampling_Rate = int(Sampling_Rate[0])
    ind_dir = 0
    Source_Position = np.array(hrtf.variables["SourcePosition"])
    unitToRad = [1.0, 1.0]
    Units = hrtf.variables["SourcePosition"].Units
    if "degree" in Units:
        unitToRad[0] = np.pi / 180
        Units = Units.replace("degree,", "", 1)
        if "degree" in Units:
            unitToRad[1] = np.pi / 180
            Units = Units.replace("degree,", "", 1)
    Source_Position_Cartesian = np.copy(Source_Position)
    for ind_dir_ptr in Source_Position:
        Source_Position_Cartesian[ind_dir][0] = (
            ind_dir_ptr[2]
            * np.cos(ind_dir_ptr[0] * np.pi / 180)
            * np.cos(ind_dir_ptr[1] * np.pi / 180)
            * np.cos(ind_dir_ptr[0] * unitToRad[0])
            * np.cos(ind_dir_ptr[1] * unitToRad[1])
        )
        Source_Position_Cartesian[ind_dir][1] = (
            ind_dir_ptr[2]
            * np.sin(ind_dir_ptr[0] * np.pi / 180)
            * np.cos(ind_dir_ptr[1] * np.pi / 180)
            * np.sin(ind_dir_ptr[0] * unitToRad[0])
            * np.cos(ind_dir_ptr[1] * unitToRad[1])
        )
        Source_Position_Cartesian[ind_dir][2] = ind_dir_ptr[2] * np.sin(
            ind_dir_ptr[1] * np.pi / 180
            ind_dir_ptr[1] * unitToRad[1]
        )
        ind_dir = ind_dir + 1
    Data_Delay = np.array(hrtf.variables["Data.Delay"])
    Data_IR_48khz = np.array(hrtf.variables["Data.IR"])
    Data_IR = np.array(hrtf.variables["Data.IR"])
    Data_IR_48khz = np.zeros(
        (
            Data_IR.shape[0],
            Data_IR.shape[1],
            int(np.ceil(Data_IR.shape[2] * 48000 / Sampling_Rate)),
        )
    )
    Data_IR_32khz = np.zeros(
        (
            Data_IR_48khz.shape[0],
            Data_IR_48khz.shape[1],
            int(np.ceil(Data_IR_48khz.shape[2] * 32 / 48)),
            Data_IR.shape[0],
            Data_IR.shape[1],
            int(np.ceil(Data_IR.shape[2] * 32000 / Sampling_Rate)),
        )
    )
    Data_IR_16khz = np.zeros(
        (
            Data_IR_48khz.shape[0],
            Data_IR_48khz.shape[1],
            int(np.ceil(Data_IR_48khz.shape[2] * 16 / 48)),
            Data_IR.shape[0],
            Data_IR.shape[1],
            int(np.ceil(Data_IR.shape[2] * 16000 / Sampling_Rate)),
        )
    )
    ind_dir = 0
    for ind_dir_ptr in Data_IR_48khz:
    for ind_dir_ptr in Data_IR:
        ind_ear = 0
        for ind_ear_ptr in ind_dir_ptr:
            tmp = np.copy(ind_ear_ptr)
            Data_IR_48khz[ind_dir][ind_ear][:] = pyaudio3dtools.audioarray.resample(
                tmp, Sampling_Rate, 48000
            )
            tmp = np.copy(ind_ear_ptr)
            Data_IR_32khz[ind_dir][ind_ear][:] = pyaudio3dtools.audioarray.resample(
                tmp, 48000, 32000
                tmp, Sampling_Rate, 32000
            )
            tmp = np.copy(ind_ear_ptr)
            Data_IR_16khz[ind_dir][ind_ear][:] = pyaudio3dtools.audioarray.resample(
                tmp, 48000, 16000
                tmp, Sampling_Rate, 16000
            )
            ind_ear = ind_ear + 1
        ind_dir = ind_dir + 1
+13 −43
Original line number Diff line number Diff line
@@ -766,7 +766,6 @@ int main( int argc, char *argv[] )

    getcwd( current_path, MAX_PATH );
    strcpy( ivas_path, current_path );
    strcpy( ivas_path, current_path );
#ifdef _WIN32
    char *tmpstr = strstr( ivas_path, "\\binauralRenderer_interface" );
#else
@@ -961,15 +960,14 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, bool no_optim )
{
    char *sofa_file_path = NULL;
    int i, indSR;
    unsigned ui;
    int32_t sofa_M, sofa_N, sofa_R, sofa_N_48k;
    const char *sofaDataBaseName = "";
    const char *sofaListenerName = "";
    double *data_IR_current = NULL;
    double *sofa_data_IR_val = NULL;
    double *sofa_data_IR_val_48k = NULL;
    double *sofa_data_IR_val_32k = NULL;
    double *sofa_data_IR_val_16k = NULL;
    double *sofa_sample_rates_val = NULL;
    double *sofa_latencys = NULL;
    double *sofa_delay_val = NULL;
    double *sofa_src_pos_val = NULL;
@@ -980,8 +978,9 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, bool no_optim )

    PyObject *py_results = NULL;
    PyArrayObject *sofa_data_IR = NULL;
    PyArrayObject *sofa_data_IR_48k = NULL;
    PyArrayObject *sofa_delay = NULL;
    PyArrayObject *sofa_sample_rates = NULL;
    long sofa_sample_rate = 0;
    PyArrayObject *sofa_src_pos_cart = NULL;
    PyArrayObject *dir_t = NULL;
    double a[3] = { 0 };
@@ -1036,48 +1035,19 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, bool no_optim )
                npy_intp *strides = NULL;
                sofaDataBaseName = PyUnicode_AsUTF8( PyTuple_GetItem( py_results, 0 ) );
                sofaListenerName = PyUnicode_AsUTF8( PyTuple_GetItem( py_results, 1 ) );
                sofa_sample_rates = (PyArrayObject *) PyTuple_GetItem( py_results, 2 );
                dims = PyArray_DIMS( sofa_sample_rates );
                sofa_sample_rates_val = (double *) PyArray_DATA( sofa_sample_rates );
                if ( dims[0] > 0 )
                {
                    int hasWantedSampleRate = 0;
                    for ( ui = 0; ui < dims[0]; ui++ )
                    {
                        if ( sofa_sample_rates_val[ui] == DEFAULT_SAMPLERATE )
                        {
                            hasWantedSampleRate = 1;
                            break;
                        }
                    }
                    if ( hasWantedSampleRate == 0 )
                    {
                        Py_DECREF( py_results );

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

                        return -1;
                    }
                }
                else
                sofa_sample_rate = PyLong_AsLong( PyTuple_GetItem( py_results, 2 ) );
                if ( sofa_sample_rate != DEFAULT_SAMPLERATE )
                {
                    Py_DECREF( py_results );

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

                    return -1;
                    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 = (PyArrayObject *) PyTuple_GetItem( py_results, 5 );
                sofa_data_IR_val = (double *) PyArray_DATA( sofa_data_IR );
                dims = PyArray_DIMS( sofa_data_IR );
                strides = PyArray_STRIDES( sofa_data_IR );
                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];
@@ -1376,7 +1346,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, bool no_optim )
        if ( frame_len_ms == 5 )
            frame_len = frame_len >> 2;

        data_IR_current = sofa_data_IR_val;
        data_IR_current = sofa_data_IR_val_48k;
        sofa_N = sofa_N_48k;


@@ -1443,7 +1413,7 @@ int generate_crend_ivas_tables_from_sofa( const char *file_path, bool no_optim )
                        sofa_N = (int32_t) ceil( 16000.f * sofa_N_48k / 48000.f );
                        break;
                    default:
                        data_IR_current = sofa_data_IR_val;
                        data_IR_current = sofa_data_IR_val_48k;
                        sofa_N = sofa_N_48k;
                        break;
                }
+9 −1
Original line number Diff line number Diff line
import os
import sys

localPath = os.getcwd()
oseperator = os.sep
localPath = localPath.replace(os.sep + "binauralRenderer_interface", "")

sys.path.append(localPath)
import SofaReader as sr
import numpy as np
import matplotlib.pyplot as plt


filename = "./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000.sofa"
filename = "./HRIRs_sofa/HRIR_128_Meth5_IRC_53_Q10_symL_Itrp1_48000_FOA.sofa"
# filename = "./HRIRs_sofa/HRIR_128_48000_dolby_SBA3.sofa"

[