-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhrir_cmdline.cpp
2400 lines (1655 loc) · 53.4 KB
/
hrir_cmdline.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
/*
Copyright (C) 2024 BrerDawg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//hrir_cmdline.cpp
//v1.01 02-mar-2024 //
//refer: https://github.com/sofacoustics/API_Cpp
//refer: https://github.com/sofacoustics/API_Cpp/blob/master/libsofa/src/sofainfo.cpp //from https://github.com/sofacoustics/API_Cpp: useful code which builds a 'sofainfo' app that dumps sofa file details, the code shows the use of libsofa functions, will 'assert' on some sofa files though, could just be versioning issues
//refer: https://github.com/hoene/libmysofa //not used for this prj
//refer: https://www.sofaconventions.org/mediawiki/index.php/Files
#include "hrir_cmdline.h"
vector<double> vcoeff0, vcoeff1;
bool gverb = 0;
int exit_code;
float srate_wav = 48000;
float srate = srate_wav;
#ifdef use_mysofa //no used
//struct MYSOFA_EASY *hrtf = NULL;
#endif
bool brecord_allow = 1; //for user record
bool brecord = 0;
audio_formats af0;
st_audio_formats_tag saf0;
string sdemo_record = "zzrecord.wav";
int record_cnt = 0;
rtaud rta; //rtaudio
st_rtaud_arg_tag st_rta_arg; //this is used in audio proc callback to work out chan in/out counts
int framecnt;
bool audio_started = 0;
float time_per_sample;
int audio_source = 0;
float aud_gain_master = 0.5f;
float aud_gain_hrft = 1.0f;
bool invert_ch1 = 0;
st_3D_src_tag st3d[cn_3d_sources_max];
int audcnt = 0; //number of audio files specified on cmdline
int cur_aud = 0; //current audio src which user can manipulate
//filter_code::st_fir fir00, fir01; //these allow filter switch over in audio proc, when a new hrir impulse resp. is avail
//filter_code::st_fir fir10, fir11;
//int fir_state00 = 0; //0: fir00/fir01 is inactive and can be modified by 'main()' thread
//1: fir00/fir01 is loaded and waiting to be flagged as active by audio proc thread
//2: fir00/fir01 is active in audio proc thread
//3: fir00/fir01 is flagged to be deactivated by audio proc thread
//int fir_state10 = 0; //10: fir10/fir10 is inactive and can be modified by 'main()' thread
//11: fir10/fir10 is loaded and waiting to be flagged as active by audio proc thread
//12: fir10/fir10 is active in audio proc thread
//13: fir10/fir10 is flagged to be deactivated by audio proc thread
string swav_out_fname = "zzzout.wav";
string swav_in_fname = "./crinkle_plastic.wav";
string ssofa_fname = "SimpleFreeFieldHRIR_1.0.sofa";
string slayout_fname = "zzlayout";
string slayout_new_fname = "zzlayout_new.txt";
int layout_idx = 0;
bool bpause_on_err = 1;
vector<st_imp_resp_tag> vsrc_pos; //holds the azim, elev and radius values for each entry found in .sofa file
//function prototypes
int find_imp_resp_idx_from_spherical( sofa::File *theFile, float azim, float elev );
bool get_imp_resp_samples_at_idx( sofa::File *theFile, sofa::SimpleFreeFieldHRIR *hrir, unsigned int idx, vector<double> &v0, vector<double> &v1 );
void do_error_exit( int exit_cde )
{
if( bpause_on_err )
{
printf( "press a key to exit.\n" );
getchar();
}
exit( exit_cde );
}
bool start_audio()
{
printf("start_audio()\n" );
rta.verbose = 1;
//rtaudio_probe(); //dump a list of devices to console
RtAudio::StreamOptions options;
options.streamName = cnsAppName;
options.numberOfBuffers = 2; //for jack (at least) this is hard set by jack's settings and can't be changed via this parameter
options.flags = 0; //0 means interleaved, use oring options, refer 'RtAudioStreamFlags': RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_SCHEDULE_REALTIME, RTAUDIO_ALSA_USE_DEFAULT, RTAUDIO_JACK_DONT_CONNECT
// !!! when using RTAUDIO_JACK_DONT_CONNECT, you can create any number of channels, you can't do this if auto connecting as 'openStream()' will fail if there is not enough channel mating ports
options.priority = 0; //only used with flag 'RTAUDIO_SCHEDULE_REALTIME'
uint16_t device_num_out = 0; //use 0 for default device to be used
int channels_out = 2; //if auto connecting (not RTAUDIO_JACK_DONT_CONNECT), there must be enough mathing ports or 'openStream()' will fail
int first_chan_out = 0;
uint16_t frames = 2048;
unsigned int audio_format = RTAUDIO_FLOAT32; //see rtaudio docs 'RtAudioFormat' for supported format types, adj audio proc code to suit
//st_osc_params.freq0 = 200; //set up some audio proc callback user params
//st_osc_params.gain0 = 0.1;
//st_osc_params.freq1 = 600;
//st_osc_params.gain1 = 0.1;
//st_rta_arg.usr_ptr = (void*)&st_osc_params;
if( !rta.start_stream_out( device_num_out, channels_out, first_chan_out, srate, frames, audio_format, &options, &st_rta_arg, (void*)cb_audio_proc_rtaudio ) ) //output only
//if( !rta.start_stream_out( device_num_out, channels_out, first_chan_out, srate, frames, audio_format, &options, &st_rta_arg, (void*)cb_audio_proc_rtaudio_migrate ) ) //output only
{
printf("start_audio() - failed to open audio device!!!!\n" );
return 0;
}
else{
printf("start_audio() - audio out device %d opened, options.flags 0x%02x, srate is: %d, framecnt: %d\n", device_num_out, options.flags, rta.get_srate(), rta.get_framecnt() ); //output only
}
srate = rta.get_srate();
framecnt = rta.get_framecnt();
printf("start_audio() - srate %f, framecnt: %d\n", srate, framecnt );
time_per_sample = 1.0/srate;
audio_started = 1;
return 1;
}
void stop_audio()
{
printf( "stop_audio()\n" );
audio_started = 0;
mystr m1;
m1.delay_ms( (float)framecnt / srate * 1e3 ); //wait one audio proc period
rta.stop_stream();
}
//refer: https://cboard.cprogramming.com/c-programming/177987-there-inkey-c.html
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
void show_usage()
{
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("Head Related Impulse Response v1.01 (HRIR Binaural 3D for Headphones)\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("Need to specify 2 params minimum, eg:\n");
printf("./hrir_cmdline sofa=SimpleFreeFieldHRIR_1.0.sofa aud=src_fname //'sofa_fname' is file that has impulse responses\n" );
printf("./hrir_cmdline sofa=SimpleFreeFieldHRIR_1.0.sofa aud=water.wav aud=crinkle.wav //multiple audio files will work, srate must match sofa srate ('.wav' '.au' only) \n" );
printf("./hrir_cmdline sofa=SimpleFreeFieldHRIR_1.0.sofa aud=audio.au -v //verbose on, shows srate of sofa file\n" );
printf("\n");
printf("Note: Set system volume to SAFE level, then adj master gain, finally adj hrir gain for each audio file\n\n");
printf("Note: use '5' or '6' to select an audio file when you have specified multiple\n");
printf("use '0' to sel white noise\n" );
printf("use '1' to sel play original audio file in a loop (without filtering)\n" );
printf("use '2' to sel play audio file(s) in a loop (with hrir filtering)\n" );
printf("use '5' '6' to change which audio file is cur. sel\n" );
printf("use '9' to sel play 2 tones (lower freq is on left channel)\n" );
printf("use 'a' 's' to change azimuth\n" );
printf("use 'w' 'z' to change elevation\n" );
//printf("use 'r' 't' to change radius(does nothing)\n" );
printf("use 'c' 'C' to zero to front(center, zero azim/elev)\n" );
printf("use 'f' 'F' to flip between front(center) and cur azim/elev\n" );
printf("use 'l' 'L' to load a layout with gain/azim/elev/radius etc from file 'zzlayoutxx.txt'\n" );
printf("use ':' to save a layout with gain/azim/elev/radius etc to file 'zzlayout_new.txt'\n" );
printf("use '[' ']' to alter master gain (use { } also )\n" );
printf("use ',' '.' to alter hrir filter gain (use < > also )\n" );
printf("use 'm' to solo the audio file\n" );
printf("use 'M' to mute audio file\n" );
printf("use 'r' to auto rotate azimuth\n" );
printf("use 'R' to auto cycle between all avail elevations\n" );
printf("use 'i' 'I' to invert phase of ch1 (right chan)\n" );
printf("use '|' to toggle record, recording saved on exit, as 'zzrecord.wav'\n" );
printf("use 'v' 'V' to toggle verbose\n" );
printf("use 'h' 'H' to show this\n" );
printf("use 'b' 'B' to bye/exit\n" );
printf("==================================================================\n");
}
#ifdef use_mysof
/*
bool load_hrir_fir( int which, int filter_length, float azim, float elev, float radius )
{
if( which < 0 )
{
printf("load_hrir_fir() - which was < 0 \n");
return 0;
}
if( which > 1 )
{
printf("load_hrir_fir() - which was > 1 \n");
return 0;
}
if( filter_length <= 0 )
{
printf("load_hrir_fir() - filter_length was <= 0\n");
return 0;
}
float spherical[3];
//refer: https://github.com/hoene/libmysofa
//with phi (azimuth in degree, measured counterclockwise from the X axis), theta (elevation in degree, measured up from the X-Y plane), and r (distance between listener and source) as parameters in the float array and x,y,z as response in the same array.
spherical[0] = azim; //azimuth (here I assume y axis goes up/down, so rotation about it should be in rotation in horizontal plane )
spherical[1] = elev; //elevation
spherical[2] = radius; //distance of source
mysofa_s2c(spherical);
float xx = spherical[0];
float yy = spherical[1];
float zz = spherical[2];
printf( "load_hrir_fir() - azimuth %f elev %f radius %f\n", azim, elev, radius );
printf( "load_hrir_fir() - xx %f yy %f zz %f\n", xx, yy, zz );
float *leftIR = new float[filter_length]; // [-1. till 1]
float *rightIR = new float[filter_length];
float leftDelay; // unit is sec.
float rightDelay;
mysofa_getfilter_float( hrtf, xx, yy, zz, leftIR, rightIR, &leftDelay, &rightDelay);
//mysofa_getfilter_float_nointerp( hrtf, xx, yy, zz, leftIR, rightIR, &leftDelay, &rightDelay);
printf( "load_hrir_fir() - leftDelay %f rightDelay %f\n", leftDelay, rightDelay );
vcoeff0.clear();
vcoeff1.clear();
for( int i = 0; i < filter_length; i++ ) //get impulse resp.
{
vcoeff0.push_back( leftIR[i] );
vcoeff1.push_back( rightIR[i] );
}
delete[] leftIR;
delete[] rightIR;
if( 0 )
{
filter_code::filter_fir_windowed( filter_code::fwt_hann, filter_code::fpt_lowpass, 500, 2400, 2400, srate, vcoeff0 );
vcoeff1 = vcoeff0;
}
if( which == 0 )
{
filter_code::delete_filter( fir00 );
filter_code::delete_filter( fir01 );
if( filter_code::create_filter_from_coeffs( fir00, vcoeff0 ) == 0 )
{
printf( "failed in call: 'create_filter_from_coeffs()' see 'fir00'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
if( filter_code::create_filter_from_coeffs( fir01, vcoeff1 ) == 0 )
{
printf( "failed in call: 'create_filter_from_coeffs()' see 'fir01'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
}
if( which == 1 )
{
filter_code::delete_filter( fir10 );
filter_code::delete_filter( fir11 );
if( filter_code::create_filter_from_coeffs( fir10, vcoeff0 ) == 0 )
{
printf( "failed in call: 'create_filter_from_coeffs()' see 'fir10'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
if( filter_code::create_filter_from_coeffs( fir11, vcoeff1 ) == 0 )
{
printf( "failed in call: 'create_filter_from_coeffs()' see 'fir11'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
}
return 1;
}
*/
#endif
//create fir using impulse response at spec idx
bool lib_sofa_load_hrir_fir( sofa::File *theFile, sofa::SimpleFreeFieldHRIR *hrir, int struct_idx, int which, float azim, float elev, float radius, bool force_internal_filter_ir_test, int &idx_used )
{
bool vb = gverb;
if( which < 0 )
{
printf("lib_sofa_load_hrir_fir() - which was < 0 \n");
return 0;
}
if( which > 1 )
{
printf("lib_sofa_load_hrir_fir() - which was > 1 \n");
return 0;
}
if(vb)printf("lib_sofa_load_hrir_fir() - struct_idx %d which %d azim %f elev %f\n", struct_idx, which, azim, elev );
//vector<double> v0, v1;
idx_used = find_imp_resp_idx_from_spherical( theFile, azim, elev );
if( idx_used < 0 )
{
printf( "did not find a close match to azim %.4f elev %.4f\n", azim, elev );
}
else{
if(vb)printf( "found a close match to azim %.4f elev %.4f at index %d\n", azim, elev, idx_used );
if( get_imp_resp_samples_at_idx( theFile, hrir, idx_used, vcoeff0, vcoeff1 ) == 0 )
{
printf( "for index %d, failed to find a data for position azim %.4f elev %.4f\n", idx_used, azim, elev );
}
else{
if(vb)printf( "for index %d, at azim %.4f elev %.4f, got imp resp. sizes %d %d\n", idx_used, azim, elev, (int)vcoeff0.size(), (int)vcoeff1.size() );
}
}
if( force_internal_filter_ir_test )
{
filter_code::filter_fir_windowed( filter_code::fwt_hann, filter_code::fpt_lowpass, 512, 18000, 18000, srate, vcoeff0 );
vcoeff1 = vcoeff0;
}
if( which == 0 )
{
filter_code::delete_filter( st3d[struct_idx].fir00 );
filter_code::delete_filter( st3d[struct_idx].fir01 );
// filter_code::delete_filter( fir00 );
// filter_code::delete_filter( fir01 );
if( filter_code::create_filter_from_coeffs( st3d[struct_idx].fir00, vcoeff0 ) == 0 )
{
printf( "lib_sofa_load_hrir_fir() - failed in call: 'create_filter_from_coeffs()' see 'fir00'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
if( filter_code::create_filter_from_coeffs( st3d[struct_idx].fir01, vcoeff1 ) == 0 )
{
printf( "lib_sofa_load_hrir_fir() - failed in call: 'create_filter_from_coeffs()' see 'fir01'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
}
if( which == 1 )
{
filter_code::delete_filter( st3d[struct_idx].fir10 );
filter_code::delete_filter( st3d[struct_idx].fir11 );
if( filter_code::create_filter_from_coeffs( st3d[struct_idx].fir10, vcoeff0 ) == 0 )
{
printf( "lib_sofa_load_hrir_fir() - failed in call: 'create_filter_from_coeffs()' see 'fir10'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
if( filter_code::create_filter_from_coeffs( st3d[struct_idx].fir11, vcoeff1 ) == 0 )
{
printf( "lib_sofa_load_hrir_fir() - failed in call: 'create_filter_from_coeffs()' see 'fir11'\n" );
exit_code = 1;
do_error_exit( exit_code );
}
}
return 1;
}
static inline const std::size_t array3DIndex(const unsigned long i,
const unsigned long j,
const unsigned long k,
const unsigned long dim1,
const unsigned long dim2,
const unsigned long dim3)
{
return dim2 * dim3 * i + dim3 * j + k;
}
static inline const std::size_t array2DIndex(const unsigned long i,
const unsigned long j,
const unsigned long dim1,
const unsigned long dim2)
{
return dim2 * i + j;
}
/*
bool get_data_2_receivers( unsigned int idx, const sofa::SimpleFreeFieldHRIR *hrir, vector<float> &vf0, vector<float> &vf1 )
{
bool vb = 1;
if(vb)printf( "get_data_2_receivers()\n" );
vf0.clear();
vf1.clear();
const unsigned int M = (unsigned int) hrir->GetNumMeasurements();
const unsigned int R = (unsigned int) hrir->GetNumReceivers();
const unsigned int N = (unsigned int) hrir->GetNumDataSamples();
if( R < 2 )
{
printf( "get_data_2_receivers() - num of receivers is insufficient, only have %d\n", R );
return 0;
}
if( idx >= M )
{
printf( "get_data_2_receivers() - idx is out of range, limit is %d\n", M );
return 0;
}
if(vb)printf( "get_data_2_receivers() - M(num measurements): %d R(num receivers): %d N(num data smpls): %d\n", M, R, N );
return 1;
}
*/
//returns index to a 'vsrc_pos' entry, that best matches 'azim' abd ''
//returns -1 if none found
int find_imp_resp_idx_from_spherical( sofa::File *theFile, float azim, float elev )
{
bool vb = gverb;
bool vb1 = 0;
if(vb)printf( "find_imp_resp_idx_from_spherical() - need azim %f elev %f\n", azim, elev );
if( vsrc_pos.size() == 0 ) return -1;
if( vsrc_pos.size() == 1 ) return 0; //only one entry ?
float delta;
float delta_min = 1e6; //some big num
int delta_min_idx = 0;
float delta_azim_min = 1e6; //some big num
int delta_azim_idx = 0;
float delta_elev_min = 1e6; //some big num
int delta_elev_idx = 0;
//find closest azim idx
//vector<int>vidx0, vidx1;
for( int i = 0; i < vsrc_pos.size(); i++ )
{
st_imp_resp_tag oir0 = vsrc_pos[i];
// st_imp_resp_tag oir1 = vsrc_pos[i+1];
float delta0 = fabs( azim - oir0.azim );
if( delta0 < delta_azim_min ) //find all entries are a minimum away from azim
{
// delta_azim_min = delta0;
// delta_azim_idx = i;
// vidx0.push_back( i );
//if(vb)printf( "get_imp_resp_idx_from_spherical() min delta azim %f\n", delta0 );
if(vb1)printf( "vsrc_pos azim %f elev %f\n", oir0.azim, oir0.elev );
}
float delta1 = fabsf( oir0.elev - elev ); //find all entries are a minimum away from elev
if( delta1 < delta_elev_min )
{
// delta_elev_min = delta1;
// delta_elev_idx = i;
// vidx1.push_back( i );
//if(vb)printf( "get_imp_resp_idx_from_spherical() min delta elev %f\n", delta1 );
}
delta = delta0 + delta1;
if( delta < delta_min )
{
delta_min = delta;
delta_min_idx = i;
}
/*
for( int i = 0; i < (vsrc_pos.size() - 1); i++ )
{
st_imp_resp_tag oir0 = vsrc_pos[i];
st_imp_resp_tag oir1 = vsrc_pos[i+1];
if( ( elev >= oir0.elev ) && ( elev <= oir1.elev ) ) //assume sorted by elev
{
if( ( azim >= oir0.azim ) && ( azim <= oir1.azim ) )
{
float delta0 = fabsf( oir0.elev - elev );
float delta1 = fabsf( oir1.elev - elev );
if( delta0 < delta1 ) //work out which entry is closest
{
return i;
}
else{
if( (i+1) >= vsrc_pos.size() ) return i;
else return i+1;
}
}
}
*/
/*
float delta_azim_min = 1e6; //some big num
float delta_elev_min = 1e6; //some big num
int delta_azim_idx;
if( ( azim >= oir0.azim ) && ( azim <= oir1.azim ) )
{
if( ( elev >= oir0.elev ) && ( elev <= oir1.elev ) ) //assume sorted by azim
{
float delta0 = fabsf( oir0.azim - azim );
float delta1 = fabsf( oir1.azim - azim );
if( delta0 < delta1 ) //work out which entry is closest
{
return i;
}
else{
if( (i+1) >= vsrc_pos.size() ) return i;
else return i+1;
}
}
}
*/
}
/*
for( int i = 0; i < vidx0.size(); i++ )
{
for( int j = 0; j < vidx1.size(); j++ )
{
if( vidx0[i] == vidx1[j] ) //both azim and elev were at minimum from this entry?
{
return vidx0[i];
}
}
}
*/
if(vb)printf( "find_imp_resp_idx_from_spherical() - nearest match found at idx %d, azim %f elev %f\n", delta_min_idx, vsrc_pos[delta_min_idx].azim, vsrc_pos[delta_min_idx].elev );
return delta_min_idx;
}
bool get_imp_resp_samples_at_idx( sofa::File *theFile, sofa::SimpleFreeFieldHRIR *hrir, unsigned int idx, vector<double> &v0, vector<double> &v1 )
{
bool vb = gverb;
if(vb)printf( "get_imp_resp_samples_at_idx() - idx %d\n", idx );
v0.clear();
v1.clear();
//if(vb)printf( "get_imp_resp_samples_at_idx() - here 1\n" );
if( idx >= vsrc_pos.size() )
{
printf( "get_imp_resp_samples_at_idx() - index %d is out of range, limit is %d\n", idx, (int)vsrc_pos.size() - 1 );
return 0;
}
if( hrir == 0 )
{
printf( "get_imp_resp_samples_at_idx() - hrir pointer is zero\n" );
return 0;
}
const unsigned int M = (unsigned int) hrir->GetNumMeasurements();
const unsigned int R = (unsigned int) hrir->GetNumReceivers();
const unsigned int N = (unsigned int) hrir->GetNumDataSamples();
//if(vb)printf( "get_imp_resp_samples_at_idx() - M %d R %d N %d\n", M, R, N );
//if(vb)printf( "get_imp_resp_samples_at_idx() - here 2\n" );
std::vector< double > data;
//v0.push_back( 0 );
//v1.push_back( 0 );
//return 1;
hrir->GetDataIR( data );
//if(vb)printf( "get_imp_resp_samples_at_idx() - here 3\n" );
int i = idx;
for( std::size_t j = 0; j < R; j++ )
{
for( std::size_t k = 0; k < N; k++ )
{
const std::size_t index = array3DIndex( i, j, k, M, R, N );
if( j == 0 )
{
v0.push_back( data[ index ] );
}
if( j == 1 )
{
v1.push_back( data[ index ] );
}
// output << data[ index ] << std::endl;
}
}
//if(vb)printf( "get_imp_resp_samples_at_idx() - here 4\n" );
return 1;
}
//refer https://github.com/sofacoustics/API_Cpp sofainfo.cpp
bool lib_sofa_open( string fname, sofa::File **theFile_in, sofa::SimpleFreeFieldHRIR **hrir_in )
{
bool vb = gverb;
sofa::File *theFile = new sofa::File( fname.c_str() );
*theFile_in = theFile;
const bool isSOFA = theFile->IsValid();
if( isSOFA == 0 )
{
*theFile_in = 0;
*hrir_in = 0;
printf( "lib_sofa_open() - sofa file not valid, file '%s'\n", ssofa_fname.c_str() );
return 0;
}
if(vb)printf( "lib_sofa_open() - sofa reported as valid, file '%s'\n", ssofa_fname.c_str() );
sofa::SimpleFreeFieldHRIR *hrir = new sofa::SimpleFreeFieldHRIR( ssofa_fname.c_str() );
*hrir_in = hrir;
const bool isHRIR = hrir->IsValid();
if( isHRIR == 0 )
{
*hrir_in = 0;
printf( "lib_sofa_open() - hrir.IsValid() failed, file '%s'\n", ssofa_fname.c_str() );
return 0;
}
if(vb)printf( "lib_sofa_open() - hrir reported as valid, file '%s'\n", ssofa_fname.c_str() );
double sr = 0.0;
bool ok = hrir->GetSamplingRate( sr );
if( ok == 0 )
{
printf( "lib_sofa_open() - hrir.GetSamplingRate() failed, file '%s'\n", ssofa_fname.c_str() );
return 0;
}
sofa::Units::Type units;
hrir->GetSamplingRateUnits( units );
if(vb)printf( "lib_sofa_open() - hrir.GetSamplingRateUnits(): %.2f %s\n", sr, sofa::Units::GetName( units ).c_str() );
//int idx = 0;
//vector<float> vf0, vf1;
//get_data_2_receivers( idx, hrir, vf0, vf1 );
const unsigned int M = (unsigned int) hrir->GetNumMeasurements();
const unsigned int R = (unsigned int) hrir->GetNumReceivers();
const unsigned int N = (unsigned int) hrir->GetNumDataSamples();
//if(vb)printf( "hrir->GetSamplingRateUnits(): M(num measurements): %d R(num receivers): %d N(num data smpls): %d\n", M, R, N );
std::vector< double > data;
hrir->GetDataIR( data );
if(vb)printf( "lib_sofa_open() - hrir.GetDataIR() data.size() %d\n", (int)data.size() );
sofa::Coordinates::Type coordinates;
ok = theFile->GetListenerUp( coordinates, units );
if( ok == 0 )
{
if(vb)printf( "lib_sofa_open() - call to GetListenerUp() failed, '%s'\n", ssofa_fname.c_str() );
}
// SOFA_ASSERT( ok == true );
return isSOFA;
}
//, float &azim_min , float &azim_max, float &elev_min, float &elev_max
//get each impulse response's details
//refer https://github.com/sofacoustics/API_Cpp sofainfo.cpp PrintSource(..)
bool lib_sofa_get_file_details( sofa::File *theFile, vector<st_imp_resp_tag> &vir, st_3D_src_tag &o3d )
{
bool vb = gverb;
bool vb1 = gverb;
if(vb)printf("lib_sofa_get_file_details()\n" );
vir.clear();
sofa::Coordinates::Type coordinates;
sofa::Units::Type units;
const bool ok = theFile->GetSourcePosition( coordinates, units );
SOFA_ASSERT( ok == true );
if(vb)printf("lib_sofa_get_file_details() - coordinates type is %d, refer to enum: sofa::Coordinates::Type (e.g: kCartesian, kSpherical)\n", (int)coordinates ); //gc
if( coordinates == sofa::Coordinates::kSpherical );
{
}
// SOFA_ASSERT( ok == true );
if(vb) printf( "lib_sofa_get_file_details() - SourcePosition:Type %s\n", sofa::Coordinates::GetName( coordinates ).c_str() );
if(vb) printf( "lib_sofa_get_file_details() - SourcePosition:Type %s\n", sofa::Units::GetName( units ).c_str() );
// output << sofa::String::PadWith( "SourcePosition:Type" ) << " = " << sofa::Coordinates::GetName( coordinates ) << std::endl;
// output << sofa::String::PadWith( "SourcePosition:Units" ) << " = " << sofa::Units::GetName( units ) << std::endl;