-
Notifications
You must be signed in to change notification settings - Fork 0
/
bformdec2.cpp
2149 lines (1792 loc) · 75.4 KB
/
bformdec2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
bformdec2.cpp
Copyright (C) 2019 Pablo Zinemanas
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include "csdl.h"
#include <math.h>
#include "csoundCore.h"
#include <string.h>
#include <new>
/* Band-splitting constants */
#define MAXZEROS 4
#define MAXPOLES 4
/* Ambisonics constants */
#define MAX_OUTPUTS 20
#define MAX_INPUTS 16
/* HRTF constants */
#define minelev (-40)
#define elevincrement (10)
/* max delay for min phase: a time value:
multiply by sr to get no of samples for memory allocation */
#define maxdeltime (0.0011)
/* additional definitions for woodworth models */
//#define c (34400.0)
static const float nonlinitd[5] = {1.570024f, 1.378733f, 1.155164f, 1.101230f,1.0f};
static const float nonlinitd48k[5] =
{1.549748f, 1.305457f, 1.124501f, 1.112852f,1.0f};
static const float nonlinitd96k[5] =
{1.550297f, 1.305671f, 1.124456f, 1.112818f,1.0f};
/* number of measurements per elev: mit data const:read only, static:exists
for whole process... */
static const int32_t elevationarray[14] =
{56, 60, 72, 72, 72, 72, 72, 60, 56, 45, 36, 24, 12, 1 };
/* assumed mit hrtf data will be used here. Otherwise delay data would need
to be extracted and replaced here... */
static const float minphasedels[368] =
{
0.000000f, 0.000045f, 0.000091f, 0.000136f, 0.000159f, 0.000204f,
0.000249f, 0.000272f, 0.000295f, 0.000317f, 0.000363f, 0.000385f,
0.000272f, 0.000408f, 0.000454f, 0.000454f, 0.000408f, 0.000385f,
0.000363f, 0.000317f, 0.000295f, 0.000295f, 0.000249f, 0.000204f,
0.000159f, 0.000136f, 0.000091f, 0.000045f, 0.000000f, 0.000000f,
0.000045f, 0.000091f, 0.000136f, 0.000181f, 0.000227f, 0.000249f,
0.000272f, 0.000317f, 0.000363f, 0.000385f, 0.000454f, 0.000476f,
0.000454f, 0.000522f, 0.000499f, 0.000499f, 0.000476f, 0.000454f,
0.000408f, 0.000408f, 0.000385f, 0.000340f, 0.000295f, 0.000272f,
0.000227f, 0.000181f, 0.000136f, 0.000091f, 0.000045f, 0.000000f,
0.000000f, 0.000045f, 0.000091f, 0.000113f, 0.000159f, 0.000204f,
0.000227f, 0.000272f, 0.000317f, 0.000317f, 0.000363f, 0.000408f,
0.000363f, 0.000522f, 0.000476f, 0.000499f, 0.000590f, 0.000567f,
0.000567f, 0.000544f, 0.000522f, 0.000499f, 0.000476f, 0.000454f,
0.000431f, 0.000408f, 0.000385f, 0.000363f, 0.000317f, 0.000295f,
0.000249f, 0.000204f, 0.000181f, 0.000136f, 0.000091f, 0.000045f,
0.000000f, 0.000000f, 0.000045f, 0.000091f, 0.000113f, 0.000159f,
0.000204f, 0.000249f, 0.000295f, 0.000317f, 0.000363f, 0.000340f,
0.000385f, 0.000431f, 0.000476f, 0.000522f, 0.000544f, 0.000612f,
0.000658f, 0.000658f, 0.000635f, 0.000658f, 0.000522f, 0.000499f,
0.000476f, 0.000454f, 0.000408f, 0.000385f, 0.000363f, 0.000340f,
0.000295f, 0.000272f, 0.000227f, 0.000181f, 0.000136f, 0.000091f,
0.000045f, 0.000000f, 0.000000f, 0.000045f, 0.000091f, 0.000136f,
0.000159f, 0.000204f, 0.000249f, 0.000295f, 0.000340f, 0.000385f,
0.000431f, 0.000476f, 0.000522f, 0.000567f, 0.000522f, 0.000567f,
0.000567f, 0.000635f, 0.000703f, 0.000748f, 0.000748f, 0.000726f,
0.000703f, 0.000658f, 0.000454f, 0.000431f, 0.000385f, 0.000363f,
0.000317f, 0.000295f, 0.000272f, 0.000227f, 0.000181f, 0.000136f,
0.000091f, 0.000045f, 0.000000f, 0.000000f, 0.000045f, 0.000091f,
0.000113f, 0.000159f, 0.000204f, 0.000249f, 0.000295f, 0.000340f,
0.000385f, 0.000408f, 0.000454f, 0.000499f, 0.000544f, 0.000522f,
0.000590f, 0.000590f, 0.000635f, 0.000658f, 0.000680f, 0.000658f,
0.000544f, 0.000590f, 0.000567f, 0.000454f, 0.000431f, 0.000385f,
0.000363f, 0.000317f, 0.000272f, 0.000272f, 0.000227f, 0.000181f,
0.000136f, 0.000091f, 0.000045f, 0.000000f, 0.000000f, 0.000045f,
0.000068f, 0.000113f, 0.000159f, 0.000204f, 0.000227f, 0.000272f,
0.000317f, 0.000340f, 0.000385f, 0.000431f, 0.000454f, 0.000499f,
0.000499f, 0.000544f, 0.000567f, 0.000590f, 0.000590f, 0.000590f,
0.000590f, 0.000567f, 0.000567f, 0.000476f, 0.000454f, 0.000408f,
0.000385f, 0.000340f, 0.000340f, 0.000295f, 0.000249f, 0.000204f,
0.000159f, 0.000136f, 0.000091f, 0.000045f, 0.000000f, 0.000000f,
0.000045f, 0.000091f, 0.000113f, 0.000159f, 0.000204f, 0.000249f,
0.000295f, 0.000340f, 0.000363f, 0.000385f, 0.000431f, 0.000454f,
0.000499f, 0.000522f, 0.000522f, 0.000522f, 0.000499f, 0.000476f,
0.000454f, 0.000431f, 0.000385f, 0.000340f, 0.000317f, 0.000272f,
0.000227f, 0.000181f, 0.000136f, 0.000091f, 0.000045f, 0.000000f,
0.000000f, 0.000045f, 0.000091f, 0.000136f, 0.000159f, 0.000204f,
0.000227f, 0.000249f, 0.000295f, 0.000340f, 0.000363f, 0.000385f,
0.000408f, 0.000431f, 0.000431f, 0.000431f, 0.000431f, 0.000408f,
0.000385f, 0.000363f, 0.000317f, 0.000317f, 0.000272f, 0.000227f,
0.000181f, 0.000136f, 0.000091f, 0.000045f, 0.000000f, 0.000000f,
0.000045f, 0.000091f, 0.000136f, 0.000181f, 0.000204f, 0.000227f,
0.000272f, 0.000295f, 0.000317f, 0.000340f, 0.000340f, 0.000363f,
0.000363f, 0.000340f, 0.000317f, 0.000295f, 0.000249f, 0.000204f,
0.000159f, 0.000113f, 0.000068f, 0.000023f, 0.000000f, 0.000045f,
0.000068f, 0.000113f, 0.000159f, 0.000181f, 0.000204f, 0.000227f,
0.000249f, 0.000249f, 0.000249f, 0.000227f, 0.000227f, 0.000181f,
0.000159f, 0.000113f, 0.000091f, 0.000045f, 0.000000f, 0.000000f,
0.000045f, 0.000091f, 0.000136f, 0.000159f, 0.000181f, 0.000181f,
0.000181f, 0.000159f, 0.000136f, 0.000091f, 0.000045f, 0.000000f,
0.000000f, 0.000045f, 0.000068f, 0.000091f, 0.000068f, 0.000045f,
0.000000f, 0.000000f
};
#ifdef WORDS_BIGENDIAN
static int32_t swap4bytes(CSOUND* csound, MEMFIL* mfp)
{
char c1, c2, c3, c4;
char *p = mfp->beginp;
int32_t size = mfp->length;
while (size >= 4)
{
c1 = p[0]; c2 = p[1]; c3 = p[2]; c4 = p[3];
p[0] = c4; p[1] = c3; p[2] = c2; p[3] = c1;
size -= 4; p +=4;
}
return OK;
}
#else
static int32_t (*swap4bytes)(CSOUND*, MEMFIL*) = NULL;
#endif
/* HRTF classes (adapted from csound/Opcodes/hrtfopcodes.c) */
class hrtf {
public:
hrtf() {}
virtual ~hrtf() {}
virtual void init(void) = 0;
virtual int32_t hrtfstat_init(CSOUND *csound, MYFLT elev, MYFLT angle, MYFLT radius, STRINGDAT *filel, STRINGDAT *filer) = 0;
virtual int32_t hrtfstat_process(CSOUND *csound, MYFLT *in, MYFLT *outsigl, MYFLT *outsigr, uint32_t offset, uint32_t early, uint32_t nsmps) = 0;
};
class hrtf_c : public hrtf {
private:
/* MYFLT *outsigl, *outsigr;
//MYFLT *in, *iangle, *ielev;
MYFLT *iangle, *ielev;
STRINGDAT *ifilel, *ifiler;
MYFLT *oradius, *osr;*/
//STRINGDAT *ifilel_p, *ifiler_p;
/*see definitions in INIT*/
int32_t irlength_p, irlengthpad_p, overlapsize_p;
MYFLT sroverN_p;
int32_t counter_p;
MYFLT sr_p;
/* hrtf data padded */
AUXCH hrtflpad_p,hrtfrpad_p;
/* in and output buffers */
AUXCH insig_p, outl_p, outr_p;
/* memory local to perform method */
/* insig fft */
AUXCH complexinsig_p;
/* hrtf buffers (rectangular complex form) */
AUXCH hrtflfloat_p, hrtfrfloat_p;
/* spectral data */
AUXCH outspecl_p, outspecr_p;
/* overlap data */
AUXCH overlapl_p, overlapr_p;
/* interpolation buffers */
AUXCH lowl1_p, lowr1_p, lowl2_p, lowr2_p, highl1_p, highr1_p, highl2_p, highr2_p;
/* buffers for impulse shift */
AUXCH leftshiftbuffer_p, rightshiftbuffer_p;
public:
virtual void init(void) {
}
/* HRTF functions (adapted from csound/Opcodes/hrtfopcodes.c) */
virtual int32_t hrtfstat_init(CSOUND *csound, MYFLT elev, MYFLT angle, MYFLT r, STRINGDAT *ifilel, STRINGDAT *ifiler)
{
/* left and right data files: spectral mag, phase format. */
MEMFIL *fpl = NULL, *fpr = NULL;
/* interpolation values */
MYFLT *lowl1;
MYFLT *lowr1;
MYFLT *lowl2;
MYFLT *lowr2;
MYFLT *highl1;
MYFLT *highr1;
MYFLT *highl2;
MYFLT *highr2;
MYFLT *hrtflfloat;
MYFLT *hrtfrfloat;
MYFLT *hrtflpad;
MYFLT *hrtfrpad;
/* MYFLT elev = *p->ielev;
MYFLT angle = *p->iangle;
MYFLT r = *p->oradius;
MYFLT sr = *p->osr;*/
/* pointers into HRTF files */
float *fpindexl=NULL;
float *fpindexr=NULL;
/* time domain impulse length, padded, overlap add */
int32_t irlength=0, irlengthpad=0, overlapsize=0;
int32_t i, skip = 0;
/* local interpolation values */
MYFLT elevindexhighper, angleindex2per, angleindex4per;
int32_t elevindexlow, elevindexhigh, angleindex1, angleindex2,
angleindex3, angleindex4;
MYFLT magl, magr, phasel, phaser, magllow, magrlow, maglhigh, magrhigh;
/* local variables, mainly used for simplification */
MYFLT elevindexstore;
MYFLT angleindexlowstore;
MYFLT angleindexhighstore;
/* woodworth values */
MYFLT radianangle, radianelev, itd=0, itdww, freq;
/* shift */
int32_t shift;
MYFLT *leftshiftbuffer;
MYFLT *rightshiftbuffer;
/* sr */
sr_p = csound->GetSr(csound);
//char filel[MAXNAME] = "hrtf-44100-left.dat"; //../hrtf/
//char filer[MAXNAME] = "hrtf-44100-right.dat";
if(sr_p != FL(44100.0) && sr_p != FL(48000.0) && sr_p != FL(96000.0))
sr_p = FL(44100.0);
if (UNLIKELY(csound->GetSr(csound) != sr_p))
csound->Message(csound,
Str("\n\nWARNING!!:\nOrchestra SR not compatible with "
"HRTF processing SR of: %.0f\n\n"), sr_p);
/* if (sr_p == FL(48000.0)) {
sprintf(filel, "%s", "hrtf-48000-left.dat");
sprintf(filer, "%s", "hrtf-48000-right.dat");
}
if (sr_p == FL(96000.0)) {
sprintf(filel, "%s", "hrtf-96000-left.dat");
sprintf(filer, "%s", "hrtf-96000-right.dat");
}*/
/* setup as per sr */
if(sr_p == 44100 || sr_p == 48000)
{
irlength = 128;
irlengthpad = 256;
overlapsize = (irlength - 1);
}
else if(sr_p == 96000)
{
irlength = 256;
irlengthpad = 512;
overlapsize = (irlength - 1);
}
char filel[MAXNAME], filer[MAXNAME];
/* copy in string name... */
strncpy(filel, (char*) ifilel->data, MAXNAME-1); //filel[MAXNAME-1]='\0';
strncpy(filer, (char*) ifiler->data, MAXNAME-1); //filel[MAXNAME-1]='\0';
/* reading files, with byte swap */
fpl = csound->ldmemfile2withCB(csound, filel, CSFTYPE_FLOATS_BINARY,
swap4bytes);
if (UNLIKELY(fpl == NULL))
return
csound->InitError(csound,
Str("\n\n\nCannot load left data file, exiting\n\n"));
fpr = csound->ldmemfile2withCB(csound, filer, CSFTYPE_FLOATS_BINARY,
swap4bytes);
if (UNLIKELY(fpr == NULL))
return
csound->InitError(csound,
Str("\n\n\nCannot load right data file, exiting\n\n"));
irlength_p = irlength;
irlengthpad_p = irlengthpad;
overlapsize_p = overlapsize;
sroverN_p = sr_p/irlength;
/* start indices at correct value (start of file)/ zero indices.
(do not need to store here, as only accessing in INIT) */
fpindexl = (float *) fpl->beginp;
fpindexr = (float *) fpr->beginp;
////
// /* buffers */
if (!insig_p.auxp || insig_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength*sizeof(MYFLT), &insig_p);
if (!outl_p.auxp || outl_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &outl_p);
if (!outr_p.auxp || outr_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &outr_p);
if (!hrtflpad_p.auxp || hrtflpad_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &hrtflpad_p);
if (!hrtfrpad_p.auxp || hrtfrpad_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &hrtfrpad_p);
if (!complexinsig_p.auxp || complexinsig_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), & complexinsig_p);
if (!hrtflfloat_p.auxp || hrtflfloat_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength*sizeof(MYFLT), &hrtflfloat_p);
if (!hrtfrfloat_p.auxp || hrtfrfloat_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength*sizeof(MYFLT), &hrtfrfloat_p);
if (!outspecl_p.auxp || outspecl_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &outspecl_p);
if (!outspecr_p.auxp || outspecr_p.size < irlengthpad * sizeof(MYFLT))
csound->AuxAlloc(csound, irlengthpad*sizeof(MYFLT), &outspecr_p);
if (!overlapl_p.auxp || overlapl_p.size < overlapsize * sizeof(MYFLT))
csound->AuxAlloc(csound, overlapsize*sizeof(MYFLT), &overlapl_p);
if (!overlapr_p.auxp || overlapr_p.size < overlapsize * sizeof(MYFLT))
csound->AuxAlloc(csound, overlapsize*sizeof(MYFLT), &overlapr_p);
memset(insig_p.auxp, 0, irlength * sizeof(MYFLT));
memset(outl_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(outr_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(hrtflpad_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(hrtfrpad_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(complexinsig_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(hrtflfloat_p.auxp, 0, irlength * sizeof(MYFLT));
memset(hrtfrfloat_p.auxp, 0, irlength * sizeof(MYFLT));
memset(outspecl_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(outspecr_p.auxp, 0, irlengthpad * sizeof(MYFLT));
memset(overlapl_p.auxp, 0, overlapsize * sizeof(MYFLT));
memset(overlapr_p.auxp, 0, overlapsize * sizeof(MYFLT));
/* interpolation values */
if (!lowl1_p.auxp || lowl1_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &lowl1_p);
if (!lowr1_p.auxp || lowr1_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &lowr1_p);
if (!lowl2_p.auxp || lowl2_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &lowl2_p);
if (!lowr2_p.auxp || lowr2_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &lowr2_p);
if (!highl1_p.auxp || highl1_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &highl1_p);
if (!highr1_p.auxp || highr1_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &highr1_p);
if (!highl2_p.auxp || highl2_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &highl2_p);
if (!highr2_p.auxp || highr2_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength * sizeof(MYFLT), &highr2_p);
/* best to zero, for future changes (filled in init) */
memset(lowl1_p.auxp, 0, irlength * sizeof(MYFLT));
memset(lowr1_p.auxp, 0, irlength * sizeof(MYFLT));
memset(lowl2_p.auxp, 0, irlength * sizeof(MYFLT));
memset(lowr2_p.auxp, 0, irlength * sizeof(MYFLT));
memset(highl1_p.auxp, 0, irlength * sizeof(MYFLT));
memset(highl2_p.auxp, 0, irlength * sizeof(MYFLT));
memset(highr1_p.auxp, 0, irlength * sizeof(MYFLT));
memset(highr2_p.auxp, 0, irlength * sizeof(MYFLT));
/* shift buffers */
if (!leftshiftbuffer_p.auxp ||
leftshiftbuffer_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength*sizeof(MYFLT), &leftshiftbuffer_p);
if (!rightshiftbuffer_p.auxp ||
rightshiftbuffer_p.size < irlength * sizeof(MYFLT))
csound->AuxAlloc(csound, irlength*sizeof(MYFLT), &rightshiftbuffer_p);
memset(leftshiftbuffer_p.auxp, 0, irlength * sizeof(MYFLT));
memset(rightshiftbuffer_p.auxp, 0, irlength * sizeof(MYFLT));
lowl1 = (MYFLT *)lowl1_p.auxp;
lowr1 = (MYFLT *)lowr1_p.auxp;
lowl2 = (MYFLT *)lowl2_p.auxp;
lowr2 = (MYFLT *)lowr2_p.auxp;
highl1 = (MYFLT *)highl1_p.auxp;
highr1 = (MYFLT *)highr1_p.auxp;
highl2 = (MYFLT *)highl2_p.auxp;
highr2 = (MYFLT *)highr2_p.auxp;
leftshiftbuffer = (MYFLT *)leftshiftbuffer_p.auxp;
rightshiftbuffer = (MYFLT *)rightshiftbuffer_p.auxp;
hrtflfloat = (MYFLT *)hrtflfloat_p.auxp;
hrtfrfloat = (MYFLT *)hrtfrfloat_p.auxp;
hrtflpad = (MYFLT *)hrtflpad_p.auxp;
hrtfrpad = (MYFLT *)hrtfrpad_p.auxp;
if(r <= 0 || r > 15)
r = FL(8.8);
if(elev > FL(90.0))
elev = FL(90.0);
if(elev < FL(-40.0))
elev = FL(-40.0);
while(angle < FL(0.0))
angle += FL(360.0);
while(angle >= FL(360.0))
angle -= FL(360.0);
/* two nearest elev indices to avoid recalculating */
elevindexstore = (elev - minelev) / elevincrement;
elevindexlow = (int32_t)elevindexstore;
if(elevindexlow < 13)
elevindexhigh = elevindexlow + 1;
/* highest index reached */
else
elevindexhigh = elevindexlow;
/* get percentage value for interpolation */
elevindexhighper = elevindexstore - elevindexlow;
/* avoid recalculation */
angleindexlowstore = angle / (FL(360.0) / elevationarray[elevindexlow]);
angleindexhighstore = angle / (FL(360.0) / elevationarray[elevindexhigh]);
/* 4 closest indices, 2 low and 2 high */
angleindex1 = (int32_t)angleindexlowstore;
angleindex2 = angleindex1 + 1;
angleindex2 = angleindex2 % elevationarray[elevindexlow];
angleindex3 = (int32_t)angleindexhighstore;
angleindex4 = angleindex3 + 1;
angleindex4 = angleindex4 % elevationarray[elevindexhigh];
/* angle percentages for interp */
angleindex2per = angleindexlowstore - angleindex1;
angleindex4per = angleindexhighstore - angleindex3;
/* read 4 nearest HRTFs */
skip = 0;
/* switch l and r */
if(angleindex1 > elevationarray[elevindexlow] / 2)
{
for(i = 0; i < elevindexlow; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < (elevationarray[elevindexlow] - angleindex1); i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
lowl1[i] = fpindexr[skip + i];
lowr1[i] = fpindexl[skip + i];
}
}
else
{
for(i = 0; i < elevindexlow; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < angleindex1; i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
lowl1[i] = fpindexl[skip + i];
lowr1[i] = fpindexr[skip + i];
}
}
skip = 0;
if(angleindex2 > elevationarray[elevindexlow] / 2)
{
for(i = 0; i < elevindexlow; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < (elevationarray[elevindexlow] - angleindex2); i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
lowl2[i] = fpindexr[skip + i];
lowr2[i] = fpindexl[skip + i];
}
}
else
{
for(i = 0; i < elevindexlow; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < angleindex2; i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
lowl2[i] = fpindexl[skip + i];
lowr2[i] = fpindexr[skip + i];
}
}
skip = 0;
if(angleindex3 > elevationarray[elevindexhigh] / 2)
{
for(i = 0; i < elevindexhigh; i++)
skip += ((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < (elevationarray[elevindexhigh] - angleindex3); i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
highl1[i] = fpindexr[skip + i];
highr1[i] = fpindexl[skip + i];
}
}
else
{
for(i = 0; i < elevindexhigh; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < angleindex3; i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
highl1[i] = fpindexl[skip + i];
highr1[i] = fpindexr[skip + i];
}
}
skip = 0;
if(angleindex4 > elevationarray[elevindexhigh] / 2)
{
for(i = 0; i < elevindexhigh; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < (elevationarray[elevindexhigh] - angleindex4); i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
highl2[i] = fpindexr[skip + i];
highr2[i] = fpindexl[skip + i];
}
}
else
{
for(i = 0; i < elevindexhigh; i++)
skip +=((int32_t)(elevationarray[i] / 2) + 1) * irlength;
for (i = 0; i < angleindex4; i++)
skip += irlength;
for(i = 0; i < irlength; i++)
{
highl2[i] = fpindexl[skip + i];
highr2[i] = fpindexr[skip + i];
}
}
/* woodworth process */
/* ITD formula, check which ear is relevant to calculate angle from */
if(angle > FL(180.0))
radianangle = (angle - FL(180.0)) * PI_F / FL(180.0);
else
radianangle = angle * PI_F / FL(180.0);
/* degrees to radians */
radianelev = elev * PI_F / FL(180.0);
/* get in correct range for formula */
if(radianangle > PI_F / FL(2.0))
radianangle = FL(PI) - radianangle;
/* woodworth formula for itd */
itdww = (radianangle + SIN(radianangle)) * r * COS(radianelev) / 34400.0;
/* 0 Hz and Nyq... */
/* these are real values...may be neg (implying phase of pi:
in phase truncation), so need fabs... */
magllow = FABS(lowl1[0]) + (FABS(lowl2[0]) - FABS(lowl1[0])) * angleindex2per;
maglhigh = FABS(highl1[0]) + (FABS(highl2[0]) - FABS(highl1[0])) *
angleindex4per;
hrtflfloat[0] = magllow + (maglhigh - magllow) * elevindexhighper;
magllow = FABS(lowl1[1]) + (FABS(lowl2[1]) - FABS(lowl1[1])) * angleindex2per;
maglhigh = FABS(highl1[1]) + (FABS(highl2[1]) - FABS(highl1[1])) *
angleindex4per;
hrtflfloat[1] = magllow + (maglhigh - magllow) * elevindexhighper;
magrlow = FABS(lowr1[0]) + (FABS(lowr2[0]) - FABS(lowr1[0])) * angleindex2per;
magrhigh = FABS(highr1[0]) + (FABS(highr2[0]) - FABS(highr1[0])) *
angleindex4per;
hrtfrfloat[0] = magrlow + (magrhigh - magrlow) * elevindexhighper;
magrlow = FABS(lowr1[1]) + (FABS(lowr2[1]) - FABS(lowr1[1])) * angleindex2per;
magrhigh = FABS(highr1[1]) + (FABS(highr2[1]) - FABS(highr1[1])) *
angleindex4per;
hrtfrfloat[1] = magrlow + (magrhigh - magrlow) * elevindexhighper;
/* magnitude interpolation */
for(i = 2; i < irlength; i+=2)
{
/* interpolate high and low mags */
magllow = lowl1[i] + (lowl2[i] - lowl1[i]) * angleindex2per;
maglhigh = highl1[i]+(highl2[i] - highl1[i]) * angleindex4per;
magrlow = lowr1[i] + (lowr2[i] - lowr1[i]) * angleindex2per;
magrhigh = highr1[i] + (highr2[i] - highr1[i]) * angleindex4per;
/* interpolate high and low results */
magl = magllow + (maglhigh - magllow) * elevindexhighper;
magr = magrlow + (magrhigh - magrlow) * elevindexhighper;
freq = (i / 2) * sroverN_p;
/* non linear itd...last value in array = 1.0, so back to itdww */
if(sr_p == 96000)
{
if ((i / 2) < 6)
itd = itdww * nonlinitd96k[(i / 2) - 1];
}
else if(sr_p == 48000)
{
if ((i / 2) < 6)
itd = itdww * nonlinitd48k[(i / 2) - 1];
}
else if(sr_p == 44100)
{
if((i / 2) < 6)
itd = itdww * nonlinitd[(i / 2) - 1];
}
if(angle > FL(180.))
{
phasel = TWOPI_F * freq * (itd / 2);
phaser = TWOPI_F * freq * -(itd / 2);
}
else
{
phasel = TWOPI_F * freq * -(itd / 2);
phaser = TWOPI_F * freq * (itd / 2);
}
/* polar to rectangular */
hrtflfloat[i] = magl * COS(phasel);
hrtflfloat[i+1] = magl * SIN(phasel);
hrtfrfloat[i] = magr * COS(phaser);
hrtfrfloat[i+1] = magr * SIN(phaser);
}
/* ifft */
csound->InverseRealFFT(csound, hrtflfloat, irlength);
csound->InverseRealFFT(csound, hrtfrfloat, irlength);
for (i = 0; i < irlength; i++)
{
/* scale and pad buffers with zeros to fftbuff */
leftshiftbuffer[i] = hrtflfloat[i];
rightshiftbuffer[i] = hrtfrfloat[i];
}
/* shift for causality...impulse as is is centred around zero time lag...
then phase added. */
/* this step centres impulse around centre tap of filter (then phase
moves it for correct itd...) */
shift = irlength / 2;
for(i = 0; i < irlength; i++)
{
hrtflpad[i] = leftshiftbuffer[shift];
hrtfrpad[i] = rightshiftbuffer[shift];
shift++;
shift = shift % irlength;
}
/* zero pad impulse */
for(i = irlength; i < irlengthpad; i++)
{
hrtflpad[i] = FL(0.0);
hrtfrpad[i] = FL(0.0);
}
/* back to freq domain */
csound->RealFFT(csound, hrtflpad, irlengthpad);
csound->RealFFT(csound, hrtfrpad, irlengthpad);
/* initialize counter */
counter_p = 0;
return OK;
}
virtual int32_t hrtfstat_process(CSOUND *csound, MYFLT *in, MYFLT *outsigl, MYFLT *outsigr, uint32_t offset, uint32_t early, uint32_t nsmps)
{
/* local pointers to p */
/*MYFLT *in = p->in->data;
MYFLT *outsigl = p->outsigl;
MYFLT *outsigr = p->outsigr;*/
/* common buffers and variables */
MYFLT *insig = (MYFLT *)insig_p.auxp;
MYFLT *outl = (MYFLT *)outl_p.auxp;
MYFLT *outr = (MYFLT *)outr_p.auxp;
MYFLT *hrtflpad = (MYFLT *)hrtflpad_p.auxp;
MYFLT *hrtfrpad = (MYFLT *)hrtfrpad_p.auxp;
MYFLT *complexinsig = (MYFLT *)complexinsig_p.auxp;
MYFLT *outspecl = (MYFLT *)outspecl_p.auxp;
MYFLT *outspecr = (MYFLT *)outspecr_p.auxp;
MYFLT *overlapl = (MYFLT *)overlapl_p.auxp;
MYFLT *overlapr = (MYFLT *)overlapr_p.auxp;
int32_t counter = counter_p;
int32_t i;
//uint32_t offset = p->h.insdshead->ksmps_offset;
//uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t j;//, nsmps = CS_KSMPS;
int32_t irlength = irlength_p;
int32_t irlengthpad = irlengthpad_p;
int32_t overlapsize = overlapsize_p;
MYFLT sr = sr_p;
/* if (UNLIKELY(offset)) {
memset(outsigl, '\0', offset*sizeof(MYFLT));
memset(outsigr, '\0', offset*sizeof(MYFLT));
}
if (UNLIKELY(early)) {
nsmps -= early;
memset(&outsigl[nsmps], '\0', early*sizeof(MYFLT));
memset(&outsigr[nsmps], '\0', early*sizeof(MYFLT));
}*/
for(j = offset; j < nsmps; j++)
{
/* ins and outs */
insig[counter] = in[j];
outsigl[j] = outl[counter];
outsigr[j] = outr[counter];
counter++;
if(counter == irlength)
{
/* process a block */
/* look after overlap add stuff */
for(i = 0; i < overlapsize ; i++)
{
overlapl[i] = outl[i+irlength];
overlapr[i] = outr[i+irlength];
}
/* insert insig for complex real,im fft, zero pad */
for (i = 0; i < irlength; i++)
complexinsig[i] = insig[i];
for (i = irlength; i < irlengthpad; i++)
complexinsig[i] = FL(0.0);
csound->RealFFT(csound, complexinsig, irlengthpad);
/* complex multiplication */
csound->RealFFTMult(csound, outspecl, hrtflpad, complexinsig,
irlengthpad, FL(1.0));
csound->RealFFTMult(csound, outspecr, hrtfrpad, complexinsig,
irlengthpad, FL(1.0));
/* convolution is the inverse FFT of above result */
csound->InverseRealFFT(csound, outspecl, irlengthpad);
csound->InverseRealFFT(csound, outspecr, irlengthpad);
/* scaled by a factor related to sr...? */
for(i = 0; i < irlengthpad; i++)
{
outl[i] = outspecl[i] / (sr / FL(38000.0));
outr[i] = outspecr[i] / (sr / FL(38000.0));
}
for(i = 0; i < irlength; i++)
{
outl[i] = outl[i] + (i < overlapsize ? overlapl[i] : FL(0.0));
outr[i] = outr[i] + (i < overlapsize ? overlapr[i] : FL(0.0));
}
/* reset counter */
counter = 0;
} /* end of irlength == counter */
} /* end of ksmps audio loop */
/* update */
counter_p = counter;
return OK;
}
};
/* Csound structure for HOAMBDEC opcode */
typedef struct {
OPDS h;
ARRAYDAT* out; /* output buffers */
MYFLT* setup; /* configuration */
ARRAYDAT* in; /* input buffers */
MYFLT* band; // 0 for mix decoder, 1 for LF decoder, 2 for HF decoder
MYFLT* r; // Distance for NFC. If r=-1 NFC off.
MYFLT* freq_cut; // frequency of band-splitting
MYFLT* type_mix; // 0 for energy, 1 for rms, 2 for amplitude
STRINGDAT* ifilel;
STRINGDAT* ifiler;
int32_t numa; /* i-var p-time storage registers */
int32_t numb;
/* band splitting coefficients */
double a[MAXPOLES];
double b_lf[MAXZEROS+1];
double b_hf[MAXZEROS+1];
/* matrices for LF and HF decoders */
double M_lf[MAX_OUTPUTS][MAX_INPUTS];
double M_hf[MAX_OUTPUTS][MAX_INPUTS];
AUXCH delay[MAX_INPUTS]; /* delay-line state memory base pointer */
double *currPos[MAX_INPUTS]; /* delay-line current position pointer */ /* >>Was float<< */
int32_t ndelay; /* length of delay line (i.e. filter order) */
// NFC temp variables
float fRec2[MAX_INPUTS][2];
float fRec0[MAX_INPUTS][2];
float fRec3[MAX_INPUTS][2];
float fRec5[MAX_INPUTS][2];
float fRec6[MAX_INPUTS][2];
float fRec8[MAX_INPUTS][2];
float fRec9[MAX_INPUTS][2];
float fRec11[MAX_INPUTS][2];
float fRec12[MAX_INPUTS][2];
float fRec14[MAX_INPUTS][2];
int order; // order
bool horizontal; // true if the configuration is horizontal
int n_signals; // number of ambisonics signals
hrtf* binaural[20]; // instances of hrtf class
AUXCH binaural_mem[20]; // aux for hrtf
} HOAMBDEC;
typedef struct FCOMPLEX {double r,i;} fcomplex;
static double readFilter(HOAMBDEC*, int32_t, int);
static void insertFilter(HOAMBDEC*,double, int);
static void process_nfc(CSOUND*,HOAMBDEC*, int, int, int, int, int);
#ifndef MAX
#define MAX(a,b) ((a>b)?(a):(b))
#define MIN(a,b) ((a>b)?(b):(a))
#endif
/*#define POLEISH (1) */ /* 1=poleish pole roots after Laguer root finding */
typedef struct FPOLAR {double mag,ph;} fpolar;
/* hoambdec initialization routine */
static int32_t ihoambdec(CSOUND *csound, HOAMBDEC* p)
{
int32_t i;
/* since i-time arguments are not guaranteed to propegate to p-time
* we must copy the i-vars into the p structure.
*/
int n1;
bool nfc = false;
/* numbers of a and b coefficients */
p->numa = 2;
p->numb = 3;
/* First check bounds on initialization arguments */
if (UNLIKELY((p->numb<1) || (p->numb>(MAXZEROS+1)) ||
(p->numa<0) || (p->numa>MAXPOLES)))
return csound->InitError(csound, Str("Filter order out of bounds: "
"(1 <= nb < 51, 0 <= na <= 50)"));
/* Calculate the total delay in samples and allocate memory for it */
p->ndelay = MAX(p->numb-1,p->numa);
int type_mix = (int)*(p->type_mix);
int n_outs = p->out->sizes[0];
int n_ins = p->in->sizes[0];
char buffer [50];
int isetup = (int)*(p->setup);
// int order;
if (isetup == 21)
n_outs = 8;
if (isetup == 31)
n_outs = 20;
switch (n_ins) {
case 4: p->order = 1; break;
case 9: p->order = 2; break;
case 16: p->order = 3; break;
case 25: p->order = 4; break;
case 36: p->order = 5; break;
default : return csound->InitError(csound, Str("illegal number of inputs"));
}
if ((isetup == 1) & (p->order >= 2)) {
return csound->InitError(csound, Str("Stereo configuration only works with first order"));
}
if ((isetup == 2) & (p->order >= 2)) {
return csound->InitError(csound, Str("Quad configuration only works with first order"));
}
if ((isetup == 3) & (p->order >= 3)) {
return csound->InitError(csound, Str("5.0 configuration only works with first and second order"));
}
if ((isetup == 4) & (p->order >= 4)) {
return csound->InitError(csound, Str("Octagon configuration only works with first, second and third order"));
}
if ((isetup == 5) & (p->order >= 2)) {
return csound->InitError(csound, Str("Cube configuration only works with first order"));
}
if ((isetup == 6) & (p->order >= 3)) {
return csound->InitError(csound, Str("Hexagon configuration only works with first and second order"));
}
if ((isetup == 21) & (p->order >= 4)) {
return csound->InitError(csound, Str("2D Binaural configuration only works with first, second and third order"));
}
if ((isetup == 31) & (p->order >= 4)) {
return csound->InitError(csound, Str("3D Binaural configuration only works with first, second and third order"));
}
p->horizontal = ((isetup == 1) | (isetup == 2) | (isetup == 3) | (isetup == 4) | (isetup == 6) | (isetup == 21));
if (p->horizontal)
p->n_signals = 2*p->order + 1; //2l+1
else
p->n_signals = n_ins; //(l+1)^2
for (int j = 0; j < n_ins; j++) {
csound->AuxAlloc(csound, p->ndelay * sizeof(double), &p->delay[j]);
}
/* Set current position pointer to beginning of delay */
for (int j = 0; j < n_ins; j++) {
p->currPos[j] = (double*)p->delay[j].auxp;
}
// Band-splitting filters coefficients
double freq;
if ((int)*(p->freq_cut) == 0) {
freq = 400;
} else {
freq = (double)*(p->freq_cut);
}