mastering.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "config.h"
  2. #include <math.h>
  3. #include "mastering.h"
  4. #include "alu.h"
  5. #include "almalloc.h"
  6. extern inline ALuint GetCompressorSampleRate(const Compressor *Comp);
  7. #define RMS_WINDOW_SIZE (1<<7)
  8. #define RMS_WINDOW_MASK (RMS_WINDOW_SIZE-1)
  9. #define RMS_VALUE_MAX (1<<24)
  10. static_assert(RMS_VALUE_MAX < (UINT_MAX / RMS_WINDOW_SIZE), "RMS_VALUE_MAX is too big");
  11. /* Multichannel compression is linked via one of two modes:
  12. *
  13. * Summed - Absolute sum of all channels.
  14. * Maxed - Absolute maximum of any channel.
  15. */
  16. static void SumChannels(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
  17. ALfloat (*restrict OutBuffer)[BUFFERSIZE])
  18. {
  19. ALsizei c, i;
  20. for(i = 0;i < SamplesToDo;i++)
  21. Comp->Envelope[i] = 0.0f;
  22. for(c = 0;c < NumChans;c++)
  23. {
  24. for(i = 0;i < SamplesToDo;i++)
  25. Comp->Envelope[i] += OutBuffer[c][i];
  26. }
  27. for(i = 0;i < SamplesToDo;i++)
  28. Comp->Envelope[i] = fabsf(Comp->Envelope[i]);
  29. }
  30. static void MaxChannels(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
  31. ALfloat (*restrict OutBuffer)[BUFFERSIZE])
  32. {
  33. ALsizei c, i;
  34. for(i = 0;i < SamplesToDo;i++)
  35. Comp->Envelope[i] = 0.0f;
  36. for(c = 0;c < NumChans;c++)
  37. {
  38. for(i = 0;i < SamplesToDo;i++)
  39. Comp->Envelope[i] = maxf(Comp->Envelope[i], fabsf(OutBuffer[c][i]));
  40. }
  41. }
  42. /* Envelope detection/sensing can be done via:
  43. *
  44. * RMS - Rectangular windowed root mean square of linking stage.
  45. * Peak - Implicit output from linking stage.
  46. */
  47. static void RmsDetection(Compressor *Comp, const ALsizei SamplesToDo)
  48. {
  49. ALuint sum = Comp->RmsSum;
  50. ALuint *window = Comp->RmsWindow;
  51. ALsizei index = Comp->RmsIndex;
  52. ALsizei i;
  53. for(i = 0;i < SamplesToDo;i++)
  54. {
  55. ALfloat sig = Comp->Envelope[i];
  56. sum -= window[index];
  57. window[index] = fastf2i(minf(sig * sig * 65536.0f, RMS_VALUE_MAX));
  58. sum += window[index];
  59. index = (index + 1) & RMS_WINDOW_MASK;
  60. Comp->Envelope[i] = sqrtf(sum / 65536.0f / RMS_WINDOW_SIZE);
  61. }
  62. Comp->RmsSum = sum;
  63. Comp->RmsIndex = index;
  64. }
  65. /* This isn't a very sophisticated envelope follower, but it gets the job
  66. * done. First, it operates at logarithmic scales to keep transitions
  67. * appropriate for human hearing. Second, it can apply adaptive (automated)
  68. * attack/release adjustments based on the signal.
  69. */
  70. static void FollowEnvelope(Compressor *Comp, const ALsizei SamplesToDo)
  71. {
  72. ALfloat attackMin = Comp->AttackMin;
  73. ALfloat attackMax = Comp->AttackMax;
  74. ALfloat releaseMin = Comp->ReleaseMin;
  75. ALfloat releaseMax = Comp->ReleaseMax;
  76. ALfloat last = Comp->EnvLast;
  77. ALsizei i;
  78. for(i = 0;i < SamplesToDo;i++)
  79. {
  80. ALfloat env = log10f(maxf(Comp->Envelope[i], 0.000001f));
  81. ALfloat slope = minf(1.0f, fabsf(env - last) / 4.5f);
  82. if(env > last)
  83. last = minf(env, last + lerp(attackMin, attackMax, 1.0f - (slope * slope)));
  84. else
  85. last = maxf(env, last + lerp(releaseMin, releaseMax, 1.0f - (slope * slope)));
  86. Comp->Envelope[i] = last;
  87. }
  88. Comp->EnvLast = last;
  89. }
  90. /* The envelope is converted to control gain with an optional soft knee. */
  91. static void EnvelopeGain(Compressor *Comp, const ALsizei SamplesToDo, const ALfloat Slope)
  92. {
  93. const ALfloat threshold = Comp->Threshold;
  94. const ALfloat knee = Comp->Knee;
  95. ALsizei i;
  96. if(!(knee > 0.0f))
  97. {
  98. for(i = 0;i < SamplesToDo;i++)
  99. {
  100. ALfloat gain = Slope * (threshold - Comp->Envelope[i]);
  101. Comp->Envelope[i] = powf(10.0f, minf(0.0f, gain));
  102. }
  103. }
  104. else
  105. {
  106. const ALfloat lower = threshold - (0.5f * knee);
  107. const ALfloat upper = threshold + (0.5f * knee);
  108. const ALfloat m = 0.5f * Slope / knee;
  109. for(i = 0;i < SamplesToDo;i++)
  110. {
  111. ALfloat env = Comp->Envelope[i];
  112. ALfloat gain;
  113. if(env > lower && env < upper)
  114. gain = m * (env - lower) * (lower - env);
  115. else
  116. gain = Slope * (threshold - env);
  117. Comp->Envelope[i] = powf(10.0f, minf(0.0f, gain));
  118. }
  119. }
  120. }
  121. Compressor *CompressorInit(const ALfloat PreGainDb, const ALfloat PostGainDb,
  122. const ALboolean SummedLink, const ALboolean RmsSensing,
  123. const ALfloat AttackTimeMin, const ALfloat AttackTimeMax,
  124. const ALfloat ReleaseTimeMin, const ALfloat ReleaseTimeMax,
  125. const ALfloat Ratio, const ALfloat ThresholdDb,
  126. const ALfloat KneeDb, const ALuint SampleRate)
  127. {
  128. Compressor *Comp;
  129. size_t size;
  130. ALsizei i;
  131. size = sizeof(*Comp);
  132. if(RmsSensing)
  133. size += sizeof(Comp->RmsWindow[0]) * RMS_WINDOW_SIZE;
  134. Comp = al_calloc(16, size);
  135. Comp->PreGain = powf(10.0f, PreGainDb / 20.0f);
  136. Comp->PostGain = powf(10.0f, PostGainDb / 20.0f);
  137. Comp->SummedLink = SummedLink;
  138. Comp->AttackMin = 1.0f / maxf(0.000001f, AttackTimeMin * SampleRate * logf(10.0f));
  139. Comp->AttackMax = 1.0f / maxf(0.000001f, AttackTimeMax * SampleRate * logf(10.0f));
  140. Comp->ReleaseMin = -1.0f / maxf(0.000001f, ReleaseTimeMin * SampleRate * logf(10.0f));
  141. Comp->ReleaseMax = -1.0f / maxf(0.000001f, ReleaseTimeMax * SampleRate * logf(10.0f));
  142. Comp->Ratio = Ratio;
  143. Comp->Threshold = ThresholdDb / 20.0f;
  144. Comp->Knee = maxf(0.0f, KneeDb / 20.0f);
  145. Comp->SampleRate = SampleRate;
  146. Comp->RmsSum = 0;
  147. if(RmsSensing)
  148. Comp->RmsWindow = (ALuint*)(Comp+1);
  149. else
  150. Comp->RmsWindow = NULL;
  151. Comp->RmsIndex = 0;
  152. for(i = 0;i < BUFFERSIZE;i++)
  153. Comp->Envelope[i] = 0.0f;
  154. Comp->EnvLast = -6.0f;
  155. return Comp;
  156. }
  157. void ApplyCompression(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
  158. ALfloat (*restrict OutBuffer)[BUFFERSIZE])
  159. {
  160. ALsizei c, i;
  161. if(Comp->PreGain != 1.0f)
  162. {
  163. for(c = 0;c < NumChans;c++)
  164. {
  165. for(i = 0;i < SamplesToDo;i++)
  166. OutBuffer[c][i] *= Comp->PreGain;
  167. }
  168. }
  169. if(Comp->SummedLink)
  170. SumChannels(Comp, NumChans, SamplesToDo, OutBuffer);
  171. else
  172. MaxChannels(Comp, NumChans, SamplesToDo, OutBuffer);
  173. if(Comp->RmsWindow)
  174. RmsDetection(Comp, SamplesToDo);
  175. FollowEnvelope(Comp, SamplesToDo);
  176. if(Comp->Ratio > 0.0f)
  177. EnvelopeGain(Comp, SamplesToDo, 1.0f - (1.0f / Comp->Ratio));
  178. else
  179. EnvelopeGain(Comp, SamplesToDo, 1.0f);
  180. if(Comp->PostGain != 1.0f)
  181. {
  182. for(i = 0;i < SamplesToDo;i++)
  183. Comp->Envelope[i] *= Comp->PostGain;
  184. }
  185. for(c = 0;c < NumChans;c++)
  186. {
  187. for(i = 0;i < SamplesToDo;i++)
  188. OutBuffer[c][i] *= Comp->Envelope[i];
  189. }
  190. }