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.

245 lines
8.5KB

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