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.

243 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. */
  26. template <typename SampleType>
  27. class Oscillator
  28. {
  29. public:
  30. /** The NumericType is the underlying primitive type used by the SampleType (which
  31. could be either a primitive or vector)
  32. */
  33. using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
  34. /** Creates an uninitialised oscillator. Call initialise before first use. */
  35. Oscillator()
  36. {}
  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 = 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 { frequency.setValue (newFrequency, force); }
  69. /** Returns the current frequency of the oscillator. */
  70. NumericType getFrequency() const noexcept { return frequency.getTargetValue(); }
  71. //==============================================================================
  72. /** Called before processing starts. */
  73. void prepare (const ProcessSpec& spec) noexcept
  74. {
  75. sampleRate = static_cast<NumericType> (spec.sampleRate);
  76. rampBuffer.resize ((int) spec.maximumBlockSize);
  77. reset();
  78. }
  79. /** Resets the internal state of the oscillator */
  80. void reset() noexcept
  81. {
  82. phase.reset();
  83. if (sampleRate > 0)
  84. frequency.reset (sampleRate, 0.05);
  85. }
  86. //==============================================================================
  87. /** Returns the result of processing a single sample. */
  88. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType input) noexcept
  89. {
  90. jassert (isInitialised());
  91. auto increment = MathConstants<NumericType>::twoPi * frequency.getNextValue() / sampleRate;
  92. return input + generator (phase.advance (increment) - MathConstants<NumericType>::pi);
  93. }
  94. /** Processes the input and output buffers supplied in the processing context. */
  95. template <typename ProcessContext>
  96. void process (const ProcessContext& context) noexcept
  97. {
  98. jassert (isInitialised());
  99. auto&& outBlock = context.getOutputBlock();
  100. auto&& inBlock = context.getInputBlock();
  101. // this is an output-only processory
  102. jassert (outBlock.getNumSamples() <= static_cast<size_t> (rampBuffer.size()));
  103. auto len = outBlock.getNumSamples();
  104. auto numChannels = outBlock.getNumChannels();
  105. auto inputChannels = inBlock.getNumChannels();
  106. auto baseIncrement = MathConstants<NumericType>::twoPi / sampleRate;
  107. if (context.isBypassed)
  108. context.getOutputBlock().clear();
  109. if (frequency.isSmoothing())
  110. {
  111. auto* buffer = rampBuffer.getRawDataPointer();
  112. for (size_t i = 0; i < len; ++i)
  113. buffer[i] = phase.advance (baseIncrement * frequency.getNextValue())
  114. - MathConstants<NumericType>::pi;
  115. if (! context.isBypassed)
  116. {
  117. size_t ch;
  118. if (context.usesSeparateInputAndOutputBlocks())
  119. {
  120. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  121. {
  122. auto* dst = outBlock.getChannelPointer (ch);
  123. auto* src = inBlock.getChannelPointer (ch);
  124. for (size_t i = 0; i < len; ++i)
  125. dst[i] = src[i] + generator (buffer[i]);
  126. }
  127. }
  128. else
  129. {
  130. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  131. {
  132. auto* dst = outBlock.getChannelPointer (ch);
  133. for (size_t i = 0; i < len; ++i)
  134. dst[i] += generator (buffer[i]);
  135. }
  136. }
  137. for (; ch < numChannels; ++ch)
  138. {
  139. auto* dst = outBlock.getChannelPointer (ch);
  140. for (size_t i = 0; i < len; ++i)
  141. dst[i] = generator (buffer[i]);
  142. }
  143. }
  144. }
  145. else
  146. {
  147. auto freq = baseIncrement * frequency.getNextValue();
  148. auto p = phase;
  149. if (context.isBypassed)
  150. {
  151. frequency.skip (static_cast<int> (len));
  152. p.advance (freq * static_cast<NumericType> (len));
  153. }
  154. else
  155. {
  156. size_t ch;
  157. if (context.usesSeparateInputAndOutputBlocks())
  158. {
  159. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  160. {
  161. p = phase;
  162. auto* dst = outBlock.getChannelPointer (ch);
  163. auto* src = inBlock.getChannelPointer (ch);
  164. for (size_t i = 0; i < len; ++i)
  165. dst[i] = src[i] + generator (p.advance (freq) - MathConstants<NumericType>::pi);
  166. }
  167. }
  168. else
  169. {
  170. for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
  171. {
  172. p = phase;
  173. auto* dst = outBlock.getChannelPointer (ch);
  174. for (size_t i = 0; i < len; ++i)
  175. dst[i] += generator (p.advance (freq) - MathConstants<NumericType>::pi);
  176. }
  177. }
  178. for (; ch < numChannels; ++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. phase = p;
  187. }
  188. }
  189. private:
  190. //==============================================================================
  191. std::function<NumericType (NumericType)> generator;
  192. ScopedPointer<LookupTableTransform<NumericType>> lookupTable;
  193. Array<NumericType> rampBuffer;
  194. LinearSmoothedValue<NumericType> frequency { static_cast<NumericType> (440.0) };
  195. NumericType sampleRate = 48000.0;
  196. Phase<NumericType> phase;
  197. };
  198. } // namespace dsp
  199. } // namespace juce