Reverb.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Reverb.h
  3. * --------
  4. * Purpose: Mixing code for reverb.
  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. #pragma once
  11. #include "openmpt/all/BuildSettings.hpp"
  12. #ifndef NO_REVERB
  13. #include "../soundlib/Mixer.h" // For MIXBUFFERSIZE
  14. OPENMPT_NAMESPACE_BEGIN
  15. ////////////////////////////////////////////////////////////////////////
  16. // Reverberation
  17. /////////////////////////////////////////////////////////////////////////////
  18. //
  19. // SW Reverb structures
  20. //
  21. // Length-1 (in samples) of the reflections delay buffer: 32K, 371ms@22kHz
  22. #define SNDMIX_REFLECTIONS_DELAY_MASK 0x1fff
  23. #define SNDMIX_PREDIFFUSION_DELAY_MASK 0x7f // 128 samples
  24. #define SNDMIX_REVERB_DELAY_MASK 0xfff // 4K samples (92ms @ 44kHz)
  25. union LR16
  26. {
  27. struct { int16 l, r; } c;
  28. int32 lr;
  29. };
  30. struct SWRvbReflection
  31. {
  32. uint32 Delay, DelayDest;
  33. LR16 Gains[2]; // g_ll, g_rl, g_lr, g_rr
  34. };
  35. struct SWRvbRefDelay
  36. {
  37. uint32 nDelayPos, nPreDifPos, nRefOutPos;
  38. int32 lMasterGain; // reflections linear master gain
  39. LR16 nCoeffs; // room low-pass coefficients
  40. LR16 History; // room low-pass history
  41. LR16 nPreDifCoeffs; // prediffusion coefficients
  42. LR16 ReflectionsGain; // master reflections gain
  43. SWRvbReflection Reflections[8]; // Up to 8 SW Reflections
  44. LR16 RefDelayBuffer[SNDMIX_REFLECTIONS_DELAY_MASK + 1]; // reflections delay buffer
  45. LR16 PreDifBuffer[SNDMIX_PREDIFFUSION_DELAY_MASK + 1]; // pre-diffusion
  46. LR16 RefOut[SNDMIX_REVERB_DELAY_MASK + 1]; // stereo output of reflections
  47. };
  48. struct SNDMIX_REVERB_PROPERTIES;
  49. // Late reverberation
  50. // Tank diffusers lengths
  51. #define RVBDIF1L_LEN (149*2) // 6.8ms
  52. #define RVBDIF1R_LEN (223*2) // 10.1ms
  53. #define RVBDIF2L_LEN (421*2) // 19.1ms
  54. #define RVBDIF2R_LEN (647*2) // 29.3ms
  55. // Tank delay lines lengths
  56. #define RVBDLY1L_LEN (683*2) // 30.9ms
  57. #define RVBDLY1R_LEN (811*2) // 36.7ms
  58. #define RVBDLY2L_LEN (773*2) // 35.1ms
  59. #define RVBDLY2R_LEN (1013*2) // 45.9ms
  60. // Tank delay lines mask
  61. #define RVBDLY_MASK 2047
  62. // Min/Max reflections delay
  63. #define RVBMINREFDELAY 96 // 96 samples
  64. #define RVBMAXREFDELAY 7500 // 7500 samples
  65. // Min/Max reverb delay
  66. #define RVBMINRVBDELAY 128 // 256 samples (11.6ms @ 22kHz)
  67. #define RVBMAXRVBDELAY 3800 // 1900 samples (86ms @ 24kHz)
  68. struct SWLateReverb
  69. {
  70. uint32 nReverbDelay; // Reverb delay (in samples)
  71. uint32 nDelayPos; // Delay line position
  72. LR16 nDifCoeffs[2]; // Reverb diffusion
  73. LR16 nDecayDC[2]; // Reverb DC decay
  74. LR16 nDecayLP[2]; // Reverb HF decay
  75. LR16 LPHistory[2]; // Low-pass history
  76. LR16 Dif2InGains[2]; // 2nd diffuser input gains
  77. LR16 RvbOutGains[2]; // 4x2 Reverb output gains
  78. int32 lMasterGain; // late reverb master gain
  79. int32 lDummyAlign;
  80. // Tank Delay lines
  81. LR16 Diffusion1[RVBDLY_MASK + 1]; // {dif1_l, dif1_r}
  82. LR16 Diffusion2[RVBDLY_MASK + 1]; // {dif2_l, dif2_r}
  83. LR16 Delay1[RVBDLY_MASK + 1]; // {dly1_l, dly1_r}
  84. LR16 Delay2[RVBDLY_MASK + 1]; // {dly2_l, dly2_r}
  85. };
  86. #define ENVIRONMENT_NUMREFLECTIONS 8
  87. struct EnvironmentReflection
  88. {
  89. int16 GainLL, GainRR, GainLR, GainRL; // +/- 32K scale
  90. uint32 Delay; // In samples
  91. };
  92. struct EnvironmentReverb
  93. {
  94. int32 ReverbLevel; // Late reverb gain (mB)
  95. int32 ReflectionsLevel; // Master reflections gain (mB)
  96. int32 RoomHF; // Room gain HF (mB)
  97. uint32 ReverbDecay; // Reverb tank decay (0-7fff scale)
  98. int32 PreDiffusion; // Reverb pre-diffusion amount (+/- 32K scale)
  99. int32 TankDiffusion; // Reverb tank diffusion (+/- 32K scale)
  100. uint32 ReverbDelay; // Reverb delay (in samples)
  101. float flReverbDamping; // HF tank gain [0.0, 1.0]
  102. int32 ReverbDecaySamples; // Reverb decay time (in samples)
  103. EnvironmentReflection Reflections[ENVIRONMENT_NUMREFLECTIONS];
  104. };
  105. class CReverbSettings
  106. {
  107. public:
  108. uint32 m_nReverbDepth = 8; // 50%
  109. uint32 m_nReverbType = 0;
  110. };
  111. class CReverb
  112. {
  113. public:
  114. CReverbSettings m_Settings;
  115. private:
  116. const SNDMIX_REVERB_PROPERTIES *m_currentPreset = nullptr;
  117. bool gnReverbSend = false;
  118. uint32 gnReverbSamples = 0;
  119. uint32 gnReverbDecaySamples = 0;
  120. // Internal reverb state
  121. bool g_bLastInPresent = 0;
  122. bool g_bLastOutPresent = 0;
  123. int g_nLastRvbIn_xl = 0;
  124. int g_nLastRvbIn_xr = 0;
  125. int g_nLastRvbIn_yl = 0;
  126. int g_nLastRvbIn_yr = 0;
  127. int g_nLastRvbOut_xl = 0;
  128. int g_nLastRvbOut_xr = 0;
  129. int32 gnDCRRvb_Y1[2] = { 0, 0 };
  130. int32 gnDCRRvb_X1[2] = { 0, 0 };
  131. // Reverb mix buffers
  132. SWRvbRefDelay g_RefDelay;
  133. SWLateReverb g_LateReverb;
  134. public:
  135. CReverb();
  136. public:
  137. void Initialize(bool bReset, MixSampleInt &gnRvbROfsVol, MixSampleInt &gnRvbLOfsVol, uint32 MixingFreq);
  138. // can be called multiple times or never (if no data is sent to reverb)
  139. void TouchReverbSendBuffer(MixSampleInt *MixReverbBuffer, MixSampleInt &gnRvbROfsVol, MixSampleInt &gnRvbLOfsVol, uint32 nSamples);
  140. // call once after all data has been sent.
  141. void Process(MixSampleInt *MixSoundBuffer, MixSampleInt *MixReverbBuffer, MixSampleInt &gnRvbROfsVol, MixSampleInt &gnRvbLOfsVol, uint32 nSamples);
  142. private:
  143. void Shutdown(MixSampleInt &gnRvbROfsVol, MixSampleInt &gnRvbLOfsVol);
  144. // Pre/Post resampling and filtering
  145. uint32 ReverbProcessPreFiltering1x(int32 *pWet, uint32 nSamples);
  146. uint32 ReverbProcessPreFiltering2x(int32 *pWet, uint32 nSamples);
  147. void ReverbProcessPostFiltering1x(const int32 *pRvb, int32 *pDry, uint32 nSamples);
  148. void ReverbProcessPostFiltering2x(const int32 *pRvb, int32 *pDry, uint32 nSamples);
  149. void ReverbDCRemoval(int32 *pBuffer, uint32 nSamples);
  150. void ReverbDryMix(int32 *pDry, int32 *pWet, int lDryVol, uint32 nSamples);
  151. // Process pre-diffusion and pre-delay
  152. static void ProcessPreDelay(SWRvbRefDelay *pPreDelay, const int32 *pIn, uint32 nSamples);
  153. // Process reflections
  154. static void ProcessReflections(SWRvbRefDelay *pPreDelay, LR16 *pRefOut, int32 *pMixOut, uint32 nSamples);
  155. // Process Late Reverb (SW Reflections): stereo reflections output, 32-bit reverb output, SW reverb gain
  156. static void ProcessLateReverb(SWLateReverb *pReverb, LR16 *pRefOut, int32 *pMixOut, uint32 nSamples);
  157. };
  158. /////////////////////////////////////////////////////////////////////////////////
  159. //
  160. // I3DL2 reverb presets
  161. //
  162. struct SNDMIX_REVERB_PROPERTIES
  163. {
  164. int32 lRoom; // [-10000, 0] default: -10000 mB
  165. int32 lRoomHF; // [-10000, 0] default: 0 mB
  166. float flDecayTime; // [0.1, 20.0] default: 1.0 s
  167. float flDecayHFRatio; // [0.1, 2.0] default: 0.5
  168. int32 lReflections; // [-10000, 1000] default: -10000 mB
  169. float flReflectionsDelay; // [0.0, 0.3] default: 0.02 s
  170. int32 lReverb; // [-10000, 2000] default: -10000 mB
  171. float flReverbDelay; // [0.0, 0.1] default: 0.04 s
  172. float flDiffusion; // [0.0, 100.0] default: 100.0 %
  173. float flDensity; // [0.0, 100.0] default: 100.0 %
  174. };
  175. enum : uint32
  176. {
  177. NUM_REVERBTYPES = 29
  178. };
  179. mpt::ustring GetReverbPresetName(uint32 preset);
  180. const SNDMIX_REVERB_PROPERTIES *GetReverbPreset(uint32 preset);
  181. OPENMPT_NAMESPACE_END
  182. #endif // NO_REVERB