summaryrefslogtreecommitdiff
path: root/engine/audio/private/voice_gain.h
blob: cd614e020bf0f46940dbe70416dbd472ca777ee7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef VOICE_GAIN_H
#define VOICE_GAIN_H
#pragma once


// ----------------------------------------------------------------------- //
// CAutoGain is fed samples and figures out a gain to apply to blocks of samples.

// Right now, this class applies gain one block behind. The assumption is that the blocks are
// small enough that gain settings for one block will usually be right for the next block.

// The ideal way to implement this class would be to have a delay the size of a block
// so it can apply the right gain to the actual block it was calculated for.
// ----------------------------------------------------------------------- //
class CAutoGain
{
public:

			CAutoGain();

	// maxGain and avgToMaxVal are used to derive the gain amount for each block of samples.
	// All samples are scaled by scale.
	void	Reset(int blockSize, float maxGain, float avgToMaxVal, float scale);

	// Process the specified samples and apply gain to them.
	void	ProcessSamples(
		short *pSamples,
		int nSamples);

private:

	enum	{AG_FIX_SHIFT=7};
	typedef long	AGFixed;

	// Parameters affecting the algorithm.
	int		m_BlockSize;			// Derive gain from blocks of this size.
	float	m_MaxGain;
	float	m_AvgToMaxVal;

	// These are calculated as samples are passed in.
	int		m_CurBlockOffset;
	int		m_CurTotal;				// Total of sample values in current block.
	int		m_CurMax;				// Highest (absolute) sample value.

	float	m_Scale;				// All samples are scaled by this amount.

	float	m_CurrentGain;			// Gain at sample 0 in this block.
	float	m_NextGain;				// Gain at the last sample in this block.

	AGFixed	m_FixedCurrentGain;		// Fixed-point m_CurrentGain.	
	AGFixed	m_GainMultiplier;		// (m_NextGain - m_CurrentGain) / (m_BlockSize - 1).
};


#endif // VOICE_GAIN_H