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