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.

249 lines
8.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. Generates a signal based on a user-supplied function.
  22. @tags{DSP}
  23. */
  24. template <typename SampleType>
  25. class Oscillator
  26. {
  27. public:
  28. /** The NumericType is the underlying primitive type used by the SampleType (which
  29. could be either a primitive or vector)
  30. */
  31. using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
  32. /** Creates an uninitialised oscillator. Call initialise before first use. */
  33. Oscillator() = default;
  34. /** Creates an oscillator with a periodic input function (-pi..pi).
  35. If lookup table is not zero, then the function will be approximated
  36. with a lookup table.
  37. */
  38. Oscillator (const std::function<NumericType (NumericType)>& function,
  39. size_t lookupTableNumPoints = 0)
  40. {
  41. initialise (function, lookupTableNumPoints);
  42. }
  43. /** Returns true if the Oscillator has been initialised. */
  44. bool isInitialised() const noexcept { return static_cast<bool> (generator); }
  45. /** Initialises the oscillator with a waveform. */
  46. void initialise (const std::function<NumericType (NumericType)>& function,
  47. size_t lookupTableNumPoints = 0)
  48. {
  49. if (lookupTableNumPoints != 0)
  50. {
  51. auto* table = new LookupTableTransform<NumericType> (function,
  52. -MathConstants<NumericType>::pi,
  53. MathConstants<NumericType>::pi,
  54. lookupTableNumPoints);
  55. lookupTable.reset (table);
  56. generator = [table] (NumericType x) { return (*table) (x); };
  57. }
  58. else
  59. {
  60. generator = function;
  61. }
  62. }
  63. //==============================================================================
  64. /** Sets the frequency of the oscillator. */
  65. void setFrequency (NumericType newFrequency, bool force = false) noexcept
  66. {
  67. if (force)
  68. {
  69. frequency.setCurrentAndTargetValue (newFrequency);
  70. return;
  71. }
  72. frequency.setTargetValue (newFrequency);
  73. }
  74. /** Returns the current frequency of the oscillator. */
  75. NumericType getFrequency() const noexcept { return frequency.getTargetValue(); }
  76. //==============================================================================
  77. /** Called before processing starts. */
  78. void prepare (const ProcessSpec& spec) noexcept
  79. {
  80. sampleRate = static_cast<NumericType> (spec.sampleRate);
  81. rampBuffer.resize ((int) spec.maximumBlockSize);
  82. reset();
  83. }
  84. /** Resets the internal state of the oscillator */
  85. void reset() noexcept
  86. {
  87. phase.reset();
  88. if (sampleRate > 0)
  89. frequency.reset (sampleRate, 0.05);
  90. }
  91. //==============================================================================
  92. /** Returns the result of processing a single sample. */
  93. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType input) noexcept
  94. {
  95. jassert (isInitialised());
  96. auto increment = MathConstants<NumericType>::twoPi * frequency.getNextValue() / sampleRate;
  97. return input + generator (phase.advance (increment) - MathConstants<NumericType>::pi);
  98. }
  99. /** Processes the input and output buffers supplied in the processing context. */
  100. template <typename ProcessContext>
  101. void process (const ProcessContext& context) noexcept
  102. {
  103. jassert (isInitialised());
  104. auto&& outBlock = context.getOutputBlock();
  105. auto&& inBlock = context.getInputBlock();
  106. // this is an output-only processor
  107. jassert (outBlock.getNumSamples() <= static_cast<size_t> (rampBuffer.size()));
  108. auto len = outBlock.getNumSamples();
  109. auto numChannels = outBlock.getNumChannels();
  110. auto inputChannels = inBlock.getNumChannels();
  111. auto baseIncrement = MathConstants<NumericType>::twoPi / sampleRate;
  112. if (context.isBypassed)
  113. context.getOutputBlock().clear();
  114. if (frequency.isSmoothing())
  115. {
  116. auto* buffer = rampBuffer.getRawDataPointer();
  117. for (size_t i = 0; i < len; ++i)
  118. buffer[i] = phase.advance (baseIncrement * frequency.getNextValue())
  119. - MathConstants<NumericType>::pi;
  120. if (! context.isBypassed)
  121. {
  122. size_t ch;
  123. if (context.usesSeparateInputAndOutputBlocks())
  124. {
  125. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  126. {
  127. auto* dst = outBlock.getChannelPointer (ch);
  128. auto* src = inBlock.getChannelPointer (ch);
  129. for (size_t i = 0; i < len; ++i)
  130. dst[i] = src[i] + generator (buffer[i]);
  131. }
  132. }
  133. else
  134. {
  135. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  136. {
  137. auto* dst = outBlock.getChannelPointer (ch);
  138. for (size_t i = 0; i < len; ++i)
  139. dst[i] += generator (buffer[i]);
  140. }
  141. }
  142. for (; ch < numChannels; ++ch)
  143. {
  144. auto* dst = outBlock.getChannelPointer (ch);
  145. for (size_t i = 0; i < len; ++i)
  146. dst[i] = generator (buffer[i]);
  147. }
  148. }
  149. }
  150. else
  151. {
  152. auto freq = baseIncrement * frequency.getNextValue();
  153. auto p = phase;
  154. if (context.isBypassed)
  155. {
  156. frequency.skip (static_cast<int> (len));
  157. p.advance (freq * static_cast<NumericType> (len));
  158. }
  159. else
  160. {
  161. size_t ch;
  162. if (context.usesSeparateInputAndOutputBlocks())
  163. {
  164. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  165. {
  166. p = phase;
  167. auto* dst = outBlock.getChannelPointer (ch);
  168. auto* src = inBlock.getChannelPointer (ch);
  169. for (size_t i = 0; i < len; ++i)
  170. dst[i] = src[i] + generator (p.advance (freq) - MathConstants<NumericType>::pi);
  171. }
  172. }
  173. else
  174. {
  175. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  176. {
  177. p = phase;
  178. auto* dst = outBlock.getChannelPointer (ch);
  179. for (size_t i = 0; i < len; ++i)
  180. dst[i] += generator (p.advance (freq) - MathConstants<NumericType>::pi);
  181. }
  182. }
  183. for (; ch < numChannels; ++ch)
  184. {
  185. p = phase;
  186. auto* dst = outBlock.getChannelPointer (ch);
  187. for (size_t i = 0; i < len; ++i)
  188. dst[i] = generator (p.advance (freq) - MathConstants<NumericType>::pi);
  189. }
  190. }
  191. phase = p;
  192. }
  193. }
  194. private:
  195. //==============================================================================
  196. std::function<NumericType (NumericType)> generator;
  197. std::unique_ptr<LookupTableTransform<NumericType>> lookupTable;
  198. Array<NumericType> rampBuffer;
  199. SmoothedValue<NumericType> frequency { static_cast<NumericType> (440.0) };
  200. NumericType sampleRate = 48000.0;
  201. Phase<NumericType> phase;
  202. };
  203. } // namespace juce::dsp