aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/mixer.c
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2011-05-01 13:59:44 -0700
committerChris Robinson <[email protected]>2011-05-01 13:59:44 -0700
commit56d9418119045d536efa3f552af6e13d9353ada9 (patch)
treea1688ab2434cd8f3c1bfdb2c928117b50c357b08 /Alc/mixer.c
parent0109104027ec4de46c236f6520cf00d89dd980ad (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/mixer.c')
-rw-r--r--Alc/mixer.c176
1 files changed, 174 insertions, 2 deletions
diff --git a/Alc/mixer.c b/Alc/mixer.c
index a5300d01..e1ecada8 100644
--- a/Alc/mixer.c
+++ b/Alc/mixer.c
@@ -62,6 +62,171 @@ static __inline ALdouble cubic8(const ALubyte *vals, ALint step, ALint frac)
{ return (cubic(vals[-step], vals[0], vals[step], vals[step+step],
frac * (1.0/FRACTIONONE))-128.0) * (1.0/127.0); }
+#ifdef __GNUC__
+#define LIKELY(x) __builtin_expect(!!(x), 1)
+#define UNLIKELY(x) __builtin_expect(!!(x), 0)
+#else
+#define LIKELY(x) (x)
+#define UNLIKELY(x) (x)
+#endif
+
+#define DECL_TEMPLATE(T, sampler) \
+void Mix_Hrtf_##T##_1_##sampler(ALsource *Source, ALCdevice *Device, \
+ const T *data, ALuint *DataPosInt, ALuint *DataPosFrac, \
+ ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize) \
+{ \
+ ALfloat (*DryBuffer)[MAXCHANNELS]; \
+ ALfloat *ClickRemoval, *PendingClicks; \
+ ALuint pos, frac; \
+ const ALfloat *HrtfCoeffs; \
+ ALfloat *HrtfHistory; \
+ ALuint HrtfOffset; \
+ FILTER *DryFilter; \
+ ALuint BufferIdx; \
+ ALuint increment; \
+ ALuint out, c; \
+ ALfloat value; \
+ \
+ increment = Source->Params.Step; \
+ \
+ DryBuffer = Device->DryBuffer; \
+ ClickRemoval = Device->ClickRemoval; \
+ PendingClicks = Device->PendingClicks; \
+ DryFilter = &Source->Params.iirFilter; \
+ \
+ HrtfCoeffs = &Source->Params.HrtfCoeffs[0][0]; \
+ HrtfHistory = Source->HrtfHistory; \
+ HrtfOffset = Source->HrtfOffset + OutPos; \
+ \
+ pos = 0; \
+ frac = *DataPosFrac; \
+ \
+ if(LIKELY(OutPos == 0)) \
+ { \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter4P(DryFilter, 0, value); \
+ \
+ HrtfHistory[HrtfOffset&HRTF_LENGTH_MASK] = value; \
+ for(c = 0;c < HRTF_LENGTH;c++) \
+ { \
+ ClickRemoval[FRONT_LEFT] -= \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 0]; \
+ ClickRemoval[FRONT_RIGHT] -= \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 1]; \
+ } \
+ } \
+ for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
+ { \
+ /* First order interpolator */ \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter4P(DryFilter, 0, value); \
+ \
+ /* Direct path final mix buffer and panning */ \
+ HrtfHistory[HrtfOffset&HRTF_LENGTH_MASK] = value; \
+ for(c = 0;c < HRTF_LENGTH;c++) \
+ { \
+ DryBuffer[OutPos][FRONT_LEFT] += \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 0]; \
+ DryBuffer[OutPos][FRONT_RIGHT] += \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 1]; \
+ } \
+ HrtfOffset++; \
+ \
+ frac += increment; \
+ pos += frac>>FRACTIONBITS; \
+ frac &= FRACTIONMASK; \
+ OutPos++; \
+ } \
+ if(LIKELY(OutPos == SamplesToDo)) \
+ { \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter4P(DryFilter, 0, value); \
+ \
+ HrtfHistory[HrtfOffset&HRTF_LENGTH_MASK] = value; \
+ for(c = 0;c < HRTF_LENGTH;c++) \
+ { \
+ PendingClicks[FRONT_LEFT] += \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 0]; \
+ PendingClicks[FRONT_RIGHT] += \
+ HrtfHistory[(HrtfOffset-c)&HRTF_LENGTH_MASK] * \
+ HrtfCoeffs[c*2 + 1]; \
+ } \
+ } \
+ \
+ for(out = 0;out < Device->NumAuxSends;out++) \
+ { \
+ ALfloat WetSend; \
+ ALfloat *WetBuffer; \
+ ALfloat *WetClickRemoval; \
+ ALfloat *WetPendingClicks; \
+ FILTER *WetFilter; \
+ \
+ if(!Source->Send[out].Slot || \
+ Source->Send[out].Slot->effect.type == AL_EFFECT_NULL) \
+ continue; \
+ \
+ WetBuffer = Source->Send[out].Slot->WetBuffer; \
+ WetClickRemoval = Source->Send[out].Slot->ClickRemoval; \
+ WetPendingClicks = Source->Send[out].Slot->PendingClicks; \
+ WetFilter = &Source->Params.Send[out].iirFilter; \
+ WetSend = Source->Params.Send[out].WetGain; \
+ \
+ pos = 0; \
+ frac = *DataPosFrac; \
+ OutPos -= BufferSize; \
+ \
+ if(LIKELY(OutPos == 0)) \
+ { \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter2PC(WetFilter, 0, value); \
+ \
+ WetClickRemoval[0] -= value*WetSend; \
+ } \
+ for(BufferIdx = 0;BufferIdx < BufferSize;BufferIdx++) \
+ { \
+ /* First order interpolator */ \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter2P(WetFilter, 0, value); \
+ \
+ /* Room path final mix buffer and panning */ \
+ WetBuffer[OutPos] += value*WetSend; \
+ \
+ frac += increment; \
+ pos += frac>>FRACTIONBITS; \
+ frac &= FRACTIONMASK; \
+ OutPos++; \
+ } \
+ if(LIKELY(OutPos == SamplesToDo)) \
+ { \
+ value = sampler(data+pos, 1, frac); \
+ value = lpFilter2PC(WetFilter, 0, value); \
+ \
+ WetPendingClicks[0] += value*WetSend; \
+ } \
+ } \
+ *DataPosInt += pos; \
+ *DataPosFrac = frac; \
+}
+
+DECL_TEMPLATE(ALfloat, point32)
+DECL_TEMPLATE(ALfloat, lerp32)
+DECL_TEMPLATE(ALfloat, cubic32)
+
+DECL_TEMPLATE(ALshort, point16)
+DECL_TEMPLATE(ALshort, lerp16)
+DECL_TEMPLATE(ALshort, cubic16)
+
+DECL_TEMPLATE(ALubyte, point8)
+DECL_TEMPLATE(ALubyte, lerp8)
+DECL_TEMPLATE(ALubyte, cubic8)
+
+#undef DECL_TEMPLATE
+
#define DECL_TEMPLATE(T, sampler) \
static void Mix_##T##_1_##sampler(ALsource *Source, ALCdevice *Device, \
@@ -401,8 +566,14 @@ static void Mix_##T##_##sampler(ALsource *Source, ALCdevice *Device, \
switch(FmtChannels) \
{ \
case FmtMono: \
- Mix_##T##_1_##sampler(Source, Device, Data, DataPosInt, DataPosFrac, \
- OutPos, SamplesToDo, BufferSize); \
+ if(Device->UseHRTF) \
+ Mix_Hrtf_##T##_1_##sampler(Source, Device, Data, \
+ DataPosInt, DataPosFrac, \
+ OutPos, SamplesToDo, BufferSize); \
+ else \
+ Mix_##T##_1_##sampler(Source, Device, Data, \
+ DataPosInt, DataPosFrac, \
+ OutPos, SamplesToDo, BufferSize); \
break; \
case FmtStereo: \
case FmtRear: \
@@ -805,4 +976,5 @@ ALvoid MixSource(ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
Source->position = DataPosInt;
Source->position_fraction = DataPosFrac;
Source->Buffer = BufferListItem->buffer;
+ Source->HrtfOffset += OutPos;
}