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) 1993-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _MULTIMEDIA_AUDIOGAIN_H
28 #define	_MULTIMEDIA_AUDIOGAIN_H
29 
30 #include <AudioTypePcm.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 // Class to handle gain calculations
37 
38 // Define bits for AudioGain::Process() type argument
39 #define	AUDIO_GAIN_INSTANT	(1)		// Gain for level meter
40 #define	AUDIO_GAIN_WEIGHTED	(2)		// Gain for agc
41 
42 
43 class AudioGain {
44 protected:
45 
46 static const double	LoSigInstantRange;	// normalization constants
47 static const double	HiSigInstantRange;
48 static const double	NoSigWeight;
49 static const double	LoSigWeightRange;
50 static const double	HiSigWeightRange;
51 static const double	PeakSig;
52 static const double	DCtimeconstant;		// DC offset time constant
53 
54 	AudioTypePcm	float_convert;		// used in signal processing
55 	unsigned	clipcnt;		// clip counter
56 	Double		DCaverage;		// weighted DC offset
57 	Double		instant_gain;		// current (instantaneous) gain
58 	Double		weighted_peaksum;	// peak weighted sum
59 	Double		weighted_sum;		// running sum of squares
60 	Double		weighted_avgsum;	// accumulated sums to averages
61 	unsigned	weighted_cnt;		// number of sums to average
62 	double		*gain_cache;		// weighted gains
63 	Double		gain_cache_size;	// number of cached gains
64 
65 protected:
66 	// Internal processing methods
67 
68 	// filter DC bias
69 	virtual void process_dcfilter(
70 	    AudioBuffer*);
71 	// calculate instant gain
72 	virtual void process_instant(
73 	    AudioBuffer*);
74 	// calculate weighted gain
75 	virtual void process_weighted(
76 	    AudioBuffer*);
77 
78 public:
79 		AudioGain();			// Constructor
80 	virtual	~AudioGain();			// Destructor
81 
82 	// TRUE if conversion ok
83 	virtual Boolean CanConvert(
84 	    const AudioHdr&) const;	// type to check against
85 
86 	// Process new audio data
87 	virtual AudioError Process(
88 	    AudioBuffer*, int);		// buffer destroyed if not referenced!
89 	virtual double InstantGain();	// Get most recent gain
90 	virtual double WeightedGain();	// Get current weighted gain
91 	virtual double WeightedPeak();	// Get peak weighted gain
92 	virtual Boolean Clipped();	// TRUE if peak since last check
93 	virtual void Flush();		// Reset state
94 };
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* !_MULTIMEDIA_AUDIOGAIN_H */
101