Use a minimum phase HRTF data set

This reduces the coefficient size from 128 down to 32, with a set of delays
This commit is contained in:
Chris Robinson 2011-06-03 01:06:00 -07:00
parent c7a80418d9
commit 7ddfacb58f
7 changed files with 162 additions and 93 deletions

View File

@ -496,6 +496,8 @@ static void alc_init(void)
ALTHUNK_INIT(); ALTHUNK_INIT();
ReadALConfig(); ReadALConfig();
InitHrtf();
tls_create(&LocalContext); tls_create(&LocalContext);
RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0); RTPrioLevel = GetConfigValueInt(NULL, "rt-prio", 0);

View File

@ -245,7 +245,9 @@ ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
if(c == lfe_chan) if(c == lfe_chan)
{ {
/* Skip LFE */ /* Skip LFE */
for(i = 0;i < HRTF_LENGTH;i++) ALSource->Params.HrtfDelay[c][0] = 0;
ALSource->Params.HrtfDelay[c][1] = 0;
for(i = 0;i < HRIR_LENGTH;i++)
{ {
ALSource->Params.HrtfCoeffs[c][i][0] = 0.0f; ALSource->Params.HrtfCoeffs[c][i][0] = 0.0f;
ALSource->Params.HrtfCoeffs[c][i][1] = 0.0f; ALSource->Params.HrtfCoeffs[c][i][1] = 0.0f;
@ -253,8 +255,11 @@ ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
continue; continue;
} }
GetHrtfCoeffs(0.0, angles[c], &hrtf_left, &hrtf_right); GetHrtfCoeffs(0.0, angles[c] * (M_PI/180.0),
for(i = 0;i < HRTF_LENGTH;i++) &hrtf_left, &hrtf_right,
&ALSource->Params.HrtfDelay[c][0],
&ALSource->Params.HrtfDelay[c][1]);
for(i = 0;i < HRIR_LENGTH;i++)
{ {
ALSource->Params.HrtfCoeffs[c][i][0] = ALSource->Params.HrtfCoeffs[c][i][0] =
hrtf_left[i]*(1.0/32767.0)*DryGain*ListenerGain; hrtf_left[i]*(1.0/32767.0)*DryGain*ListenerGain;
@ -676,10 +681,11 @@ ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
Position[2] *= invlen; Position[2] *= invlen;
} }
GetHrtfCoeffs(asin(Position[1]) * (180.0/M_PI), GetHrtfCoeffs(asin(Position[1]), atan2(Position[0], -Position[2]*ZScale),
atan2(Position[0], -Position[2]*ZScale) * (180.0/M_PI), &hrtf_left, &hrtf_right,
&hrtf_left, &hrtf_right); &ALSource->Params.HrtfDelay[0][0],
for(i = 0;i < HRTF_LENGTH;i++) &ALSource->Params.HrtfDelay[0][1]);
for(i = 0;i < HRIR_LENGTH;i++)
{ {
ALSource->Params.HrtfCoeffs[0][i][0] = hrtf_left[i]*(1.0/32767.0) * DryGain; ALSource->Params.HrtfCoeffs[0][i][0] = hrtf_left[i]*(1.0/32767.0) * DryGain;
ALSource->Params.HrtfCoeffs[0][i][1] = hrtf_right[i]*(1.0/32767.0) * DryGain; ALSource->Params.HrtfCoeffs[0][i][1] = hrtf_right[i]*(1.0/32767.0) * DryGain;

View File

@ -23,56 +23,99 @@
#include "AL/al.h" #include "AL/al.h"
#include "AL/alc.h" #include "AL/alc.h"
#include "alMain.h" #include "alMain.h"
#include "alSource.h"
#define HRIR_COUNT 828
typedef struct { static const ALubyte evCount = 19;
ALsizei num_angles; static const ALushort evOffset[19] = { 0, 1, 13, 37, 73, 118, 174, 234, 306, 378, 450, 522, 594, 654, 710, 755, 791, 815, 827 };
ALsizei max_angle; static const ALubyte azCount[19] = { 1, 12, 24, 36, 45, 56, 60, 72, 72, 72, 72, 72, 60, 56, 45, 36, 24, 12, 1 };
ALshort coeffs[][2][HRTF_LENGTH];
} HrtfFilterCoeffs;
#include "hrtf_tables.inc" static struct HRTF {
ALshort coeffs[HRIR_COUNT][HRIR_LENGTH];
ALubyte delays[HRIR_COUNT];
} Hrtf;
static void get_angle_coeffs(const HrtfFilterCoeffs *elev, ALfloat angle, const ALshort **left, const ALshort **right) static ALuint CalcEvIndex(ALdouble ev)
{ {
if(angle < 0) ev = (M_PI/2.0 + ev) * (evCount-1) / M_PI;
{ return (ALuint)(ev+0.5);
int idx = ((angle > -elev->max_angle) ?
(int)(angle*(elev->num_angles-1)/-elev->max_angle + 0.5) :
(elev->num_angles-1));
*left = elev->coeffs[idx][1];
*right = elev->coeffs[idx][0];
}
else
{
int idx = ((angle < elev->max_angle) ?
(int)(angle*(elev->num_angles-1)/elev->max_angle + 0.5) :
(elev->num_angles-1));
*left = elev->coeffs[idx][0];
*right = elev->coeffs[idx][1];
}
} }
void GetHrtfCoeffs(ALfloat elevation, ALfloat angle, const ALshort **left, const ALshort **right) static ALuint CalcAzIndex(ALint evidx, ALdouble az)
{ {
int idx; az = (M_PI*2.0 + az) * azCount[evidx] / (M_PI*2.0);
return (ALuint)(az+0.5) % azCount[evidx];
if(elevation > 90.f) elevation = 90.f - (elevation - 90.f); }
else if(elevation < -90.f) elevation = -90.f - (elevation - -90.f);
void GetHrtfCoeffs(ALfloat elevation, ALfloat angle, const ALshort **left, const ALshort **right, ALuint *ldelay, ALuint *rdelay)
idx = (int)(elevation/10.0 + 0.5); {
if(idx >= 9) return get_angle_coeffs(&Elev90, angle, left, right); ALuint lidx, ridx;
if(idx >= 8) return get_angle_coeffs(&Elev80, angle, left, right); ALuint evidx, azidx;
if(idx >= 7) return get_angle_coeffs(&Elev70, angle, left, right);
if(idx >= 6) return get_angle_coeffs(&Elev60, angle, left, right); evidx = CalcEvIndex(elevation);
if(idx >= 5) return get_angle_coeffs(&Elev50, angle, left, right); azidx = CalcAzIndex(evidx, angle);
if(idx >= 4) return get_angle_coeffs(&Elev40, angle, left, right);
if(idx >= 3) return get_angle_coeffs(&Elev30, angle, left, right); lidx = evOffset[evidx] + azidx;
if(idx >= 2) return get_angle_coeffs(&Elev20, angle, left, right); ridx = evOffset[evidx] + ((azCount[evidx]-azidx) % azCount[evidx]);
if(idx >= 1) return get_angle_coeffs(&Elev10, angle, left, right);
if(idx >= 0) return get_angle_coeffs(&Elev0, angle, left, right); *ldelay = Hrtf.delays[lidx];
if(idx >= -1) return get_angle_coeffs(&Elev10n, angle, left, right); *rdelay = Hrtf.delays[ridx];
if(idx >= -2) return get_angle_coeffs(&Elev20n, angle, left, right);
if(idx >= -3) return get_angle_coeffs(&Elev30n, angle, left, right); *left = Hrtf.coeffs[lidx];
return get_angle_coeffs(&Elev40n, angle, left, right); *right = Hrtf.coeffs[ridx];
}
void InitHrtf(void)
{
const char *str;
FILE *f = NULL;
str = GetConfigValue(NULL, "hrtf_tables", "");
if(str[0] != '\0')
f = fopen(str, "rb");
if(f != NULL)
{
const ALubyte maxDelay = SRC_HISTORY_LENGTH - HRIR_LENGTH;
struct HRTF newdata;
size_t i, j;
union {
ALfloat f;
ALubyte ub[4];
} val;
for(i = 0;i < HRIR_COUNT;i++)
{
for(j = 0;j < HRIR_LENGTH;j++)
{
val.ub[0] = fgetc(f);
val.ub[1] = fgetc(f);
val.ub[2] = fgetc(f);
val.ub[3] = fgetc(f);
if(val.f > 1.0f) newdata.coeffs[i][j] = 32767;
else if(val.f < -1.0f) newdata.coeffs[i][j] = -32768;
else newdata.coeffs[i][j] = (ALshort)(val.f*32767.0f);
}
}
val.ub[0] = fgetc(f);
val.ub[1] = fgetc(f);
val.ub[2] = fgetc(f);
val.ub[3] = fgetc(f);
/* skip maxHrtd */
for(i = 0;i < HRIR_COUNT;i++)
{
val.ub[0] = fgetc(f);
val.ub[1] = fgetc(f);
val.ub[2] = fgetc(f);
val.ub[3] = fgetc(f);
val.f *= 44100.0f;
if(val.f >= maxDelay) newdata.delays[i] = maxDelay;
else newdata.delays[i] = (ALubyte)val.f;
}
if(!feof(f))
Hrtf = newdata;
fclose(f);
f = NULL;
}
} }

View File

@ -79,9 +79,6 @@ static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
const T *RESTRICT data = srcdata; \ const T *RESTRICT data = srcdata; \
ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS]; \ ALfloat (*RESTRICT DryBuffer)[MAXCHANNELS]; \
ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks; \ ALfloat *RESTRICT ClickRemoval, *RESTRICT PendingClicks; \
ALfloat (*RESTRICT HrtfCoeffs)[HRTF_LENGTH][2]; \
ALfloat (*RESTRICT HrtfHistory)[HRTF_LENGTH]; \
ALuint HrtfOffset; \
ALuint pos, frac; \ ALuint pos, frac; \
FILTER *DryFilter; \ FILTER *DryFilter; \
ALuint BufferIdx; \ ALuint BufferIdx; \
@ -96,15 +93,17 @@ static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
PendingClicks = Device->PendingClicks; \ PendingClicks = Device->PendingClicks; \
DryFilter = &Source->Params.iirFilter; \ DryFilter = &Source->Params.iirFilter; \
\ \
HrtfCoeffs = Source->Params.HrtfCoeffs; \
HrtfHistory = Source->HrtfHistory; \
HrtfOffset = Source->HrtfOffset + OutPos; \
\
pos = 0; \ pos = 0; \
frac = *DataPosFrac; \ frac = *DataPosFrac; \
\ \
for(i = 0;i < NumChannels;i++) \ for(i = 0;i < NumChannels;i++) \
{ \ { \
static const ALuint MaxDelay = SRC_HISTORY_LENGTH - HRIR_LENGTH; \
ALfloat (*RESTRICT Coeffs)[2] = Source->Params.HrtfCoeffs[i]; \
const ALuint *RESTRICT Delay = Source->Params.HrtfDelay[i]; \
ALfloat (*RESTRICT History)[2] = Source->HrtfHistory[i]; \
ALuint Offset = Source->HrtfOffset + OutPos; \
\
pos = 0; \ pos = 0; \
frac = *DataPosFrac; \ frac = *DataPosFrac; \
\ \
@ -113,15 +112,18 @@ static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
value = sampler(data + pos*NumChannels + i, NumChannels, frac); \ value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
value = lpFilter2PC(DryFilter, i, value); \ value = lpFilter2PC(DryFilter, i, value); \
\ \
HrtfHistory[i][HrtfOffset&HRTF_LENGTH_MASK] = value; \ History[Offset&SRC_HISTORY_MASK][0] = value; \
for(c = 0;c < HRTF_LENGTH;c++) \ History[Offset&SRC_HISTORY_MASK][1] = value; \
\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][0] = \
History[(Offset-Delay[0])&SRC_HISTORY_MASK][0];\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][1] = \
History[(Offset-Delay[1])&SRC_HISTORY_MASK][1];\
for(c = 0;c < HRIR_LENGTH;c++) \
{ \ { \
ClickRemoval[FRONT_LEFT] -= \ const ALuint off = (Offset-MaxDelay-c)&SRC_HISTORY_MASK; \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \ ClickRemoval[FRONT_LEFT] -= History[off][0] * Coeffs[c][0]; \
HrtfCoeffs[i][c][0]; \ ClickRemoval[FRONT_RIGHT] -= History[off][1] * Coeffs[c][1]; \
ClickRemoval[FRONT_RIGHT] -= \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
HrtfCoeffs[i][c][1]; \
} \ } \
} \ } \
for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \ for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
@ -129,17 +131,20 @@ static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
value = sampler(data + pos*NumChannels + i, NumChannels, frac); \ value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
value = lpFilter2P(DryFilter, i, value); \ value = lpFilter2P(DryFilter, i, value); \
\ \
HrtfHistory[i][HrtfOffset&HRTF_LENGTH_MASK] = value; \ History[Offset&SRC_HISTORY_MASK][0] = value; \
for(c = 0;c < HRTF_LENGTH;c++) \ History[Offset&SRC_HISTORY_MASK][1] = value; \
\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][0] = \
History[(Offset-Delay[0])&SRC_HISTORY_MASK][0];\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][1] = \
History[(Offset-Delay[1])&SRC_HISTORY_MASK][1];\
for(c = 0;c < HRIR_LENGTH;c++) \
{ \ { \
DryBuffer[OutPos][FRONT_LEFT] += \ const ALuint off = (Offset-MaxDelay-c)&SRC_HISTORY_MASK; \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \ DryBuffer[OutPos][FRONT_LEFT] += History[off][0] * Coeffs[c][0];\
HrtfCoeffs[i][c][0]; \ DryBuffer[OutPos][FRONT_RIGHT] += History[off][1] * Coeffs[c][1];\
DryBuffer[OutPos][FRONT_RIGHT] += \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
HrtfCoeffs[i][c][1]; \
} \ } \
HrtfOffset++; \ Offset++; \
\ \
frac += increment; \ frac += increment; \
pos += frac>>FRACTIONBITS; \ pos += frac>>FRACTIONBITS; \
@ -151,15 +156,18 @@ static void Mix_Hrtf_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
value = sampler(data + pos*NumChannels + i, NumChannels, frac); \ value = sampler(data + pos*NumChannels + i, NumChannels, frac); \
value = lpFilter2PC(DryFilter, i, value); \ value = lpFilter2PC(DryFilter, i, value); \
\ \
HrtfHistory[i][HrtfOffset&HRTF_LENGTH_MASK] = value; \ History[Offset&SRC_HISTORY_MASK][0] = value; \
for(c = 0;c < HRTF_LENGTH;c++) \ History[Offset&SRC_HISTORY_MASK][1] = value; \
\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][0] = \
History[(Offset-Delay[0])&SRC_HISTORY_MASK][0];\
History[(Offset-MaxDelay)&SRC_HISTORY_MASK][1] = \
History[(Offset-Delay[1])&SRC_HISTORY_MASK][1];\
for(c = 0;c < HRIR_LENGTH;c++) \
{ \ { \
PendingClicks[FRONT_LEFT] += \ const ALuint off = (Offset-MaxDelay-c)&SRC_HISTORY_MASK; \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \ PendingClicks[FRONT_LEFT] += History[off][0] * Coeffs[c][0]; \
HrtfCoeffs[i][c][0]; \ PendingClicks[FRONT_RIGHT] += History[off][1] * Coeffs[c][1]; \
PendingClicks[FRONT_RIGHT] += \
HrtfHistory[i][(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
HrtfCoeffs[i][c][1]; \
} \ } \
} \ } \
OutPos -= BufferSize; \ OutPos -= BufferSize; \

