Commit bbda05b2 authored by Adam Mills's avatar Adam Mills
Browse files

Adding matlab scripts to convert SD HRIRs to SHD HRIRs

parent 93750309
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line

### 2023-07-07
Entry point to convert SD HRTFs to SHD HRTFs is the convert_SD2SHD_HRIRs.m script.

Python 3.9.x needs to be installed with the sofar python module. The convert_SD2SHD_HRIRs.m script needs the path to this python.
+60 −0
Original line number Diff line number Diff line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%    (C) 2022-2023 Baseline Development Group with portions copyright Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation. All Rights Reserved.
% 
%    This software is protected by copyright law and by international treaties.
%    The Baseline Development Group consisting of Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., and VoiceAge Corporation retain full ownership
%    rights in their respective contributions in the software. No license of any kind, including but not
%    limited to patent license, of any foregoing parties is hereby granted by implication, estoppel or
%    otherwise.
% 
%    This software is provided "AS IS", without any express or implied warranties. The software is in the
%    development stage. It is intended exclusively for experts who have experience with such software and
%    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
%    and/or fitness for a particular purpose are hereby disclaimed and excluded.
% 
%    Any dispute, controversy or claim arising under or in relation to providing this software shall be
%    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
%    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
%    the United Nations Convention on Contracts on the International Sales of Goods.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function convert_SD2SHD_HRIRs(python_path, sofa_path, sofa_file, IR_size)

data_struct = struct.empty(3,0);

sr           = [48000, 32000, 16000];
sr_short     = [48, 32, 16];
sr_dft_size  = [240, 160, 80];

% FOA
data_struct(1).IR_data = generate_HOA_HRIRs_MOD_lens(1, python_path, sofa_path, sofa_file, IR_size);
data_struct(1).HOA_name     = 'FOA';
data_struct(1).n_HOA_ch     = 4;
data_struct(1).sr           = sr;
data_struct(1).sr_short     = sr_short;
data_struct(1).sr_dft_size  = sr_dft_size;

% HOA2
data_struct(2).IR_data = generate_HOA_HRIRs_MOD_lens(2, python_path, sofa_path, sofa_file, IR_size);
data_struct(2).HOA_name     = 'HOA2';
data_struct(2).n_HOA_ch     = 9;
data_struct(2).sr           = sr;
data_struct(2).sr_short     = sr_short;
data_struct(2).sr_dft_size  = sr_dft_size;

% HOA3
data_struct(3).IR_data = generate_HOA_HRIRs_MOD_lens(3, python_path, sofa_path, sofa_file, IR_size);
data_struct(3).HOA_name     = 'HOA3';
data_struct(3).n_HOA_ch     = 16;
data_struct(3).sr           = sr;
data_struct(3).sr_short     = sr_short;
data_struct(3).sr_dft_size  = sr_dft_size;

generate_rom_tables(data_struct)
+131 −0
Original line number Diff line number Diff line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%    (C) 2022-2023 Baseline Development Group with portions copyright Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation. All Rights Reserved.
% 
%    This software is protected by copyright law and by international treaties.
%    The Baseline Development Group consisting of Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., and VoiceAge Corporation retain full ownership
%    rights in their respective contributions in the software. No license of any kind, including but not
%    limited to patent license, of any foregoing parties is hereby granted by implication, estoppel or
%    otherwise.
% 
%    This software is provided "AS IS", without any express or implied warranties. The software is in the
%    development stage. It is intended exclusively for experts who have experience with such software and
%    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
%    and/or fitness for a particular purpose are hereby disclaimed and excluded.
% 
%    Any dispute, controversy or claim arising under or in relation to providing this software shall be
%    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
%    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
%    the United Nations Convention on Contracts on the International Sales of Goods.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function IR_data = generate_HOA_HRIRs_MOD_lens(order, python_path, sofa_path, sofa_file_name, ir_len)
    % HRIR convertor - Takes sphere sampled HRIRs and converts them to
    % HOA HRIRs.
    %
    % order - HOA order to be converted to.
    % python_path - path that points to the python binary that has the
    %   sofar python module installed. sofar is needed to read the .sofa
    %   files that contain the HRIRs.
    % sofa_path - path to the directory that contains the sofa files to be
    % converted.
    % sofa_file - file name of the HRTFs to be converted
    % ir_len - length of the IRs to be used.
    %
    % Typical usage:
    %   generate_HOA_HRIRs_MOD_lens(1, "~/.pyenv/versions/3.8.16/bin/python", '~/git/ivas-pc-testfiles/sofa-files/', 'HRIR_128_48000.sofa', 128)
    %

