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.

253 lines
8.8KB

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