1 /* Effect: change sample rate  Copyright (c) 2008,12 robs@users.sourceforge.net
2  *
3  * This library is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2.1 of the License, or (at
6  * your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 /* Resample using an interpolated poly-phase FIR with length LEN.*/
19 /* Input must be followed by LEN-1 samples. */
20 
21 #define a (coef(p->shared->poly_fir_coefs, COEF_INTERP, FIR_LENGTH, phase, 0,j))
22 #define b (coef(p->shared->poly_fir_coefs, COEF_INTERP, FIR_LENGTH, phase, 1,j))
23 #define c (coef(p->shared->poly_fir_coefs, COEF_INTERP, FIR_LENGTH, phase, 2,j))
24 #define d (coef(p->shared->poly_fir_coefs, COEF_INTERP, FIR_LENGTH, phase, 3,j))
25 #if COEF_INTERP == 0
26   #define _ sum += a *in[j], ++j;
27 #elif COEF_INTERP == 1
28   #define _ sum += (b *x + a)*in[j], ++j;
29 #elif COEF_INTERP == 2
30   #define _ sum += ((c *x + b)*x + a)*in[j], ++j;
31 #elif COEF_INTERP == 3
32   #define _ sum += (((d*x + c)*x + b)*x + a)*in[j], ++j;
33 #else
34   #error COEF_INTERP
35 #endif
36 
FUNCTION(stage_t * p,fifo_t * output_fifo)37 static void FUNCTION(stage_t * p, fifo_t * output_fifo)
38 {
39   sample_t const * input = stage_read_p(p);
40   int i, num_in = stage_occupancy(p), max_num_out = 1 + num_in*p->out_in_ratio;
41   sample_t * output = fifo_reserve(output_fifo, max_num_out);
42 
43 #if defined HI_PREC_CLOCK
44   if (p->use_hi_prec_clock) {
45     hi_prec_clock_t at = p->at.hi_prec_clock;
46     for (i = 0; (int)at < num_in; ++i, at += p->step.hi_prec_clock) {
47       sample_t const * in = input + (int)at;
48       hi_prec_clock_t fraction = at - (int)at;
49       int phase = fraction * (1 << PHASE_BITS);
50 #if COEF_INTERP > 0
51       sample_t x = fraction * (1 << PHASE_BITS) - phase;
52 #endif
53       sample_t sum = 0;
54       int j = 0;
55       CONVOLVE
56       output[i] = sum;
57     }
58     fifo_read(&p->fifo, (int)at, NULL);
59     p->at.hi_prec_clock = at - (int)at;
60   } else
61 #endif
62   {
63     for (i = 0; p->at.parts.integer < num_in; ++i, p->at.all += p->step.all) {
64       sample_t const * in = input + p->at.parts.integer;
65       uint32_t fraction = p->at.parts.fraction;
66       int phase = fraction >> (32 - PHASE_BITS); /* high-order bits */
67 #if COEF_INTERP > 0              /* low-order bits, scaled to [0,1) */
68       sample_t x = (sample_t) (fraction << PHASE_BITS) * (1 / MULT32);
69 #endif
70       sample_t sum = 0;
71       int j = 0;
72       CONVOLVE
73       output[i] = sum;
74     }
75     fifo_read(&p->fifo, p->at.parts.integer, NULL);
76     p->at.parts.integer = 0;
77   }
78   assert(max_num_out - i >= 0);
79   fifo_trim_by(output_fifo, max_num_out - i);
80 }
81 
82 #undef _
83 #undef a
84 #undef b
85 #undef c
86 #undef d
87 #undef COEF_INTERP
88 #undef CONVOLVE
89 #undef FIR_LENGTH
90 #undef FUNCTION
91 #undef PHASE_BITS
92