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.

81 lines
2.8KB

  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. Applies waveshaping to audio samples as single samples or AudioBlocks.
  22. @tags{DSP}
  23. */
  24. template <typename FloatType, typename Function = FloatType (*) (FloatType)>
  25. struct WaveShaper
  26. {
  27. Function functionToUse;
  28. //==============================================================================
  29. /** Called before processing starts. */
  30. void prepare (const ProcessSpec&) noexcept {}
  31. //==============================================================================
  32. /** Returns the result of processing a single sample. */
  33. template <typename SampleType>
  34. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType inputSample) const noexcept
  35. {
  36. return functionToUse (inputSample);
  37. }
  38. /** Processes the input and output buffers supplied in the processing context. */
  39. template <typename ProcessContext>
  40. void process (const ProcessContext& context) const noexcept
  41. {
  42. if (context.isBypassed)
  43. {
  44. if (context.usesSeparateInputAndOutputBlocks())
  45. context.getOutputBlock().copyFrom (context.getInputBlock());
  46. }
  47. else
  48. {
  49. AudioBlock<FloatType>::process (context.getInputBlock(),
  50. context.getOutputBlock(),
  51. functionToUse);
  52. }
  53. }
  54. void reset() noexcept {}
  55. };
  56. //==============================================================================
  57. #if ! ((JUCE_MAC || JUCE_IOS) && JUCE_CLANG && __clang_major__ < 10)
  58. template <typename Functor>
  59. static WaveShaper<typename std::invoke_result<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  60. #else
  61. template <typename Functor>
  62. static WaveShaper<typename std::result_of<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  63. #endif
  64. } // namespace juce::dsp