1 /*****************************************************************************
2 * sofalizer.c : SOFAlizer filter for virtual binaural acoustics
3 *****************************************************************************
4 * Copyright (C) 2013-2015 Andreas Fuchs, Wolfgang Hrauda,
5 * Acoustics Research Institute (ARI), Vienna, Austria
6 *
7 * Authors: Andreas Fuchs <andi.fuchs.mail@gmail.com>
8 * Wolfgang Hrauda <wolfgang.hrauda@gmx.at>
9 *
10 * SOFAlizer project coordinator at ARI, main developer of SOFA:
11 * Piotr Majdak <piotr@majdak.at>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
27
28 #include <math.h>
29 #include <mysofa.h>
30
42
44 #define FREQUENCY_DOMAIN 1
45
46 typedef struct MySofa {
/* contains data of one SOFA file */
50 int ir_samples;
/* length of one impulse response (IR) */
52 float *
lir, *
rir;
/* IRs (time-domain) */
56
62
65
68
72 char *
speakers_pos;
/* custom positions of the virtual loudspeakers */
73 float lfe_gain;
/* initial gain for the LFE channel */
74 float gain_lfe;
/* gain applied to LFE channel */
76
77 int n_conv;
/* number of channels to convolute */
78
79 /* buffer variables (for convolution) */
80 float *
ringbuffer[2];
/* buffers input samples, length of one buffer: */
81 /* no. input ch. (incl. LFE) x buffer_length */
82 int write[2];
/* current write position to ringbuffer */
83 int buffer_length;
/* is: longest IR plus max. delay in all SOFA files */
84 /* then choose next power of 2 */
85 int n_fft;
/* number of samples in one FFT block */
87
88 /* netCDF variables */
89 int *
delay[2];
/* broadband delay for each channel/IR to be convolved */
90
91 float *
data_ir[2];
/* IRs for all channels to be convolved */
92 /* (this excludes the LFE) */
97
98 /* control variables */
99 float gain;
/* filter gain (in dB) */
100 float rotation;
/* rotation of virtual loudspeakers (in degrees) */
101 float elevation;
/* elevation of virtual loudspeakers (in deg.) */
102 float radius;
/* distance virtual loudspeakers to listener (in metres) */
103 int type;
/* processing type */
105 int normalize;
/* should all IRs be normalized upon import ? */
106 int interpolate;
/* should wanted IRs be interpolated from neighbors ? */
107 int minphase;
/* should all IRs be minphased upon import ? */
108 float anglestep;
/* neighbor search angle step, in agles */
109 float radstep;
/* neighbor search radius step, in meters */
110
112
116
119
121 {
126 mysofa_lookup_free(sofa->
lookup);
129 mysofa_free(sofa->
hrtf);
132
133 return 0;
134 }
135
137 {
139 struct MYSOFA_HRTF *mysofa;
140 char *license;
142
143 mysofa = mysofa_load(filename, &
ret);
144 s->sofa.hrtf = mysofa;
145 if (
ret || !mysofa) {
148 }
149
150 ret = mysofa_check(mysofa);
151 if (
ret != MYSOFA_OK) {
154 }
155
157 mysofa_loudness(
s->sofa.hrtf);
158
160 mysofa_minphase(
s->sofa.hrtf, 0.01f);
161
162 mysofa_tocartesian(
s->sofa.hrtf);
163
164 s->sofa.lookup = mysofa_lookup_init(
s->sofa.hrtf);
165 if (
s->sofa.lookup ==
NULL)
167
169 s->sofa.neighborhood = mysofa_neighborhood_init_withstepdefine(
s->sofa.hrtf,
173
174 s->sofa.fir =
av_calloc(
s->sofa.hrtf->N *
s->sofa.hrtf->R,
sizeof(*
s->sofa.fir));
177
178 if (mysofa->DataSamplingRate.elements != 1)
181 *samplingrate = mysofa->DataSamplingRate.values[0];
182 license = mysofa_getAttribute(mysofa->attributes, (char *)"License");
183 if (license)
185
186 return 0;
187 }
188
190 {
193 char buf[8] = {0};
194
195 /* try to parse a channel name, e.g. "FL" */
198 if (channel_id < 0 || channel_id >= 64) {
201 }
202
203 *rchannel = channel_id;
205 return 0;
207 if (channel_id < 0 || channel_id >= 64) {
210 }
211 *rchannel = channel_id;
213 return 0;
214 }
216 }
217
219 {
221 char *
arg, *tokenizer, *p, *args =
av_strdup(
s->speakers_pos);
222
223 if (!args)
224 return;
225 p = args;
226
228 float azim, elev;
229 int out_ch_id;
230
233 continue;
234 }
236 s->vspkrpos[out_ch_id].set = 1;
237 s->vspkrpos[out_ch_id].azim = azim;
238 s->vspkrpos[out_ch_id].elev = elev;
240 s->vspkrpos[out_ch_id].set = 1;
241 s->vspkrpos[out_ch_id].azim = azim;
242 s->vspkrpos[out_ch_id].elev = 0;
243 }
244 }
245
247 }
248
250 float *speaker_azim, float *speaker_elev)
251 {
254 float azim[64] = { 0 };
255 float elev[64] = { 0 };
256 int ch,
n_conv =
ctx->inputs[0]->ch_layout.nb_channels;
/* get no. input channels */
257
258 if (n_conv < 0 || n_conv > 64)
260
262
265
266 /* set speaker positions according to input channel configuration: */
267 for (ch = 0; ch <
n_conv; ch++) {
269
270 switch (chan) {
284 elev[ch] = 90; break;
286 elev[ch] = 45; break;
288 elev[ch] = 45; break;
290 elev[ch] = 45; break;
292 elev[ch] = 45; break;
294 elev[ch] = 45; break;
296 elev[ch] = 45; break;
303 default:
305 }
306
307 if (
s->vspkrpos[ch].set) {
308 azim[ch] =
s->vspkrpos[ch].azim;
309 elev[ch] =
s->vspkrpos[ch].elev;
310 }
311 }
312
315
316 return 0;
317
318 }
319
332
334 {
339 int *write = &td->
write[jobnr];
340 const int *
const delay = td->
delay[jobnr];
341 const float *
const ir = td->
ir[jobnr];
344 float *temp_src = td->
temp_src[jobnr];
345 const int ir_samples =
s->sofa.ir_samples;
/* length of one IR */
346 const int n_samples =
s->sofa.n_samples;
349 const float *
src = (
const float *)in->
extended_data[0];
/* get pointer to audio input buffer */
350 float *
dst = (
float *)
out->extended_data[jobnr *
planar];
/* get pointer to audio output buffer */
351 const int in_channels =
s->n_conv;
/* number of input channels */
352 /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
353 const int buffer_length =
s->buffer_length;
354 /* -1 for AND instead of MODULO (applied to powers of 2): */
355 const uint32_t modulo = (uint32_t)buffer_length - 1;
356 float *
buffer[64];
/* holds ringbuffer for each input channel */
357 int wr = *write;
360
363
364 for (l = 0; l < in_channels; l++) {
365 /* get starting address of ringbuffer for each input channel */
366 buffer[l] = ringbuffer + l * buffer_length;
367 }
368
370 const float *temp_ir = ir; /* using same set of IRs for each sample */
371
374 for (l = 0; l < in_channels; l++) {
376
377 /* write current input sample to ringbuffer (for each channel) */
379 }
380 } else {
381 for (l = 0; l < in_channels; l++) {
382 /* write current input sample to ringbuffer (for each channel) */
384 }
385 }
386
387 /* loop goes through all channels to be convolved */
388 for (l = 0; l < in_channels; l++) {
389 const float *
const bptr =
buffer[l];
390
391 if (l ==
s->lfe_channel) {
392 /* LFE is an input channel but requires no convolution */
393 /* apply gain to LFE signal and add to output buffer */
394 dst[0] += *(
buffer[
s->lfe_channel] + wr) *
s->gain_lfe;
395 temp_ir += n_samples;
396 continue;
397 }
398
399 /* current read position in ringbuffer: input sample write position
400 * - delay for l-th ch. + diff. betw. IR length and buffer length
401 * (mod buffer length) */
402 read = (wr - delay[l] - (ir_samples - 1) + buffer_length) & modulo;
403
404 if (
read + ir_samples < buffer_length) {
405 memmove(temp_src, bptr +
read, ir_samples *
sizeof(*temp_src));
406 } else {
408
409 memmove(temp_src, bptr +
read,
len *
sizeof(*temp_src));
410 memmove(temp_src +
len, bptr, (n_samples -
len) *
sizeof(*temp_src));
411 }
412
413 /* multiply signal and IR, and add up the results */
414 dst[0] +=
s->fdsp->scalarproduct_float(temp_ir, temp_src,
FFALIGN(ir_samples, 32));
415 temp_ir += n_samples;
416 }
417
418 /* clippings counter */
420 n_clippings[0]++;
421
422 /* move output buffer pointer by +2 to get to next sample of processed channel: */
425 wr = (wr + 1) & modulo; /* update ringbuffer write position */
426 }
427
428 *write = wr; /* remember write position in ringbuffer for next call */
429
430 return 0;
431 }
432
434 {
439 int *write = &td->
write[jobnr];
440 AVComplexFloat *hrtf =
s->data_hrtf[jobnr];
/* get pointers to current HRTF data */
443 const int ir_samples =
s->sofa.ir_samples;
/* length of one IR */
446 float *
dst = (
float *)
out->extended_data[jobnr *
planar];
/* get pointer to audio output buffer */
447 const int in_channels =
s->n_conv;
/* number of input channels */
448 /* ring buffer length is: longest IR plus max. delay -> next power of 2 */
449 const int buffer_length =
s->buffer_length;
450 /* -1 for AND instead of MODULO (applied to powers of 2): */
451 const uint32_t modulo = (uint32_t)buffer_length - 1;
452 AVComplexFloat *fft_in =
s->in_fft[jobnr];
/* temporary array for FFT input data */
453 AVComplexFloat *fft_out =
s->out_fft[jobnr];
/* temporary array for FFT output data */
459 const int n_conv =
s->n_conv;
460 const int n_fft =
s->n_fft;
461 const float fft_scale = 1.0f /
s->n_fft;
463 int wr = *write;
464 int n_read;
466
469
470 /* find minimum between number of samples and output buffer length:
471 * (important, if one IR is longer than the output buffer) */
473 for (j = 0; j < n_read; j++) {
474 /* initialize output buf with saved signal from overflow buf */
475 dst[
mult * j] = ringbuffer[wr];
476 ringbuffer[wr] = 0.0f; /* re-set read samples to zero */
477 /* update ringbuffer read/write position */
478 wr = (wr + 1) & modulo;
479 }
480
481 /* initialize rest of output buffer with 0 */
484 }
485
486 /* fill FFT accumulation with 0 */
488
489 for (
i = 0;
i < n_conv;
i++) {
491
492 if (
i ==
s->lfe_channel) {
/* LFE */
495 /* apply gain to LFE signal and add to output buffer */
496 dst[2 * j] +=
src[
i + j * in_channels] *
s->gain_lfe;
497 }
498 } else {
500 /* apply gain to LFE signal and add to output buffer */
501 dst[j] +=
src[j] *
s->gain_lfe;
502 }
503 }
504 continue;
505 }
506
507 /* outer loop: go through all input channels to be convolved */
508 offset =
i * n_fft;
/* no. samples already processed */
509 hrtf_offset = hrtf +
offset;
510
511 /* fill FFT input with 0 (we want to zero-pad) */
513
516 /* prepare input for FFT */
517 /* write all samples of current input channel to FFT input array */
518 fft_in[j].
re =
src[j * in_channels +
i];
519 }
520 } else {
522 /* prepare input for FFT */
523 /* write all samples of current input channel to FFT input array */
524 fft_in[j].
re =
src[j];
525 }
526 }
527
528 /* transform input signal of current channel to frequency domain */
529 tx_fn(fft, fft_out, fft_in, sizeof(*fft_in));
530
531 for (j = 0; j < n_fft; j++) {
533 const float re = fft_out[j].
re;
534 const float im = fft_out[j].
im;
535
536 /* complex multiplication of input signal and HRTFs */
537 /* output channel (real): */
538 fft_acc[j].
re += re * hcomplex->
re - im * hcomplex->
im;
539 /* output channel (imag): */
540 fft_acc[j].
im += re * hcomplex->
im + im * hcomplex->
re;
541 }
542 }
543
544 /* transform output signal of current channel back to time domain */
545 itx_fn(ifft, fft_out, fft_acc, sizeof(*fft_acc));
546
548 /* write output signal of current channel to output buffer */
549 dst[
mult * j] += fft_out[j].
re * fft_scale;
550 }
551
552 for (j = 0; j < ir_samples - 1; j++) { /* overflow length is IR length - 1 */
553 /* write the rest of output signal to overflow buffer */
554 int write_pos = (wr + j) & modulo;
555
556 *(ringbuffer + write_pos) += fft_out[in->
nb_samples + j].
re * fft_scale;
557 }
558
559 /* go through all samples of current output buffer: count clippings */
560 for (
i = 0;
i <
out->nb_samples;
i++) {
561 /* clippings counter */
562 if (
fabsf(
dst[
i *
mult]) > 1) {
/* if current output sample > 1 */
563 n_clippings[0]++;
564 }
565 }
566
567 /* remember read/write position in ringbuffer for next call */
568 *write = wr;
569
570 return 0;
571 }
572
574 {
578 int n_clippings[2] = { 0 };
581
586 }
588
595
600 }
601
602 /* display error message if clipping occurred */
603 if (n_clippings[0] + n_clippings[1] > 0) {
605 n_clippings[0] + n_clippings[1],
out->nb_samples * 2);
606 }
607
610 }
611
613 {
619
621
624 else
630
633
635 }
636
640 {
647 };
648
652
656
660
665
669
672 }
673
675 float *
left,
float *right,
676 float *delay_left, float *delay_right)
677 {
679 float c[3], delays[2];
680 float *fl, *fr;
681 int nearest;
682 int *neighbors;
683 float *res;
684
685 c[0] = x,
c[1] = y,
c[2] = z;
686 nearest = mysofa_lookup(
s->sofa.lookup,
c);
687 if (nearest < 0)
689
690 if (
s->interpolate) {
691 neighbors = mysofa_neighborhood(
s->sofa.neighborhood, nearest);
692 res = mysofa_interpolate(
s->sofa.hrtf,
c,
693 nearest, neighbors,
694 s->sofa.fir, delays);
695 } else {
696 if (
s->sofa.hrtf->DataDelay.elements >
s->sofa.hrtf->R) {
697 delays[0] =
s->sofa.hrtf->DataDelay.values[nearest *
s->sofa.hrtf->R];
698 delays[1] =
s->sofa.hrtf->DataDelay.values[nearest *
s->sofa.hrtf->R + 1];
699 } else {
700 delays[0] =
s->sofa.hrtf->DataDelay.values[0];
701 delays[1] =
s->sofa.hrtf->DataDelay.values[1];
702 }
703 res =
s->sofa.hrtf->DataIR.values + nearest *
s->sofa.hrtf->N *
s->sofa.hrtf->R;
704 }
705
706 *delay_left = delays[0];
707 *delay_right = delays[1];
708
709 fl = res;
710 fr = res +
s->sofa.hrtf->N;
711
712 memcpy(
left, fl,
sizeof(
float) *
s->sofa.hrtf->N);
713 memcpy(right, fr,
sizeof(
float) *
s->sofa.hrtf->N);
714
715 return 0;
716 }
717
719 {
721 int n_samples;
722 int ir_samples;
723 int n_conv =
s->n_conv;
/* no. channels to convolve */
725 float delay_l; /* broadband delay for each IR */
726 float delay_r;
727 int nb_input_channels =
ctx->inputs[0]->ch_layout.nb_channels;
/* no. input channels */
728 float gain_lin =
expf((
s->gain - 3 * nb_input_channels) / 20 *
M_LN10);
/* gain - 3dB/channel */
735 float *data_ir_l =
NULL;
736 float *data_ir_r =
NULL;
737 int offset = 0;
/* used for faster pointer arithmetics in for-loop */
738 int i, j, azim_orig = azim, elev_orig = elev;
740 int n_current;
741 int n_max = 0;
742
744 s->sofa.ir_samples =
s->sofa.hrtf->N;
745 s->sofa.n_samples = 1 << (32 -
ff_clz(
s->sofa.ir_samples));
746
747 n_samples =
s->sofa.n_samples;
748 ir_samples =
s->sofa.ir_samples;
749
751 s->data_ir[0] =
av_calloc(n_samples,
sizeof(
float) *
s->n_conv);
752 s->data_ir[1] =
av_calloc(n_samples,
sizeof(
float) *
s->n_conv);
753
754 if (!
s->data_ir[0] || !
s->data_ir[1]) {
757 }
758 }
759
762
763 if (!
s->delay[0] || !
s->delay[1]) {
766 }
767
768 /* get temporary IR for L and R channel */
771 if (!data_ir_r || !data_ir_l) {
774 }
775
777 s->temp_src[0] =
av_calloc(n_samples,
sizeof(
float));
778 s->temp_src[1] =
av_calloc(n_samples,
sizeof(
float));
779 if (!
s->temp_src[0] || !
s->temp_src[1]) {
782 }
783 }
784
785 s->speaker_azim =
av_calloc(
s->n_conv,
sizeof(*
s->speaker_azim));
786 s->speaker_elev =
av_calloc(
s->n_conv,
sizeof(*
s->speaker_elev));
787 if (!
s->speaker_azim || !
s->speaker_elev) {
790 }
791
792 /* get speaker positions */
794 av_log(
ctx,
AV_LOG_ERROR,
"Couldn't get speaker positions. Input channel configuration not supported.\n");
796 }
797
798 for (
i = 0;
i <
s->n_conv;
i++) {
799 float coordinates[3];
800
801 /* load and store IRs and corresponding delays */
802 azim = (int)(
s->speaker_azim[
i] + azim_orig) % 360;
803 elev = (int)(
s->speaker_elev[
i] + elev_orig) % 90;
804
805 coordinates[0] = azim;
806 coordinates[1] = elev;
808
809 mysofa_s2c(coordinates);
810
811 /* get id of IR closest to desired position */
813 data_ir_l + n_samples *
i,
814 data_ir_r + n_samples *
i,
815 &delay_l, &delay_r);
818
821
822 s->sofa.max_delay =
FFMAX3(
s->sofa.max_delay,
s->delay[0][
i],
s->delay[1][
i]);
823 }
824
825 /* get size of ringbuffer (longest IR plus max. delay) */
826 /* then choose next power of 2 for performance optimization */
827 n_current = n_samples +
s->sofa.max_delay;
828 /* length of longest IR plus max. delay */
829 n_max =
FFMAX(n_max, n_current);
830
831 /* buffer length is longest IR plus max. delay -> next power of 2
832 (32 - count leading zeros gives required exponent) */
833 s->buffer_length = 1 << (32 -
ff_clz(n_max));
835
838
855 }
856
858 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
859 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float) * nb_input_channels);
861 /* get temporary HRTF memory for L and R channel */
864 if (!data_hrtf_r || !data_hrtf_l) {
867 }
868
869 s->ringbuffer[0] =
av_calloc(
s->buffer_length,
sizeof(
float));
870 s->ringbuffer[1] =
av_calloc(
s->buffer_length,
sizeof(
float));
877 if (!
s->in_fft[0] || !
s->in_fft[1] ||
878 !
s->out_fft[0] || !
s->out_fft[1] ||
879 !
s->temp_afft[0] || !
s->temp_afft[1]) {
882 }
883 }
884
885 if (!
s->ringbuffer[0] || !
s->ringbuffer[1]) {
888 }
889
895 if (!fft_in_l || !fft_in_r ||
896 !fft_out_l || !fft_out_r) {
899 }
900 }
901
902 for (
i = 0;
i <
s->n_conv;
i++) {
903 float *lir, *rir;
904
905 offset =
i * n_samples;
/* no. samples already written */
906
909
911 for (j = 0; j < ir_samples; j++) {
912 /* load reversed IRs of the specified source position
913 * sample-by-sample for left and right ear; and apply gain */
914 s->data_ir[0][
offset + j] = lir[ir_samples - 1 - j] * gain_lin;
915 s->data_ir[1][
offset + j] = rir[ir_samples - 1 - j] * gain_lin;
916 }
918 memset(fft_in_l, 0,
n_fft *
sizeof(*fft_in_l));
919 memset(fft_in_r, 0,
n_fft *
sizeof(*fft_in_r));
920
922 for (j = 0; j < ir_samples; j++) {
923 /* load non-reversed IRs of the specified source position
924 * sample-by-sample and apply gain,
925 * L channel is loaded to real part, R channel to imag part,
926 * IRs are shifted by L and R delay */
927 fft_in_l[
s->delay[0][
i] + j].
re = lir[j] * gain_lin;
928 fft_in_r[
s->delay[1][
i] + j].
re = rir[j] * gain_lin;
929 }
930
931 /* actually transform to frequency domain (IRs -> HRTFs) */
932 s->tx_fn[0](
s->fft[0], fft_out_l, fft_in_l,
sizeof(*fft_in_l));
933 memcpy(data_hrtf_l +
offset, fft_out_l,
n_fft *
sizeof(*fft_out_l));
934 s->tx_fn[1](
s->fft[1], fft_out_r, fft_in_r,
sizeof(*fft_in_r));
935 memcpy(data_hrtf_r +
offset, fft_out_r,
n_fft *
sizeof(*fft_out_r));
936 }
937 }
938
942 if (!
s->data_hrtf[0] || !
s->data_hrtf[1]) {
945 }
946
947 memcpy(
s->data_hrtf[0], data_hrtf_l,
/* copy HRTF data to */
949 memcpy(
s->data_hrtf[1], data_hrtf_r,
951 }
952
954 av_freep(&data_hrtf_l);
/* free temporary HRTF memory */
956
957 av_freep(&data_ir_l);
/* free temprary IR memory */
959
960 av_freep(&fft_out_l);
/* free temporary FFT memory */
962
963 av_freep(&fft_in_l);
/* free temporary FFT memory */
965
967 }
968
970 {
973
977 }
978
979 /* preload SOFA file, */
982 /* file loading error */
984 } else { /* no file loading error, resampling not required */
986 }
987
991 }
992
996
997 return 0;
998 }
999
1001 {
1005
1007 s->nb_samples =
s->framesize;
1008
1009 /* gain -3 dB per channel */
1010 s->gain_lfe =
expf((
s->gain - 3 *
inlink->ch_layout.nb_channels +
s->lfe_gain) / 20 *
M_LN10);
1011
1012 s->n_conv =
inlink->ch_layout.nb_channels;
1013
1014 /* load IRs to data_ir[0] and data_ir[1] for required directions */
1017
1018 av_log(
ctx,
AV_LOG_DEBUG,
"Samplerate: %d Channels to convolute: %d, Length of ringbuffer: %d x %d\n",
1019 inlink->sample_rate,
s->n_conv,
inlink->ch_layout.nb_channels,
s->buffer_length);
1020
1021 return 0;
1022 }
1023
1025 {
1027
1056 }
1057
1058 #define OFFSET(x) offsetof(SOFAlizerContext, x)
1059 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
1060
1079 };
1080
1082
1084 {
1088 },
1089 };
1090
1092 .
name =
"sofalizer",
1095 .priv_class = &sofalizer_class,
1103 };