% Pointing to the correct python binary
pyenv(Version=python_path);

% Load in the support coefs
load('hrtf_support_coefs.mat', 'hrtf_support_coefs');
rmsSphere   = hrtf_support_coefs(order).rmsSphere;
LR_Odd      = hrtf_support_coefs(order).LR_Odd;
XYZ_to_Pan  = hrtf_support_coefs(order).XYZ_to_Pan;
AllPass     = hrtf_support_coefs(order).AP;

% Choose a hi-res set of positions to sample the input HRTFs
Vs_hi_res = load("sphere_packing_2562.mat");
Vs_hi_res = Vs_hi_res.Vs_hi_res;
N = 512;

% Fetch the HRTFs, and figure out the ITD for every direction
% name = ['HRIR_', num2str(ir_len), '_Meth5_IRC_53_Q10_symL_Itrp1_48000'];
H = hrtf_library_loader();
H.readSOFA(char(fullfile(sofa_path, sofa_file_name)));

IRs_hi_res = H.XYZ_to_IR(Vs_hi_res);
FRs_hi_res = m_dft(IRs_hi_res, N); % freq x ears x Vs

FRs_hi_res_minP = mag2min_phase(FRs_hi_res);

Excess_Phase = squeeze(unwrap(diff(angle(FRs_hi_res), 1, 2) - ...
    diff(angle(FRs_hi_res_minP), 1, 2)));

bin1200 = ceil( 1200/24000*N );
ITD_hi_res = Excess_Phase(bin1200,:)' / ((bin1200-0.5)/N*24000*2*pi);

MaxDel = max(ITD_hi_res, [], 'all');

% Create 2 ears
Ear_dels_hi_res = (repmat(ITD_hi_res, 1, 2) .* [0.5 -0.5]) + 0.5*MaxDel .* ...
  (1 - 2/pi*cos(ITD_hi_res * pi/2 / MaxDel));

MRs_hi_res = abs(FRs_hi_res_minP);
% Generate permutation
[~, perm] = ismembertol(...
    Vs_hi_res', Vs_hi_res'.*[1,-1,1], ...
    1e-4, "ByRows",true);
MRs_hi_res(:,2,:) = MRs_hi_res(:,2,perm);

New_FreqResp_L = mag2min_phase(squeeze(mean(MRs_hi_res, 2))) .* ...
    m_dft(get_allpass_IRs(AllPass, Ear_dels_hi_res(:, 1) * 48000), N, 1);

% Create solving weights
weights = abs(New_FreqResp_L);
weights(weights < 0.1) = 0.1;
weights = 1 ./ (sqrt(sqrt(weights)));

% Solve to compute the HOA frequency responses.
[m, ~] = size(XYZ_to_Pan);
FreqResp_HOA = zeros(m, N);
for k=1:N
    Aw = New_FreqResp_L(k,:) .* weights(k,:);
    Bw = XYZ_to_Pan .* weights(k,:);
    
    FreqResp_HOA(:,k) = Aw * pinv(Bw, 0);
end

