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.7KB

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