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.

185 lines
6.9KB

  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, 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, size_t lookupTableNumPoints = 0)
  49. {
  50. if (lookupTableNumPoints != 0)
  51. {
  52. auto* table = new LookupTableTransform<NumericType> (function,
  53. static_cast<NumericType> (-1.0 * MathConstants<double>::pi),
  54. static_cast<NumericType> (MathConstants<double>::pi),
  55. lookupTableNumPoints);
  56. lookupTable = table;
  57. generator = [table] (NumericType x) { return (*table) (x); };
  58. }
  59. else
  60. {
  61. generator = function;
  62. }
  63. }
  64. //==============================================================================
  65. /** Sets the frequency of the oscillator. */
  66. void setFrequency (NumericType newFrequency, bool force = false) noexcept { frequency.setValue (newFrequency, force); }
  67. /** Returns the current frequency of the oscillator. */
  68. NumericType getFrequency() const noexcept { return frequency.getTargetValue(); }
  69. //==============================================================================
  70. /** Called before processing starts. */
  71. void prepare (const ProcessSpec& spec) noexcept
  72. {
  73. sampleRate = static_cast<NumericType> (spec.sampleRate);
  74. rampBuffer.resize ((int) spec.maximumBlockSize);
  75. reset();
  76. }
  77. /** Resets the internal state of the oscillator */
  78. void reset() noexcept
  79. {
  80. pos = 0.0;
  81. if (sampleRate > 0)
  82. frequency.reset (sampleRate, 0.05);
  83. }
  84. //==============================================================================
  85. /** Returns the result of processing a single sample. */
  86. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType) noexcept
  87. {
  88. jassert (isInitialised());
  89. auto increment = static_cast<NumericType> (MathConstants<double>::twoPi) * frequency.getNextValue() / sampleRate;
  90. auto value = generator (pos - static_cast<NumericType> (MathConstants<double>::pi));
  91. pos = std::fmod (pos + increment, static_cast<NumericType> (MathConstants<double>::twoPi));
  92. return value;
  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. // this is an output-only processory
  101. jassert (context.getInputBlock().getNumChannels() == 0 || (! context.usesSeparateInputAndOutputBlocks()));
  102. jassert (outBlock.getNumSamples() <= static_cast<size_t> (rampBuffer.size()));
  103. auto len = outBlock.getNumSamples();
  104. auto numChannels = outBlock.getNumChannels();
  105. auto baseIncrement = static_cast<NumericType> (MathConstants<double>::twoPi) / sampleRate;
  106. if (frequency.isSmoothing())
  107. {
  108. auto* buffer = rampBuffer.getRawDataPointer();
  109. for (size_t i = 0; i < len; ++i)
  110. {
  111. buffer[i] = pos - static_cast<NumericType> (MathConstants<double>::pi);
  112. pos = std::fmod (pos + (baseIncrement * frequency.getNextValue()),
  113. static_cast<NumericType> (MathConstants<double>::twoPi));
  114. }
  115. for (size_t ch = 0; ch < numChannels; ++ch)
  116. {
  117. auto* dst = outBlock.getChannelPointer (ch);
  118. for (size_t i = 0; i < len; ++i)
  119. dst[i] = generator (buffer[i]);
  120. }
  121. }
  122. else
  123. {
  124. auto freq = baseIncrement * frequency.getNextValue();
  125. for (size_t ch = 0; ch < numChannels; ++ch)
  126. {
  127. auto p = pos;
  128. auto* dst = outBlock.getChannelPointer (ch);
  129. for (size_t i = 0; i < len; ++i)
  130. {
  131. dst[i] = generator (p - static_cast<NumericType> (MathConstants<double>::pi));
  132. p = std::fmod (p + freq, static_cast<NumericType> (MathConstants<double>::twoPi));
  133. }
  134. }
  135. pos = std::fmod (pos + freq * static_cast<NumericType> (len),
  136. static_cast<NumericType> (MathConstants<double>::twoPi));
  137. }
  138. }
  139. private:
  140. //==============================================================================
  141. std::function<NumericType (NumericType)> generator;
  142. ScopedPointer<LookupTableTransform<NumericType>> lookupTable;
  143. Array<NumericType> rampBuffer;
  144. LinearSmoothedValue<NumericType> frequency {static_cast<NumericType> (440.0)};
  145. NumericType sampleRate = 48000.0, pos = 0.0;
  146. };
  147. } // namespace dsp
  148. } // namespace juce