xref: /illumos-gate/usr/src/cmd/audio/include/Resample.h (revision 9726429325e0e93eb1a1f724f6db6a9fd6ad63ce)
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 /*
31  * To convert the sampling rate of fi of input signal to fo of output signal,
32  * the least common multiple fm = L * fi = M * fo needs to be derived first.
33  * Then the input signal is
34  * 1) up-sampled to fm by inserting (L - 1) zero-valued samples after each
35  *    input sample,
36  * 2) low-pass filtered to half of the lower of fi and fo, and
37  * 3) down-sampled to fo by saving one out of every M samples.
38  *
39  * The low-pass filter is implemented with an FIR filter which is a truncated
40  * ideal low pass filter whose order is dependent on its bandwidth.
41  *
42  * Refer to Fir.h for explanations of filter() and flush().
43  *
44  */
45 #include <Fir.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 class ResampleFilter : public Fir {
52 	int	num_state;		// interpolation requires less states
53 	int	up;			// upsampling ratio
54 	int	down;			// down sampling ratio
55 	int	down_offset;		// 0 <= index in down-sampling < down
56 	int	up_offset;		// -up < index in up_sampling <= 0
57 
58 	void updateState(double *in, int size);
59 
60 	// if fi is a multiple of fo, LP filtering followed by down_sampling
61 	int decimate_noadjust(short *in, int size, short *out);
62 	int decimate_flush(short *);
63 	int decimate(short *in, int size, short *out);
64 
65 	// if fo is a multiple of fi, up-sampling followed by LP filtering
66 	int interpolate_noadjust(short *in, int size, short *out);
67 	int interpolate_flush(short *);
68 	int interpolate(short *in, int size, short *out);
69 
70 	int flush(short *out);
71 
72 public:
73 	ResampleFilter(int rate_in, int rate_out);
74 	virtual int	filter_noadjust(short *in, int size, short *out);
75 	virtual int	filter(short *in, int size, short *out);
76 	virtual int	getFlushSize(void);
77 };
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif /* !_MULTIMEDIA_RESAMPLE_H */
84