diff options
author | Chris Robinson <[email protected]> | 2011-05-01 13:59:44 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2011-05-01 13:59:44 -0700 |
commit | 56d9418119045d536efa3f552af6e13d9353ada9 (patch) | |
tree | a1688ab2434cd8f3c1bfdb2c928117b50c357b08 /Alc/hrtf.c | |
parent | 0109104027ec4de46c236f6520cf00d89dd980ad (diff) |
Add an HRTF filter for mono sources
The data is based on the KEMAR HRTF data provided by MIT, which can be found at
<http://sound.media.mit.edu/resources/KEMAR.html>. The compact measurements
were used. See hrtf_tables.inc for more information.
The filter is only available for stereo output, using a 44100hz playback rate.
Note also that it currently only applies to mono sounds, and the cf_level and
head_dampen config options are ignored while it is active.
Diffstat (limited to 'Alc/hrtf.c')
-rw-r--r-- | Alc/hrtf.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/Alc/hrtf.c b/Alc/hrtf.c new file mode 100644 index 00000000..6972ec27 --- /dev/null +++ b/Alc/hrtf.c @@ -0,0 +1,75 @@ +/** + * OpenAL cross platform audio library + * Copyright (C) 2011 by Chris Robinson + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * Or go to http://www.gnu.org/copyleft/lgpl.html + */ + +#include "config.h" + +#include "AL/al.h" +#include "AL/alc.h" +#include "alMain.h" + + +typedef struct { + ALsizei num_angles; + ALshort coeffs[][2][HRTF_LENGTH]; +} HrtfFilterCoeffs; + +#include "hrtf_tables.inc" + +static void get_angle_coeffs(const HrtfFilterCoeffs *elev, ALfloat angle, const ALshort **left, const ALshort **right) +{ + if(angle < 0) + { + int idx = ((angle > -180.0) ? (int)(angle*(elev->num_angles-1)/-180.0 + 0.5) : + (elev->num_angles-1)); + *left = elev->coeffs[idx][1]; + *right = elev->coeffs[idx][0]; + } + else + { + int idx = ((angle < 180.0) ? (int)(angle*(elev->num_angles-1)/180.0 + 0.5) : + (elev->num_angles-1)); + *left = elev->coeffs[idx][0]; + *right = elev->coeffs[idx][1]; + } +} + +void GetHrtfCoeffs(ALfloat azimuth, ALfloat angle, const ALshort **left, const ALshort **right) +{ + int idx; + + if(azimuth > 90.f) azimuth = 90.f - (azimuth - 90.f); + else if(azimuth < -90.f) azimuth = -90.f - (azimuth - -90.f); + + idx = (int)(azimuth/10.0 + 0.5); + if(idx >= 9) return get_angle_coeffs(&Elev90, angle, left, right); + if(idx >= 8) return get_angle_coeffs(&Elev80, angle, left, right); + if(idx >= 7) return get_angle_coeffs(&Elev70, angle, left, right); + if(idx >= 6) return get_angle_coeffs(&Elev60, angle, left, right); + if(idx >= 5) return get_angle_coeffs(&Elev50, angle, left, right); + if(idx >= 4) return get_angle_coeffs(&Elev40, angle, left, right); + if(idx >= 3) return get_angle_coeffs(&Elev30, angle, left, right); + if(idx >= 2) return get_angle_coeffs(&Elev20, angle, left, right); + if(idx >= 1) return get_angle_coeffs(&Elev10, angle, left, right); + if(idx >= 0) return get_angle_coeffs(&Elev0, angle, left, right); + if(idx >= -1) return get_angle_coeffs(&Elev10n, angle, left, right); + if(idx >= -2) return get_angle_coeffs(&Elev20n, angle, left, right); + if(idx >= -3) return get_angle_coeffs(&Elev30n, angle, left, right); + return get_angle_coeffs(&Elev40n, angle, left, right); +} |