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.

77 lines
2.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. #if JUCE_CXX17_IS_AVAILABLE
  55. template <typename Functor>
  56. static WaveShaper<typename std::invoke_result<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  57. #else
  58. template <typename Functor>
  59. static WaveShaper<typename std::result_of<Functor>, Functor> CreateWaveShaper (Functor functionToUse) { return {functionToUse}; }
  60. #endif
  61. } // namespace dsp
  62. } // namespace juce