View File

@ -567,10 +567,11 @@ const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
ALboolean IsValidType(ALenum type); ALboolean IsValidType(ALenum type);
ALboolean IsValidChannels(ALenum type); ALboolean IsValidChannels(ALenum type);
#define HRTF_BITS (7) #define HRIR_BITS (5)
#define HRTF_LENGTH (1<<HRTF_BITS) #define HRIR_LENGTH (1<<HRIR_BITS)
#define HRTF_LENGTH_MASK (HRTF_LENGTH-1) #define HRIR_LENGTH_MASK (HRIR_LENGTH-1)
void GetHrtfCoeffs(ALfloat elevation, ALfloat angle, const ALshort **left, const ALshort **right); void InitHrtf(void);
void GetHrtfCoeffs(ALfloat elevation, ALfloat angle, const ALshort **left, const ALshort **right, ALuint *ldelay, ALuint *rdelay);
void al_print(const char *fname, unsigned int line, const char *fmt, ...) void al_print(const char *fname, unsigned int line, const char *fmt, ...)
PRINTF_STYLE(3,4); PRINTF_STYLE(3,4);

View File

@ -11,6 +11,10 @@
extern "C" { extern "C" {
#endif #endif
#define SRC_HISTORY_BITS (7)
#define SRC_HISTORY_LENGTH (1<<SRC_HISTORY_BITS)
#define SRC_HISTORY_MASK (SRC_HISTORY_LENGTH-1)
typedef enum { typedef enum {
POINT_RESAMPLER = 0, POINT_RESAMPLER = 0,
LINEAR_RESAMPLER, LINEAR_RESAMPLER,
@ -90,20 +94,21 @@ typedef struct ALsource
ALuint SampleSize; ALuint SampleSize;
/* HRTF info */ /* HRTF info */
ALfloat HrtfHistory[MAXCHANNELS][HRTF_LENGTH]; ALfloat HrtfHistory[MAXCHANNELS][SRC_HISTORY_LENGTH][2];
ALuint HrtfOffset; ALuint HrtfOffset;
// Current target parameters used for mixing /* Current target parameters used for mixing */
ALboolean NeedsUpdate;
struct { struct {
ALint Step; ALint Step;
ALfloat HrtfCoeffs[MAXCHANNELS][HRTF_LENGTH][2]; ALfloat HrtfCoeffs[MAXCHANNELS][HRIR_LENGTH][2];
ALuint HrtfDelay[MAXCHANNELS][2];
/* A mixing matrix. First subscript is the channel number of the input /* A mixing matrix. First subscript is the channel number of the input
* data (regardless of channel configuration) and the second is the * data (regardless of channel configuration) and the second is the
* channel target (eg. FRONT_LEFT) */ * channel target (eg. FRONT_LEFT) */
ALfloat DryGains[MAXCHANNELS][MAXCHANNELS]; ALfloat DryGains[MAXCHANNELS][MAXCHANNELS];
FILTER iirFilter; FILTER iirFilter;
ALfloat history[MAXCHANNELS*2]; ALfloat history[MAXCHANNELS*2];
@ -113,6 +118,7 @@ typedef struct ALsource
ALfloat history[MAXCHANNELS]; ALfloat history[MAXCHANNELS];
} Send[MAX_SENDS]; } Send[MAX_SENDS];
} Params; } Params;
ALboolean NeedsUpdate;
ALvoid (*Update)(struct ALsource *self, const ALCcontext *context); ALvoid (*Update)(struct ALsource *self, const ALCcontext *context);
ALvoid (*DoMix)(struct ALsource *self, ALCdevice *Device, ALvoid (*DoMix)(struct ALsource *self, ALCdevice *Device,

View File

@ -1355,8 +1355,11 @@ AL_API ALvoid AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources)
for(j = 0;j < MAXCHANNELS;j++) for(j = 0;j < MAXCHANNELS;j++)
{ {
ALuint k; ALuint k;
for(k = 0;k < HRTF_LENGTH;k++) for(k = 0;k < SRC_HISTORY_LENGTH;k++)
Source->HrtfHistory[j][k] = 0.0f; {
Source->HrtfHistory[j][k][0] = 0.0f;
Source->HrtfHistory[j][k][1] = 0.0f;
}
} }
Source->HrtfOffset = 0; Source->HrtfOffset = 0;
} }