zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mixer_inc.c
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include "alMain.h"
4 #include "alSource.h"
5 #include "mixer_defs.h"
6 
7 #ifdef __GNUC__
8 #define LIKELY(x) __builtin_expect(!!(x), 1)
9 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
10 #else
11 #define LIKELY(x) (x)
12 #define UNLIKELY(x) (x)
13 #endif
14 
15 #define REAL_MERGE2(a,b) a##b
16 #define MERGE2(a,b) REAL_MERGE2(a,b)
17 
18 #define MixDirect_Hrtf MERGE2(MixDirect_Hrtf_,SUFFIX)
19 
20 
21 static __inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*RESTRICT Values)[2],
22  const ALuint irSize,
23  ALfloat (*RESTRICT Coeffs)[2],
24  const ALfloat (*RESTRICT CoeffStep)[2],
26 static __inline void ApplyCoeffs(ALuint Offset, ALfloat (*RESTRICT Values)[2],
27  const ALuint irSize,
28  ALfloat (*RESTRICT Coeffs)[2],
30 
31 
32 void MixDirect_Hrtf(const DirectParams *params, const ALfloat *RESTRICT data, ALuint srcchan,
33  ALuint OutPos, ALuint SamplesToDo, ALuint BufferSize)
34 {
35  ALfloat (*RESTRICT DryBuffer)[BUFFERSIZE] = params->OutBuffer;
36  ALfloat *RESTRICT ClickRemoval = params->ClickRemoval;
37  ALfloat *RESTRICT PendingClicks = params->PendingClicks;
38  const ALuint IrSize = params->Hrtf.Params.IrSize;
39  const ALint *RESTRICT DelayStep = params->Hrtf.Params.DelayStep;
40  const ALfloat (*RESTRICT CoeffStep)[2] = params->Hrtf.Params.CoeffStep;
41  const ALfloat (*RESTRICT TargetCoeffs)[2] = params->Hrtf.Params.Coeffs[srcchan];
42  const ALuint *RESTRICT TargetDelay = params->Hrtf.Params.Delay[srcchan];
43  ALfloat *RESTRICT History = params->Hrtf.State->History[srcchan];
44  ALfloat (*RESTRICT Values)[2] = params->Hrtf.State->Values[srcchan];
45  ALint Counter = maxu(params->Hrtf.State->Counter, OutPos) - OutPos;
46  ALuint Offset = params->Hrtf.State->Offset + OutPos;
47  ALIGN(16) ALfloat Coeffs[HRIR_LENGTH][2];
48  ALuint Delay[2];
50  ALuint pos;
51  ALuint c;
52 
53  pos = 0;
54  for(c = 0;c < IrSize;c++)
55  {
56  Coeffs[c][0] = TargetCoeffs[c][0] - (CoeffStep[c][0]*Counter);
57  Coeffs[c][1] = TargetCoeffs[c][1] - (CoeffStep[c][1]*Counter);
58  }
59 
60  Delay[0] = TargetDelay[0] - (DelayStep[0]*Counter);
61  Delay[1] = TargetDelay[1] - (DelayStep[1]*Counter);
62 
63  if(LIKELY(OutPos == 0))
64  {
65  History[Offset&SRC_HISTORY_MASK] = data[pos];
66  left = lerp(History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
67  History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
68  (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
69  right = lerp(History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
70  History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
71  (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
72 
73  ClickRemoval[FrontLeft] -= Values[(Offset+1)&HRIR_MASK][0] +
74  Coeffs[0][0] * left;
75  ClickRemoval[FrontRight] -= Values[(Offset+1)&HRIR_MASK][1] +
76  Coeffs[0][1] * right;
77  }
78  for(pos = 0;pos < BufferSize && Counter > 0;pos++)
79  {
80  History[Offset&SRC_HISTORY_MASK] = data[pos];
81  left = lerp(History[(Offset-(Delay[0]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
82  History[(Offset-(Delay[0]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
83  (Delay[0]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
84  right = lerp(History[(Offset-(Delay[1]>>HRTFDELAY_BITS))&SRC_HISTORY_MASK],
85  History[(Offset-(Delay[1]>>HRTFDELAY_BITS)-1)&SRC_HISTORY_MASK],
86  (Delay[1]&HRTFDELAY_MASK)*(1.0f/HRTFDELAY_FRACONE));
87 
88  Delay[0] += DelayStep[0];
89  Delay[1] += DelayStep[1];
90 
91  Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
92  Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
93  Offset++;
94 
95  ApplyCoeffsStep(Offset, Values, IrSize, Coeffs, CoeffStep, left, right);
96  DryBuffer[FrontLeft][OutPos] += Values[Offset&HRIR_MASK][0];
97  DryBuffer[FrontRight][OutPos] += Values[Offset&HRIR_MASK][1];
98 
99  OutPos++;
100  Counter--;
101  }
102 
103  Delay[0] >>= HRTFDELAY_BITS;
104  Delay[1] >>= HRTFDELAY_BITS;
105  for(;pos < BufferSize;pos++)
106  {
107  History[Offset&SRC_HISTORY_MASK] = data[pos];
108  left = History[(Offset-Delay[0])&SRC_HISTORY_MASK];
109  right = History[(Offset-Delay[1])&SRC_HISTORY_MASK];
110 
111  Values[(Offset+IrSize)&HRIR_MASK][0] = 0.0f;
112  Values[(Offset+IrSize)&HRIR_MASK][1] = 0.0f;
113  Offset++;
114 
115  ApplyCoeffs(Offset, Values, IrSize, Coeffs, left, right);
116  DryBuffer[FrontLeft][OutPos] += Values[Offset&HRIR_MASK][0];
117  DryBuffer[FrontRight][OutPos] += Values[Offset&HRIR_MASK][1];
118 
119  OutPos++;
120  }
121  if(LIKELY(OutPos == SamplesToDo))
122  {
123  History[Offset&SRC_HISTORY_MASK] = data[pos];
124  left = History[(Offset-Delay[0])&SRC_HISTORY_MASK];
125  right = History[(Offset-Delay[1])&SRC_HISTORY_MASK];
126 
127  PendingClicks[FrontLeft] += Values[(Offset+1)&HRIR_MASK][0] +
128  Coeffs[0][0] * left;
129  PendingClicks[FrontRight] += Values[(Offset+1)&HRIR_MASK][1] +
130  Coeffs[0][1] * right;
131  }
132 }
133 
134 
135 #undef MixDirect_Hrtf
136 
137 #undef MERGE2
138 #undef REAL_MERGE2
139 
140 #undef UNLIKELY
141 #undef LIKELY
ALfloat(* OutBuffer)[BUFFERSIZE]
Definition: alSource.h:50
ALuint IrSize
Definition: alSource.h:46
GLint left
Definition: glew.h:7291
GLclampf f
Definition: glew.h:3390
static __inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
Definition: alu.h:90
#define HRTFDELAY_BITS
Definition: alMain.h:778
int ALint
Definition: al.h:56
ALint DelayStep[2]
Definition: alSource.h:45
ALuint Delay[MaxChannels][2]
Definition: alSource.h:44
#define HRIR_LENGTH
Definition: alMain.h:776
float ALfloat
Definition: al.h:68
static __inline void ApplyCoeffs(ALuint Offset, ALfloat(*RESTRICT Values)[2], const ALuint irSize, ALfloat(*RESTRICT Coeffs)[2], ALfloat left, ALfloat right)
#define SRC_HISTORY_MASK
Definition: alSource.h:16
#define HRTFDELAY_FRACONE
Definition: alMain.h:779
GLenum GLvoid ** params
Definition: gl2ext.h:806
#define MixDirect_Hrtf
Definition: mixer_inc.c:18
#define HRTFDELAY_MASK
Definition: alMain.h:780
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
const GLfloat * c
Definition: glew.h:14913
HrtfParams Params
Definition: alSource.h:55
#define LIKELY(x)
Definition: mixer_inc.c:11
unsigned int ALuint
Definition: al.h:59
static __inline void ApplyCoeffsStep(ALuint Offset, ALfloat(*RESTRICT Values)[2], const ALuint irSize, ALfloat(*RESTRICT Coeffs)[2], const ALfloat(*RESTRICT CoeffStep)[2], ALfloat left, ALfloat right)
ALfloat * PendingClicks
Definition: alSource.h:52
struct DirectParams::@60 Hrtf
ALuint Counter
Definition: alSource.h:33
HrtfState * State
Definition: alSource.h:56
#define HRIR_MASK
Definition: alMain.h:777
GLfloat right
Definition: glew.h:13816
#define BUFFERSIZE
Definition: alMain.h:556
static __inline ALuint maxu(ALuint a, ALuint b)
Definition: alu.h:63
ALfloat * ClickRemoval
Definition: alSource.h:51
ALuint Offset
Definition: alSource.h:36