├── .gitignore ├── README.md ├── CMakeLists.txt └── mr-hrtf ├── generate_hrtf_database.py ├── kiss_fft.h ├── _kiss_fft_guts.h ├── mr-hrtf.h ├── kiss_fft.c └── mr-hrtf.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.user 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mr-hrtf 2 | A simple 3d sound head related transfer function (HRTF) library implementation. 3 | 4 | A HRTF filter can be used to simulate the direction a sound is coming from. 5 | That is if used correct you can close your eyes and hear where a sound is 6 | coming from. 7 | 8 | This uses data from the CIPIC database [1], and fast fourier code from the 9 | kiss_fft library [2] to implement a hrtf filter. 10 | 11 | It is presented as a simple to use C interface. 12 | 13 | ## Simple code example 14 | 15 | TODO: (sorry) 16 | 17 | ## Dependencies 18 | 19 | * Python 3 + scipy 20 | * Cmake 3.3 or later 21 | * C++11 compatable compiler 22 | 23 | ## Getting ready 24 | 25 | ### Getting the CIPIC data 26 | 27 | Download the HRTF data from the CIPIC website [2] and save it somewhere. 28 | 29 | ### Creating the data files 30 | 31 | Use `generate_hrtf_database.py` to process the CIPIC matlab data to a data 32 | format the library can use. 33 | 34 | ### Building 35 | 36 | ``` 37 | cd mr-hrtf 38 | mkdir build 39 | cd build 40 | cmake .. 41 | make 42 | ``` 43 | 44 | ## This is a simple library 45 | 46 | * It doesn't use a real-only FFT 47 | * It doesn't use SIMD 48 | * It doesn't interpolate between HRTFs 49 | * It uses float based sample mixing 50 | * It uses simple linear blending when crossing HRTF boundaries 51 | 52 | ## License 53 | This code is licesned under the AGPL V3 license, execpt for files for kiss_fft, 54 | which are licensed under the BSD license. 55 | 56 | [1]: http://interface.cipic.ucdavis.edu/sound/hrtf.html 57 | [2]: http://kissfft.sourceforge.net/ 58 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # mr-hrtf. A simple 3d sound head related transfer function (HRTF) filter 2 | # Copyright (C) 2015 Richard Maxwell 3 | # This file is part of mr-hrtf 4 | # mr-hrtf is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU Affero General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU Affero General Public License for more details. 12 | # You should have received a copy of the GNU Affero General Public License 13 | # along with this program. If not, see 14 | 15 | # ---------------------------------------------------------------------------- 16 | # Project info 17 | # ---------------------------------------------------------------------------- 18 | 19 | set(PROJECT_NAME "mr-hrtf") 20 | project(${PROJECT_NAME} CXX C) 21 | 22 | cmake_minimum_required(VERSION 3.3.0 FATAL_ERROR) 23 | 24 | # ---------------------------------------------------------------------------- 25 | # Prerequisites 26 | # ---------------------------------------------------------------------------- 27 | 28 | include_directories(${CMAKE_SOURCE_DIR}) 29 | 30 | # C++11/14 features I use. 31 | set(NEEDED_FEATURES 32 | cxx_auto_type 33 | cxx_trailing_return_types 34 | cxx_right_angle_brackets 35 | cxx_nullptr) 36 | 37 | # ---------------------------------------------------------------------------- 38 | # Source 39 | # ---------------------------------------------------------------------------- 40 | set(SOURCE 41 | mr-hrtf/mr-hrtf.cpp 42 | mr-hrtf/mr-hrtf.h 43 | mr-hrtf/_kiss_fft_guts.h 44 | mr-hrtf/kiss_fft.c 45 | mr-hrtf/kiss_fft.h) 46 | 47 | # ---------------------------------------------------------------------------- 48 | # Link 49 | # ---------------------------------------------------------------------------- 50 | 51 | add_library(${PROJECT_NAME} ${SOURCE}) 52 | target_compile_features(${PROJECT_NAME} PRIVATE ${NEEDED_FEATURES}) 53 | target_link_libraries(${PROJECT_NAME} kiss_fft) 54 | -------------------------------------------------------------------------------- /mr-hrtf/generate_hrtf_database.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # mr-hrtf. A simple 3d sound head related transfer function (HRTF) filter 3 | # Copyright (C) 2015 Richard Maxwell 4 | # This file is part of mr-hrtf 5 | # mr-hrtf is free software: you can redistribute it and/or modify 6 | # it under the terms of the GNU Affero General Public License as published by 7 | # the Free Software Foundation, either version 3 of the License, or 8 | # (at your option) any later version. 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU Affero General Public License for more details. 13 | # You should have received a copy of the GNU Affero General Public License 14 | # along with this program. If not, see 15 | 16 | import scipy.io as io 17 | import os 18 | import textwrap 19 | import numpy 20 | 21 | from struct import * 22 | 23 | cipic_root = os.path.expanduser('.') 24 | 25 | anthro = io.loadmat(cipic_root + '/anthropometry/anthro.mat') 26 | 27 | subject_root = cipic_root + '/standard_hrir_database' 28 | subjects = os.listdir(subject_root) 29 | 30 | anthros = 17 + (2 * (8 + 2)) 31 | 32 | with open('cipic.hrim', 'bw+') as meta_data: 33 | 34 | size_in_bytes = 4 + (4 * len(anthro['id']) * (1 + anthros)) 35 | 36 | meta_data.write(pack('bbbb', ord('H'),ord('R'),ord('I'),ord('M'))) 37 | meta_data.write(pack(' 5 | #include 6 | #include 7 | #include 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | /* 14 | ATTENTION! 15 | If you would like a : 16 | -- a utility that will handle the caching of fft objects 17 | -- real-only (no imaginary time component ) FFT 18 | -- a multi-dimensional FFT 19 | -- a command-line utility to perform ffts 20 | -- a command-line utility to perform fast-convolution filtering 21 | 22 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c 23 | in the tools/ directory. 24 | */ 25 | 26 | #ifdef USE_SIMD 27 | # include 28 | # define kiss_fft_scalar __m128 29 | #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) 30 | #define KISS_FFT_FREE _mm_free 31 | #else 32 | #define KISS_FFT_MALLOC malloc 33 | #define KISS_FFT_FREE free 34 | #endif 35 | 36 | 37 | #ifdef FIXED_POINT 38 | #include 39 | # if (FIXED_POINT == 32) 40 | # define kiss_fft_scalar int32_t 41 | # else 42 | # define kiss_fft_scalar int16_t 43 | # endif 44 | #else 45 | # ifndef kiss_fft_scalar 46 | /* default is float */ 47 | # define kiss_fft_scalar float 48 | # endif 49 | #endif 50 | 51 | typedef struct { 52 | kiss_fft_scalar r; 53 | kiss_fft_scalar i; 54 | }kiss_fft_cpx; 55 | 56 | typedef struct kiss_fft_state* kiss_fft_cfg; 57 | 58 | /* 59 | * kiss_fft_alloc 60 | * 61 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. 62 | * 63 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); 64 | * 65 | * The return value from fft_alloc is a cfg buffer used internally 66 | * by the fft routine or NULL. 67 | * 68 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. 69 | * The returned value should be free()d when done to avoid memory leaks. 70 | * 71 | * The state can be placed in a user supplied buffer 'mem': 72 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, 73 | * then the function places the cfg in mem and the size used in *lenmem 74 | * and returns mem. 75 | * 76 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), 77 | * then the function returns NULL and places the minimum cfg 78 | * buffer size in *lenmem. 79 | * */ 80 | 81 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); 82 | 83 | /* 84 | * kiss_fft(cfg,in_out_buf) 85 | * 86 | * Perform an FFT on a complex input buffer. 87 | * for a forward FFT, 88 | * fin should be f[0] , f[1] , ... ,f[nfft-1] 89 | * fout will be F[0] , F[1] , ... ,F[nfft-1] 90 | * Note that each element is complex and can be accessed like 91 | f[k].r and f[k].i 92 | * */ 93 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 94 | 95 | /* 96 | A more generic version of the above function. It reads its input from every Nth sample. 97 | * */ 98 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); 99 | 100 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous 101 | buffer and can be simply free()d when no longer needed*/ 102 | #define kiss_fft_free free 103 | 104 | /* 105 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up 106 | your compiler output to call this before you exit. 107 | */ 108 | void kiss_fft_cleanup(void); 109 | 110 | 111 | /* 112 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) 113 | */ 114 | int kiss_fft_next_fast_size(int n); 115 | 116 | /* for real ffts, we need an even size */ 117 | #define kiss_fftr_next_fast_size_real(n) \ 118 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /mr-hrtf/_kiss_fft_guts.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2010, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | /* kiss_fft.h 16 | defines kiss_fft_scalar as either short or a float type 17 | and defines 18 | typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ 19 | #include "kiss_fft.h" 20 | #include 21 | 22 | #define MAXFACTORS 32 23 | /* e.g. an fft of length 128 has 4 factors 24 | as far as kissfft is concerned 25 | 4*4*4*2 26 | */ 27 | 28 | struct kiss_fft_state{ 29 | int nfft; 30 | int inverse; 31 | int factors[2*MAXFACTORS]; 32 | kiss_fft_cpx twiddles[1]; 33 | }; 34 | 35 | /* 36 | Explanation of macros dealing with complex math: 37 | 38 | C_MUL(m,a,b) : m = a*b 39 | C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise 40 | C_SUB( res, a,b) : res = a - b 41 | C_SUBFROM( res , a) : res -= a 42 | C_ADDTO( res , a) : res += a 43 | * */ 44 | #ifdef FIXED_POINT 45 | #if (FIXED_POINT==32) 46 | # define FRACBITS 31 47 | # define SAMPPROD int64_t 48 | #define SAMP_MAX 2147483647 49 | #else 50 | # define FRACBITS 15 51 | # define SAMPPROD int32_t 52 | #define SAMP_MAX 32767 53 | #endif 54 | 55 | #define SAMP_MIN -SAMP_MAX 56 | 57 | #if defined(CHECK_OVERFLOW) 58 | # define CHECK_OVERFLOW_OP(a,op,b) \ 59 | if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 60 | fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } 61 | #endif 62 | 63 | 64 | # define smul(a,b) ( (SAMPPROD)(a)*(b) ) 65 | # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) 66 | 67 | # define S_MUL(a,b) sround( smul(a,b) ) 68 | 69 | # define C_MUL(m,a,b) \ 70 | do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ 71 | (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) 72 | 73 | # define DIVSCALAR(x,k) \ 74 | (x) = sround( smul( x, SAMP_MAX/k ) ) 75 | 76 | # define C_FIXDIV(c,div) \ 77 | do { DIVSCALAR( (c).r , div); \ 78 | DIVSCALAR( (c).i , div); }while (0) 79 | 80 | # define C_MULBYSCALAR( c, s ) \ 81 | do{ (c).r = sround( smul( (c).r , s ) ) ;\ 82 | (c).i = sround( smul( (c).i , s ) ) ; }while(0) 83 | 84 | #else /* not FIXED_POINT*/ 85 | 86 | # define S_MUL(a,b) ( (a)*(b) ) 87 | #define C_MUL(m,a,b) \ 88 | do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ 89 | (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) 90 | # define C_FIXDIV(c,div) /* NOOP */ 91 | # define C_MULBYSCALAR( c, s ) \ 92 | do{ (c).r *= (s);\ 93 | (c).i *= (s); }while(0) 94 | #endif 95 | 96 | #ifndef CHECK_OVERFLOW_OP 97 | # define CHECK_OVERFLOW_OP(a,op,b) /* noop */ 98 | #endif 99 | 100 | #define C_ADD( res, a,b)\ 101 | do { \ 102 | CHECK_OVERFLOW_OP((a).r,+,(b).r)\ 103 | CHECK_OVERFLOW_OP((a).i,+,(b).i)\ 104 | (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ 105 | }while(0) 106 | #define C_SUB( res, a,b)\ 107 | do { \ 108 | CHECK_OVERFLOW_OP((a).r,-,(b).r)\ 109 | CHECK_OVERFLOW_OP((a).i,-,(b).i)\ 110 | (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ 111 | }while(0) 112 | #define C_ADDTO( res , a)\ 113 | do { \ 114 | CHECK_OVERFLOW_OP((res).r,+,(a).r)\ 115 | CHECK_OVERFLOW_OP((res).i,+,(a).i)\ 116 | (res).r += (a).r; (res).i += (a).i;\ 117 | }while(0) 118 | 119 | #define C_SUBFROM( res , a)\ 120 | do {\ 121 | CHECK_OVERFLOW_OP((res).r,-,(a).r)\ 122 | CHECK_OVERFLOW_OP((res).i,-,(a).i)\ 123 | (res).r -= (a).r; (res).i -= (a).i; \ 124 | }while(0) 125 | 126 | 127 | #ifdef FIXED_POINT 128 | # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) 129 | # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) 130 | # define HALF_OF(x) ((x)>>1) 131 | #elif defined(USE_SIMD) 132 | # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) 133 | # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) 134 | # define HALF_OF(x) ((x)*_mm_set1_ps(.5)) 135 | #else 136 | # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) 137 | # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) 138 | # define HALF_OF(x) ((x)*.5) 139 | #endif 140 | 141 | #define kf_cexp(x,phase) \ 142 | do{ \ 143 | (x)->r = KISS_FFT_COS(phase);\ 144 | (x)->i = KISS_FFT_SIN(phase);\ 145 | }while(0) 146 | 147 | 148 | /* a debugging function */ 149 | #define pcpx(c)\ 150 | fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) 151 | 152 | 153 | #ifdef KISS_FFT_USE_ALLOCA 154 | // define this to allow use of alloca instead of malloc for temporary buffers 155 | // Temporary buffers are used in two case: 156 | // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 157 | // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 158 | #include 159 | #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 160 | #define KISS_FFT_TMP_FREE(ptr) 161 | #else 162 | #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 163 | #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) 164 | #endif 165 | -------------------------------------------------------------------------------- /mr-hrtf/mr-hrtf.h: -------------------------------------------------------------------------------- 1 | // mr-hrtf. A simple 3d sound head related transfer function (HRTF) filter 2 | // Copyright (C) 2015 Richard Maxwell 3 | // This file is part of mr-hrtf 4 | // mr-hrtf is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Affero General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // This program is distributed in the hope that it will be useful, 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | // GNU Affero General Public License for more details. 12 | // You should have received a copy of the GNU Affero General Public License 13 | // along with this program. If not, see 14 | 15 | #ifndef MR_HRTF_H 16 | #define MR_HRTF_H 17 | 18 | #include 19 | 20 | #ifdef __cplusplus 21 | extern "C" 22 | { 23 | #endif 24 | 25 | // ----------------------------------------------------------------------------- 26 | // Internal housekeeping 27 | // ----------------------------------------------------------------------------- 28 | #ifndef MR_HRTF_REQUIRE 29 | #include 30 | #define MR_HRTF_REQUIRE(test) assert(test) 31 | #endif 32 | 33 | // ----------------------------------------------------------------------------- 34 | // Subject and Ear Descriptions (Anthropometry) 35 | // ----------------------------------------------------------------------------- 36 | 37 | #define CIPIC_COUNT_X 17 38 | #define CIPIC_COUNT_D 8 39 | #define CIPIC_COUNT_THETA 2 40 | #define CIPIC_COUNT_ALL CIPIC_COUNT_X \ 41 | + (2 * (CIPIC_COUNT_D + CIPIC_COUNT_THETA)) 42 | 43 | typedef struct Mr_hrtf_anthropometry_raw 44 | { 45 | float data[CIPIC_COUNT_ALL]; 46 | } 47 | Mr_hrtf_anthropometry_raw; 48 | 49 | typedef struct Mr_hrtf_anthropometry_pinnae_short 50 | { 51 | float d[CIPIC_COUNT_D]; 52 | float theta[CIPIC_COUNT_THETA]; 53 | } 54 | Mr_hrtf_anthropometry_pinnae_short; 55 | 56 | typedef struct Mr_hrtf_anthropometry_short 57 | { 58 | float x[CIPIC_COUNT_X]; 59 | 60 | Mr_hrtf_anthropometry_pinnae_short pinnae_left; 61 | Mr_hrtf_anthropometry_pinnae_short pinnae_right; 62 | } 63 | Mr_hrtf_anthropometry_short; 64 | 65 | 66 | typedef struct Mr_hrtf_anthropometry_pinnae_long 67 | { 68 | float cavum_concha_height; 69 | float cymba_concha_height; 70 | float cavum_concha_width; 71 | float fossa_height; 72 | float pinna_height; 73 | float pinna_width; 74 | float intertragal_incisure_width; 75 | float cavum_concha_depth; 76 | 77 | float pinna_rotation_angle; 78 | float pinna_flare_angle; 79 | } 80 | Mr_hrtf_anthropometry_pinnae_long; 81 | 82 | typedef struct Mr_hrtf_anthropometry_long 83 | { 84 | float head_width; 85 | float head_height; 86 | float head_depth; 87 | float pinna_offset_down; 88 | float pinna_offset_back; 89 | float neck_width; 90 | float neck_height; 91 | float neck_depth; 92 | float torso_top_width; 93 | float torso_top_height; 94 | float torso_top_depth; 95 | float shoulder_width; 96 | float head_offset_forward; 97 | float height; 98 | float seated_height; 99 | float head_circumference; 100 | float shoulder_circumference; 101 | 102 | Mr_hrtf_anthropometry_pinnae_long pinnae_left; 103 | Mr_hrtf_anthropometry_pinnae_long pinnae_right; 104 | } 105 | Mr_hrtf_anthropometry_long; 106 | 107 | typedef union Mr_hrtf_anthropometry 108 | { 109 | Mr_hrtf_anthropometry_raw r; 110 | Mr_hrtf_anthropometry_short s; 111 | Mr_hrtf_anthropometry_long l; 112 | } 113 | Mr_hrtf_anthropometry; 114 | 115 | // ----------------------------------------------------------------------------- 116 | // HRTF Structures 117 | // ----------------------------------------------------------------------------- 118 | 119 | typedef struct Mr_hrtf_location 120 | { 121 | float azimuth; 122 | float elevation; 123 | } 124 | Mr_hrtf_location; 125 | 126 | typedef struct Mr_hrtf_source 127 | { 128 | Mr_hrtf_location location; 129 | short* source; 130 | } 131 | Mr_hrtf_source; 132 | 133 | // ----------------------------------------------------------------------------- 134 | // Setup structures 135 | // ----------------------------------------------------------------------------- 136 | 137 | enum Mr_hrtf_result 138 | { 139 | MR_HRTF_RESULT_ALL_OK 140 | 141 | , MR_HRTF_RESULT_SETTINGS_NULL 142 | 143 | , MR_HRTF_RESULT_HRIM_CANT_OPEN 144 | , MR_HRTF_RESULT_HRIM_INVALID_SIZE 145 | , MR_HRTF_RESULT_HRIM_INVALID_VERSION 146 | 147 | , MR_HRTF_RESULT_HRIR_CANT_OPEN 148 | , MR_HRTF_RESULT_HRIR_INVALID_SIZE 149 | , MR_HRTF_RESULT_HRIR_INVALID_VERSION 150 | 151 | , MR_HRTF_RESULT_COUNT 152 | }; 153 | 154 | // opaque handle 155 | struct Mr_hrtf; 156 | typedef struct Mr_hrtf Mr_hrtf; 157 | 158 | struct Mr_hrtf_model_result 159 | { 160 | Mr_hrtf_result result; 161 | Mr_hrtf* model; 162 | }; 163 | 164 | struct Mr_hrtf_owned 165 | { 166 | void* p; 167 | unsigned length_in_bytes; 168 | }; 169 | //twiddles; 30 | kiss_fft_cpx t; 31 | Fout2 = Fout + m; 32 | do{ 33 | C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); 34 | 35 | C_MUL (t, *Fout2 , *tw1); 36 | tw1 += fstride; 37 | C_SUB( *Fout2 , *Fout , t ); 38 | C_ADDTO( *Fout , t ); 39 | ++Fout2; 40 | ++Fout; 41 | }while (--m); 42 | } 43 | 44 | static void kf_bfly4( 45 | kiss_fft_cpx * Fout, 46 | const size_t fstride, 47 | const kiss_fft_cfg st, 48 | const size_t m 49 | ) 50 | { 51 | kiss_fft_cpx *tw1,*tw2,*tw3; 52 | kiss_fft_cpx scratch[6]; 53 | size_t k=m; 54 | const size_t m2=2*m; 55 | const size_t m3=3*m; 56 | 57 | 58 | tw3 = tw2 = tw1 = st->twiddles; 59 | 60 | do { 61 | C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); 62 | 63 | C_MUL(scratch[0],Fout[m] , *tw1 ); 64 | C_MUL(scratch[1],Fout[m2] , *tw2 ); 65 | C_MUL(scratch[2],Fout[m3] , *tw3 ); 66 | 67 | C_SUB( scratch[5] , *Fout, scratch[1] ); 68 | C_ADDTO(*Fout, scratch[1]); 69 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); 70 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); 71 | C_SUB( Fout[m2], *Fout, scratch[3] ); 72 | tw1 += fstride; 73 | tw2 += fstride*2; 74 | tw3 += fstride*3; 75 | C_ADDTO( *Fout , scratch[3] ); 76 | 77 | if(st->inverse) { 78 | Fout[m].r = scratch[5].r - scratch[4].i; 79 | Fout[m].i = scratch[5].i + scratch[4].r; 80 | Fout[m3].r = scratch[5].r + scratch[4].i; 81 | Fout[m3].i = scratch[5].i - scratch[4].r; 82 | }else{ 83 | Fout[m].r = scratch[5].r + scratch[4].i; 84 | Fout[m].i = scratch[5].i - scratch[4].r; 85 | Fout[m3].r = scratch[5].r - scratch[4].i; 86 | Fout[m3].i = scratch[5].i + scratch[4].r; 87 | } 88 | ++Fout; 89 | }while(--k); 90 | } 91 | 92 | static void kf_bfly3( 93 | kiss_fft_cpx * Fout, 94 | const size_t fstride, 95 | const kiss_fft_cfg st, 96 | size_t m 97 | ) 98 | { 99 | size_t k=m; 100 | const size_t m2 = 2*m; 101 | kiss_fft_cpx *tw1,*tw2; 102 | kiss_fft_cpx scratch[5]; 103 | kiss_fft_cpx epi3; 104 | epi3 = st->twiddles[fstride*m]; 105 | 106 | tw1=tw2=st->twiddles; 107 | 108 | do{ 109 | C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); 110 | 111 | C_MUL(scratch[1],Fout[m] , *tw1); 112 | C_MUL(scratch[2],Fout[m2] , *tw2); 113 | 114 | C_ADD(scratch[3],scratch[1],scratch[2]); 115 | C_SUB(scratch[0],scratch[1],scratch[2]); 116 | tw1 += fstride; 117 | tw2 += fstride*2; 118 | 119 | Fout[m].r = Fout->r - HALF_OF(scratch[3].r); 120 | Fout[m].i = Fout->i - HALF_OF(scratch[3].i); 121 | 122 | C_MULBYSCALAR( scratch[0] , epi3.i ); 123 | 124 | C_ADDTO(*Fout,scratch[3]); 125 | 126 | Fout[m2].r = Fout[m].r + scratch[0].i; 127 | Fout[m2].i = Fout[m].i - scratch[0].r; 128 | 129 | Fout[m].r -= scratch[0].i; 130 | Fout[m].i += scratch[0].r; 131 | 132 | ++Fout; 133 | }while(--k); 134 | } 135 | 136 | static void kf_bfly5( 137 | kiss_fft_cpx * Fout, 138 | const size_t fstride, 139 | const kiss_fft_cfg st, 140 | int m 141 | ) 142 | { 143 | kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; 144 | int u; 145 | kiss_fft_cpx scratch[13]; 146 | kiss_fft_cpx * twiddles = st->twiddles; 147 | kiss_fft_cpx *tw; 148 | kiss_fft_cpx ya,yb; 149 | ya = twiddles[fstride*m]; 150 | yb = twiddles[fstride*2*m]; 151 | 152 | Fout0=Fout; 153 | Fout1=Fout0+m; 154 | Fout2=Fout0+2*m; 155 | Fout3=Fout0+3*m; 156 | Fout4=Fout0+4*m; 157 | 158 | tw=st->twiddles; 159 | for ( u=0; ur += scratch[7].r + scratch[8].r; 174 | Fout0->i += scratch[7].i + scratch[8].i; 175 | 176 | scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); 177 | scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); 178 | 179 | scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); 180 | scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); 181 | 182 | C_SUB(*Fout1,scratch[5],scratch[6]); 183 | C_ADD(*Fout4,scratch[5],scratch[6]); 184 | 185 | scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); 186 | scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); 187 | scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); 188 | scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); 189 | 190 | C_ADD(*Fout2,scratch[11],scratch[12]); 191 | C_SUB(*Fout3,scratch[11],scratch[12]); 192 | 193 | ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; 194 | } 195 | } 196 | 197 | /* perform the butterfly for one stage of a mixed radix FFT */ 198 | static void kf_bfly_generic( 199 | kiss_fft_cpx * Fout, 200 | const size_t fstride, 201 | const kiss_fft_cfg st, 202 | int m, 203 | int p 204 | ) 205 | { 206 | int u,k,q1,q; 207 | kiss_fft_cpx * twiddles = st->twiddles; 208 | kiss_fft_cpx t; 209 | int Norig = st->nfft; 210 | 211 | kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); 212 | 213 | for ( u=0; u=Norig) twidx-=Norig; 228 | C_MUL(t,scratch[q] , twiddles[twidx] ); 229 | C_ADDTO( Fout[ k ] ,t); 230 | } 231 | k += m; 232 | } 233 | } 234 | KISS_FFT_TMP_FREE(scratch); 235 | } 236 | 237 | static 238 | void kf_work( 239 | kiss_fft_cpx * Fout, 240 | const kiss_fft_cpx * f, 241 | const size_t fstride, 242 | int in_stride, 243 | int * factors, 244 | const kiss_fft_cfg st 245 | ) 246 | { 247 | kiss_fft_cpx * Fout_beg=Fout; 248 | const int p=*factors++; /* the radix */ 249 | const int m=*factors++; /* stage's fft length/p */ 250 | const kiss_fft_cpx * Fout_end = Fout + p*m; 251 | 252 | #ifdef _OPENMP 253 | // use openmp extensions at the 254 | // top-level (not recursive) 255 | if (fstride==1 && p<=5) 256 | { 257 | int k; 258 | 259 | // execute the p different work units in different threads 260 | # pragma omp parallel for 261 | for (k=0;k floor_sqrt) 324 | p = n; /* no more factors, skip to end */ 325 | } 326 | n /= p; 327 | *facbuf++ = p; 328 | *facbuf++ = n; 329 | } while (n > 1); 330 | } 331 | 332 | /* 333 | * 334 | * User-callable function to allocate all necessary storage space for the fft. 335 | * 336 | * The return value is a contiguous block of memory, allocated with malloc. As such, 337 | * It can be freed with free(), rather than a kiss_fft-specific function. 338 | * */ 339 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) 340 | { 341 | kiss_fft_cfg st=NULL; 342 | size_t memneeded = sizeof(struct kiss_fft_state) 343 | + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ 344 | 345 | if ( lenmem==NULL ) { 346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); 347 | }else{ 348 | if (mem != NULL && *lenmem >= memneeded) 349 | st = (kiss_fft_cfg)mem; 350 | *lenmem = memneeded; 351 | } 352 | if (st) { 353 | int i; 354 | st->nfft=nfft; 355 | st->inverse = inverse_fft; 356 | 357 | for (i=0;iinverse) 361 | phase *= -1; 362 | kf_cexp(st->twiddles+i, phase ); 363 | } 364 | 365 | kf_factor(nfft,st->factors); 366 | } 367 | return st; 368 | } 369 | 370 | 371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) 372 | { 373 | if (fin == fout) { 374 | //NOTE: this is not really an in-place FFT algorithm. 375 | //It just performs an out-of-place FFT into a temp buffer 376 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); 377 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st); 378 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); 379 | KISS_FFT_TMP_FREE(tmpbuf); 380 | }else{ 381 | kf_work( fout, fin, 1,in_stride, st->factors,st ); 382 | } 383 | } 384 | 385 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 386 | { 387 | kiss_fft_stride(cfg,fin,fout,1); 388 | } 389 | 390 | 391 | void kiss_fft_cleanup(void) 392 | { 393 | // nothing needed any more 394 | } 395 | 396 | int kiss_fft_next_fast_size(int n) 397 | { 398 | while(1) { 399 | int m=n; 400 | while ( (m%2) == 0 ) m/=2; 401 | while ( (m%3) == 0 ) m/=3; 402 | while ( (m%5) == 0 ) m/=5; 403 | if (m<=1) 404 | break; /* n is completely factorable by twos, threes, and fives */ 405 | n++; 406 | } 407 | return n; 408 | } 409 | -------------------------------------------------------------------------------- /mr-hrtf/mr-hrtf.cpp: -------------------------------------------------------------------------------- 1 | // mr-hrtf. A simple 3d sound head related transfer function (HRTF) filter 2 | // Copyright (C) 2015 Richard Maxwell 3 | // This file is part of mr-hrtf 4 | // mr-hrtf is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Affero General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // This program is distributed in the hope that it will be useful, 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | // GNU Affero General Public License for more details. 12 | // You should have received a copy of the GNU Affero General Public License 13 | // along with this program. If not, see 14 | 15 | #include "mr-hrtf.h" 16 | 17 | #include "kiss_fft.h" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | #define CIPIC_COUNT_HRIR_AZIMUTH 25 26 | #define CIPIC_COUNT_HRIR_ELEVATION 50 27 | #define CIPIC_COUNT_HRIR_SAMPLES 200 28 | #define CIPIC_COUNT_SUBJECTS 45 29 | #define CIPIC_COUNT_HRIR \ 30 | (CIPIC_COUNT_HRIR_AZIMUTH * CIPIC_COUNT_HRIR_ELEVATION) 31 | 32 | static constexpr unsigned HRTF_LENGTH = CIPIC_COUNT_HRIR_SAMPLES; 33 | static constexpr auto HRTF_OVERLAP = HRTF_LENGTH - 1; 34 | 35 | static constexpr auto VERSION_HRIM = 0u; 36 | static constexpr auto VERSION_HRIR = 0u; 37 | 38 | static constexpr auto INT16_FLOAT_MAX = ((1 << 15) - 1.0f); 39 | static constexpr auto INT16_FLOAT_MIN = (-1.0f * (1 << 15)); 40 | 41 | using Overlap_array = std::array; 42 | // Use length and waste 4 bytes so it's 16 byte aligned. 43 | 44 | namespace { 45 | // ----------------------------------------------------------------------------- 46 | // http://the-witness.net/news/2012/11/scopeexit-in-c11/ 47 | 48 | template 49 | struct Scoped_exit 50 | { 51 | Scoped_exit(F f) : f(f) {} 52 | ~Scoped_exit() { f(); } 53 | F f; 54 | }; 55 | 56 | template 57 | Scoped_exit make_scoped_exit(F f) 58 | { 59 | return Scoped_exit(f); 60 | }; 61 | 62 | #define MR_HRTF_STRING_JOIN2(arg1, arg2) MR_HRTF_DO_STRING_JOIN2(arg1, arg2) 63 | #define MR_HRTF_DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2 64 | #define MR_HRTF_SCOPED_EXIT(code) \ 65 | auto MR_HRTF_STRING_JOIN2(scope_exit_, __LINE__) = \ 66 | make_scoped_exit([=](){code;}) 67 | 68 | // ----------------------------------------------------------------------------- 69 | 70 | template 71 | struct Span 72 | { 73 | T* p; 74 | std::size_t count; 75 | 76 | auto operator[](int i) -> T& { return p[i]; } 77 | auto operator[](int i) const -> const T& { return p[i]; } 78 | 79 | auto data() -> T* { return p; } 80 | auto constexpr data() const -> T* { return p; } 81 | 82 | auto constexpr size() const -> std::size_t { return count; } 83 | 84 | auto begin() -> T* { return p; } 85 | auto constexpr begin() const -> T* { return p; } 86 | auto end() -> T* { return p + count; } 87 | auto constexpr end() const -> T* { return p + count; } 88 | }; 89 | 90 | template 91 | auto constexpr span(T* array, std::size_t array_size) -> Span 92 | { 93 | return 94 | { 95 | array 96 | , array_size 97 | }; 98 | } 99 | 100 | template 101 | auto constexpr span(T& array) -> Span 102 | { 103 | return 104 | { 105 | array.data() 106 | , array.size() 107 | }; 108 | } 109 | 110 | } // namespace 111 | 112 | // ----------------------------------------------------------------------------- 113 | 114 | union Tag 115 | { 116 | std::uint32_t dword; 117 | char c[4]; 118 | }; 119 | 120 | struct Hrir_file 121 | { 122 | using Hrir_pair = 123 | std::array, 2>; 124 | 125 | using Hrir_elevation = std::array; 126 | using Hrirs = std::array; 127 | 128 | Tag tag; 129 | std::uint32_t length; 130 | std::uint32_t version; 131 | Mr_hrtf_anthropometry anthropometry; 132 | Hrirs hrirs; 133 | }; 134 | 135 | struct Hrim_map 136 | { 137 | std::uint32_t id; 138 | Mr_hrtf_anthropometry anthropometry; 139 | }; 140 | 141 | struct Hrim_file 142 | { 143 | Tag tag; 144 | std::uint32_t length; 145 | std::uint32_t version; 146 | std::array anthropometry; 147 | }; 148 | 149 | struct Mr_hrtf 150 | { 151 | Mr_hrtf_settings settings; 152 | 153 | std::size_t size_fft; 154 | 155 | kiss_fft_cfg fft; 156 | kiss_fft_cfg ifft; 157 | 158 | std::array locations; 159 | 160 | Span hrtfs; 161 | Span temps; 162 | Span mix; 163 | Span last_indicies; 164 | 165 | std::array, 2> channel_overlaps; 166 | }; 167 | 168 | enum Mr_hrtf_temp 169 | { 170 | MR_HRTF_TEMP_INPUT_PRE_FFT 171 | , MR_HRTF_TEMP_INPUT_FFT 172 | , MR_HRTF_TEMP_OUTPUT_FFT 173 | , MR_HRTF_TEMP_OUTPUT_FFT_INVERSE 174 | , MR_HRTF_TEMP_OUTPUT_FFT_LAST 175 | , MR_HRTF_TEMP_OUTPUT_FFT_INVERSE_LAST 176 | 177 | , MR_HRTF_TEMP_COUNT 178 | }; 179 | 180 | // ----------------------------------------------------------------------------- 181 | // Malloc and array math helpers 182 | // ----------------------------------------------------------------------------- 183 | 184 | template 185 | auto malloc_span(unsigned count, Mr_hrtf_malloc mr_malloc) -> Span 186 | { 187 | auto bytes = sizeof(T) * count; 188 | return Span{ static_cast(mr_malloc(bytes)), count}; 189 | } 190 | 191 | template 192 | void free_span(Span& span, Mr_hrtf_free mr_free) 193 | { 194 | mr_free(span.data()); 195 | span = {nullptr, 0}; 196 | } 197 | 198 | enum Mr_hrtf_side 199 | { 200 | MR_HRTF_SIDE_LEFT = 0 201 | , MR_HRTF_SIDE_RIGHT = 1 202 | 203 | , MR_HRTF_SIDE_COUNT 204 | }; 205 | 206 | auto get_hrtf 207 | ( 208 | Mr_hrtf& mix 209 | , Mr_hrtf_side side 210 | , unsigned index 211 | ) 212 | -> kiss_fft_cpx* 213 | { 214 | const auto size_fft = mix.size_fft; 215 | auto hrtfs = mix.hrtfs; 216 | 217 | const auto hrtf_set_size = size_fft * CIPIC_COUNT_HRIR; 218 | 219 | index *= size_fft; 220 | index += hrtf_set_size * side; 221 | 222 | return &hrtfs[index]; 223 | } 224 | 225 | auto get_temp(Mr_hrtf& mix, Mr_hrtf_temp index) -> kiss_fft_cpx* 226 | { 227 | return &mix.temps[index * mix.size_fft]; 228 | } 229 | 230 | auto get_mix(Mr_hrtf& mix, Mr_hrtf_side side) -> Span 231 | { 232 | return span 233 | ( 234 | &mix.mix[side * mix.settings.size_input] 235 | , mix.settings.size_input 236 | ); 237 | } 238 | 239 | auto default_ro_file_data 240 | ( 241 | Mr_hrtf_malloc mr_hrtf_malloc, 242 | const char* filename 243 | ) 244 | -> Mr_hrtf_owned 245 | { 246 | Mr_hrtf_owned result = 247 | { 248 | 0 249 | , 0 250 | }; 251 | 252 | if ((!filename) || (!mr_hrtf_malloc)) 253 | { 254 | return result; 255 | } 256 | 257 | auto* f = fopen(filename, "r"); 258 | 259 | if (!f) 260 | { 261 | return result; 262 | } 263 | 264 | MR_HRTF_SCOPED_EXIT(fclose(f)); 265 | 266 | if (!fseek(f, 0, SEEK_END)) 267 | { 268 | auto bytes = ftell(f); 269 | rewind(f); 270 | 271 | auto data = mr_hrtf_malloc(bytes); 272 | fread(data, 1, bytes, f); 273 | 274 | result.p = data; 275 | result.length_in_bytes = bytes; 276 | } 277 | 278 | return result; 279 | } 280 | 281 | inline auto operator- 282 | ( 283 | Mr_hrtf_anthropometry lhs 284 | , const Mr_hrtf_anthropometry& rhs 285 | ) 286 | -> Mr_hrtf_anthropometry 287 | { 288 | for (unsigned i = 0; i < CIPIC_COUNT_ALL; ++i) 289 | { 290 | lhs.r.data[i] -= rhs.r.data[i]; 291 | } 292 | 293 | return lhs; 294 | } 295 | 296 | inline auto operator* 297 | ( 298 | Mr_hrtf_anthropometry lhs 299 | , const Mr_hrtf_anthropometry& rhs 300 | ) 301 | -> Mr_hrtf_anthropometry 302 | { 303 | for (unsigned i = 0; i < CIPIC_COUNT_ALL; ++i) 304 | { 305 | lhs.r.data[i] *= rhs.r.data[i]; 306 | } 307 | 308 | return lhs; 309 | } 310 | 311 | inline auto is_zero(const Mr_hrtf_anthropometry& lhs) -> bool 312 | { 313 | for (unsigned i = 0; i < CIPIC_COUNT_ALL; ++i) 314 | { 315 | if (lhs.r.data[i] != 0.0f) 316 | { 317 | return false; 318 | } 319 | } 320 | 321 | return true; 322 | } 323 | 324 | inline auto sum(const Mr_hrtf_anthropometry& lhs) -> float 325 | { 326 | float result = 0.0f; 327 | 328 | for (unsigned i = 0; i < CIPIC_COUNT_ALL; ++i) 329 | { 330 | result += lhs.r.data[i]; 331 | } 332 | 333 | return result; 334 | } 335 | 336 | auto closest_anthro 337 | ( 338 | const Hrim_file& anthro 339 | , const Mr_hrtf_anthropometry& wanted 340 | , const Mr_hrtf_anthropometry& weights 341 | 342 | ) 343 | -> std::uint32_t 344 | { 345 | std::uint32_t result = 0xFFFFFFFF; 346 | float base_error = 1000000000.0f; 347 | 348 | // values of zero are treated as invalid / missing 349 | for (unsigned i = 0; i < CIPIC_COUNT_ALL; ++i) 350 | { 351 | const auto& current = anthro.anthropometry[i].anthropometry; 352 | if (is_zero(current)) 353 | { 354 | continue; 355 | } 356 | 357 | const auto error = wanted - current; 358 | const auto error_weighted = error * weights; 359 | const auto error_squared = error_weighted * error_weighted; 360 | const auto error_value = sum(error_squared); 361 | 362 | if (error_value < base_error) 363 | { 364 | base_error = error_value; 365 | result = anthro.anthropometry[i].id; 366 | } 367 | } 368 | 369 | return result; 370 | } 371 | 372 | static const Mr_hrtf_anthropometry g_simple_mask = 373 | { 374 | // x1, x3, x6, x12, d1, d3, d5, d6 375 | { 376 | 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f 377 | , 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f 378 | 379 | , 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f 380 | , 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f 381 | } 382 | }; 383 | 384 | int get_closest_index 385 | ( 386 | Mr_hrtf_location location 387 | , Span locations 388 | ) 389 | { 390 | auto error = 10000000.0f; 391 | int result = -1; 392 | 393 | const auto size = locations.size(); 394 | for (unsigned i = 0; i < size; ++i) 395 | { 396 | const auto& test = locations[i]; 397 | 398 | auto diff = 399 | ( 400 | (location.azimuth - test.azimuth) 401 | * (location.azimuth - test.azimuth) 402 | ) 403 | + 404 | ( 405 | (location.elevation - test.elevation) 406 | * (location.elevation - test.elevation) 407 | ); 408 | 409 | if (diff < error) 410 | { 411 | result = i; 412 | error = diff; 413 | } 414 | } 415 | 416 | return result; 417 | } 418 | 419 | // ----------------------------------------------------------------------------- 420 | // API (mixing) 421 | // ----------------------------------------------------------------------------- 422 | 423 | void mr_hrtf_mix_to_seperate_lr_work 424 | ( 425 | Mr_hrtf* mix 426 | , const Mr_hrtf_source* sources 427 | , unsigned channels 428 | ) 429 | { 430 | MR_HRTF_REQUIRE(mix != nullptr); 431 | MR_HRTF_REQUIRE(sources != nullptr); 432 | MR_HRTF_REQUIRE(channels <= mix->settings.channel_count); 433 | MR_HRTF_REQUIRE(mix->settings.size_input >= HRTF_LENGTH); 434 | 435 | MR_HRTF_REQUIRE 436 | ( 437 | mix->size_fft >= (mix->settings.size_input + HRTF_LENGTH - 1) 438 | ); 439 | 440 | const auto size_input = mix->settings.size_input; 441 | const auto size_fft = mix->size_fft; 442 | 443 | auto input_pre_fft = get_temp(*mix, MR_HRTF_TEMP_INPUT_PRE_FFT); 444 | auto input_fft = get_temp(*mix, MR_HRTF_TEMP_INPUT_FFT); 445 | auto output_fft = get_temp(*mix, MR_HRTF_TEMP_OUTPUT_FFT); 446 | auto output_fft_last = get_temp(*mix, MR_HRTF_TEMP_OUTPUT_FFT_LAST); 447 | auto output_fft_inverse = get_temp(*mix, MR_HRTF_TEMP_OUTPUT_FFT_INVERSE); 448 | 449 | auto output_fft_inverse_last = 450 | get_temp(*mix, MR_HRTF_TEMP_OUTPUT_FFT_INVERSE_LAST); 451 | 452 | memset(mix->mix.data(), 0, 4 * mix->mix.size()); 453 | auto out = std::array, 2> 454 | { 455 | get_mix(*mix, MR_HRTF_SIDE_LEFT) 456 | , get_mix(*mix, MR_HRTF_SIDE_RIGHT) 457 | }; 458 | 459 | const auto fft_mult = [] 460 | ( 461 | const kiss_fft_cpx* lhs 462 | , const kiss_fft_cpx* rhs 463 | , kiss_fft_cpx* out 464 | , unsigned size_fft 465 | ) 466 | { 467 | for (unsigned i = 0; i < size_fft; ++i) 468 | { 469 | out[i].r = (lhs[i].r * rhs[i].r) - (lhs[i].i * rhs[i].i); 470 | out[i].i = (lhs[i].r * rhs[i].i) + (lhs[i].i * rhs[i].r); 471 | } 472 | }; 473 | 474 | for (unsigned channel_index = 0; channel_index < channels; ++channel_index) 475 | { 476 | const auto& source = sources[channel_index]; 477 | 478 | const auto hrtf_index = 479 | get_closest_index(source.location, span(mix->locations)); 480 | 481 | const auto last_index = mix->last_indicies[channel_index]; 482 | 483 | mix->last_indicies[channel_index] = hrtf_index; 484 | 485 | const auto* in = source.source; 486 | 487 | if (!in) 488 | { 489 | continue; 490 | } 491 | 492 | for (int side = 0; side < 2; ++side) 493 | { 494 | const auto hrtf = 495 | get_hrtf(*mix, static_cast(side), hrtf_index); 496 | 497 | const auto hrtf_last = [&]() -> kiss_fft_cpx* 498 | { 499 | if ((hrtf_index != last_index) && (last_index >=0)) 500 | { 501 | return get_hrtf 502 | ( 503 | *mix 504 | , static_cast(side) 505 | , last_index 506 | ); 507 | } 508 | 509 | return nullptr; 510 | }(); 511 | 512 | auto& overlap = mix->channel_overlaps[side][channel_index]; 513 | 514 | for (unsigned i = 0; i < HRTF_OVERLAP; ++i) 515 | { 516 | input_pre_fft[i].r = overlap[i]; 517 | input_pre_fft[i].i = 0; 518 | } 519 | for (unsigned i = 0 ; i < size_input; ++i) 520 | { 521 | input_pre_fft[i + HRTF_OVERLAP].r = in[i]; 522 | input_pre_fft[i + HRTF_OVERLAP].i = 0; 523 | } 524 | for (unsigned i = size_input + HRTF_OVERLAP; i < size_fft; ++i) 525 | { 526 | input_pre_fft[i].r = 0; 527 | input_pre_fft[i].i = 0; 528 | } 529 | 530 | const auto offset_input_overlap = size_input - HRTF_OVERLAP; 531 | for (unsigned i = 0; i < HRTF_OVERLAP; ++i) 532 | { 533 | overlap[i] = in[i + offset_input_overlap]; 534 | } 535 | 536 | kiss_fft(mix->fft, input_pre_fft, input_fft); 537 | fft_mult(input_fft, hrtf, output_fft, size_fft); 538 | kiss_fft(mix->ifft, output_fft, output_fft_inverse); 539 | 540 | // Mix with the hrtf of the previous location if needed 541 | if (!hrtf_last) 542 | { 543 | auto& out_side = out[side]; 544 | for (unsigned i = 0; i < size_input; ++i) 545 | { 546 | out_side[i] += 547 | output_fft_inverse[i + HRTF_OVERLAP].r / size_fft; 548 | } 549 | } 550 | else 551 | { 552 | fft_mult(input_fft, hrtf_last, output_fft_last, size_fft); 553 | kiss_fft(mix->ifft, output_fft_last, output_fft_inverse_last); 554 | 555 | auto& out_side = out[side]; 556 | const auto step = 1.0f / size_input; 557 | for (unsigned i = 0; i < size_input; ++i) 558 | { 559 | const auto lerp = (i * step); 560 | const auto lerp_i = 1.0f - lerp; 561 | const auto mixed = 562 | ( 563 | (lerp * output_fft_inverse[i + HRTF_OVERLAP].r) 564 | + (lerp_i * output_fft_inverse_last[i + HRTF_OVERLAP].r) 565 | ) 566 | / size_fft; 567 | 568 | out_side[i] += mixed; 569 | } 570 | } 571 | } 572 | } 573 | } 574 | 575 | auto inline constexpr clip_to_short(float to_clip) -> std::int16_t 576 | { 577 | return std::round 578 | ( 579 | (to_clip < INT16_FLOAT_MAX) 580 | ? (to_clip > INT16_FLOAT_MIN) 581 | ? to_clip 582 | : INT16_FLOAT_MIN 583 | : INT16_FLOAT_MAX 584 | ); 585 | } 586 | 587 | void mr_hrtf_mix_to_interleaved_lr 588 | ( 589 | Mr_hrtf* mix 590 | , const Mr_hrtf_source* channels 591 | , unsigned channels_count 592 | , short* destination 593 | ) 594 | { 595 | mr_hrtf_mix_to_seperate_lr_work(mix, channels, channels_count); 596 | 597 | auto out_left = get_mix(*mix, MR_HRTF_SIDE_LEFT); 598 | auto out_right = get_mix(*mix, MR_HRTF_SIDE_RIGHT); 599 | 600 | const auto size_input = mix->settings.size_input; 601 | for (unsigned i = 0; i < size_input; ++i) 602 | { 603 | destination[i * 2 + 0] = clip_to_short(out_left[i]); 604 | destination[i * 2 + 1] = clip_to_short(out_right[i]); 605 | } 606 | } 607 | 608 | void mr_hrtf_mix_to_seperate_lr 609 | ( 610 | Mr_hrtf* mix 611 | , const Mr_hrtf_source* channels 612 | , unsigned channels_count 613 | , short* left 614 | , short* right 615 | ) 616 | { 617 | MR_HRTF_REQUIRE(left != nullptr); 618 | MR_HRTF_REQUIRE(right != nullptr); 619 | 620 | mr_hrtf_mix_to_seperate_lr_work(mix, channels, channels_count); 621 | 622 | auto out_left = get_mix(*mix, MR_HRTF_SIDE_LEFT); 623 | auto out_right = get_mix(*mix, MR_HRTF_SIDE_RIGHT); 624 | 625 | const auto size_input = mix->settings.size_input; 626 | for (unsigned i = 0; i < size_input; ++i) 627 | { 628 | left[i] = clip_to_short(out_left[i]); 629 | right[i] = clip_to_short(out_right[i]); 630 | } 631 | } 632 | 633 | auto mr_hrtf_get_default_settings 634 | ( 635 | unsigned channel_count 636 | , unsigned size_input 637 | ) 638 | -> Mr_hrtf_settings 639 | { 640 | return 641 | { 642 | channel_count 643 | , size_input 644 | , nullptr 645 | , nullptr 646 | 647 | , nullptr 648 | , nullptr 649 | , nullptr 650 | }; 651 | } 652 | 653 | auto mr_hrtf_get_model(const Mr_hrtf_settings* settings) -> Mr_hrtf_model_result 654 | { 655 | if (!settings) 656 | { 657 | return {MR_HRTF_RESULT_SETTINGS_NULL, nullptr}; 658 | } 659 | 660 | Mr_hrtf_settings config = *settings; 661 | 662 | if 663 | ( 664 | (!config.malloc) 665 | && (!config.free) 666 | && (!config.file_read) 667 | ) 668 | { 669 | config.malloc = malloc; 670 | config.free = free; 671 | config.file_read = default_ro_file_data; 672 | } 673 | 674 | // ------------------------------------------------------------------------- 675 | 676 | auto raw_hrim = config.file_read(config.malloc, "cipic.hrim"); 677 | if (!raw_hrim.p) 678 | { 679 | return {MR_HRTF_RESULT_HRIM_CANT_OPEN, nullptr}; 680 | } 681 | MR_HRTF_SCOPED_EXIT(config.free(raw_hrim.p)); 682 | 683 | if (raw_hrim.length_in_bytes != sizeof(Hrim_file)) 684 | { 685 | return {MR_HRTF_RESULT_HRIM_INVALID_SIZE, nullptr}; 686 | } 687 | 688 | const auto* hrim_file = static_cast(raw_hrim.p); 689 | if (hrim_file->version != VERSION_HRIM) 690 | { 691 | return {MR_HRTF_RESULT_HRIM_INVALID_VERSION, nullptr}; 692 | } 693 | 694 | // ------------------------------------------------------------------------- 695 | 696 | std::uint32_t hrir_id = 3; 697 | 698 | if (settings->anthropemetry) 699 | { 700 | const auto weights = 701 | settings->anthropemetry_weights 702 | ? settings->anthropemetry_weights 703 | : &g_simple_mask; 704 | 705 | hrir_id = closest_anthro 706 | ( 707 | *hrim_file 708 | , *settings->anthropemetry 709 | , *weights 710 | ); 711 | } 712 | 713 | char file_name[32]; 714 | 715 | sprintf(file_name, "subject_%03d.hrir", hrir_id); 716 | 717 | const auto raw_hrir = config.file_read(config.malloc, file_name); 718 | 719 | if (!raw_hrir.p) 720 | { 721 | return {MR_HRTF_RESULT_HRIR_CANT_OPEN, nullptr}; 722 | } 723 | MR_HRTF_SCOPED_EXIT(config.free(raw_hrir.p)); 724 | 725 | if (raw_hrir.length_in_bytes != sizeof(Hrir_file)) 726 | { 727 | return {MR_HRTF_RESULT_HRIR_INVALID_SIZE, nullptr}; 728 | } 729 | 730 | const auto hrir_file = static_cast(raw_hrir.p); 731 | if (hrir_file->version != VERSION_HRIR) 732 | { 733 | return {MR_HRTF_RESULT_HRIR_INVALID_VERSION, nullptr}; 734 | } 735 | 736 | // ------------------------------------------------------------------------- 737 | 738 | const auto size_input = settings->size_input; 739 | const auto size_fft = 740 | ((size_input + HRTF_OVERLAP) + 63) & ~63; 741 | // Round up the fft size to the nearest 64 bins so that it's more fft 742 | // than dft. That is, we want more 4, 2, 3 and 5 radix butterflys than 743 | // large prime number butterflys. 744 | 745 | auto result = static_cast(config.malloc(sizeof(Mr_hrtf))); 746 | 747 | result->settings = config; 748 | result->size_fft = size_fft; 749 | 750 | result->channel_overlaps[MR_HRTF_SIDE_LEFT] = 751 | malloc_span(config.channel_count, config.malloc); 752 | 753 | result->channel_overlaps[MR_HRTF_SIDE_RIGHT] = 754 | malloc_span(config.channel_count, config.malloc); 755 | 756 | { 757 | size_t fft_size_in_bytes = 0; 758 | 759 | kiss_fft_alloc(size_fft, 0, nullptr, &fft_size_in_bytes); 760 | 761 | result->fft = 762 | kiss_fft_alloc 763 | ( 764 | size_fft 765 | , 0 766 | , config.malloc(fft_size_in_bytes) 767 | , &fft_size_in_bytes 768 | ); 769 | 770 | result->ifft = 771 | kiss_fft_alloc 772 | ( 773 | size_fft 774 | , 1 775 | , config.malloc(fft_size_in_bytes) 776 | , &fft_size_in_bytes 777 | ); 778 | } 779 | 780 | // ------------------------------------------------------------------------- 781 | 782 | result->hrtfs = 783 | malloc_span 784 | ( 785 | size_fft 786 | * CIPIC_COUNT_HRIR 787 | * MR_HRTF_SIDE_COUNT 788 | , config.malloc 789 | ); 790 | 791 | result->temps = 792 | malloc_span 793 | ( 794 | size_fft 795 | * MR_HRTF_TEMP_COUNT 796 | , config.malloc 797 | ); 798 | 799 | result->mix = 800 | malloc_span 801 | ( 802 | size_input 803 | * MR_HRTF_SIDE_COUNT 804 | , config.malloc 805 | ); 806 | 807 | result->last_indicies = 808 | malloc_span 809 | ( 810 | config.channel_count 811 | , config.malloc 812 | ); 813 | 814 | for (auto& index: result->last_indicies) 815 | { 816 | index = -1; 817 | } 818 | 819 | // ------------------------------------------------------------------------- 820 | 821 | const auto az_lookup = std::array 822 | { 823 | -80, -65, -55, -45, -40, -35, -30, -25, -20, -15, -10, -5 824 | , 0 825 | , 5, 10, 15, 20, 25, 30, 35, 40, 45, 55, 65, 80 826 | }; 827 | 828 | unsigned loc_index = 0; 829 | for (unsigned az = 0 ; az < CIPIC_COUNT_HRIR_AZIMUTH; ++az) 830 | { 831 | for (unsigned e = 0 ; e < CIPIC_COUNT_HRIR_ELEVATION; ++e) 832 | { 833 | result->locations[loc_index].elevation = e * 5.625f + -45.0f; 834 | result->locations[loc_index].azimuth = az_lookup[az]; 835 | 836 | ++loc_index; 837 | } 838 | } 839 | 840 | // ------------------------------------------------------------------------- 841 | 842 | auto hrir_pre_fft = get_temp(*result, MR_HRTF_TEMP_INPUT_PRE_FFT); 843 | memset(hrir_pre_fft, 0, size_fft * sizeof(kiss_fft_cpx)); 844 | 845 | static_assert(MR_HRTF_SIDE_LEFT == 0, "MR_HRTF_SIDE_LEFT isn't 0."); 846 | static_assert(MR_HRTF_SIDE_RIGHT == 1, "MR_HRTF_SIDE_RIGHT isn't 1."); 847 | 848 | unsigned index = 0; 849 | for (unsigned az = 0 ; az < CIPIC_COUNT_HRIR_AZIMUTH; ++az) 850 | { 851 | for (unsigned e = 0 ; e < CIPIC_COUNT_HRIR_ELEVATION; ++e) 852 | { 853 | for (unsigned side = 0; side < 2; ++side) 854 | { 855 | const auto& hrir = hrir_file->hrirs[az][e][side]; 856 | 857 | auto hrtf = 858 | get_hrtf(*result, static_cast(side), index); 859 | 860 | for (unsigned i = 0; i < CIPIC_COUNT_HRIR_SAMPLES; ++i) 861 | { 862 | hrir_pre_fft[i].r = hrir[i]; 863 | } 864 | 865 | kiss_fft(result->fft, hrir_pre_fft, hrtf); 866 | } 867 | 868 | ++index; 869 | } 870 | } 871 | 872 | return {MR_HRTF_RESULT_ALL_OK, result}; 873 | } 874 | 875 | void mr_hrtf_free_model(Mr_hrtf *model) 876 | { 877 | if ((!model) || (!model->settings.free)) 878 | { 879 | return; 880 | } 881 | 882 | const auto& settings = model->settings; 883 | 884 | free_span(model->mix, settings.free); 885 | free_span(model->temps, settings.free); 886 | free_span(model->hrtfs, settings.free); 887 | free_span(model->last_indicies, settings.free); 888 | 889 | free_span(model->channel_overlaps[MR_HRTF_SIDE_LEFT], settings.free); 890 | free_span(model->channel_overlaps[MR_HRTF_SIDE_RIGHT], settings.free); 891 | 892 | settings.free(model->fft); 893 | settings.free(model->ifft); 894 | 895 | settings.free(model); 896 | } 897 | --------------------------------------------------------------------------------