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.

79 lines
2.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. /**
  18. Applies waveshaping to audio samples as single samples or AudioBlocks.
  19. @tags{DSP}
  20. */
  21. template <typename FloatType, typename Function = FloatType (*) (FloatType)>
  22. struct WaveShaper
  23. {
  24. Function functionToUse;
  25. //==============================================================================
  26. /** Called before processing starts. */
  27. void prepare (const ProcessSpec&) noexcept {}
  28. //==============================================================================
  29. /** Returns the result of processing a single sample. */
  30. template <typename SampleType>
  31. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType inputSample) const noexcept
  32. {
  33. return functionToUse (inputSample);
  34. }
  35. /** Processes the input and output buffers supplied in the processing context. */
  36. template <typename ProcessContext>
  37. void process (const ProcessContext& context) const noexcept
  38. {
  39. if (context.isBypassed)
  40. {
  41. if (context.usesSeparateInputAndOutputBlocks())
  42. context.getOutputBlock().copyFrom (context.getInputBlock());
  43. }
  44. else
  45. {
  46. AudioBlock<FloatType>::process (context.getInputBlock(),
  47. context.getOutputBlock(),
  48. functionToUse);
  49. }
  50. }
  51. void reset() noexcept {}
  52. };
  53. //==============================================================================
  54. // Although clang supports C++17, their standard library still has no invoke_result
  55. // support. Remove the "|| JUCE_CLANG" once clang supports this properly!
  56. #if (! JUCE_CXX17_IS_AVAILABLE) || JUCE_CLANG
  57. template <typename Functor>
  58. static WaveShaper<typename std::result_of<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  59. #else
  60. template <typename Functor>
  61. static WaveShaper<typename std::invoke_result<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  62. #endif
  63. } // namespace dsp
  64. } // namespace juce