xref: /titanic_41/usr/src/cmd/audio/include/Resample.h (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _MULTIMEDIA_RESAMPLE_H
28 #define	_MULTIMEDIA_RESAMPLE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * To convert the sampling rate of fi of input signal to fo of output signal,
34  * the least common multiple fm = L * fi = M * fo needs to be derived first.
35  * Then the input signal is
36  * 1) up-sampled to fm by inserting (L - 1) zero-valued samples after each
37  *    input sample,
38  * 2) low-pass filtered to half of the lower of fi and fo, and
39  * 3) down-sampled to fo by saving one out of every M samples.
40  *
41  * The low-pass filter is implemented with an FIR filter which is a truncated
42  * ideal low pass filter whose order is dependent on its bandwidth.
43  *
44  * Refer to Fir.h for explanations of filter() and flush().
45  *
46  */
47 #include <Fir.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 class ResampleFilter : public Fir {
54 	int	num_state;		// interpolation requires less states
55 	int	up;			// upsampling ratio
56 	int	down;			// down sampling ratio
57 	int	down_offset;		// 0 <= index in down-sampling < down
58 	int	up_offset;		// -up < index in up_sampling <= 0
59 
60 	void updateState(double *in, int size);
61 
62 	// if fi is a multiple of fo, LP filtering followed by down_sampling
63 	int decimate_noadjust(short *in, int size, short *out);
64 	int decimate_flush(short *);
65 	int decimate(short *in, int size, short *out);
66 
67 	// if fo is a multiple of fi, up-sampling followed by LP filtering
68 	int interpolate_noadjust(short *in, int size, short *out);
69 	int interpolate_flush(short *);
70 	int interpolate(short *in, int size, short *out);
71 
72 	int flush(short *out);
73 
74 public:
75 	ResampleFilter(int rate_in, int rate_out);
76 	virtual int	filter_noadjust(short *in, int size, short *out);
77 	virtual int	filter(short *in, int size, short *out);
78 	virtual int	getFlushSize(void);
79 };
80 
81 #ifdef __cplusplus
82 }
83 #endif
84 
85 #endif /* !_MULTIMEDIA_RESAMPLE_H */
86