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.

181 lines
6.5KB

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