-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgc_srateconv.cpp
1832 lines (1198 loc) · 65.1 KB
/
gc_srateconv.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) 2023 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.
*/
//make SURE to ENABLE compiler optimizer, has halved resampler proc times: -O3 -mfpmath=sse -ffast-math -msse3
//gc_srateconv.cpp
//v1.01 06-sep-2018 //see rem'd out table based test srconv: test_srconv_tbl()
//v1.02 07-nov-2020 //changed 'twopi' to a constant
//v1.03 07-dec-2020 //added: 'qdss_resample_float_complete()'
//v1.04 18-dec-2020 //added: 'float farrow_resample_float()', 'float farrow_resample_double()'
//v1.05 19-dec-2022 //added: 'qdss_resample_float_vector()', 'farrow_resample_float_vector()'
//v1.06 22-jul-2023 //moded for namespace 'filter_code::'
//v1.07 18-sep-2023 //added namespace 'gc_srateconv_code' and 'farrow_resample_double_vector()'
//v1.08 16-oct-2023 //added 'qdss_resample_double_continuous()' 'qdss_resample_float_continuous()'
#include "gc_srateconv.h"
namespace gc_srateconv_code
{
//extern double pi;
//extern double twopi;
#define twopi (2.0*M_PI)
#define ftwopi (2.0f*(float)M_PI)
//--- converted from Basic language code ----
// refer: http://www.nicholson.com/rhn/dsp.html
//
// rem: use 'baudline' to assess quality
// this function can be used in sample rate conversion code,
// it generates one intermediate sample from a supplied collection of original samples, it calcs this sample
// using a windowed sinc that implements a lowpass fir. The position of the new sample point is defined by the 'x'
// value. If 'x' is 50.5 then the fir is centered at that location and the original samples either side of this (see 'wnwdth')
// are convolved with fir to produce the new sample value.
// x - set 'x' to the position of the sample to generate, the repeated calling of this function and the amount of 'x' stepping between
// calls will determine the resultant sample rate.
// if 'x' stepping is: 2, you will be up-converting source srate by 2, (e.g: 48KHz->96KHz, use fmax around 24KHz)
// if 'x' stepping is: 0.25, you will be down-converting source srate by 4, (e.g: 48KHz->12KHz, use fmax around 5.5KHz)
// fsrate - is the sample rate of the source signal.
// fmax - is cutoff freq of interpolation filter (the 'on the fly' calc'd windowed sinc filter), set this to less than half the desired srate,
// so if down-converting 48KHz to 24KHz, set fmax to say 11KHz, a safety margin is req, and is related to how big the 'wnwdth' has been set to,
// if up-converting 11KHz to 24KHz, set fmax to say 12KHz, a safety margin here is not so important as the original signal would
// only contain freqs to 12KHz in anycase.
//
// wnwdth - is number of calc'd coeffs(taps) to use for the fir interpolation filter, bigger 'wnwdth' gives a quicker cutoff transition and
// better antialias lowpass filtering, but it increases execution times, try: 32, 64, 200(there is no need for it to be a factor of two)
// buf - is a ptr to an buf holding source samples, best results are obtained if the samples are either side of
// the sample to generate, and that there are enough samples to match the fir coeff count spec with: wnwdth
// bufsz - size of the buf
//
// Ron Nicholson's QDSS ReSampler cookbook recipe
// QDSS = Quick, Dirty, Simple and Short
// Version 0.1b - 2007-Aug-01
// Copyright 2007 Ronald H. Nicholson Jr.
// No warranties implied. Error checking, optimization, and
// quality assessment of the "results" is left as an exercise
// for the student.
// (consider this code Open Source under a BSD style license)
// IMHO. YMMV. http://www.nicholson.com/rhn/dsp.html
// QDSS Windowed-Sinc ReSampling subroutine in Basic
// This function can also be used for interpolation of FFT results
// function parameters
// x = new sample point location (relative to old indexes)
// (e.g. every other integer for 0.5x decimation)
// buf = original data array
// bufsz = size of data array
// fmax = low pass filter cutoff frequency
// fsrate = sample rate
// wnwdth = width of windowed Sinc used as the low pass filter
// resamp() returns a filtered new sample point
// Notes:
// fmax should be less than half of fsrate (src srate), and less than half of new_fsrate (the reciprocal of the x step size).
// Filter quality increases with a larger window width. The wider the window, the closer fmax can approach half of fsr or new_fsrate.
// Several operations inside the FOR loop can be pre-calculated.
// There are more optimal windows than the von Hann window.
// If the x step size is rational the same Window and Sinc values will be recalculated repeatedly.
// Therefore these values can either be cached, or pre-calculated and stored in a table (polyphase interpolation); or
// interpolated from a smaller pre-calculated table; or computed from a set of low-order polynomials fitted to each section or
// lobe between zero-crossings of the windowed Sinc (Farrow). (Performance optimization is left as an exercise for the student).
double qdss_resample( double x, double *buf, unsigned int bufsz, double fmax, double fsrate, int wnwdth )
{
double r_g,r_w,r_a,r_snc,r_y; //some local variables
int i, j;
if( wnwdth <= 0 ) return 0;
if( bufsz < ( wnwdth / 2 ) ) return 0;
r_g = 2.0 * fmax / fsrate; //calc gain correction factor
r_y = 0;
for ( i = -( wnwdth / 2 ); i <= ( wnwdth / 2 - 1 ); i++ ) //for 1 window width
{
j = (int) x + i; //calc input sample index
r_w = 0.5 - 0.5 * cos( twopi * ( 0.5 + ( j - x ) / wnwdth ) ); //make a von hann sample, will be used taper sinc's length
r_a = twopi * ( j - x ) * fmax / fsrate; //curr sinc location
r_snc = 1;
if ( r_a != 0 ) r_snc = sin( r_a ) / r_a; //make a sinc (sin x/x) lpf sample
//printf("i: %d, r_w: %f, r_a: %f, r_snc: %f\n", i, r_w, r_a, r_snc );
if ( ( j >= 0 ) & ( j < bufsz ) ) //src sample avail?
{
r_y = r_y + r_g * r_w * r_snc * buf[ j ]; //convolve/mac: first: apply von hann tapering to sinc, then both these to a src sample (adj gain as well)
}
else{
//printf("qdss_resample() - forcing zero as j is %d\n", j );
}
}
return r_y; //new filtered intermediate sample
}
float qdss_resample_float( float x, float *buf, unsigned int bufsz, double fmax, double fsrate, int wnwdth )
{
double r_g,r_w,r_a,r_snc,r_y; //some local variables
int i, j;
if( wnwdth <= 0 ) return 0;
if( bufsz < ( wnwdth / 2 ) ) return 0;
r_g = 2.0 * fmax / fsrate; //calc gain correction factor
r_y = 0.0f;
for ( i = -( wnwdth / 2 ); i <= ( wnwdth / 2 - 1 ); i++ ) //for 1 window width
{
j = (int) x + i; //calc input sample index
r_w = 0.5 - 0.5 * cos( twopi * ( 0.5 + ( j - x ) / wnwdth ) ); //make a von hann sample, will be used taper sinc's length
r_a = twopi * ( j - x ) * fmax / fsrate; //curr sinc location
r_snc = 1;
if ( r_a != 0 ) r_snc = sin( r_a ) / r_a; //make a sinc (sin x/x) lpf sample
//printf("i: %d, r_w: %f, r_a: %f, r_snc: %f\n", i, r_w, r_a, r_snc );
if ( ( j >= 0 ) & ( j < bufsz ) ) //src sample avail?
{
r_y = r_y + r_g * r_w * r_snc * buf[ j ]; //convolve/mac: first: apply von hann tapering to sinc, then both these to a src sample (adj gain as well)
}
}
return r_y; //new filtered intermediate sample
}
float qdss_resample_float_complete( float x, float *buf, unsigned int bufsz, float fmax, float fsrate, int wnwdth )
{
//return buf[(int)x];
float r_g,r_w,r_a,r_snc,r_y; //some local variables
int i, j;
if( wnwdth <= 0 ) return 0;
if( bufsz < ( wnwdth / 2 ) ) return 0;
r_g = 2.0f * fmax / fsrate; //calc gain correction factor
r_y = 0.0f;
for ( i = -( wnwdth / 2 ); i <= ( wnwdth / 2 - 1 ); i++ ) //for 1 window width
{
j = (int) x + i; //calc input sample index
r_w = 0.5f - 0.5f * cosf( ftwopi * ( 0.5f + ( j - x ) / wnwdth ) ); //make a von hann sample, will be used taper sinc's length
r_a = ftwopi * ( j - x ) * fmax / fsrate; //curr sinc location
r_snc = 1.0f;
if ( r_a != 0 ) r_snc = sinf( r_a ) / r_a; //make a sinc (sin x/x) lpf sample
//printf("i: %d, r_w: %f, r_a: %f, r_snc: %f\n", i, r_w, r_a, r_snc );
if ( ( j >= 0 ) & ( j < bufsz ) ) //src sample avail?
{
r_y = r_y + r_g * r_w * r_snc * buf[ j ]; //convolve/mac: first: apply von hann tapering to sinc, then both these to a src sample (adj gain as well)
}
}
return r_y; //new filtered intermediate sample
}
//----------------------------------------------------------------------
//see 'farrow.cpp' c code in '....MyPrj/gc_music/farrow/' folder, it provides a graph and an indication of multiply/add operations, it also
//shows chebyshev impulse response and explains degree 0, 1 and 2 ( constant, linear interpolation and quadratic interpolation) and when they are used
//refer: https://www.dsprelated.com/showarticle/149.php
//refer: https://www.dsprelated.com/showcode/205.php
//refer: https://www.dsprelated.com/showarticle/22.php
//refer: https://www.dsprelated.com/showcode/203.php //this has octave code, see ..../aa_octave_code/farrow.m
/* ****************************************************
* Farrow resampler (with optional bank switching)
* M. Nentwig, 2011
* Input stream is taken from stdin
* Output stream goes to stdout
* Main target was readability, is not optimized for efficiency.
* ****************************************************/
#define order_2 //define this for '2nd' order polynomial 'cMatrix' ie. degree 0 and degree 1, which is a constant and linear interpolation respectively of the chebyshev filter impulse response),
//stepping rate of exactly 1.0f, 2.0f, 3.0f etc only use bank 0, other rates that have a fractonal component use bank 0, 1, and 2
//Also '#define order_2' uses slighly less multiply/add operations than '3rd' order polynomial.
//the '3rd' order polynomial 'cMatrix' has degree 0, degree 1, degree 2(which is: constant, linear and quadratic interpolation of the chebyshev filter impulse response),
//set 'gain_adj' to AVOID CLIPPING, a different value is needed when compiling for '2nd' order or '3rd' (try '1.0f/3.0f' or '1.0f/4.0f' respectively)
#ifdef order_2
/* **************************************************************
* example coefficients.
* Each column [c0; c1; c2; ...] describes a polynomial for one tap coefficent in fractional time ft [0, 1]:
* tapCoeff = c0 + c1 * ft + c2 * ft ^ 2 + ...
* Each column corresponds to one tap.
* The example filters is based on a 6th order Chebyshev Laplace-domain prototype.
* This version uses three sub-segments per tap (NBANKS = 3)
* **************************************************************/
#define cn_farrow_gain_adj 1.0f/9.0f //'order_2'
const float cMatrix_float[] =
{
2.87810386e-4, 4.70096244e-3, 7.93412570e-2, 4.39824536e-1, 1.31192924e+000, 2.67892232e+000, 4.16465421e+000, 5.16499621e+000, 5.15592605e+000, 3.99000369e+000, 2.00785470e+000, -7.42377060e-2, -1.52569354e+000, -1.94402804e+000, -1.40915797e+000, -3.86484652e-1, 5.44712939e-1, 9.77559688e-1, 8.32191447e-1, 3.22691788e-1, -2.13133045e-1, -5.08501962e-1, -4.82928807e-1, -2.36313854e-1, 4.76034568e-2, 2.16891966e-1, 2.20894063e-1, 1.08361553e-1, -2.63421832e-2, -1.06276015e-1, -1.07491548e-1, -5.53793711e-2, 4.86314061e-3, 3.94357182e-2, 4.06217506e-2, 2.17199064e-2, 1.60318761e-3, -8.40370106e-3, -8.10525279e-3, -3.62112499e-3, -4.13413072e-4, 2.33101911e-4,
-3.26760325e-3, -6.46028234e-3, 1.46793247e-1, 5.90235537e-1, 1.18931309e+000, 1.57853546e+000, 1.40402774e+000, 5.76506323e-1, -6.33522788e-1, -1.74564700e+000, -2.24153717e+000, -1.91309453e+000, -9.55568978e-1, 1.58239169e-1, 9.36193787e-1, 1.10969783e+000, 7.33284446e-1, 1.06542194e-1, -4.15412084e-1, -6.06616434e-1, -4.54898908e-1, -1.20841199e-1, 1.82941623e-1, 3.12543429e-1, 2.49935829e-1, 8.05376898e-2, -7.83213666e-2, -1.47769751e-1, -1.18735248e-1, -3.70656555e-2, 3.72608374e-2, 6.71425397e-2, 5.17812605e-2, 1.55564930e-2, -1.40896327e-2, -2.35058137e-2, -1.59635057e-2, -3.44701792e-3, 4.14108065e-3, 4.56234829e-3, 1.59503132e-3, -3.17301882e-4,
5.64310141e-3, 7.74786707e-2, 2.11791763e-1, 2.84703201e-1, 1.85158633e-1, -8.41118142e-2, -3.98497442e-1, -5.86821615e-1, -5.40397941e-1, -2.47558080e-1, 1.50864737e-1, 4.59312895e-1, 5.41539400e-1, 3.84673917e-1, 9.39576331e-2, -1.74932542e-1, -3.01635463e-1, -2.56239225e-1, -9.87146864e-2, 6.82216764e-2, 1.59795852e-1, 1.48668245e-1, 6.62563431e-2, -2.71234898e-2, -8.07045577e-2, -7.76841351e-2, -3.55333136e-2, 1.23206602e-2, 3.88535040e-2, 3.64199073e-2, 1.54608563e-2, -6.59814558e-3, -1.72735099e-2, -1.46307777e-2, -5.04363288e-3, 3.31049461e-3, 6.01267607e-3, 3.83904192e-3, 3.92549958e-4, -1.36315264e-3, -9.76017430e-4, 7.46699178e-5
};
const double cMatrix_double[] =
{
2.87810386e-4, 4.70096244e-3, 7.93412570e-2, 4.39824536e-1, 1.31192924e+000, 2.67892232e+000, 4.16465421e+000, 5.16499621e+000, 5.15592605e+000, 3.99000369e+000, 2.00785470e+000, -7.42377060e-2, -1.52569354e+000, -1.94402804e+000, -1.40915797e+000, -3.86484652e-1, 5.44712939e-1, 9.77559688e-1, 8.32191447e-1, 3.22691788e-1, -2.13133045e-1, -5.08501962e-1, -4.82928807e-1, -2.36313854e-1, 4.76034568e-2, 2.16891966e-1, 2.20894063e-1, 1.08361553e-1, -2.63421832e-2, -1.06276015e-1, -1.07491548e-1, -5.53793711e-2, 4.86314061e-3, 3.94357182e-2, 4.06217506e-2, 2.17199064e-2, 1.60318761e-3, -8.40370106e-3, -8.10525279e-3, -3.62112499e-3, -4.13413072e-4, 2.33101911e-4,
-3.26760325e-3, -6.46028234e-3, 1.46793247e-1, 5.90235537e-1, 1.18931309e+000, 1.57853546e+000, 1.40402774e+000, 5.76506323e-1, -6.33522788e-1, -1.74564700e+000, -2.24153717e+000, -1.91309453e+000, -9.55568978e-1, 1.58239169e-1, 9.36193787e-1, 1.10969783e+000, 7.33284446e-1, 1.06542194e-1, -4.15412084e-1, -6.06616434e-1, -4.54898908e-1, -1.20841199e-1, 1.82941623e-1, 3.12543429e-1, 2.49935829e-1, 8.05376898e-2, -7.83213666e-2, -1.47769751e-1, -1.18735248e-1, -3.70656555e-2, 3.72608374e-2, 6.71425397e-2, 5.17812605e-2, 1.55564930e-2, -1.40896327e-2, -2.35058137e-2, -1.59635057e-2, -3.44701792e-3, 4.14108065e-3, 4.56234829e-3, 1.59503132e-3, -3.17301882e-4,
5.64310141e-3, 7.74786707e-2, 2.11791763e-1, 2.84703201e-1, 1.85158633e-1, -8.41118142e-2, -3.98497442e-1, -5.86821615e-1, -5.40397941e-1, -2.47558080e-1, 1.50864737e-1, 4.59312895e-1, 5.41539400e-1, 3.84673917e-1, 9.39576331e-2, -1.74932542e-1, -3.01635463e-1, -2.56239225e-1, -9.87146864e-2, 6.82216764e-2, 1.59795852e-1, 1.48668245e-1, 6.62563431e-2, -2.71234898e-2, -8.07045577e-2, -7.76841351e-2, -3.55333136e-2, 1.23206602e-2, 3.88535040e-2, 3.64199073e-2, 1.54608563e-2, -6.59814558e-3, -1.72735099e-2, -1.46307777e-2, -5.04363288e-3, 3.31049461e-3, 6.01267607e-3, 3.83904192e-3, 3.92549958e-4, -1.36315264e-3, -9.76017430e-4, 7.46699178e-5
};
#define NTAPS (14)
#define NBANKS (3)
#define ORDER (2)
#else
/* Alternative example
* Similar impulse response as above
* A conventional Farrow design (no subdivisions => NBANKS = 1), order 3
*/
#define cn_farrow_gain_adj 1.0f/16.0f //order 3
const double cMatrix_float[] =
{
-8.57738278e-3, 7.82989032e-1, 7.19303539e+000, 6.90955718e+000, -2.62377450e+000, -6.85327127e-1, 1.44681608e+000, -8.79147907e-1, 7.82633997e-2, 1.91318985e-1, -1.88573400e-1, 6.91790782e-2, 3.07723786e-3, -6.74800912e-3,
2.32448021e-1, 2.52624309e+000, 7.67543936e+000, -8.83951796e+000, -5.49838636e+000, 6.07298348e+000, -2.16053205e+000, -7.59142947e-1, 1.41269409e+000, -8.17735712e-1, 1.98119464e-1, 9.15904145e-2, -9.18092030e-2, 2.74136108e-2,
-1.14183319e+000, 6.86126458e+000, -6.86015957e+000, -6.35135894e+000, 1.10745051e+001, -3.34847578e+000, -2.22405694e+000, 3.14374725e+000, -1.68249886e+000, 2.54083065e-1, 3.22275037e-1, -3.04794927e-1, 1.29393976e-1, -3.32026332e-2,
1.67363115e+000, -2.93090391e+000, -1.13549165e+000, 5.65274939e+000, -3.60291782e+000, -6.20715544e-1, 2.06619782e+000, -1.42159644e+000, 3.75075865e-1, 1.88433333e-1, -2.64135123e-1, 1.47117661e-1, -4.71871047e-2, 1.24921920e-2
};
const double cMatrix_double[] =
{
-8.57738278e-3, 7.82989032e-1, 7.19303539e+000, 6.90955718e+000, -2.62377450e+000, -6.85327127e-1, 1.44681608e+000, -8.79147907e-1, 7.82633997e-2, 1.91318985e-1, -1.88573400e-1, 6.91790782e-2, 3.07723786e-3, -6.74800912e-3,
2.32448021e-1, 2.52624309e+000, 7.67543936e+000, -8.83951796e+000, -5.49838636e+000, 6.07298348e+000, -2.16053205e+000, -7.59142947e-1, 1.41269409e+000, -8.17735712e-1, 1.98119464e-1, 9.15904145e-2, -9.18092030e-2, 2.74136108e-2,
-1.14183319e+000, 6.86126458e+000, -6.86015957e+000, -6.35135894e+000, 1.10745051e+001, -3.34847578e+000, -2.22405694e+000, 3.14374725e+000, -1.68249886e+000, 2.54083065e-1, 3.22275037e-1, -3.04794927e-1, 1.29393976e-1, -3.32026332e-2,
1.67363115e+000, -2.93090391e+000, -1.13549165e+000, 5.65274939e+000, -3.60291782e+000, -6.20715544e-1, 2.06619782e+000, -1.42159644e+000, 3.75075865e-1, 1.88433333e-1, -2.64135123e-1, 1.47117661e-1, -4.71871047e-2, 1.24921920e-2
};
#define NTAPS (14)
#define NBANKS (1)
#define ORDER (3)
#endif
/* Set here the ratio between output and input sample rate.
//It could be changed even during runtime!
#define INCR (1.0f / 6.28f / 3.0f) //set the ratio (orig)
#define INCR ( 0.5f ) //set the ratio
#define INCR (0.9438743126816935f) //semintone down
#define INCR (1.0594630943592953f) //semintone up
#define INCR (0.9438743126816935f) //
*/
// Coefficient lookup "table"
static double c(int ixTap, int ixBank, int ixPower)
{
return cMatrix_double[ixPower * (NTAPS * NBANKS) + ixTap * NBANKS + ixBank];
}
//double farrow_min = 0;
//double farrow_max = 0;
//this uses same fractional 'x' mechanism as the various other 'qdss_resample()' functions,
//here 'x' is called 'sampleIndexOutput', just step this value at the required rate to get any pitch you need
//returns one interpolated sample position at 'sampleIndexOutput', runs much faster that tapered SinC based functions
//see '#define order_2' above for which 'CMatrix' to use,
float farrow_resample_float( float sampleIndexOutput, float *bf, unsigned int bfsize, bool &end_reached )
{
if( sampleIndexOutput >= bfsize )
{
end_reached = 1;
return 0.0f;
}
end_reached = 0;
float out;
int ptr_in;
//Split output sample location into integer and fractional part:
//Integer part corresponds to sample index in input stream
//fractional part [0, 1[ spans one tap (covering NBANKS segments)
int sio_int = floorf(sampleIndexOutput);
float sio_fracInTap = sampleIndexOutput - (float)sio_int;
// assert((sio_fracInTap >= 0) && (sio_fracInTap < 1));
//Further split the fractional part into
//bank index
//fractional part within the bank
int sio_intBank = floorf(sio_fracInTap * (float) NBANKS);
float sio_fracInBank = sio_fracInTap * (float) NBANKS - (float)sio_intBank;
// assert((sio_fracInBank >= 0) && (sio_fracInBank < 1));
//****************************************************
//load new samples into the delay line, until the last
//processed input sample (sampleIndexInput) catches
//up with the integer part of the output stream position (sio_int)
//****************************************************
int sampleIndexInput;
sampleIndexInput = (int) sampleIndexOutput - 1;
out = 0;
while(sampleIndexInput < sio_int)
{
//Advance the delay line one step
++sampleIndexInput;
ptr_in = sampleIndexInput + 1;
if( ptr_in >= bfsize )
{
end_reached = 1;
goto done;
// printf( "farrow() - samplerate conversion completed - saving audio....\n" );
}
} //while delay line behind output data
//****************************************************
//Calculate one output sample:
//"out" sums up the contribution of each tap
//***************************************************
for ( int ixTap = 0; ixTap < NTAPS; ++ixTap)
{
//****************************************************
//* Contribution of tap "ixTap" to output:
//* ***************************************************
//* Evaluate polynomial in sio_fracInBank:
//c(ixTap, sio_intBank, 0) is the constant coefficient
//c(ixTap, sio_intBank, 1) is the linear coefficient etc
// double hornerSum = c(ixTap, sio_intBank, ORDER);
float hornerSum = cMatrix_float[ ORDER * (NTAPS * NBANKS) + ixTap * (int)NBANKS + sio_intBank];
for( int ixPower = ORDER-1; ixPower >= 0; --ixPower)
{
hornerSum *= sio_fracInBank;
// double d0 = c(ixTap, sio_intBank, ixPower);
// float f1 = cMatrix_float[ ixPower * (NTAPS * NBANKS) + ixTap * (int)NBANKS + sio_intBank];
hornerSum += cMatrix_float[ ixPower * (NTAPS * NBANKS) + ixTap * (int)NBANKS + sio_intBank];
//if( d0 != d1 )
{
//printf( "farrow %f %f\n", d0, d1 );
}
// if( d0 < farrow_min ) farrow_min = d0;
// if( d0 > farrow_max ) farrow_max = d0;
//static double c(int ixTap, int ixBank, int ixPower)
// {
// return cMatrix[ixPower * (NTAPS * NBANKS) + ixTap * NBANKS + ixBank];
// }
} //for ixPower
//* ****************************************************
//* Weigh the delay line sample of this tap with the
//* polynomial result and add to output
//* ***************************************************
int pp = ptr_in - ixTap;
if( pp >= 0 )
{
//printf( "hornerSum %f\n", hornerSum );
out += hornerSum * bf[ pp ];
}
else{
out += 0.0f;
}
} // for ixTap
done: // out of input data => break loops and continue here
return out * cn_farrow_gain_adj;
}
//see float version for usage
double farrow_resample_double( double sampleIndexOutput, double *bf, unsigned int bfsize, bool &end_reached )
{
if( sampleIndexOutput >= bfsize )
{
end_reached = 1;
return 0.0f;
}
double out;
int ptr_in;
end_reached = 0;
//Split output sample location into integer and fractional part:
//Integer part corresponds to sample index in input stream
//fractional part [0, 1[ spans one tap (covering NBANKS segments)
int sio_int = floor(sampleIndexOutput);
double sio_fracInTap = sampleIndexOutput - (double)sio_int;
// assert((sio_fracInTap >= 0) && (sio_fracInTap < 1));
//Further split the fractional part into
//bank index
//fractional part within the bank
int sio_intBank = floor(sio_fracInTap * (double) NBANKS);
double sio_fracInBank = sio_fracInTap * (double) NBANKS - (double)sio_intBank;
// assert((sio_fracInBank >= 0) && (sio_fracInBank < 1));
//****************************************************
//load new samples into the delay line, until the last
//processed input sample (sampleIndexInput) catches
//up with the integer part of the output stream position (sio_int)
//****************************************************
int sampleIndexInput;
sampleIndexInput = (int) sampleIndexOutput - 1;
while(sampleIndexInput < sio_int)
{
//loop0_cnt++;
//Advance the delay line one step
++sampleIndexInput;
ptr_in = sampleIndexInput + 1;
//ptr_in++;
// for (ix = NTAPS-1; ix > 0; --ix) delayLine[ix] = delayLine[ix-1];
//Read one input sample
//delayLine[0] = af0.pch0[ptr] * gain_adj_in;
// ptr++;
if( ptr_in >= bfsize )
{
end_reached = 1;
goto done;
// printf( "farrow() - samplerate conversion completed - saving audio....\n" );
}
// int flag = scanf("%lf", &delayLine[0]);
} //while delay line behind output data
//printf( "main() - \n" );
//****************************************************
//Calculate one output sample:
//"out" sums up the contribution of each tap
//***************************************************
out = 0;
for (int ixTap = 0; ixTap < NTAPS; ++ixTap)
{
//loop1_cnt++;
//****************************************************
//* Contribution of tap "ixTap" to output:
//* ***************************************************
//* Evaluate polynomial in sio_fracInBank:
//c(ixTap, sio_intBank, 0) is the constant coefficient
//c(ixTap, sio_intBank, 1) is the linear coefficient etc
double hornerSum = cMatrix_float[ ORDER * (NTAPS * NBANKS) + ixTap * (int)NBANKS + sio_intBank];
for( int ixPower = ORDER-1; ixPower >= 0; --ixPower)
{
hornerSum *= sio_fracInBank;
hornerSum += cMatrix_float[ ixPower * (NTAPS * NBANKS) + ixTap * (int)NBANKS + sio_intBank];
} //for ixPower
//* ****************************************************
//* Weigh the delay line sample of this tap with the
//* polynomial result and add to output
//* ***************************************************
// out += hornerSum * delayLine[ixTap];
int pp = ptr_in - ixTap;
if( pp >= 0 )
{
out += hornerSum * bf[ pp ];
}
else{
out += 0;
}
} // for ixTap
//printf(" loop0_cnt %d %d\n", loop0_cnt, loop1_cnt);
//****************************************************
//* Generate output sample and advance the position of
//* the next output sample in the input data stream
//* ***************************************************
// printf("%1.12le\n", out);
//afout.push_ch0( out/3.0f );
//afout.push_ch1( out/3.0f );
// sampleIndexOutput += INCR;
//sample_out_cnt++;
//
//if( sample_out_cnt == 103 ) //reset start somewhere within sample, not at begining
// {
// loop0_cnt = 0;
// loop1_cnt = 0;
// }
//if( sample_out_cnt == 104 ) goto done;
//
// } // loop until out of input data
done: // out of input data => break loops and continue here
//printf("farrow_resample_double() - sampleIndexOutput %f out %f\n", sampleIndexOutput, out );
return out * cn_farrow_gain_adj;
}
//----------------------------------------------------------------------
bool qdss_resample_float_vector( int output_count, unsigned int srate_in, float fmax, int wnwdth, vector<float>vin, vector<float>&vout )
{
if( vin.size() < 2 ) return 0;
vout.clear();
float *ptr = vin.data();
unsigned int bufsz = vin.size();
float step = (float)vin.size() / output_count;
float x = 0.0f;
for( int i = 0; i < output_count; i++ )
{
float ff = qdss_resample_float( x, ptr, bufsz, fmax, srate_in, wnwdth );
vout.push_back( ff );
x += step;
// if( ceilf( x ) > output_count ) break;
}
//printf( "qdss_resample_float_vector() - x %f\n", x );
return 1;
}
bool farrow_resample_float_vector( int output_count, vector<float>vin, vector<float>&vout )
{
bool vb = 0;
if( vin.size() < 2 ) return 0;
vout.clear();
float *ptr = vin.data();
unsigned int bufsz = vin.size();
float step = (float)vin.size() / output_count;
float x = 0.0f;
for( int i = 0; i < output_count; i++ )
{
bool end_reached;
float ff = farrow_resample_float( x, ptr, bufsz, end_reached );
vout.push_back( ff );
x += step;
}
if(vb) printf( "farrow_resample_float_vector() - x %f\n", x );
return 1;
}
bool farrow_resample_double_vector( int output_count, vector<double>vin, vector<double>&vout )
{
bool vb = 0;
if( vin.size() < 2 ) return 0;
vout.clear();
double *ptr = vin.data();
unsigned int bufsz = vin.size();
float step = (float)vin.size() / output_count;
float x = 0.0f;
for( int i = 0; i < output_count; i++ )
{
bool end_reached;
double dd = farrow_resample_double( x, ptr, bufsz, end_reached );
vout.push_back( dd );
x += step;
}
if(vb) printf( "farrow_resample_double_vector() - x %f\n", x );
return 1;
}
//see 'qdss_resample()' for a desciption of what is code does, BUT additionaly,
//this version is 'frame buffer' freindly, allowing a stream of sample buffers to be supplied in succession and provides interpolation overlap across buffers,
//'x' is shifted backwards by 'wnwdth/2' so filter kernel convolution does not go past end of supplied 'buf',
//when 'x' is near the end of 'buf' it also copies some tailing samples to 'head_bf' for use when function is next called with 'x' near zero, this allows filter kernel convolution
//to access samples before 'buf[0]' (using the previous calls tailing samples),
//as mentioned this will happen when 'x' is near zero (ie. when the left side of the Hann windowed sinc impulse response sits over samples from previous call),
//also, to avoid forced zero samples don't allow 'x' to exceed 'bufsz', ie. don't allow filter kernel convolution to reach passed 'buf[bufsz-1]'
//'head_bf' must be big enough to hold 'wnwdth' samples
//NOTE the initial call of this function may use 'head_bf' so init its contents to zero, interpolation of the first few samples may not be accurate on this first call only
double qdss_resample_double_continuous( double xin, double *buf, unsigned int bufsz, double fmax, double fsrate, int wnwdth, double *head_bf )
{
double r_g,r_w,r_a,r_snc,r_y; //some local variables
int i, j;
if( wnwdth <= 0 ) return 0;
int half_wnd = wnwdth / 2;
if( bufsz < wnwdth ) return 0;
double x = xin - half_wnd; //force a shift of 'x' position into so filter kernel convolution does not go past 'bufsz', this will cause use
//of 'head_bf' samples (from prev call) when needing samples around the beginning of 'buf', i.e. when spec 'x' is at or near zero
r_g = 2.0 * fmax / fsrate; //calc gain correction factor
r_y = 0;
for ( i = -( half_wnd ); i <= ( half_wnd - 1 ); i++ ) //for 1 window width
{
j = (int) x + i; //calc input sample index
r_w = 0.5 - 0.5 * cos( twopi * ( 0.5 + ( j - x ) / wnwdth ) ); //make a Hann sample, will be used taper sinc's impulse response length
r_a = twopi * ( j - x ) * fmax / fsrate; //cur sinc location
r_snc = 1;
if ( r_a != 0 ) r_snc = sin( r_a ) / r_a; //make a sinc (sin x/x) lpf sample
//printf("i: %d, r_w: %f, r_a: %f, r_snc: %f\n", i, r_w, r_a, r_snc );
bool bneg_idx = 0;
if( j < 0 ) bneg_idx = 1; //need samples from previous call ?
bool bforce_zero = 0;
if( j >= ( ( (int)bufsz) ) ) bforce_zero = 1;
//if( j<0 ) printf("qdss_resample_double_continuous() - x %f i %d j %d \n", x, i, j );
int idx;
if ( !bforce_zero ) //src sample avail?
{
if( bneg_idx ) //src sample pos in previous calls buf?
{
idx = wnwdth + j; //j is negative here
r_y = r_y + r_g * r_w * r_snc * head_bf[ idx ]; //convolve/mac: first: apply hann tapering (window) to sinc impulse response, then both these to a src sample (adj gain as well)
//printf("qdss_resample_double_continuous() - xin %f x %f smpl is at %d, getting smpl from head_bf[%d]\n", xin, x, j, idx );
// if ( xin < 10 ) printf("qdss_resample_double_continuous()0 - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
// if ( xin > 4080 ) printf("qdss_resample_double_continuous()0 - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
}
else{
r_y = r_y + r_g * r_w * r_snc * buf[ j ]; //convolve
}
}
else{
printf("qdss_resample_double_continuous() - forcing zero as xin %f x %f bufsz %d i %d j is %d\n", xin, x, bufsz, i, j );
}
//if( ( dbg_cnt3 == 0 ) || ( dbg_cnt3 == 1 ) || ( dbg_cnt3 == 2 ) || ( dbg_cnt3 == 3 ) || ( dbg_cnt3 == 4 ) )
// if( ( dbg_cnt3 <= 15 ) )
{
// if( bneg_idx ) printf("qdss_resample_double_continuous()neg - cnt %d i %d j %d xin %f x %f idx %d hbf[%f] r_y %f\n", dbg_cnt3, i, j, xin, x, idx, head_bf[idx], r_y );
// else printf("qdss_resample_double_continuous()pos - cnt %d i %d j %d xin %f x %f idx %d buf[%f] r_y %f\n", dbg_cnt3, i, j, xin, x, idx, buf[idx], r_y );
// r_y = -1;
}
}
//dbg_cnt3++;
//if ( xin < 10 ) printf("qdss_resample_double_continuous() - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
//if ( xin > 4080 ) printf("qdss_resample_double_continuous() - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
//for ( int i = 0; i < half_wnd; i++ )
// {
// head_bf[i] = buf[ (bufsz - 1) - ( half_wnd - i ) ]; //copy tailing samples to 'head_bf[]' for next call use if any
// }
bool bcopy_buf = 0;
if( (floorf(x) >= ( bufsz - 16*wnwdth ) ) ) bcopy_buf = 1; //approching samples near end of 'buf' ?, copy ending samples to 'head_bf'
if( bcopy_buf )
{
for ( int i = 0; i < wnwdth; i++ )
{
int idx = (bufsz) - ( wnwdth - i );
head_bf[i] = buf[ idx ]; //copy tailing samples to 'head_bf[]' for next call use if any
//if( user_flag == 1 ) printf("qdss_resample_double_continuous() - head_bf[%d] = buf[%d] val %f\n", i, idx, head_bf[i] );
}
}
//if( user_flag == 1 ) user_flag = 2;
return r_y; //new filtered intermediate sample
}
//see 'qdss_resample()' for a desciption of what is code does, BUT additionaly,
//this version is 'frame buffer' freindly, allowing a stream of sample buffers to be supplied in succession and provides interpolation overlap across buffers,
//'x' is shifted backwards by 'wnwdth/2' so filter kernel convolution does not go past end of supplied 'buf',
//when 'x' is near the end of 'buf' it also copies some tailing samples to 'head_bf' for use when function is next called with 'x' near zero, this allows filter kernel convolution
//to access samples before 'buf[0]' (using the previous calls tailing samples),
//as mentioned this will happen when 'x' is near zero (ie. when the left side of the Hann windowed sinc impulse response sits over samples from previous call),
//also, to avoid forced zero samples don't allow 'x' to exceed 'bufsz', ie. don't allow filter kernel convolution to reach passed 'buf[bufsz-1]'
//'head_bf' must be big enough to hold 'wnwdth' samples
//NOTE the initial call of this function may use 'head_bf' so init its contents to zero, interpolation of the first few samples may not be accurate on this first call only
float qdss_resample_float_continuous( float xin, float *buf, unsigned int bufsz, float fmax, float fsrate, int wnwdth, float *head_bf )
{
double r_g,r_w,r_a,r_snc,r_y; //some local variables
int i, j;
if( wnwdth <= 0 ) return 0;
int half_wnd = wnwdth / 2;
if( bufsz < wnwdth ) return 0;
float x = xin - half_wnd; //force a shift of 'x' position into so filter kernel convolution does not go past 'bufsz', this will cause use
//of 'head_bf' samples (from prev call) when needing samples around the beginning of 'buf', i.e. when spec 'x' is at or near zero
r_g = 2.0 * fmax / fsrate; //calc gain correction factor
r_y = 0;
for ( i = -( half_wnd ); i <= ( half_wnd - 1 ); i++ ) //for 1 window width
{
j = (int) x + i; //calc input sample index
r_w = 0.5 - 0.5 * cos( twopi * ( 0.5 + ( j - x ) / wnwdth ) ); //make a Hann sample, will be used taper sinc's impulse response length
r_a = ftwopi * ( j - x ) * fmax / fsrate; //cur sinc location
r_snc = 1;
if ( r_a != 0 ) r_snc = sin( r_a ) / r_a; //make a sinc (sin x/x) lpf sample
//printf("i: %d, r_w: %f, r_a: %f, r_snc: %f\n", i, r_w, r_a, r_snc );
bool bneg_idx = 0;
if( j < 0 ) bneg_idx = 1; //need samples from previous call ?
bool bforce_zero = 0;
if( j >= ( ( (int)bufsz) ) ) bforce_zero = 1;
//if( j<0 ) printf("qdss_resample_double_continuous() - x %f i %d j %d \n", x, i, j );
int idx;
if ( !bforce_zero ) //src sample avail?
{
if( bneg_idx ) //src sample pos in previous calls buf?
{
idx = wnwdth + j; //j is negative here
r_y = r_y + r_g * r_w * r_snc * head_bf[ idx ]; //convolve/mac: first: apply hann tapering (window) to sinc impulse response, then both these to a src sample (adj gain as well)
//printf("qdss_resample_double_continuous() - xin %f x %f smpl is at %d, getting smpl from head_bf[%d]\n", xin, x, j, idx );
// if ( xin < 10 ) printf("qdss_resample_double_continuous()0 - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
// if ( xin > 4080 ) printf("qdss_resample_double_continuous()0 - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
}
else{
r_y = r_y + r_g * r_w * r_snc * buf[ j ]; //convolve
}
}
else{
printf("qdss_resample_float_continuous() - forcing zero as xin %f x %f bufsz %d i %d j is %d\n", xin, x, bufsz, i, j );
}
//if( ( dbg_cnt3 == 0 ) || ( dbg_cnt3 == 1 ) || ( dbg_cnt3 == 2 ) || ( dbg_cnt3 == 3 ) || ( dbg_cnt3 == 4 ) )
// if( ( dbg_cnt3 <= 15 ) )
{
// if( bneg_idx ) printf("qdss_resample_double_continuous()neg - cnt %d i %d j %d xin %f x %f idx %d hbf[%f] r_y %f\n", dbg_cnt3, i, j, xin, x, idx, head_bf[idx], r_y );
// else printf("qdss_resample_double_continuous()pos - cnt %d i %d j %d xin %f x %f idx %d buf[%f] r_y %f\n", dbg_cnt3, i, j, xin, x, idx, buf[idx], r_y );
// r_y = -1;
}
}
//dbg_cnt3++;
//if ( xin < 10 ) printf("qdss_resample_double_continuous() - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
//if ( xin > 4080 ) printf("qdss_resample_double_continuous() - j %d idx %d xin %f x %f r_y %f\n", j, idx, xin, x, r_y );
//for ( int i = 0; i < half_wnd; i++ )
// {
// head_bf[i] = buf[ (bufsz - 1) - ( half_wnd - i ) ]; //copy tailing samples to 'head_bf[]' for next call use if any
// }
bool bcopy_buf = 0;