Audio plugin host https://kx.studio/carla
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.

juce_Oscillator.h 8.8KB

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