Mercurial > mplayer.hg
annotate libaf/af_hrtf.c @ 15082:71570687c1a3
HRTF filter updates:
- Bass compensation gain corrected (which was set too low), now the
sound should be even more transparent
- A (unified) dual axes active matrix decoder with adaptive steering
- capable of decoding matrix surround encoded inputs, with stereo rear
- capable of decoding matrix encoded rear center
- Purely stereo mixing without unneccessary rear filter calculations
- The decoding structure message is moved, because at the old place it
gave incorrect messages.
Patch by Yue Shi Lai <ylai@users.sourceforge.net>
author | henry |
---|---|
date | Sun, 10 Apr 2005 08:47:16 +0000 |
parents | 0a22a046f0d3 |
children | e267fad254fb |
rev | line source |
---|---|
13996 | 1 /* Experimental audio filter that mixes 5.1 and 5.1 with matrix |
2 encoded rear channels into headphone signal using FIR filtering | |
3 with HRTF. | |
4 */ | |
5 //#include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <string.h> | |
8 #include <unistd.h> | |
9 #include <inttypes.h> | |
10 | |
11 #include <math.h> | |
12 | |
13 #include "af.h" | |
14 #include "dsp.h" | |
15 | |
16 /* HRTF filter coefficients and adjustable parameters */ | |
17 #include "af_hrtf.h" | |
18 | |
19 typedef struct af_hrtf_s { | |
20 /* Lengths */ | |
21 int dlbuflen, hrflen, basslen; | |
22 /* L, C, R, Ls, Rs channels */ | |
23 float *lf, *rf, *lr, *rr, *cf, *cr; | |
24 float *cf_ir, *af_ir, *of_ir, *ar_ir, *or_ir, *cr_ir; | |
25 int cf_o, af_o, of_o, ar_o, or_o, cr_o; | |
26 /* Bass */ | |
27 float *ba_l, *ba_r; | |
28 float *ba_ir; | |
29 /* Whether to matrix decode the rear center channel */ | |
30 int matrix_mode; | |
15082 | 31 /* How to decode the input: |
32 0 = 5/5+1 channels | |
33 1 = 2 channels | |
34 2 = matrix encoded 2 channels */ | |
35 int decode_mode; | |
36 /* Full wave rectified (FWR) amplitudes and gain used to steer the | |
37 active matrix decoding of front channels (variable names | |
38 lpr/lmr means Lt + Rt, Lt - Rt) */ | |
39 float l_fwr, r_fwr, lpr_fwr, lmr_fwr; | |
40 float adapt_l_gain, adapt_r_gain, adapt_lpr_gain, adapt_lmr_gain; | |
41 /* Matrix input decoding require special FWR buffer, since the | |
42 decoding is done in place. */ | |
43 float *fwrbuf_l, *fwrbuf_r, *fwrbuf_lr, *fwrbuf_rr; | |
44 /* Rear channel delay buffer for matrix decoding */ | |
45 float *rear_dlbuf; | |
46 /* Full wave rectified amplitude and gain used to steer the active | |
47 matrix decoding of center rear channel */ | |
48 float lr_fwr, rr_fwr, lrprr_fwr, lrmrr_fwr; | |
49 float adapt_lr_gain, adapt_rr_gain; | |
50 float adapt_lrprr_gain, adapt_lrmrr_gain; | |
13996 | 51 /* Cyclic position on the ring buffer */ |
52 int cyc_pos; | |
15082 | 53 int print_flag; |
13996 | 54 } af_hrtf_t; |
55 | |
56 /* Convolution on a ring buffer | |
57 * nx: length of the ring buffer | |
58 * nk: length of the convolution kernel | |
59 * sx: ring buffer | |
60 * sk: convolution kernel | |
61 * offset: offset on the ring buffer, can be | |
62 */ | |
63 static float conv(const int nx, const int nk, float *sx, float *sk, | |
64 const int offset) | |
65 { | |
66 /* k = reminder of offset / nx */ | |
67 int k = offset >= 0 ? offset % nx : nx + (offset % nx); | |
68 | |
69 if(nk + k <= nx) | |
14275
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
70 return af_filter_fir(nk, sx + k, sk); |
13996 | 71 else |
14275
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
72 return af_filter_fir(nk + k - nx, sx, sk + nx - k) + |
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
73 af_filter_fir(nx - k, sx + k, sk); |
13996 | 74 } |
75 | |
76 /* Detect when the impulse response starts (significantly) */ | |
77 int pulse_detect(float *sx) | |
78 { | |
79 /* nmax must be the reference impulse response length (128) minus | |
80 s->hrflen */ | |
81 const int nmax = 128 - HRTFFILTLEN; | |
82 const float thresh = IRTHRESH; | |
83 int i; | |
84 | |
85 for(i = 0; i < nmax; i++) | |
86 if(fabs(sx[i]) > thresh) | |
87 return i; | |
88 return 0; | |
89 } | |
90 | |
15082 | 91 /* Unified active matrix decoder for 2 channel matrix encoded surround |
92 sources */ | |
93 inline void matrix_decode(short *in, const int k, const int il, | |
94 const int ir, const int decode_rear, | |
95 const int dlbuflen, | |
96 float l_fwr, float r_fwr, | |
97 float lpr_fwr, float lmr_fwr, | |
98 float *adapt_l_gain, float *adapt_r_gain, | |
99 float *adapt_lpr_gain, float *adapt_lmr_gain, | |
100 float *lf, float *rf, float *lr, | |
101 float *rr, float *cf) | |
102 { | |
103 const int kr = (k + MATREARDELAY) % dlbuflen; | |
104 float l_gain = (l_fwr + r_fwr) / | |
105 (1 + l_fwr + l_fwr); | |
106 float r_gain = (l_fwr + r_fwr) / | |
107 (1 + r_fwr + r_fwr); | |
108 float lpr_gain = (lpr_fwr + lmr_fwr) / | |
109 (1 + lpr_fwr + lpr_fwr); | |
110 float lmr_gain = (lpr_fwr + lmr_fwr) / | |
111 (1 + lmr_fwr + lmr_fwr); | |
112 float lpr, lmr; | |
113 float l_agc, r_agc, lpr_agc, lmr_agc; | |
114 float f, d_gain; | |
115 | |
116 #if 0 | |
117 static int counter = 0; | |
118 static FILE *fp_out; | |
119 | |
120 if(counter == 0) | |
121 fp_out = fopen("af_hrtf.log", "w"); | |
122 fprintf(fp_out, "%g %g %g %g %g ", counter * (1.0 / 48000), | |
123 l_gain, r_gain, lpr_gain, lmr_gain); | |
124 #endif | |
125 | |
126 /*** AXIS NO. 1: (Lt, Rt) -> (C, Ls, Rs) ***/ | |
127 /* AGC adaption */ | |
128 d_gain = (fabs(l_gain - *adapt_l_gain) + | |
129 fabs(r_gain - *adapt_r_gain)) * 0.5; | |
130 f = d_gain * (1.0 / MATAGCTRIG); | |
131 f = MATAGCDECAY - MATAGCDECAY / (1 + f * f); | |
132 *adapt_l_gain = (1 - f) * *adapt_l_gain + f * l_gain; | |
133 *adapt_r_gain = (1 - f) * *adapt_r_gain + f * r_gain; | |
134 /* Matrix */ | |
135 l_agc = in[il] * *adapt_l_gain; | |
136 r_agc = in[ir] * *adapt_r_gain; | |
137 cf[k] = (l_agc + r_agc) * M_SQRT1_2; | |
138 if(decode_rear) { | |
139 lr[kr] = rr[kr] = (l_agc - r_agc) * M_SQRT1_2; | |
140 /* Stereo rear channel is steered with the same AGC steering as | |
141 the decoding matrix. Note this requires a fast updating AGC | |
142 at the order of 20 ms (which is the case here). */ | |
143 lr[kr] *= (l_fwr + l_fwr) / | |
144 (1 + l_fwr + r_fwr); | |
145 rr[kr] *= (r_fwr + r_fwr) / | |
146 (1 + l_fwr + r_fwr); | |
147 } | |
148 | |
149 /*** AXIS NO. 2: (Lt + Rt, Lt - Rt) -> (L, R) ***/ | |
150 lpr = (in[il] + in[ir]) * M_SQRT1_2; | |
151 lmr = (in[il] - in[ir]) * M_SQRT1_2; | |
152 /* AGC adaption */ | |
153 d_gain = (fabs(lpr_gain - *adapt_lpr_gain) + | |
154 fabs(lmr_gain - *adapt_lmr_gain)) * 0.5; | |
155 f = d_gain * (1.0 / MATAGCTRIG); | |
156 f = MATAGCDECAY - MATAGCDECAY / (1 + f * f); | |
157 *adapt_lpr_gain = (1 - f) * *adapt_lpr_gain + f * lpr_gain; | |
158 *adapt_lmr_gain = (1 - f) * *adapt_lmr_gain + f * lmr_gain; | |
159 /* The 2nd axis has strong gain fluctuations, and therefore require | |
160 limits. The factor is tricky. I think 2 is the reasonable | |
161 value here, which phase inverts the L, R channel if Lt, Rt is | |
162 strongly correlated (e.g. during dialogues) (1 would inhibit the | |
163 steering behavior, > 4 appears to result in distortions). */ | |
164 if(*adapt_lmr_gain > 2 * *adapt_lpr_gain) | |
165 *adapt_lmr_gain = 2 * *adapt_lpr_gain; | |
166 /* Matrix */ | |
167 lpr_agc = lpr * *adapt_lpr_gain; | |
168 lmr_agc = lmr * *adapt_lmr_gain; | |
169 lf[k] = (lpr_agc + lmr_agc) * M_SQRT1_2; | |
170 rf[k] = (lpr_agc - lmr_agc) * M_SQRT1_2; | |
171 | |
172 #if 0 | |
173 fprintf(fp_out, "%g %g %g %g\n", | |
174 *adapt_l_gain, *adapt_r_gain, | |
175 *adapt_lpr_gain, *adapt_lmr_gain); | |
176 counter++; | |
177 #endif | |
178 } | |
179 | |
13996 | 180 inline void update_ch(af_hrtf_t *s, short *in, const int k) |
181 { | |
15082 | 182 const int fwr_pos = (k + FWRDURATION) % s->dlbuflen; |
183 /* Update the full wave rectified total amplitude */ | |
184 /* Input matrix decoder */ | |
185 if(s->decode_mode == HRTF_MIX_MATRIX2CH) { | |
186 s->l_fwr += abs(in[0]) - fabs(s->fwrbuf_l[fwr_pos]); | |
187 s->r_fwr += abs(in[1]) - fabs(s->fwrbuf_r[fwr_pos]); | |
188 s->lpr_fwr += abs(in[0] + in[1]) - | |
189 fabs(s->fwrbuf_l[fwr_pos] + s->fwrbuf_r[fwr_pos]); | |
190 s->lmr_fwr += abs(in[0] - in[1]) - | |
191 fabs(s->fwrbuf_l[fwr_pos] - s->fwrbuf_r[fwr_pos]); | |
192 } | |
193 /* Rear matrix decoder */ | |
194 if(s->matrix_mode) { | |
195 s->lr_fwr += abs(in[2]) - fabs(s->fwrbuf_lr[fwr_pos]); | |
196 s->rr_fwr += abs(in[3]) - fabs(s->fwrbuf_rr[fwr_pos]); | |
197 s->lrprr_fwr += abs(in[2] + in[3]) - | |
198 fabs(s->fwrbuf_lr[fwr_pos] + s->fwrbuf_rr[fwr_pos]); | |
199 s->lrmrr_fwr += abs(in[2] - in[3]) - | |
200 fabs(s->fwrbuf_lr[fwr_pos] - s->fwrbuf_rr[fwr_pos]); | |
201 } | |
13996 | 202 |
15082 | 203 switch (s->decode_mode) { |
204 case HRTF_MIX_51: | |
205 /* 5/5+1 channel sources */ | |
206 s->lf[k] = in[0]; | |
207 s->cf[k] = in[4]; | |
208 s->rf[k] = in[1]; | |
209 s->fwrbuf_lr[k] = s->lr[k] = in[2]; | |
210 s->fwrbuf_rr[k] = s->rr[k] = in[3]; | |
211 break; | |
212 case HRTF_MIX_MATRIX2CH: | |
213 /* Matrix encoded 2 channel sources */ | |
214 s->fwrbuf_l[k] = in[0]; | |
215 s->fwrbuf_r[k] = in[1]; | |
216 matrix_decode(in, k, 0, 1, 1, s->dlbuflen, | |
217 s->l_fwr, s->r_fwr, | |
218 s->lpr_fwr, s->lmr_fwr, | |
219 &(s->adapt_l_gain), &(s->adapt_r_gain), | |
220 &(s->adapt_lpr_gain), &(s->adapt_lmr_gain), | |
221 s->lf, s->rf, s->lr, s->rr, s->cf); | |
222 break; | |
223 case HRTF_MIX_STEREO: | |
224 /* Stereo sources */ | |
225 s->lf[k] = in[0]; | |
226 s->rf[k] = in[1]; | |
227 s->cf[k] = s->lr[k] = s->rr[k] = 0; | |
228 break; | |
229 } | |
13996 | 230 |
15082 | 231 /* We need to update the bass compensation delay line, too. */ |
13996 | 232 s->ba_l[k] = in[0] + in[4] + in[2]; |
233 s->ba_r[k] = in[4] + in[1] + in[3]; | |
234 } | |
235 | |
236 /* Initialization and runtime control */ | |
237 static int control(struct af_instance_s *af, int cmd, void* arg) | |
238 { | |
239 af_hrtf_t *s = af->setup; | |
240 char mode; | |
241 | |
242 switch(cmd) { | |
243 case AF_CONTROL_REINIT: | |
244 af->data->rate = ((af_data_t*)arg)->rate; | |
245 if(af->data->rate != 48000) { | |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
13996
diff
changeset
|
246 // automatic samplerate adjustment in the filter chain |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
13996
diff
changeset
|
247 // is not yet supported. |
13996 | 248 af_msg(AF_MSG_ERROR, |
249 "[hrtf] ERROR: Sampling rate is not 48000 Hz (%d)!\n", | |
250 af->data->rate); | |
251 return AF_ERROR; | |
252 } | |
253 af->data->nch = ((af_data_t*)arg)->nch; | |
254 if(af->data->nch < 5) { | |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
13996
diff
changeset
|
255 af->data->nch = 5; |
15082 | 256 if(af->data->nch == 2) { |
257 /* 2 channel input */ | |
258 if(s->decode_mode != HRTF_MIX_MATRIX2CH) { | |
259 /* Default behavior is stereo mixing. */ | |
260 s->decode_mode = HRTF_MIX_STEREO; | |
261 } | |
262 } | |
13996 | 263 } |
14245 | 264 af->data->format = AF_FORMAT_S16_NE; |
13996 | 265 af->data->bps = 2; |
15082 | 266 s->print_flag = 1; |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
13996
diff
changeset
|
267 return af_test_output(af, (af_data_t*)arg); |
13996 | 268 case AF_CONTROL_COMMAND_LINE: |
269 sscanf((char*)arg, "%c", &mode); | |
270 switch(mode) { | |
271 case 'm': | |
15082 | 272 /* Use matrix rear decoding. */ |
13996 | 273 s->matrix_mode = 1; |
274 break; | |
15082 | 275 case 's': |
276 /* Input needs matrix decoding. */ | |
277 s->decode_mode = HRTF_MIX_MATRIX2CH; | |
278 break; | |
13996 | 279 case '0': |
280 s->matrix_mode = 0; | |
281 break; | |
282 default: | |
283 af_msg(AF_MSG_ERROR, | |
15082 | 284 "[hrtf] Mode is neither 'm', 's', nor '0' (%c).\n", |
13996 | 285 mode); |
286 return AF_ERROR; | |
287 } | |
15082 | 288 s->print_flag = 1; |
13996 | 289 return AF_OK; |
290 } | |
291 | |
292 return AF_UNKNOWN; | |
293 } | |
294 | |
295 /* Deallocate memory */ | |
296 static void uninit(struct af_instance_s *af) | |
297 { | |
298 if(af->setup) { | |
299 af_hrtf_t *s = af->setup; | |
300 | |
301 if(s->lf) | |
302 free(s->lf); | |
303 if(s->rf) | |
304 free(s->rf); | |
305 if(s->lr) | |
306 free(s->lr); | |
307 if(s->rr) | |
308 free(s->rr); | |
309 if(s->cf) | |
310 free(s->cf); | |
311 if(s->cr) | |
312 free(s->cr); | |
313 if(s->ba_l) | |
314 free(s->ba_l); | |
315 if(s->ba_r) | |
316 free(s->ba_r); | |
317 if(s->ba_ir) | |
318 free(s->ba_ir); | |
15082 | 319 if(s->fwrbuf_l) |
320 free(s->fwrbuf_l); | |
321 if(s->fwrbuf_r) | |
322 free(s->fwrbuf_r); | |
323 if(s->fwrbuf_lr) | |
324 free(s->fwrbuf_lr); | |
325 if(s->fwrbuf_rr) | |
326 free(s->fwrbuf_rr); | |
13996 | 327 free(af->setup); |
328 } | |
329 if(af->data) | |
330 free(af->data); | |
331 } | |
332 | |
333 /* Filter data through filter | |
334 | |
335 Two "tricks" are used to compensate the "color" of the KEMAR data: | |
336 | |
337 1. The KEMAR data is refiltered to ensure that the front L, R channels | |
338 on the same side of the ear are equalized (especially in the high | |
339 frequencies). | |
340 | |
341 2. A bass compensation is introduced to ensure that 0-200 Hz are not | |
342 damped (without any real 3D acoustical image, however). | |
343 */ | |
344 static af_data_t* play(struct af_instance_s *af, af_data_t *data) | |
345 { | |
346 af_hrtf_t *s = af->setup; | |
347 short *in = data->audio; // Input audio data | |
348 short *out = NULL; // Output audio data | |
349 short *end = in + data->len / sizeof(short); // Loop end | |
350 float common, left, right, diff, left_b, right_b; | |
351 const int dblen = s->dlbuflen, hlen = s->hrflen, blen = s->basslen; | |
352 | |
353 if(AF_OK != RESIZE_LOCAL_BUFFER(af, data)) | |
354 return NULL; | |
355 | |
15082 | 356 if(s->print_flag) { |
357 s->print_flag = 0; | |
358 switch (s->decode_mode) { | |
359 case HRTF_MIX_51: | |
360 af_msg(AF_MSG_INFO, | |
361 "[hrtf] Using HRTF to mix %s discrete surround into " | |
362 "L, R channels\n", s->matrix_mode ? "5+1" : "5"); | |
363 break; | |
364 case HRTF_MIX_STEREO: | |
365 af_msg(AF_MSG_INFO, | |
366 "[hrtf] Using HRTF to mix stereo into " | |
367 "L, R channels\n"); | |
368 break; | |
369 case HRTF_MIX_MATRIX2CH: | |
370 af_msg(AF_MSG_INFO, | |
371 "[hrtf] Using active matrix to decode 2 channel " | |
372 "input, HRTF to mix %s matrix surround into " | |
373 "L, R channels\n", "3/2"); | |
374 break; | |
375 default: | |
376 af_msg(AF_MSG_WARN, | |
377 "[hrtf] bogus decode_mode: %d\n", s->decode_mode); | |
378 break; | |
379 } | |
380 | |
381 if(s->matrix_mode) | |
382 af_msg(AF_MSG_INFO, | |
383 "[hrtf] Using active matrix to decode rear center " | |
384 "channel\n"); | |
385 } | |
386 | |
13996 | 387 out = af->data->audio; |
388 | |
389 /* MPlayer's 5 channel layout (notation for the variable): | |
390 * | |
391 * 0: L (LF), 1: R (RF), 2: Ls (LR), 3: Rs (RR), 4: C (CF), matrix | |
392 * encoded: Cs (CR) | |
393 * | |
394 * or: L = left, C = center, R = right, F = front, R = rear | |
395 * | |
396 * Filter notation: | |
397 * | |
398 * CF | |
399 * OF AF | |
400 * Ear-> | |
401 * OR AR | |
402 * CR | |
403 * | |
404 * or: C = center, A = same side, O = opposite, F = front, R = rear | |
405 */ | |
406 | |
407 while(in < end) { | |
408 const int k = s->cyc_pos; | |
409 | |
410 update_ch(s, in, k); | |
411 | |
412 /* Simulate a 7.5 ms -20 dB echo of the center channel in the | |
413 front channels (like reflection from a room wall) - a kind of | |
414 psycho-acoustically "cheating" to focus the center front | |
415 channel, which is normally hard to be perceived as front */ | |
416 s->lf[k] += CFECHOAMPL * s->cf[(k + CFECHODELAY) % s->dlbuflen]; | |
417 s->rf[k] += CFECHOAMPL * s->cf[(k + CFECHODELAY) % s->dlbuflen]; | |
418 | |
15082 | 419 switch (s->decode_mode) { |
420 case HRTF_MIX_51: | |
421 case HRTF_MIX_MATRIX2CH: | |
422 /* Mixer filter matrix */ | |
423 common = conv(dblen, hlen, s->cf, s->cf_ir, k + s->cf_o); | |
424 if(s->matrix_mode) { | |
425 /* In matrix decoding mode, the rear channel gain must be | |
426 renormalized, as there is an additional channel. */ | |
427 matrix_decode(in, k, 2, 3, 0, s->dlbuflen, | |
428 s->lr_fwr, s->rr_fwr, | |
429 s->lrprr_fwr, s->lrmrr_fwr, | |
430 &(s->adapt_lr_gain), &(s->adapt_rr_gain), | |
431 &(s->adapt_lrprr_gain), &(s->adapt_lrmrr_gain), | |
432 s->lr, s->rr, NULL, NULL, s->cr); | |
433 common += | |
434 conv(dblen, hlen, s->cr, s->cr_ir, k + s->cr_o) * | |
435 M1_76DB; | |
436 left = | |
437 ( conv(dblen, hlen, s->lf, s->af_ir, k + s->af_o) + | |
438 conv(dblen, hlen, s->rf, s->of_ir, k + s->of_o) + | |
439 (conv(dblen, hlen, s->lr, s->ar_ir, k + s->ar_o) + | |
440 conv(dblen, hlen, s->rr, s->or_ir, k + s->or_o)) * | |
441 M1_76DB + common); | |
442 right = | |
443 ( conv(dblen, hlen, s->rf, s->af_ir, k + s->af_o) + | |
444 conv(dblen, hlen, s->lf, s->of_ir, k + s->of_o) + | |
445 (conv(dblen, hlen, s->rr, s->ar_ir, k + s->ar_o) + | |
446 conv(dblen, hlen, s->lr, s->or_ir, k + s->or_o)) * | |
447 M1_76DB + common); | |
448 } else { | |
449 left = | |
450 ( conv(dblen, hlen, s->lf, s->af_ir, k + s->af_o) + | |
451 conv(dblen, hlen, s->rf, s->of_ir, k + s->of_o) + | |
452 conv(dblen, hlen, s->lr, s->ar_ir, k + s->ar_o) + | |
453 conv(dblen, hlen, s->rr, s->or_ir, k + s->or_o) + | |
454 common); | |
455 right = | |
456 ( conv(dblen, hlen, s->rf, s->af_ir, k + s->af_o) + | |
457 conv(dblen, hlen, s->lf, s->of_ir, k + s->of_o) + | |
458 conv(dblen, hlen, s->rr, s->ar_ir, k + s->ar_o) + | |
459 conv(dblen, hlen, s->lr, s->or_ir, k + s->or_o) + | |
460 common); | |
461 } | |
462 break; | |
463 case HRTF_MIX_STEREO: | |
464 left = | |
465 ( conv(dblen, hlen, s->lf, s->af_ir, k + s->af_o) + | |
466 conv(dblen, hlen, s->rf, s->of_ir, k + s->of_o)); | |
467 right = | |
468 ( conv(dblen, hlen, s->rf, s->af_ir, k + s->af_o) + | |
469 conv(dblen, hlen, s->lf, s->of_ir, k + s->of_o)); | |
470 break; | |
471 default: | |
472 /* make gcc happy */ | |
473 left = 0.0; | |
474 right = 0.0; | |
475 break; | |
13996 | 476 } |
477 | |
478 /* Bass compensation for the lower frequency cut of the HRTF. A | |
479 cross talk of the left and right channel is introduced to | |
480 match the directional characteristics of higher frequencies. | |
481 The bass will not have any real 3D perception, but that is | |
15082 | 482 OK (note at 180 Hz, the wavelength is about 2 m, and any |
483 spatial perception is impossible). */ | |
13996 | 484 left_b = conv(dblen, blen, s->ba_l, s->ba_ir, k); |
485 right_b = conv(dblen, blen, s->ba_r, s->ba_ir, k); | |
486 left += (1 - BASSCROSS) * left_b + BASSCROSS * right_b; | |
487 right += (1 - BASSCROSS) * right_b + BASSCROSS * left_b; | |
488 /* Also mix the LFE channel (if available) */ | |
489 if(af->data->nch >= 6) { | |
490 left += out[5] * M3_01DB; | |
491 right += out[5] * M3_01DB; | |
492 } | |
493 | |
494 /* Amplitude renormalization. */ | |
495 left *= AMPLNORM; | |
496 right *= AMPLNORM; | |
497 | |
15082 | 498 switch (s->decode_mode) { |
499 case HRTF_MIX_51: | |
500 case HRTF_MIX_STEREO: | |
501 /* "Cheating": linear stereo expansion to amplify the 3D | |
502 perception. Note: Too much will destroy the acoustic space | |
503 and may even result in headaches. */ | |
504 diff = STEXPAND2 * (left - right); | |
505 out[0] = (int16_t)(left + diff); | |
506 out[1] = (int16_t)(right - diff); | |
507 break; | |
508 case HRTF_MIX_MATRIX2CH: | |
509 /* Do attempt any stereo expansion with matrix encoded | |
510 sources. The L, R channels are already stereo expanded | |
511 by the steering, any further stereo expansion will sound | |
512 very unnatural. */ | |
513 out[0] = (int16_t)left; | |
514 out[1] = (int16_t)right; | |
515 break; | |
516 } | |
13996 | 517 |
518 /* The remaining channels are not needed any more */ | |
519 out[2] = out[3] = out[4] = 0; | |
520 if(af->data->nch >= 6) | |
521 out[5] = 0; | |
522 | |
523 /* Next sample... */ | |
524 in = &in[data->nch]; | |
525 out = &out[af->data->nch]; | |
526 (s->cyc_pos)--; | |
527 if(s->cyc_pos < 0) | |
528 s->cyc_pos += dblen; | |
529 } | |
530 | |
531 /* Set output data */ | |
532 data->audio = af->data->audio; | |
533 data->len = (data->len * af->mul.n) / af->mul.d; | |
534 data->nch = af->data->nch; | |
535 | |
536 return data; | |
537 } | |
538 | |
539 static int allocate(af_hrtf_t *s) | |
540 { | |
541 if ((s->lf = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
542 if ((s->rf = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
543 if ((s->lr = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
544 if ((s->rr = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
545 if ((s->cf = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
546 if ((s->cr = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
547 if ((s->ba_l = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
548 if ((s->ba_r = malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
15082 | 549 if ((s->fwrbuf_l = |
550 malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
551 if ((s->fwrbuf_r = | |
552 malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
553 if ((s->fwrbuf_lr = | |
554 malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
555 if ((s->fwrbuf_rr = | |
556 malloc(s->dlbuflen * sizeof(float))) == NULL) return -1; | |
13996 | 557 return 0; |
558 } | |
559 | |
560 /* Allocate memory and set function pointers */ | |
561 static int open(af_instance_t* af) | |
562 { | |
563 int i; | |
564 af_hrtf_t *s; | |
565 float fc; | |
566 | |
567 af->control = control; | |
568 af->uninit = uninit; | |
569 af->play = play; | |
570 af->mul.n = 1; | |
571 af->mul.d = 1; | |
572 af->data = calloc(1, sizeof(af_data_t)); | |
573 af->setup = calloc(1, sizeof(af_hrtf_t)); | |
574 if((af->data == NULL) || (af->setup == NULL)) | |
575 return AF_ERROR; | |
576 | |
577 s = af->setup; | |
578 | |
579 s->dlbuflen = DELAYBUFLEN; | |
580 s->hrflen = HRTFFILTLEN; | |
581 s->basslen = BASSFILTLEN; | |
582 | |
583 s->cyc_pos = s->dlbuflen - 1; | |
15082 | 584 /* With a full (two axis) steering matrix decoder, s->matrix_mode |
585 should not be enabled lightly (it will also steer the Ls, Rs | |
586 channels). */ | |
587 s->matrix_mode = 0; | |
588 s->decode_mode = HRTF_MIX_51; | |
589 | |
590 s->print_flag = 1; | |
13996 | 591 |
592 if (allocate(s) != 0) { | |
593 af_msg(AF_MSG_ERROR, "[hrtf] Memory allocation error.\n"); | |
594 return AF_ERROR; | |
595 } | |
596 | |
597 for(i = 0; i < s->dlbuflen; i++) | |
598 s->lf[i] = s->rf[i] = s->lr[i] = s->rr[i] = s->cf[i] = | |
599 s->cr[i] = 0; | |
600 | |
601 s->lr_fwr = | |
602 s->rr_fwr = 0; | |
603 | |
604 s->cf_ir = cf_filt + (s->cf_o = pulse_detect(cf_filt)); | |
605 s->af_ir = af_filt + (s->af_o = pulse_detect(af_filt)); | |
606 s->of_ir = of_filt + (s->of_o = pulse_detect(of_filt)); | |
607 s->ar_ir = ar_filt + (s->ar_o = pulse_detect(ar_filt)); | |
608 s->or_ir = or_filt + (s->or_o = pulse_detect(or_filt)); | |
609 s->cr_ir = cr_filt + (s->cr_o = pulse_detect(cr_filt)); | |
610 | |
611 if((s->ba_ir = malloc(s->basslen * sizeof(float))) == NULL) { | |
612 af_msg(AF_MSG_ERROR, "[hrtf] Memory allocation error.\n"); | |
613 return AF_ERROR; | |
614 } | |
615 fc = 2.0 * BASSFILTFREQ / (float)af->data->rate; | |
14275
de13fd557440
less namespace pollution #2 (prefixed globals in filter.c with af_filter_)
alex
parents:
14245
diff
changeset
|
616 if(af_filter_design_fir(s->basslen, s->ba_ir, &fc, LP | KAISER, 4 * M_PI) == |
13996 | 617 -1) { |
618 af_msg(AF_MSG_ERROR, "[hrtf] Unable to design low-pass " | |
619 "filter.\n"); | |
620 return AF_ERROR; | |
621 } | |
622 for(i = 0; i < s->basslen; i++) | |
623 s->ba_ir[i] *= BASSGAIN; | |
624 | |
625 return AF_OK; | |
626 } | |
627 | |
628 /* Description of this filter */ | |
629 af_info_t af_info_hrtf = { | |
630 "HRTF Headphone", | |
631 "hrtf", | |
632 "ylai", | |
633 "", | |
634 AF_FLAGS_REENTRANT, | |
635 open | |
636 }; |