The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

369 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::dsp
  19. {
  20. //==============================================================================
  21. template <typename SampleType>
  22. DryWetMixer<SampleType>::DryWetMixer()
  23. : DryWetMixer (0)
  24. {
  25. }
  26. template <typename SampleType>
  27. DryWetMixer<SampleType>::DryWetMixer (int maximumWetLatencyInSamplesIn)
  28. : dryDelayLine (maximumWetLatencyInSamplesIn),
  29. maximumWetLatencyInSamples (maximumWetLatencyInSamplesIn)
  30. {
  31. dryDelayLine.setDelay (0);
  32. update();
  33. reset();
  34. }
  35. //==============================================================================
  36. template <typename SampleType>
  37. void DryWetMixer<SampleType>::setMixingRule (MixingRule newRule)
  38. {
  39. currentMixingRule = newRule;
  40. update();
  41. }
  42. template <typename SampleType>
  43. void DryWetMixer<SampleType>::setWetMixProportion (SampleType newWetMixProportion)
  44. {
  45. jassert (isPositiveAndNotGreaterThan (newWetMixProportion, 1.0));
  46. mix = jlimit (static_cast<SampleType> (0.0), static_cast<SampleType> (1.0), newWetMixProportion);
  47. update();
  48. }
  49. template <typename SampleType>
  50. void DryWetMixer<SampleType>::setWetLatency (SampleType wetLatencySamples)
  51. {
  52. dryDelayLine.setDelay (wetLatencySamples);
  53. }
  54. //==============================================================================
  55. template <typename SampleType>
  56. void DryWetMixer<SampleType>::prepare (const ProcessSpec& spec)
  57. {
  58. jassert (spec.sampleRate > 0);
  59. jassert (spec.numChannels > 0);
  60. sampleRate = spec.sampleRate;
  61. dryDelayLine.prepare (spec);
  62. bufferDry.setSize ((int) spec.numChannels, (int) spec.maximumBlockSize, false, false, true);
  63. update();
  64. reset();
  65. }
  66. template <typename SampleType>
  67. void DryWetMixer<SampleType>::reset()
  68. {
  69. dryVolume.reset (sampleRate, 0.05);
  70. wetVolume.reset (sampleRate, 0.05);
  71. dryDelayLine.reset();
  72. fifo = SingleThreadedAbstractFifo (nextPowerOfTwo (bufferDry.getNumSamples()));
  73. bufferDry.setSize (bufferDry.getNumChannels(), fifo.getSize(), false, false, true);
  74. }
  75. //==============================================================================
  76. template <typename SampleType>
  77. void DryWetMixer<SampleType>::pushDrySamples (const AudioBlock<const SampleType> drySamples)
  78. {
  79. jassert (drySamples.getNumChannels() <= (size_t) bufferDry.getNumChannels());
  80. jassert (drySamples.getNumSamples() <= (size_t) fifo.getRemainingSpace());
  81. auto offset = 0;
  82. for (const auto& range : fifo.write ((int) drySamples.getNumSamples()))
  83. {
  84. if (range.getLength() == 0)
  85. continue;
  86. auto block = AudioBlock<SampleType> (bufferDry).getSubsetChannelBlock (0, drySamples.getNumChannels())
  87. .getSubBlock ((size_t) range.getStart(), (size_t) range.getLength());
  88. auto inputBlock = drySamples.getSubBlock ((size_t) offset, (size_t) range.getLength());
  89. if (maximumWetLatencyInSamples == 0)
  90. block.copyFrom (inputBlock);
  91. else
  92. dryDelayLine.process (ProcessContextNonReplacing<SampleType> (inputBlock, block));
  93. offset += range.getLength();
  94. }
  95. }
  96. template <typename SampleType>
  97. void DryWetMixer<SampleType>::mixWetSamples (AudioBlock<SampleType> inOutBlock)
  98. {
  99. inOutBlock.multiplyBy (wetVolume);
  100. jassert (inOutBlock.getNumSamples() <= (size_t) fifo.getNumReadable());
  101. auto offset = 0;
  102. for (const auto& range : fifo.read ((int) inOutBlock.getNumSamples()))
  103. {
  104. if (range.getLength() == 0)
  105. continue;
  106. auto block = AudioBlock<SampleType> (bufferDry).getSubsetChannelBlock (0, inOutBlock.getNumChannels())
  107. .getSubBlock ((size_t) range.getStart(), (size_t) range.getLength());
  108. block.multiplyBy (dryVolume);
  109. inOutBlock.getSubBlock ((size_t) offset).add (block);
  110. offset += range.getLength();
  111. }
  112. }
  113. //==============================================================================
  114. template <typename SampleType>
  115. void DryWetMixer<SampleType>::update()
  116. {
  117. SampleType dryValue, wetValue;
  118. switch (currentMixingRule)
  119. {
  120. case MixingRule::balanced:
  121. dryValue = static_cast<SampleType> (2.0) * jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - mix);
  122. wetValue = static_cast<SampleType> (2.0) * jmin (static_cast<SampleType> (0.5), mix);
  123. break;
  124. case MixingRule::linear:
  125. dryValue = static_cast<SampleType> (1.0) - mix;
  126. wetValue = mix;
  127. break;
  128. case MixingRule::sin3dB:
  129. dryValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)));
  130. wetValue = static_cast<SampleType> (std::sin (0.5 * MathConstants<double>::pi * mix));
  131. break;
  132. case MixingRule::sin4p5dB:
  133. dryValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)), 1.5));
  134. wetValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * mix), 1.5));
  135. break;
  136. case MixingRule::sin6dB:
  137. dryValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * (1.0 - mix)), 2.0));
  138. wetValue = static_cast<SampleType> (std::pow (std::sin (0.5 * MathConstants<double>::pi * mix), 2.0));
  139. break;
  140. case MixingRule::squareRoot3dB:
  141. dryValue = std::sqrt (static_cast<SampleType> (1.0) - mix);
  142. wetValue = std::sqrt (mix);
  143. break;
  144. case MixingRule::squareRoot4p5dB:
  145. dryValue = static_cast<SampleType> (std::pow (std::sqrt (1.0 - mix), 1.5));
  146. wetValue = static_cast<SampleType> (std::pow (std::sqrt (mix), 1.5));
  147. break;
  148. default:
  149. dryValue = jmin (static_cast<SampleType> (0.5), static_cast<SampleType> (1.0) - mix);
  150. wetValue = jmin (static_cast<SampleType> (0.5), mix);
  151. break;
  152. }
  153. dryVolume.setTargetValue (dryValue);
  154. wetVolume.setTargetValue (wetValue);
  155. }
  156. //==============================================================================
  157. template class DryWetMixer<float>;
  158. template class DryWetMixer<double>;
  159. //==============================================================================
  160. //==============================================================================
  161. #if JUCE_UNIT_TESTS
  162. struct DryWetMixerTests : public UnitTest
  163. {
  164. DryWetMixerTests() : UnitTest ("DryWetMixer", UnitTestCategories::dsp) {}
  165. enum class Kind { down, up };
  166. static auto getRampBuffer (ProcessSpec spec, Kind kind)
  167. {
  168. AudioBuffer<float> buffer ((int) spec.numChannels, (int) spec.maximumBlockSize);
  169. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  170. {
  171. for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
  172. {
  173. const auto ramp = kind == Kind::up ? sample : spec.maximumBlockSize - sample;
  174. buffer.setSample ((int) channel,
  175. (int) sample,
  176. jmap ((float) ramp, 0.0f, (float) spec.maximumBlockSize, 0.0f, 1.0f));
  177. }
  178. }
  179. return buffer;
  180. }
  181. void runTest() override
  182. {
  183. constexpr ProcessSpec spec { 44100.0, 512, 2 };
  184. constexpr auto numBlocks = 5;
  185. const auto wetBuffer = getRampBuffer (spec, Kind::up);
  186. const auto dryBuffer = getRampBuffer (spec, Kind::down);
  187. for (auto maxLatency : { 0, 100, 200, 512 })
  188. {
  189. beginTest ("Mixer can push multiple small buffers");
  190. {
  191. DryWetMixer<float> mixer (maxLatency);
  192. mixer.setWetMixProportion (0.5f);
  193. mixer.prepare (spec);
  194. for (auto block = 0; block < numBlocks; ++block)
  195. {
  196. // Push samples one-by-one
  197. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  198. mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (sample, 1));
  199. // Mix wet samples in one go
  200. auto outputBlock = wetBuffer;
  201. mixer.mixWetSamples ({ outputBlock });
  202. // The output block should contain the wet and dry samples averaged
  203. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  204. {
  205. for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
  206. {
  207. const auto outputValue = outputBlock.getSample ((int) channel, (int) sample);
  208. expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
  209. }
  210. }
  211. }
  212. }
  213. beginTest ("Mixer can pop multiple small buffers");
  214. {
  215. DryWetMixer<float> mixer (maxLatency);
  216. mixer.setWetMixProportion (0.5f);
  217. mixer.prepare (spec);
  218. for (auto block = 0; block < numBlocks; ++block)
  219. {
  220. // Push samples in one go
  221. mixer.pushDrySamples ({ dryBuffer });
  222. // Process wet samples one-by-one
  223. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  224. {
  225. AudioBuffer<float> outputBlock ((int) spec.numChannels, 1);
  226. AudioBlock<const float> (wetBuffer).getSubBlock (sample, 1).copyTo (outputBlock);
  227. mixer.mixWetSamples ({ outputBlock });
  228. // The output block should contain the wet and dry samples averaged
  229. for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
  230. {
  231. const auto outputValue = outputBlock.getSample ((int) channel, 0);
  232. expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
  233. }
  234. }
  235. }
  236. }
  237. beginTest ("Mixer can push and pop multiple small buffers");
  238. {
  239. DryWetMixer<float> mixer (maxLatency);
  240. mixer.setWetMixProportion (0.5f);
  241. mixer.prepare (spec);
  242. for (auto block = 0; block < numBlocks; ++block)
  243. {
  244. // Push dry samples and process wet samples one-by-one
  245. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  246. {
  247. mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (sample, 1));
  248. AudioBuffer<float> outputBlock ((int) spec.numChannels, 1);
  249. AudioBlock<const float> (wetBuffer).getSubBlock (sample, 1).copyTo (outputBlock);
  250. mixer.mixWetSamples ({ outputBlock });
  251. // The output block should contain the wet and dry samples averaged
  252. for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
  253. {
  254. const auto outputValue = outputBlock.getSample ((int) channel, 0);
  255. expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
  256. }
  257. }
  258. }
  259. }
  260. beginTest ("Mixer can push and pop full-sized blocks after encountering a shorter block");
  261. {
  262. DryWetMixer<float> mixer (maxLatency);
  263. mixer.setWetMixProportion (0.5f);
  264. mixer.prepare (spec);
  265. constexpr auto shortBlockLength = spec.maximumBlockSize / 2;
  266. AudioBuffer<float> shortBlock (spec.numChannels, shortBlockLength);
  267. mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (shortBlockLength));
  268. mixer.mixWetSamples ({ shortBlock });
  269. for (auto block = 0; block < numBlocks; ++block)
  270. {
  271. // Push a full block of dry samples
  272. mixer.pushDrySamples ({ dryBuffer });
  273. // Mix a full block of wet samples
  274. auto outputBlock = wetBuffer;
  275. mixer.mixWetSamples ({ outputBlock });
  276. // The output block should contain the wet and dry samples averaged
  277. for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
  278. {
  279. for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
  280. {
  281. const auto outputValue = outputBlock.getSample ((int) channel, (int) sample);
  282. expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. };
  290. static const DryWetMixerTests dryWetMixerTests;
  291. #endif
  292. } // namespace juce::dsp