AGC.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * AGC.cpp
  3. * -------
  4. * Purpose: Automatic Gain Control
  5. * Notes : Ugh... This should really be removed at some point.
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "../sounddsp/AGC.h"
  12. OPENMPT_NAMESPACE_BEGIN
  13. //////////////////////////////////////////////////////////////////////////////////
  14. // Automatic Gain Control
  15. #ifndef NO_AGC
  16. #define AGC_PRECISION 10
  17. #define AGC_UNITY (1 << AGC_PRECISION)
  18. // Limiter
  19. #define MIXING_LIMITMAX (0x08100000)
  20. #define MIXING_LIMITMIN (-MIXING_LIMITMAX)
  21. static UINT ProcessAGC(int *pBuffer, int *pRearBuffer, std::size_t nSamples, std::size_t nChannels, int nAGC)
  22. {
  23. if(nChannels == 1)
  24. {
  25. while(nSamples--)
  26. {
  27. int val = (int)(((int64)*pBuffer * (int32)nAGC) >> AGC_PRECISION);
  28. if(val < MIXING_LIMITMIN || val > MIXING_LIMITMAX) nAGC--;
  29. *pBuffer = val;
  30. pBuffer++;
  31. }
  32. } else
  33. {
  34. if(nChannels == 2)
  35. {
  36. while(nSamples--)
  37. {
  38. int fl = (int)(((int64)pBuffer[0] * (int32)nAGC) >> AGC_PRECISION);
  39. int fr = (int)(((int64)pBuffer[1] * (int32)nAGC) >> AGC_PRECISION);
  40. bool dec = false;
  41. dec = dec || (fl < MIXING_LIMITMIN || fl > MIXING_LIMITMAX);
  42. dec = dec || (fr < MIXING_LIMITMIN || fr > MIXING_LIMITMAX);
  43. if(dec) nAGC--;
  44. pBuffer[0] = fl;
  45. pBuffer[1] = fr;
  46. pBuffer += 2;
  47. }
  48. } else if(nChannels == 4)
  49. {
  50. while(nSamples--)
  51. {
  52. int fl = (int)(((int64)pBuffer[0] * (int32)nAGC) >> AGC_PRECISION);
  53. int fr = (int)(((int64)pBuffer[1] * (int32)nAGC) >> AGC_PRECISION);
  54. int rl = (int)(((int64)pRearBuffer[0] * (int32)nAGC) >> AGC_PRECISION);
  55. int rr = (int)(((int64)pRearBuffer[1] * (int32)nAGC) >> AGC_PRECISION);
  56. bool dec = false;
  57. dec = dec || (fl < MIXING_LIMITMIN || fl > MIXING_LIMITMAX);
  58. dec = dec || (fr < MIXING_LIMITMIN || fr > MIXING_LIMITMAX);
  59. dec = dec || (rl < MIXING_LIMITMIN || rl > MIXING_LIMITMAX);
  60. dec = dec || (rr < MIXING_LIMITMIN || rr > MIXING_LIMITMAX);
  61. if(dec) nAGC--;
  62. pBuffer[0] = fl;
  63. pBuffer[1] = fr;
  64. pRearBuffer[0] = rl;
  65. pRearBuffer[1] = rr;
  66. pBuffer += 2;
  67. pRearBuffer += 2;
  68. }
  69. }
  70. }
  71. return nAGC;
  72. }
  73. CAGC::CAGC()
  74. {
  75. Initialize(true, 44100);
  76. }
  77. void CAGC::Process(int *MixSoundBuffer, int *RearSoundBuffer, std::size_t count, std::size_t nChannels)
  78. {
  79. UINT agc = ProcessAGC(MixSoundBuffer, RearSoundBuffer, count, nChannels, m_nAGC);
  80. // Some kind custom law, so that the AGC stays quite stable, but slowly
  81. // goes back up if the sound level stays below a level inversely proportional
  82. // to the AGC level. (J'me comprends)
  83. if((agc >= m_nAGC) && (m_nAGC < AGC_UNITY))
  84. {
  85. m_nAGCRecoverCount += count;
  86. if(m_nAGCRecoverCount >= m_Timeout)
  87. {
  88. m_nAGCRecoverCount = 0;
  89. m_nAGC++;
  90. }
  91. } else
  92. {
  93. m_nAGC = agc;
  94. m_nAGCRecoverCount = 0;
  95. }
  96. }
  97. void CAGC::Adjust(UINT oldVol, UINT newVol)
  98. {
  99. m_nAGC = m_nAGC * oldVol / newVol;
  100. if (m_nAGC > AGC_UNITY) m_nAGC = AGC_UNITY;
  101. }
  102. void CAGC::Initialize(bool bReset, DWORD MixingFreq)
  103. {
  104. if(bReset)
  105. {
  106. m_nAGC = AGC_UNITY;
  107. m_nAGCRecoverCount = 0;
  108. }
  109. m_Timeout = (MixingFreq >> (AGC_PRECISION-8)) >> 1;
  110. }
  111. #else
  112. MPT_MSVC_WORKAROUND_LNK4221(AGC)
  113. #endif // NO_AGC
  114. OPENMPT_NAMESPACE_END