Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VOICE_GAIN_H
  8. #define VOICE_GAIN_H
  9. #pragma once
  10. // ----------------------------------------------------------------------- //
  11. // CAutoGain is fed samples and figures out a gain to apply to blocks of samples.
  12. // Right now, this class applies gain one block behind. The assumption is that the blocks are
  13. // small enough that gain settings for one block will usually be right for the next block.
  14. // The ideal way to implement this class would be to have a delay the size of a block
  15. // so it can apply the right gain to the actual block it was calculated for.
  16. // ----------------------------------------------------------------------- //
  17. class CAutoGain
  18. {
  19. public:
  20. CAutoGain();
  21. // maxGain and avgToMaxVal are used to derive the gain amount for each block of samples.
  22. // All samples are scaled by scale.
  23. void Reset(int blockSize, float maxGain, float avgToMaxVal, float scale);
  24. // Process the specified samples and apply gain to them.
  25. void ProcessSamples(
  26. short *pSamples,
  27. int nSamples);
  28. private:
  29. enum {AG_FIX_SHIFT=7};
  30. typedef long AGFixed;
  31. // Parameters affecting the algorithm.
  32. int m_BlockSize; // Derive gain from blocks of this size.
  33. float m_MaxGain;
  34. float m_AvgToMaxVal;
  35. // These are calculated as samples are passed in.
  36. int m_CurBlockOffset;
  37. int m_CurTotal; // Total of sample values in current block.
  38. int m_CurMax; // Highest (absolute) sample value.
  39. float m_Scale; // All samples are scaled by this amount.
  40. float m_CurrentGain; // Gain at sample 0 in this block.
  41. float m_NextGain; // Gain at the last sample in this block.
  42. AGFixed m_FixedCurrentGain; // Fixed-point m_CurrentGain.
  43. AGFixed m_GainMultiplier; // (m_NextGain - m_CurrentGain) / (m_BlockSize - 1).
  44. };
  45. #endif // VOICE_GAIN_H