1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * This is a impementation of sampling rate conversion for low-passed signals.
31*7c478bd9Sstevel@tonic-gate *
32*7c478bd9Sstevel@tonic-gate * To convert the input signal of sampling rate of fi to another rate of fo,
33*7c478bd9Sstevel@tonic-gate * the least common multiple of fi and fo is derived first:
34*7c478bd9Sstevel@tonic-gate * fm = L * fi = M * fo.
35*7c478bd9Sstevel@tonic-gate * Then the input signal is up-sampled to fm by inserting (L -1) zero valued
36*7c478bd9Sstevel@tonic-gate * samples after each input sample, low-pass filtered, and down-smapled by
37*7c478bd9Sstevel@tonic-gate * saving one sample for every M samples. The implementation takes advantages
38*7c478bd9Sstevel@tonic-gate * of the (L - 1) zero values in the filter input and the (M - 1) output
39*7c478bd9Sstevel@tonic-gate * samples to be disregarded.
40*7c478bd9Sstevel@tonic-gate *
41*7c478bd9Sstevel@tonic-gate * If L = 1 or M = 1, a simpler decimation or interpolation is used.
42*7c478bd9Sstevel@tonic-gate */
43*7c478bd9Sstevel@tonic-gate #include <memory.h>
44*7c478bd9Sstevel@tonic-gate #include <math.h>
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #include <Resample.h>
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate extern "C" {
49*7c478bd9Sstevel@tonic-gate char *bcopy(char *, char *, int);
50*7c478bd9Sstevel@tonic-gate char *memmove(char *, char *, int);
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate #define BCOPY(src, dest, num) memmove(dest, src, num)
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate // convolve(), short2double() and double2short() are defined in Fir.cc.
56*7c478bd9Sstevel@tonic-gate extern double convolve(double *, double *, int);
57*7c478bd9Sstevel@tonic-gate extern void short2double(double *, short *, int);
58*7c478bd9Sstevel@tonic-gate extern short double2short(double);
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate // generate truncated ideal LPF
sinc_coef(int fold,int order,double * coef)61*7c478bd9Sstevel@tonic-gate static void sinc_coef(int fold, // sample rate change
62*7c478bd9Sstevel@tonic-gate int order, // LP FIR filter order
63*7c478bd9Sstevel@tonic-gate double *coef) // filter coefficients
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate int i;
66*7c478bd9Sstevel@tonic-gate float alpha;
67*7c478bd9Sstevel@tonic-gate double bandwidth = M_PI / fold; // digital bandwidth of pass band
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate int half = order >> 1;
70*7c478bd9Sstevel@tonic-gate if (order & 1) { // order is odd, center = half + 0.5
71*7c478bd9Sstevel@tonic-gate float center = half + 0.5;
72*7c478bd9Sstevel@tonic-gate for (i = 0; i <= half; i++) {
73*7c478bd9Sstevel@tonic-gate alpha = center - i;
74*7c478bd9Sstevel@tonic-gate coef[i] = sin(bandwidth * alpha) / (M_PI * alpha);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate } else { // order is even, center = half
77*7c478bd9Sstevel@tonic-gate for (i = 0; i < half; i++) {
78*7c478bd9Sstevel@tonic-gate alpha = half - i;
79*7c478bd9Sstevel@tonic-gate coef[i] = sin(bandwidth * alpha) / (M_PI * alpha);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate coef[i++] = bandwidth / M_PI;
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate for (; i <= order; i++) // symmetric FIR
84*7c478bd9Sstevel@tonic-gate coef[i] = coef[order - i];
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate * poly_conv() = coef[0] * data[length - 1] +
89*7c478bd9Sstevel@tonic-gate * coef[inc_coef] * data[length - 2] +
90*7c478bd9Sstevel@tonic-gate * ...
91*7c478bd9Sstevel@tonic-gate * coef[(length - 1) * inc_coef] * data[0] +
92*7c478bd9Sstevel@tonic-gate *
93*7c478bd9Sstevel@tonic-gate * convolution of coef[order + 1] and data[length] up-sampled by a factor
94*7c478bd9Sstevel@tonic-gate * of inc_coef. The terms of coef[1, 2, ... inc_coef - 1] multiplying 0
95*7c478bd9Sstevel@tonic-gate * are ignored.
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate // polyphase filter convolution
98*7c478bd9Sstevel@tonic-gate double
poly_conv(double * coef,int order,int inc_coef,double * data,int length)99*7c478bd9Sstevel@tonic-gate poly_conv(double *coef, // filter coef array
100*7c478bd9Sstevel@tonic-gate int order, // filter order
101*7c478bd9Sstevel@tonic-gate int inc_coef, // 1-to-L up-sample for data[length]
102*7c478bd9Sstevel@tonic-gate double *data, // data array
103*7c478bd9Sstevel@tonic-gate int length) // data array length
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate if ((order < 0) || (inc_coef < 1) || (length < 1))
106*7c478bd9Sstevel@tonic-gate return (0.0);
107*7c478bd9Sstevel@tonic-gate else {
108*7c478bd9Sstevel@tonic-gate double sum = 0.0;
109*7c478bd9Sstevel@tonic-gate double *coef_end = coef + order;
110*7c478bd9Sstevel@tonic-gate double *data_end = data + length;
111*7c478bd9Sstevel@tonic-gate while ((coef <= coef_end) && (data < data_end)) {
112*7c478bd9Sstevel@tonic-gate sum += *coef * *--data_end;
113*7c478bd9Sstevel@tonic-gate coef += inc_coef;
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate return (sum);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate int
gcf(int a,int b)120*7c478bd9Sstevel@tonic-gate gcf(int a, int b) // greatest common factor between a and b
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate int remainder = a % b;
123*7c478bd9Sstevel@tonic-gate return (remainder == 0)? b : gcf(b, remainder);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate void ResampleFilter::
updateState(double * in,int size)127*7c478bd9Sstevel@tonic-gate updateState(
128*7c478bd9Sstevel@tonic-gate double *in,
129*7c478bd9Sstevel@tonic-gate int size)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate if (up <= 1)
132*7c478bd9Sstevel@tonic-gate Fir::updateState(in, size);
133*7c478bd9Sstevel@tonic-gate else if (size >= num_state)
134*7c478bd9Sstevel@tonic-gate memcpy(state, in + size - num_state,
135*7c478bd9Sstevel@tonic-gate num_state * sizeof (double));
136*7c478bd9Sstevel@tonic-gate else {
137*7c478bd9Sstevel@tonic-gate int old = num_state - size;
138*7c478bd9Sstevel@tonic-gate BCOPY((char *)(state + size), (char *)state,
139*7c478bd9Sstevel@tonic-gate old * sizeof (double));
140*7c478bd9Sstevel@tonic-gate memcpy(state + old, in, size * sizeof (double));
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate ResampleFilter:: // constructor
ResampleFilter(int rate_in,int rate_out)145*7c478bd9Sstevel@tonic-gate ResampleFilter(
146*7c478bd9Sstevel@tonic-gate int rate_in, // sampling rate of input signal
147*7c478bd9Sstevel@tonic-gate int rate_out) // sampling rate of output signal
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate // filter sampling rate = common multiple of rate_in and rate_out
150*7c478bd9Sstevel@tonic-gate int commonfactor = gcf(rate_in, rate_out);
151*7c478bd9Sstevel@tonic-gate up = rate_out / commonfactor;
152*7c478bd9Sstevel@tonic-gate down = rate_in / commonfactor;
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate int fold = (up > down)? up : down; // take the bigger rate change
155*7c478bd9Sstevel@tonic-gate order = (fold << 4) - 2; // filter order = fold * 16 - 2
156*7c478bd9Sstevel@tonic-gate coef = new double[order + 1];
157*7c478bd9Sstevel@tonic-gate sinc_coef(fold, order, coef); // required bandwidth = PI/fold
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate if (up > 1) { // need (order/up) states
160*7c478bd9Sstevel@tonic-gate num_state = (order + up - 1) / up;
161*7c478bd9Sstevel@tonic-gate state = new double[num_state];
162*7c478bd9Sstevel@tonic-gate for (int i = 0; i < num_state; i++) // reset states
163*7c478bd9Sstevel@tonic-gate state[i] = 0.0;
164*7c478bd9Sstevel@tonic-gate } else {
165*7c478bd9Sstevel@tonic-gate num_state = order;
166*7c478bd9Sstevel@tonic-gate state = new double[order]; // need order states
167*7c478bd9Sstevel@tonic-gate resetState();
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate delay = (order + 1) >> 1; // assuming symmetric FIR
170*7c478bd9Sstevel@tonic-gate down_offset = 0;
171*7c478bd9Sstevel@tonic-gate up_offset = 0;
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate
174*7c478bd9Sstevel@tonic-gate // down-to-1 decimation
175*7c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate_noadjust(short * in,int size,short * out)176*7c478bd9Sstevel@tonic-gate decimate_noadjust(short *in,
177*7c478bd9Sstevel@tonic-gate int size,
178*7c478bd9Sstevel@tonic-gate short *out)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate int i;
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate if (size <= 0)
183*7c478bd9Sstevel@tonic-gate return (0);
184*7c478bd9Sstevel@tonic-gate else if (down <= 1) // normal filter
185*7c478bd9Sstevel@tonic-gate return (Fir::filter_noadjust(in, size, out));
186*7c478bd9Sstevel@tonic-gate else if (size <= down_offset) { // just update states
187*7c478bd9Sstevel@tonic-gate update_short(in, size);
188*7c478bd9Sstevel@tonic-gate down_offset -= size;
189*7c478bd9Sstevel@tonic-gate return (0);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate double *in_buf = new double[size];
193*7c478bd9Sstevel@tonic-gate short2double(in_buf, in, size);
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate // filter and decimate and output
196*7c478bd9Sstevel@tonic-gate short *out_ptr = out;
197*7c478bd9Sstevel@tonic-gate int init_size = (size <= order)? size : order;
198*7c478bd9Sstevel@tonic-gate for (i = down_offset; i < init_size; i += down)
199*7c478bd9Sstevel@tonic-gate *out_ptr++ = double2short(convolve(coef, in_buf, i + 1) +
200*7c478bd9Sstevel@tonic-gate convolve(coef + i + 1, state + i, order - i));
201*7c478bd9Sstevel@tonic-gate for (; i < size; i += down)
202*7c478bd9Sstevel@tonic-gate *out_ptr++ = double2short(convolve(coef, in_buf + i - order,
203*7c478bd9Sstevel@tonic-gate order + 1));
204*7c478bd9Sstevel@tonic-gate down_offset = i - size;
205*7c478bd9Sstevel@tonic-gate
206*7c478bd9Sstevel@tonic-gate updateState(in_buf, size);
207*7c478bd9Sstevel@tonic-gate delete in_buf;
208*7c478bd9Sstevel@tonic-gate return (out_ptr - out);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate // decimate with group delay adjusted to 0
212*7c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate(short * in,int size,short * out)213*7c478bd9Sstevel@tonic-gate decimate(short *in,
214*7c478bd9Sstevel@tonic-gate int size,
215*7c478bd9Sstevel@tonic-gate short *out)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate if (delay <= 0)
218*7c478bd9Sstevel@tonic-gate return (decimate_noadjust(in, size, out));
219*7c478bd9Sstevel@tonic-gate else if (size <= delay) {
220*7c478bd9Sstevel@tonic-gate update_short(in, size);
221*7c478bd9Sstevel@tonic-gate delay -= size;
222*7c478bd9Sstevel@tonic-gate return (0);
223*7c478bd9Sstevel@tonic-gate } else {
224*7c478bd9Sstevel@tonic-gate update_short(in, delay);
225*7c478bd9Sstevel@tonic-gate in += delay;
226*7c478bd9Sstevel@tonic-gate size -= delay;
227*7c478bd9Sstevel@tonic-gate delay = 0;
228*7c478bd9Sstevel@tonic-gate return (decimate_noadjust(in, size, out));
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate // flush decimate filter
233*7c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate_flush(short * out)234*7c478bd9Sstevel@tonic-gate decimate_flush(short *out)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate int num_in = Fir::getFlushSize();
237*7c478bd9Sstevel@tonic-gate short *in = new short[num_in];
238*7c478bd9Sstevel@tonic-gate memset(in, 0, num_in * sizeof (short));
239*7c478bd9Sstevel@tonic-gate int num_out = decimate_noadjust(in, num_in, out);
240*7c478bd9Sstevel@tonic-gate delay += num_in;
241*7c478bd9Sstevel@tonic-gate delete in;
242*7c478bd9Sstevel@tonic-gate return (num_out);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate // 1-to-up interpolation
246*7c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate_noadjust(short * in,int size,short * out)247*7c478bd9Sstevel@tonic-gate interpolate_noadjust(short *in,
248*7c478bd9Sstevel@tonic-gate int size,
249*7c478bd9Sstevel@tonic-gate short *out)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate int i, j;
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate if (size <= 0)
254*7c478bd9Sstevel@tonic-gate return (0);
255*7c478bd9Sstevel@tonic-gate else if (up <= 1) // regular filter
256*7c478bd9Sstevel@tonic-gate return (Fir::filter_noadjust(in, size, out));
257*7c478bd9Sstevel@tonic-gate
258*7c478bd9Sstevel@tonic-gate double *in_buf = new double[size];
259*7c478bd9Sstevel@tonic-gate short2double(in_buf, in, size);
260*7c478bd9Sstevel@tonic-gate short *out_ptr = out;
261*7c478bd9Sstevel@tonic-gate // befor the 1st input sample, generate "-up_offset" output samples
262*7c478bd9Sstevel@tonic-gate int coef_offset = up + up_offset;
263*7c478bd9Sstevel@tonic-gate for (j = up_offset; j < 0; j++) {
264*7c478bd9Sstevel@tonic-gate *out_ptr++ = double2short(up * poly_conv(coef + coef_offset,
265*7c478bd9Sstevel@tonic-gate order - coef_offset, up, state, num_state));
266*7c478bd9Sstevel@tonic-gate coef_offset++;
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate // for each of the rest input samples, generate up output samples
269*7c478bd9Sstevel@tonic-gate for (i = 1; i < size; i++) {
270*7c478bd9Sstevel@tonic-gate for (j = 0; j < up; j++) {
271*7c478bd9Sstevel@tonic-gate *out_ptr++ = double2short(up * (poly_conv(coef + j,
272*7c478bd9Sstevel@tonic-gate order - j, up, in_buf, i) + poly_conv(
273*7c478bd9Sstevel@tonic-gate coef + coef_offset, order - coef_offset, up, state,
274*7c478bd9Sstevel@tonic-gate num_state)));
275*7c478bd9Sstevel@tonic-gate coef_offset++;
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate
279*7c478bd9Sstevel@tonic-gate // for the last input samples, generate "up_offset + up" output samples
280*7c478bd9Sstevel@tonic-gate for (j = 0; j < (up_offset + up); j++) {
281*7c478bd9Sstevel@tonic-gate *out_ptr++ = double2short(up * (poly_conv(coef + j,
282*7c478bd9Sstevel@tonic-gate order - j, up, in_buf, size) + poly_conv(
283*7c478bd9Sstevel@tonic-gate coef + coef_offset, order - coef_offset, up, state,
284*7c478bd9Sstevel@tonic-gate num_state)));
285*7c478bd9Sstevel@tonic-gate coef_offset++;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate updateState(in_buf, size);
288*7c478bd9Sstevel@tonic-gate delete in_buf;
289*7c478bd9Sstevel@tonic-gate return (out_ptr - out);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate
292*7c478bd9Sstevel@tonic-gate // flush interpolate filter
293*7c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate_flush(short * out)294*7c478bd9Sstevel@tonic-gate interpolate_flush(short *out)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate int num = (Fir::getFlushSize() + up - 1) / up;
297*7c478bd9Sstevel@tonic-gate
298*7c478bd9Sstevel@tonic-gate short *in = new short[num];
299*7c478bd9Sstevel@tonic-gate memset(in, 0, num * sizeof (short));
300*7c478bd9Sstevel@tonic-gate int out_num = interpolate_noadjust(in, num, out);
301*7c478bd9Sstevel@tonic-gate delay += num * up;
302*7c478bd9Sstevel@tonic-gate delete in;
303*7c478bd9Sstevel@tonic-gate return (out_num);
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate
306*7c478bd9Sstevel@tonic-gate // interpolate with delay adjusted
307*7c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate(short * in,int size,short * out)308*7c478bd9Sstevel@tonic-gate interpolate(short *in,
309*7c478bd9Sstevel@tonic-gate int size,
310*7c478bd9Sstevel@tonic-gate short *out)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate if (size <= 0)
313*7c478bd9Sstevel@tonic-gate return (interpolate_flush(out));
314*7c478bd9Sstevel@tonic-gate else if (delay <= 0)
315*7c478bd9Sstevel@tonic-gate return (interpolate_noadjust(in, size, out));
316*7c478bd9Sstevel@tonic-gate else {
317*7c478bd9Sstevel@tonic-gate int delay_in = (delay + up - 1) / up;
318*7c478bd9Sstevel@tonic-gate if (size < delay_in)
319*7c478bd9Sstevel@tonic-gate delay_in = size;
320*7c478bd9Sstevel@tonic-gate double *in_buf = new double[delay_in];
321*7c478bd9Sstevel@tonic-gate short2double(in_buf, in, delay_in);
322*7c478bd9Sstevel@tonic-gate updateState(in_buf, delay_in);
323*7c478bd9Sstevel@tonic-gate delete in_buf;
324*7c478bd9Sstevel@tonic-gate delay -= delay_in * up;
325*7c478bd9Sstevel@tonic-gate up_offset = delay;
326*7c478bd9Sstevel@tonic-gate return (interpolate_noadjust(in + delay_in, size -
327*7c478bd9Sstevel@tonic-gate delay_in, out));
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate int ResampleFilter::
filter_noadjust(short * in,int size,short * out)332*7c478bd9Sstevel@tonic-gate filter_noadjust(short *in, // non-integer sampling rate conversion
333*7c478bd9Sstevel@tonic-gate int size,
334*7c478bd9Sstevel@tonic-gate short *out)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate int i, j;
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate if (size <= 0)
339*7c478bd9Sstevel@tonic-gate return (0);
340*7c478bd9Sstevel@tonic-gate else if (up <= 1)
341*7c478bd9Sstevel@tonic-gate return (decimate_noadjust(in, size, out));
342*7c478bd9Sstevel@tonic-gate else if (down <= 1)
343*7c478bd9Sstevel@tonic-gate return (interpolate_noadjust(in, size, out));
344*7c478bd9Sstevel@tonic-gate
345*7c478bd9Sstevel@tonic-gate double *in_buf = new double[size];
346*7c478bd9Sstevel@tonic-gate short2double(in_buf, in, size);
347*7c478bd9Sstevel@tonic-gate short *init_out = out;
348*7c478bd9Sstevel@tonic-gate int coef_offset = up_offset + down_offset + up;
349*7c478bd9Sstevel@tonic-gate
350*7c478bd9Sstevel@tonic-gate /*
351*7c478bd9Sstevel@tonic-gate * before the 1st input sample,
352*7c478bd9Sstevel@tonic-gate * start from "up_offset + down_offset"th up-sampled sample
353*7c478bd9Sstevel@tonic-gate */
354*7c478bd9Sstevel@tonic-gate for (j = up_offset + down_offset; j < 0; j += down) {
355*7c478bd9Sstevel@tonic-gate *out++ = double2short(up * poly_conv(coef + coef_offset,
356*7c478bd9Sstevel@tonic-gate order - coef_offset, up, state, num_state));
357*7c478bd9Sstevel@tonic-gate coef_offset += down;
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate
360*7c478bd9Sstevel@tonic-gate // process the input samples until the last one
361*7c478bd9Sstevel@tonic-gate for (i = 1; i < size; i++) {
362*7c478bd9Sstevel@tonic-gate for (; j < up; j += down) {
363*7c478bd9Sstevel@tonic-gate *out++ = double2short(up * (poly_conv(coef + j,
364*7c478bd9Sstevel@tonic-gate order - j, up, in_buf, i) + poly_conv(
365*7c478bd9Sstevel@tonic-gate coef + coef_offset, order - coef_offset, up, state,
366*7c478bd9Sstevel@tonic-gate num_state)));
367*7c478bd9Sstevel@tonic-gate coef_offset += down;
368*7c478bd9Sstevel@tonic-gate }
369*7c478bd9Sstevel@tonic-gate j -= up;
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate
372*7c478bd9Sstevel@tonic-gate // for the last input sample, end at the "up + up_offset"th
373*7c478bd9Sstevel@tonic-gate for (; j < (up + up_offset); j += down) {
374*7c478bd9Sstevel@tonic-gate *out++ = double2short(up * (poly_conv(coef + j, order - j, up,
375*7c478bd9Sstevel@tonic-gate in_buf, size) + poly_conv(coef + coef_offset,
376*7c478bd9Sstevel@tonic-gate order - coef_offset, up, state, num_state)));
377*7c478bd9Sstevel@tonic-gate coef_offset += down;
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate down_offset = j - (up + up_offset);
380*7c478bd9Sstevel@tonic-gate
381*7c478bd9Sstevel@tonic-gate updateState(in_buf, size);
382*7c478bd9Sstevel@tonic-gate delete in_buf;
383*7c478bd9Sstevel@tonic-gate return (out - init_out);
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate
386*7c478bd9Sstevel@tonic-gate int ResampleFilter::
getFlushSize(void)387*7c478bd9Sstevel@tonic-gate getFlushSize(void)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate int num_in = (Fir::getFlushSize() + up - 1) / up;
390*7c478bd9Sstevel@tonic-gate return ((num_in * up + down - 1 - down_offset) / down);
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate
393*7c478bd9Sstevel@tonic-gate int ResampleFilter::
flush(short * out)394*7c478bd9Sstevel@tonic-gate flush(short *out) // flush resampling filter
395*7c478bd9Sstevel@tonic-gate
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate if (down <= 1)
398*7c478bd9Sstevel@tonic-gate return (interpolate_flush(out));
399*7c478bd9Sstevel@tonic-gate else if (up <= 1)
400*7c478bd9Sstevel@tonic-gate return (decimate_flush(out));
401*7c478bd9Sstevel@tonic-gate
402*7c478bd9Sstevel@tonic-gate int num = (Fir::getFlushSize() + up - 1) / up;
403*7c478bd9Sstevel@tonic-gate
404*7c478bd9Sstevel@tonic-gate short *in = new short[num];
405*7c478bd9Sstevel@tonic-gate memset(in, 0, num * sizeof (short));
406*7c478bd9Sstevel@tonic-gate int out_num = filter_noadjust(in, num, out);
407*7c478bd9Sstevel@tonic-gate delete in;
408*7c478bd9Sstevel@tonic-gate delay += num * up;
409*7c478bd9Sstevel@tonic-gate return (out_num);
410*7c478bd9Sstevel@tonic-gate }
411*7c478bd9Sstevel@tonic-gate
412*7c478bd9Sstevel@tonic-gate /*
413*7c478bd9Sstevel@tonic-gate * sampling rate conversion with filter delay adjusted
414*7c478bd9Sstevel@tonic-gate */
415*7c478bd9Sstevel@tonic-gate int ResampleFilter::
filter(short * in,int size,short * out)416*7c478bd9Sstevel@tonic-gate filter(
417*7c478bd9Sstevel@tonic-gate short *in,
418*7c478bd9Sstevel@tonic-gate int size,
419*7c478bd9Sstevel@tonic-gate short *out)
420*7c478bd9Sstevel@tonic-gate {
421*7c478bd9Sstevel@tonic-gate if (size <= 0)
422*7c478bd9Sstevel@tonic-gate return (flush(out));
423*7c478bd9Sstevel@tonic-gate else if (up <= 1)
424*7c478bd9Sstevel@tonic-gate return (decimate(in, size, out));
425*7c478bd9Sstevel@tonic-gate else if (down <= 1)
426*7c478bd9Sstevel@tonic-gate return (interpolate(in, size, out));
427*7c478bd9Sstevel@tonic-gate else if (delay <= 0)
428*7c478bd9Sstevel@tonic-gate return (filter_noadjust(in, size, out));
429*7c478bd9Sstevel@tonic-gate else {
430*7c478bd9Sstevel@tonic-gate int delay_in = (delay + up - 1) / up;
431*7c478bd9Sstevel@tonic-gate if (size < delay_in)
432*7c478bd9Sstevel@tonic-gate delay_in = size;
433*7c478bd9Sstevel@tonic-gate double *in_buf = new double[delay_in];
434*7c478bd9Sstevel@tonic-gate short2double(in_buf, in, delay_in);
435*7c478bd9Sstevel@tonic-gate updateState(in_buf, delay_in);
436*7c478bd9Sstevel@tonic-gate delete in_buf;
437*7c478bd9Sstevel@tonic-gate delay -= up * delay_in;
438*7c478bd9Sstevel@tonic-gate if (delay <= 0) {
439*7c478bd9Sstevel@tonic-gate up_offset = delay;
440*7c478bd9Sstevel@tonic-gate down_offset = 0;
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate return (filter_noadjust(in + delay_in, size - delay_in, out));
443*7c478bd9Sstevel@tonic-gate }
444*7c478bd9Sstevel@tonic-gate }
445