FreqResp_HOA_abs2 = real(FreqResp_HOA.*conj(FreqResp_HOA)); 
FreqResp_HOA = FreqResp_HOA .* mag2min_phase(((FreqResp_HOA_abs2' * rmsSphere.^2) .^ (-0.5)), 1).';
% Convert back to IRs
IR_HOA = m_idft(FreqResp_HOA.', [], 1);

IR_HOA = cat(3, IR_HOA, (IR_HOA .* (1-2*LR_Odd)'));
% Put matrix dimensions in the right order
IR_HOA = permute(IR_HOA, [3, 1, 2]);

% Get the IRs to the right length
IR_HOA = IR_HOA(:,1:ir_len,:) .* sin(interp1([0,150/192*ir_len,ir_len+1],[1,1,0]*pi/2, 1:ir_len));

IR = permute(IR_HOA, [2, 1, 3]);
HOAformat_str = ['HOA',num2str(order),'S'];
save(fullfile(erase(sofa_file_name, ".sofa") + "_converted_" +  HOAformat_str + ".mat"), "IR")

H.updateSOFA(IR);
H.writeSOFA(fullfile(erase(sofa_file_name, ".sofa") + "_converted_" +  HOAformat_str + ".sofa"));

IR_data = IR;




+250 −0
Original line number Diff line number Diff line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%    (C) 2022-2023 Baseline Development Group with portions copyright Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation. All Rights Reserved.
% 
%    This software is protected by copyright law and by international treaties.
%    The Baseline Development Group consisting of Dolby International AB, Ericsson AB,
%    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
%    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies OY, Orange,
%    Panasonic Corporation, Qualcomm Technologies, Inc., and VoiceAge Corporation retain full ownership
%    rights in their respective contributions in the software. No license of any kind, including but not
%    limited to patent license, of any foregoing parties is hereby granted by implication, estoppel or
%    otherwise.
% 
%    This software is provided "AS IS", without any express or implied warranties. The software is in the
%    development stage. It is intended exclusively for experts who have experience with such software and
%    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
%    and/or fitness for a particular purpose are hereby disclaimed and excluded.
% 
%    Any dispute, controversy or claim arising under or in relation to providing this software shall be
%    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
%    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
%    the United Nations Convention on Contracts on the International Sales of Goods.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function generate_rom_tables(data_struct)

    h_file_name = 'ivas_rom_binaural_crend_head.h';
    c_file_name = 'ivas_rom_binaural_crend_head.c';
    tab = '    ';
    
    copyright_str = string(join({
    '/******************************************************************************************************'
    ''
    '   (C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,'
    '   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,'
    '   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,'
    '   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other'
    '   contributors to this repository. All Rights Reserved.'
    ''
    '   This software is protected by copyright law and by international treaties.'
    '   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,'
    '   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,'
    '   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,'
    '   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other'
    '   contributors to this repository retain full ownership rights in their respective contributions in'
    '   the software. This notice grants no license of any kind, including but not limited to patent'
    '   license, nor is any license granted by implication, estoppel or otherwise.'
    ''
    '   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making'
    '   contributions.'
    ''
    '   This software is provided "AS IS", without any express or implied warranties. The software is in the'
    '   development stage. It is intended exclusively for experts who have experience with such software and'
    '   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability'
    '   and fitness for a particular purpose are hereby disclaimed and excluded.'
    ''
    '   Any dispute, controversy or claim arising under or in relation to providing this software shall be'
    '   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in'
    '   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and'
    '   the United Nations Convention on Contracts on the International Sales of Goods.'
    ''
    '*******************************************************************************************************/'
    ''
    '/* clang-format off */'
    ''
    '/*-------------------------------------------------------------------------'
    ' * Binaural rendering related ROM tables'
    ' *------------------------------------------------------------------------*/'
    ''
    '/* Binaural rendering data set based on HRIRs */'
    '/* Tables generated by scripts/binauralRenderer_interface/generate_cren_ivas_tables.c, see mixer_conv_sofa_to_rom_table_converter_readme.txt */'
    '/* Can be replaced by your own generated HRIR or BRIR tables */'
    ''
    ''
    }, newline));
    
    %% Write out the header
    fileID = fopen(h_file_name,'w');
    fprintf(fileID,'%s', copyright_str);

    h_file_content = string(join({
    ''
    ''
    '#ifndef _IVAS_ROM_BINAURAL_CREND_HEAD_'
    '#define _IVAS_ROM_BINAURAL_CREND_HEAD_'
    ''
    '#include "ivas_cnst.h"'
    ''
    ''
    }, newline));
    fprintf(fileID,'%s', h_file_content);

    for hoa_idx=1:length(data_struct)
        HOA_name = data_struct(hoa_idx).HOA_name;
        n_HOA_ch = data_struct(hoa_idx).n_HOA_ch;

        h_file_content = string(join({
        sprintf('\n/********************** CRendBin_%s_HRIR **********************/', HOA_name)
        ''
        sprintf('extern float CRendBin_%s_HRIR_latency_s;', HOA_name)
        ''
        }, newline));
        fprintf(fileID,'%s', h_file_content);
        for sr_idx=1:length(data_struct(hoa_idx).sr)
            sr = data_struct(hoa_idx).sr(sr_idx);
            sr_short = data_struct(hoa_idx).sr_short(sr_idx);
            sr_dft_size = data_struct(hoa_idx).sr_dft_size(sr_idx);

            fprintf(fileID, '\n/* Sample Rate = %d */\n\n', sr);
            fprintf(fileID, 'extern int16_t CRendBin_%s_HRIR_max_num_iterations_%dkHz;\n', HOA_name, sr_short);
            fprintf(fileID, 'extern uint16_t CRendBin_%s_HRIR_num_iterations_%dkHz[%d][BINAURAL_CHANNELS];\n', HOA_name, sr_short, n_HOA_ch);
            fprintf(fileID, 'extern uint16_t CRendBin_%s_HRIR_num_iterations_diffuse_%dkHz[BINAURAL_CHANNELS];\n', HOA_name, sr_short);
            fprintf(fileID, 'extern uint16_t CRendBin_%s_HRIR_pIndex_frequency_max_%dkHz[%d][BINAURAL_CHANNELS][1];\n', HOA_name, sr_short, n_HOA_ch);
            fprintf(fileID, 'extern uint16_t CRendBin_%s_HRIR_index_frequency_max_diffuse_%dkHz;\n', HOA_name, sr_short);
            fprintf(fileID, 'extern float CRendBin_%s_HRIR_inv_diffuse_weight_%dkHz[%d];\n', HOA_name, sr_short, n_HOA_ch);
            fprintf(fileID, 'extern uint16_t *CRendBin_%s_HRIR_pIndex_frequency_max_diffuse_%dkHz[BINAURAL_CHANNELS];\n', HOA_name, sr_short);
            fprintf(fileID, 'extern float CRendBin_%s_HRIR_coeff_re_%dkHz[%d][BINAURAL_CHANNELS][%d];\n', HOA_name, sr_short, n_HOA_ch, sr_dft_size);
            fprintf(fileID, 'extern float CRendBin_%s_HRIR_coeff_im_%dkHz[%d][BINAURAL_CHANNELS][%d];\n', HOA_name, sr_short, n_HOA_ch, sr_dft_size);
            fprintf(fileID, 'extern float *CRendBin_%s_HRIR_coeff_diffuse_re_%dkHz[BINAURAL_CHANNELS];\n', HOA_name, sr_short);
            fprintf(fileID, 'extern float *CRendBin_%s_HRIR_coeff_diffuse_im_%dkHz[BINAURAL_CHANNELS];\n', HOA_name, sr_short);
        end
    end
    fprintf(fileID,'%s\n', '#endif /* _IVAS_ROM_BINAURAL_CREND_HEAD_ */');
    fclose(fileID);
    
    %% Write out the c file
    fileID = fopen(c_file_name,'w');
    fprintf(fileID,'%s', copyright_str);

    c_file_content = string(join({
    ''
    ''
    '#include <stddef.h>'
    '#include "ivas_cnst.h"'
    ''
    '/* clang-format off */'
    ''
    '#define WMC_TOOL_SKIP'
    ''
    ''
    ''
    }, newline));
    fprintf(fileID,'%s', c_file_content);
    
    for hoa_idx=1:length(data_struct)
        HOA_name = data_struct(hoa_idx).HOA_name;
        n_HOA_ch = data_struct(hoa_idx).n_HOA_ch;
        
        % First Sample rate must be 48k
        assert(data_struct(hoa_idx).sr(1) == 48000)

        % Compute latency of IRs
        [~, I] = max(abs(data_struct(hoa_idx).IR_data));
        latency = (min(squeeze(I), [], "all") - 1) / data_struct(hoa_idx).sr(1);
        latency = latency + 0.000000001;

        fprintf(fileID,'\n/********************** CRendBin_%s_HRIR **********************/\n\n', HOA_name);
        fprintf(fileID,'const float CRendBin_%s_HRIR_latency_s = %10.9ff;\n', HOA_name, latency);

        % 48k data
        resampled_data{1} = data_struct(hoa_idx).IR_data;
        % Resample to 32k
        resampled_data{2} = resample(data_struct(hoa_idx).IR_data, 2, 3, 128, 'Dimension', 1);
        % Resample to 16k
        resampled_data{3} = resample(data_struct(hoa_idx).IR_data, 1, 3, 128, 'Dimension', 1);
        for sr_idx=1:length(data_struct(hoa_idx).sr)
            sr = data_struct(hoa_idx).sr(sr_idx);
            sr_short = data_struct(hoa_idx).sr_short(sr_idx);
            sr_dft_size = data_struct(hoa_idx).sr_dft_size(sr_idx);
        
            FR = m_dft(resampled_data{1}, data_struct(hoa_idx).sr_dft_size(sr_idx), 1);
            
            fprintf(fileID, '\n');
            fprintf(fileID, '/* Sample Rate = %d */\n', sr);
            fprintf(fileID, '\n');
            fprintf(fileID, 'const int16_t CRendBin_%s_HRIR_max_num_iterations_%dkHz = 1;\n', HOA_name, sr_short);
            fprintf(fileID, 'const uint16_t CRendBin_%s_HRIR_num_iterations_%dkHz[%d][BINAURAL_CHANNELS]={', HOA_name, sr_short, n_HOA_ch);
            c_file_content = '';
            for ch_idx=1:n_HOA_ch
                c_file_content = [c_file_content, '{1, 1}, '];
            end
            c_file_content(end-1:end) = [];
            fprintf(fileID, '%s', c_file_content);
            fprintf(fileID, ' };\n');
            fprintf(fileID, 'const uint16_t CRendBin_%s_HRIR_num_iterations_diffuse_%dkHz[BINAURAL_CHANNELS] = {0, 0};\n', HOA_name, sr_short);
            fprintf(fileID, 'const uint16_t CRendBin_%s_HRIR_pIndex_frequency_max_%dkHz[%d][BINAURAL_CHANNELS][1]={', HOA_name, sr_short, n_HOA_ch);
            c_file_content = '';
            for ch_idx=1:n_HOA_ch
                c_file_content = [c_file_content '{'];
                for ear_idx=1:2
                    c_file_content = [c_file_content, sprintf('{%d},', sr_dft_size)];
                end
                c_file_content(end) = [];
                c_file_content = [c_file_content '},'];
            end
            c_file_content(end) = [];
            fprintf(fileID, '%s', c_file_content);
            fprintf(fileID, '};\n');
            fprintf(fileID, 'const uint16_t CRendBin_%s_HRIR_index_frequency_max_diffuse_%dkHz = 0;\n', HOA_name, sr_short);
            fprintf(fileID, 'const float CRendBin_%s_HRIR_inv_diffuse_weight_%dkHz[%d]={', HOA_name, sr_short, n_HOA_ch);
            c_file_content = '';
            for ch_idx=1:data_struct(hoa_idx).n_HOA_ch
                c_file_content = [c_file_content, sprintf('%ff, ', 0.0)];
            end
            c_file_content(end-1:end) = [];
            fprintf(fileID, '%s', c_file_content);
            fprintf(fileID, '};\n');
            fprintf(fileID, 'const uint16_t *CRendBin_%s_HRIR_pIndex_frequency_max_diffuse_%dkHz[BINAURAL_CHANNELS]={NULL,NULL};\n', HOA_name, sr_short);
        
            % Real Part
            write_out_HRIR(fileID, tab, n_HOA_ch, 2, sr_dft_size, FR, @real, 're', HOA_name, sr_short);
            % Imag Part
            write_out_HRIR(fileID, tab, n_HOA_ch, 2, sr_dft_size, FR, @imag, 'im', HOA_name, sr_short);
            
            fprintf(fileID, 'const float *CRendBin_%s_HRIR_coeff_diffuse_re_%dkHz[BINAURAL_CHANNELS]={NULL,NULL};\n', HOA_name, sr_short);
            fprintf(fileID, 'const float *CRendBin_%s_HRIR_coeff_diffuse_im_%dkHz[BINAURAL_CHANNELS]={NULL,NULL};\n', HOA_name, sr_short);
        end
    end
    fprintf(fileID, '\n#undef WMC_TOOL_SKIP\n\n');
    
    fclose(fileID);

end

function write_out_HRIR(fileID, tab, n_hoa_ch, n_ears, dft_size, freq_resp, func_handle, func_name, hoa_name, sr_short)
    fprintf(fileID,'%s\n', sprintf('const float CRendBin_%s_HRIR_coeff_%s_%dkHz[%d][BINAURAL_CHANNELS][%d]={', hoa_name, func_name, sr_short, n_hoa_ch, dft_size));
    for hoa_idx=1:n_hoa_ch
        fprintf(fileID,'%s%s\n', tab, '{');
        for ear_idx=1:n_ears
            fprintf(fileID,'%s%s%s', tab, tab, '{');
            for bin_idx=1:dft_size
                fprintf(fileID,'%ff, ', func_handle(freq_resp(bin_idx, ear_idx, hoa_idx)));
                if(mod(bin_idx, 96) == 0)
                    fprintf(fileID,'\n%s%s', tab, tab);
                end
            end
            fseek(fileID, -2, "cof");
            fprintf(fileID,'%s,\n', '}');
        end
        fseek(fileID, -2, "cof");
        fprintf(fileID,'\n%s%s,\n', tab, '}');
    end
    fseek(fileID, -2, "cof");
    fprintf(fileID,'\n%s;\n', '}');
end

+70 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading