17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This is a impementation of sampling rate conversion for low-passed signals.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * To convert the input signal of sampling rate of fi to another rate of fo,
317c478bd9Sstevel@tonic-gate  * the least common multiple of fi and fo is derived first:
327c478bd9Sstevel@tonic-gate  *	fm = L * fi = M * fo.
337c478bd9Sstevel@tonic-gate  * Then the input signal is up-sampled to fm by inserting (L -1) zero valued
347c478bd9Sstevel@tonic-gate  * samples after each input sample, low-pass filtered, and down-smapled by
357c478bd9Sstevel@tonic-gate  * saving one sample for every M samples. The implementation takes advantages
367c478bd9Sstevel@tonic-gate  * of the (L - 1) zero values in the filter input and the (M - 1) output
377c478bd9Sstevel@tonic-gate  * samples to be disregarded.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * If L = 1 or M = 1, a simpler decimation or interpolation is used.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <memory.h>
427c478bd9Sstevel@tonic-gate #include <math.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <Resample.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate extern "C" {
477c478bd9Sstevel@tonic-gate 	char *bcopy(char *, char *, int);
487c478bd9Sstevel@tonic-gate 	char *memmove(char *, char *, int);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	BCOPY(src, dest, num) memmove(dest, src, num)
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate // convolve(), short2double() and double2short() are defined in Fir.cc.
547c478bd9Sstevel@tonic-gate extern double convolve(double *, double *, int);
557c478bd9Sstevel@tonic-gate extern void short2double(double *, short *, int);
567c478bd9Sstevel@tonic-gate extern short double2short(double);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate // generate truncated ideal LPF
sinc_coef(int fold,int order,double * coef)597c478bd9Sstevel@tonic-gate static void sinc_coef(int	fold,	// sample rate change
607c478bd9Sstevel@tonic-gate 	int	order,			// LP FIR filter order
617c478bd9Sstevel@tonic-gate 	double  *coef)			// filter coefficients
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int	i;
647c478bd9Sstevel@tonic-gate 	float   alpha;
657c478bd9Sstevel@tonic-gate 	double  bandwidth = M_PI / fold; // digital bandwidth of pass band
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	int half = order >> 1;
687c478bd9Sstevel@tonic-gate 	if (order & 1) {		// order is odd, center = half + 0.5
697c478bd9Sstevel@tonic-gate 		float center = half + 0.5;
707c478bd9Sstevel@tonic-gate 		for (i = 0; i <= half; i++) {
717c478bd9Sstevel@tonic-gate 			alpha = center - i;
727c478bd9Sstevel@tonic-gate 			coef[i] = sin(bandwidth * alpha) / (M_PI * alpha);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	} else {	// order is even, center = half
757c478bd9Sstevel@tonic-gate 		for (i = 0; i < half; i++) {
767c478bd9Sstevel@tonic-gate 			alpha = half - i;
777c478bd9Sstevel@tonic-gate 			coef[i] = sin(bandwidth * alpha) / (M_PI * alpha);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 		coef[i++] = bandwidth / M_PI;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	for (; i <= order; i++)		// symmetric FIR
827c478bd9Sstevel@tonic-gate 		coef[i] = coef[order - i];
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * poly_conv() = coef[0] * data[length - 1] +
877c478bd9Sstevel@tonic-gate  *		 coef[inc_coef] * data[length - 2] +
887c478bd9Sstevel@tonic-gate  *		 ...
897c478bd9Sstevel@tonic-gate  *		 coef[(length - 1) * inc_coef] * data[0] +
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * convolution of coef[order + 1] and data[length] up-sampled by a factor
927c478bd9Sstevel@tonic-gate  * of inc_coef. The terms of coef[1, 2, ... inc_coef - 1] multiplying 0
937c478bd9Sstevel@tonic-gate  * are ignored.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate // polyphase filter convolution
967c478bd9Sstevel@tonic-gate double
poly_conv(double * coef,int order,int inc_coef,double * data,int length)977c478bd9Sstevel@tonic-gate poly_conv(double	*coef,		// filter coef array
987c478bd9Sstevel@tonic-gate 	    int		order,		// filter order
997c478bd9Sstevel@tonic-gate 	    int		inc_coef,	// 1-to-L up-sample for data[length]
1007c478bd9Sstevel@tonic-gate 	    double	*data,		// data array
1017c478bd9Sstevel@tonic-gate 	    int		length)		// data array length
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	if ((order < 0) || (inc_coef < 1) || (length < 1))
1047c478bd9Sstevel@tonic-gate 		return (0.0);
1057c478bd9Sstevel@tonic-gate 	else {
1067c478bd9Sstevel@tonic-gate 		double sum = 0.0;
1077c478bd9Sstevel@tonic-gate 		double *coef_end = coef + order;
1087c478bd9Sstevel@tonic-gate 		double *data_end = data + length;
1097c478bd9Sstevel@tonic-gate 		while ((coef <= coef_end) && (data < data_end)) {
1107c478bd9Sstevel@tonic-gate 			sum += *coef * *--data_end;
1117c478bd9Sstevel@tonic-gate 			coef += inc_coef;
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 		return (sum);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
gcf(int a,int b)1187c478bd9Sstevel@tonic-gate gcf(int a, int b)		// greatest common factor between a and b
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int remainder = a % b;
1217c478bd9Sstevel@tonic-gate 	return (remainder == 0)? b : gcf(b, remainder);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate void ResampleFilter::
updateState(double * in,int size)1257c478bd9Sstevel@tonic-gate updateState(
1267c478bd9Sstevel@tonic-gate 	double *in,
1277c478bd9Sstevel@tonic-gate 	int size)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	if (up <= 1)
1307c478bd9Sstevel@tonic-gate 		Fir::updateState(in, size);
1317c478bd9Sstevel@tonic-gate 	else if (size >= num_state)
1327c478bd9Sstevel@tonic-gate 		memcpy(state, in + size - num_state,
1337c478bd9Sstevel@tonic-gate 		    num_state * sizeof (double));
1347c478bd9Sstevel@tonic-gate 	else {
1357c478bd9Sstevel@tonic-gate 		int old = num_state - size;
1367c478bd9Sstevel@tonic-gate 		BCOPY((char *)(state + size), (char *)state,
1377c478bd9Sstevel@tonic-gate 		    old * sizeof (double));
1387c478bd9Sstevel@tonic-gate 		memcpy(state + old, in, size * sizeof (double));
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate ResampleFilter::			// constructor
ResampleFilter(int rate_in,int rate_out)1437c478bd9Sstevel@tonic-gate ResampleFilter(
1447c478bd9Sstevel@tonic-gate 	int rate_in,			// sampling rate of input signal
1457c478bd9Sstevel@tonic-gate 	int rate_out)			// sampling rate of output signal
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	// filter sampling rate = common multiple of rate_in and rate_out
1487c478bd9Sstevel@tonic-gate 	int commonfactor = gcf(rate_in, rate_out);
1497c478bd9Sstevel@tonic-gate 	up = rate_out / commonfactor;
1507c478bd9Sstevel@tonic-gate 	down = rate_in / commonfactor;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	int fold = (up > down)? up : down;	// take the bigger rate change
1537c478bd9Sstevel@tonic-gate 	order = (fold << 4) - 2;		// filter order = fold * 16 - 2
1547c478bd9Sstevel@tonic-gate 	coef = new double[order + 1];
1557c478bd9Sstevel@tonic-gate 	sinc_coef(fold, order, coef);		// required bandwidth = PI/fold
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (up > 1) {				// need (order/up) states
1587c478bd9Sstevel@tonic-gate 		num_state = (order + up  - 1) / up;
1597c478bd9Sstevel@tonic-gate 		state = new double[num_state];
1607c478bd9Sstevel@tonic-gate 		for (int i = 0; i < num_state; i++)	// reset states
1617c478bd9Sstevel@tonic-gate 			state[i] = 0.0;
1627c478bd9Sstevel@tonic-gate 	} else {
1637c478bd9Sstevel@tonic-gate 		num_state = order;
1647c478bd9Sstevel@tonic-gate 		state = new double[order];	// need order states
1657c478bd9Sstevel@tonic-gate 		resetState();
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	delay = (order + 1) >> 1;	// assuming symmetric FIR
1687c478bd9Sstevel@tonic-gate 	down_offset = 0;
1697c478bd9Sstevel@tonic-gate 	up_offset = 0;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate // down-to-1 decimation
1737c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate_noadjust(short * in,int size,short * out)1747c478bd9Sstevel@tonic-gate decimate_noadjust(short	*in,
1757c478bd9Sstevel@tonic-gate 		int	size,
1767c478bd9Sstevel@tonic-gate 		short	*out)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	int	i;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (size <= 0)
1817c478bd9Sstevel@tonic-gate 		return (0);
1827c478bd9Sstevel@tonic-gate 	else if (down <= 1)		// normal filter
1837c478bd9Sstevel@tonic-gate 		return (Fir::filter_noadjust(in, size, out));
1847c478bd9Sstevel@tonic-gate 	else if (size <= down_offset) {	// just update states
1857c478bd9Sstevel@tonic-gate 		update_short(in, size);
1867c478bd9Sstevel@tonic-gate 		down_offset -= size;
1877c478bd9Sstevel@tonic-gate 		return (0);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	double *in_buf = new double[size];
1917c478bd9Sstevel@tonic-gate 	short2double(in_buf, in, size);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	// filter and decimate and output
1947c478bd9Sstevel@tonic-gate 	short   *out_ptr = out;
1957c478bd9Sstevel@tonic-gate 	int init_size = (size <= order)? size : order;
1967c478bd9Sstevel@tonic-gate 	for (i = down_offset; i < init_size; i += down)
1977c478bd9Sstevel@tonic-gate 		*out_ptr++ = double2short(convolve(coef, in_buf, i + 1) +
1987c478bd9Sstevel@tonic-gate 		    convolve(coef + i + 1, state + i, order - i));
1997c478bd9Sstevel@tonic-gate 	for (; i < size; i += down)
2007c478bd9Sstevel@tonic-gate 		*out_ptr++ = double2short(convolve(coef, in_buf + i - order,
2017c478bd9Sstevel@tonic-gate 		    order + 1));
2027c478bd9Sstevel@tonic-gate 	down_offset = i - size;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	updateState(in_buf, size);
205*7cad4b84SToomas Soome 	delete[] in_buf;
2067c478bd9Sstevel@tonic-gate 	return (out_ptr - out);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate // decimate with group delay adjusted to 0
2107c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate(short * in,int size,short * out)2117c478bd9Sstevel@tonic-gate decimate(short	*in,
2127c478bd9Sstevel@tonic-gate 	int	size,
2137c478bd9Sstevel@tonic-gate 	short	*out)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	if (delay <= 0)
2167c478bd9Sstevel@tonic-gate 		return (decimate_noadjust(in, size, out));
2177c478bd9Sstevel@tonic-gate 	else if (size <= delay) {
2187c478bd9Sstevel@tonic-gate 		update_short(in, size);
2197c478bd9Sstevel@tonic-gate 		delay -= size;
2207c478bd9Sstevel@tonic-gate 		return (0);
2217c478bd9Sstevel@tonic-gate 	} else {
2227c478bd9Sstevel@tonic-gate 		update_short(in, delay);
2237c478bd9Sstevel@tonic-gate 		in += delay;
2247c478bd9Sstevel@tonic-gate 		size -= delay;
2257c478bd9Sstevel@tonic-gate 		delay = 0;
2267c478bd9Sstevel@tonic-gate 		return (decimate_noadjust(in, size, out));
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate // flush decimate filter
2317c478bd9Sstevel@tonic-gate int ResampleFilter::
decimate_flush(short * out)2327c478bd9Sstevel@tonic-gate decimate_flush(short	*out)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	int num_in = Fir::getFlushSize();
2357c478bd9Sstevel@tonic-gate 	short *in = new short[num_in];
2367c478bd9Sstevel@tonic-gate 	memset(in, 0, num_in * sizeof (short));
2377c478bd9Sstevel@tonic-gate 	int num_out = decimate_noadjust(in, num_in, out);
2387c478bd9Sstevel@tonic-gate 	delay += num_in;
239*7cad4b84SToomas Soome 	delete[] in;
2407c478bd9Sstevel@tonic-gate 	return (num_out);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate // 1-to-up interpolation
2447c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate_noadjust(short * in,int size,short * out)2457c478bd9Sstevel@tonic-gate interpolate_noadjust(short	*in,
2467c478bd9Sstevel@tonic-gate 		    int		size,
2477c478bd9Sstevel@tonic-gate 		    short	*out)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	int	i, j;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (size <= 0)
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 	else if (up <= 1)			// regular filter
2547c478bd9Sstevel@tonic-gate 		return (Fir::filter_noadjust(in, size, out));
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	double *in_buf = new double[size];
2577c478bd9Sstevel@tonic-gate 	short2double(in_buf, in, size);
2587c478bd9Sstevel@tonic-gate 	short *out_ptr = out;
2597c478bd9Sstevel@tonic-gate 	// befor the 1st input sample, generate  "-up_offset" output samples
2607c478bd9Sstevel@tonic-gate 	int coef_offset = up + up_offset;
2617c478bd9Sstevel@tonic-gate 	for (j = up_offset; j < 0; j++) {
2627c478bd9Sstevel@tonic-gate 		*out_ptr++ = double2short(up * poly_conv(coef + coef_offset,
2637c478bd9Sstevel@tonic-gate 		    order - coef_offset, up, state, num_state));
2647c478bd9Sstevel@tonic-gate 		coef_offset++;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	// for each of the rest input samples, generate up output samples
2677c478bd9Sstevel@tonic-gate 	for (i = 1; i < size; i++) {
2687c478bd9Sstevel@tonic-gate 		for (j = 0; j < up; j++) {
2697c478bd9Sstevel@tonic-gate 			*out_ptr++ = double2short(up * (poly_conv(coef + j,
2707c478bd9Sstevel@tonic-gate 			    order - j, up, in_buf, i) + poly_conv(
2717c478bd9Sstevel@tonic-gate 			    coef + coef_offset, order - coef_offset, up, state,
2727c478bd9Sstevel@tonic-gate 			    num_state)));
2737c478bd9Sstevel@tonic-gate 			coef_offset++;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	// for the last input samples, generate "up_offset + up" output samples
2787c478bd9Sstevel@tonic-gate 	for (j = 0; j < (up_offset + up); j++) {
2797c478bd9Sstevel@tonic-gate 		*out_ptr++ = double2short(up * (poly_conv(coef + j,
2807c478bd9Sstevel@tonic-gate 		    order - j, up, in_buf, size) + poly_conv(
2817c478bd9Sstevel@tonic-gate 		    coef + coef_offset, order - coef_offset, up, state,
2827c478bd9Sstevel@tonic-gate 		    num_state)));
2837c478bd9Sstevel@tonic-gate 		coef_offset++;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	updateState(in_buf, size);
286*7cad4b84SToomas Soome 	delete[] in_buf;
2877c478bd9Sstevel@tonic-gate 	return (out_ptr - out);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate // flush interpolate filter
2917c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate_flush(short * out)2927c478bd9Sstevel@tonic-gate interpolate_flush(short	*out)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	int num = (Fir::getFlushSize() + up - 1) / up;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	short *in = new short[num];
2977c478bd9Sstevel@tonic-gate 	memset(in, 0, num * sizeof (short));
2987c478bd9Sstevel@tonic-gate 	int out_num = interpolate_noadjust(in, num, out);
2997c478bd9Sstevel@tonic-gate 	delay += num * up;
300*7cad4b84SToomas Soome 	delete[] in;
3017c478bd9Sstevel@tonic-gate 	return (out_num);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate // interpolate with delay adjusted
3057c478bd9Sstevel@tonic-gate int ResampleFilter::
interpolate(short * in,int size,short * out)3067c478bd9Sstevel@tonic-gate interpolate(short *in,
3077c478bd9Sstevel@tonic-gate 	    int size,
3087c478bd9Sstevel@tonic-gate 	    short *out)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	if (size <= 0)
3117c478bd9Sstevel@tonic-gate 		return (interpolate_flush(out));
3127c478bd9Sstevel@tonic-gate 	else if (delay <= 0)
3137c478bd9Sstevel@tonic-gate 		return (interpolate_noadjust(in, size, out));
3147c478bd9Sstevel@tonic-gate 	else {
3157c478bd9Sstevel@tonic-gate 		int delay_in = (delay + up - 1) / up;
3167c478bd9Sstevel@tonic-gate 		if (size < delay_in)
3177c478bd9Sstevel@tonic-gate 			delay_in = size;
3187c478bd9Sstevel@tonic-gate 		double *in_buf = new double[delay_in];
3197c478bd9Sstevel@tonic-gate 		short2double(in_buf, in, delay_in);
3207c478bd9Sstevel@tonic-gate 		updateState(in_buf, delay_in);
321*7cad4b84SToomas Soome 		delete[] in_buf;
3227c478bd9Sstevel@tonic-gate 		delay -= delay_in * up;
3237c478bd9Sstevel@tonic-gate 		up_offset = delay;
3247c478bd9Sstevel@tonic-gate 		return (interpolate_noadjust(in + delay_in, size -
3257c478bd9Sstevel@tonic-gate 		    delay_in, out));
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate int ResampleFilter::
filter_noadjust(short * in,int size,short * out)3307c478bd9Sstevel@tonic-gate filter_noadjust(short	*in,		// non-integer sampling rate conversion
3317c478bd9Sstevel@tonic-gate 	int	size,
3327c478bd9Sstevel@tonic-gate 	short	*out)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	int	i, j;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (size <= 0)
3377c478bd9Sstevel@tonic-gate 		return (0);
3387c478bd9Sstevel@tonic-gate 	else if (up <= 1)
3397c478bd9Sstevel@tonic-gate 		return (decimate_noadjust(in, size, out));
3407c478bd9Sstevel@tonic-gate 	else if (down <= 1)
3417c478bd9Sstevel@tonic-gate 		return (interpolate_noadjust(in, size, out));
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	double *in_buf = new double[size];
3447c478bd9Sstevel@tonic-gate 	short2double(in_buf, in, size);
3457c478bd9Sstevel@tonic-gate 	short *init_out = out;
3467c478bd9Sstevel@tonic-gate 	int coef_offset = up_offset + down_offset + up;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * before the 1st input sample,
3507c478bd9Sstevel@tonic-gate 	 * start from "up_offset + down_offset"th up-sampled sample
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	for (j = up_offset + down_offset; j < 0; j += down) {
3537c478bd9Sstevel@tonic-gate 		*out++ = double2short(up * poly_conv(coef + coef_offset,
3547c478bd9Sstevel@tonic-gate 		    order - coef_offset, up, state, num_state));
3557c478bd9Sstevel@tonic-gate 		coef_offset += down;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	// process the input samples until the last one
3597c478bd9Sstevel@tonic-gate 	for (i = 1; i < size; i++) {
3607c478bd9Sstevel@tonic-gate 		for (; j < up; j += down) {
3617c478bd9Sstevel@tonic-gate 			*out++ = double2short(up * (poly_conv(coef + j,
3627c478bd9Sstevel@tonic-gate 			    order - j, up, in_buf, i) + poly_conv(
3637c478bd9Sstevel@tonic-gate 			    coef + coef_offset, order - coef_offset, up, state,
3647c478bd9Sstevel@tonic-gate 			    num_state)));
3657c478bd9Sstevel@tonic-gate 			coef_offset += down;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		j -= up;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	// for the last input sample, end at the "up + up_offset"th
3717c478bd9Sstevel@tonic-gate 	for (; j < (up + up_offset); j += down) {
3727c478bd9Sstevel@tonic-gate 		*out++ = double2short(up * (poly_conv(coef + j, order - j, up,
3737c478bd9Sstevel@tonic-gate 		    in_buf, size) + poly_conv(coef + coef_offset,
3747c478bd9Sstevel@tonic-gate 		    order - coef_offset, up, state, num_state)));
3757c478bd9Sstevel@tonic-gate 		coef_offset += down;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	down_offset = j - (up + up_offset);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	updateState(in_buf, size);
380*7cad4b84SToomas Soome 	delete[] in_buf;
3817c478bd9Sstevel@tonic-gate 	return (out - init_out);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate int ResampleFilter::
getFlushSize(void)3857c478bd9Sstevel@tonic-gate getFlushSize(void)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	int num_in = (Fir::getFlushSize() + up - 1) / up;
3887c478bd9Sstevel@tonic-gate 	return ((num_in * up + down - 1 - down_offset) / down);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate int ResampleFilter::
flush(short * out)3927c478bd9Sstevel@tonic-gate flush(short	*out)		// flush resampling filter
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	if (down <= 1)
3967c478bd9Sstevel@tonic-gate 		return (interpolate_flush(out));
3977c478bd9Sstevel@tonic-gate 	else if (up <= 1)
3987c478bd9Sstevel@tonic-gate 		return (decimate_flush(out));
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	int num = (Fir::getFlushSize() + up - 1) / up;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	short *in = new short[num];
4037c478bd9Sstevel@tonic-gate 	memset(in, 0, num * sizeof (short));
4047c478bd9Sstevel@tonic-gate 	int out_num = filter_noadjust(in, num, out);
405*7cad4b84SToomas Soome 	delete[] in;
4067c478bd9Sstevel@tonic-gate 	delay += num * up;
4077c478bd9Sstevel@tonic-gate 	return (out_num);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * sampling rate conversion with filter delay adjusted
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate int ResampleFilter::
filter(short * in,int size,short * out)4147c478bd9Sstevel@tonic-gate filter(
4157c478bd9Sstevel@tonic-gate 	short	*in,
4167c478bd9Sstevel@tonic-gate 	int	size,
4177c478bd9Sstevel@tonic-gate 	short	*out)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	if (size <= 0)
4207c478bd9Sstevel@tonic-gate 		return (flush(out));
4217c478bd9Sstevel@tonic-gate 	else if (up <= 1)
4227c478bd9Sstevel@tonic-gate 		return (decimate(in, size, out));
4237c478bd9Sstevel@tonic-gate 	else if (down <= 1)
4247c478bd9Sstevel@tonic-gate 		return (interpolate(in, size, out));
4257c478bd9Sstevel@tonic-gate 	else if (delay <= 0)
4267c478bd9Sstevel@tonic-gate 		return (filter_noadjust(in, size, out));
4277c478bd9Sstevel@tonic-gate 	else {
4287c478bd9Sstevel@tonic-gate 		int delay_in = (delay + up - 1) / up;
4297c478bd9Sstevel@tonic-gate 		if (size < delay_in)
4307c478bd9Sstevel@tonic-gate 			delay_in = size;
4317c478bd9Sstevel@tonic-gate 		double *in_buf = new double[delay_in];
4327c478bd9Sstevel@tonic-gate 		short2double(in_buf, in, delay_in);
4337c478bd9Sstevel@tonic-gate 		updateState(in_buf, delay_in);
434*7cad4b84SToomas Soome 		delete[] in_buf;
4357c478bd9Sstevel@tonic-gate 		delay -= up * delay_in;
4367c478bd9Sstevel@tonic-gate 		if (delay <= 0) {
4377c478bd9Sstevel@tonic-gate 			up_offset = delay;
4387c478bd9Sstevel@tonic-gate 			down_offset = 0;
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 		return (filter_noadjust(in + delay_in, size - delay_in, out));
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate }
443