Audio plugin host https://kx.studio/carla
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.

juce_WaveShaper.h 2.8KB

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