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.

178 lines
6.3KB

  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) noexcept
  89. {
  90. jassert (isInitialised());
  91. auto increment = MathConstants<NumericType>::twoPi * frequency.getNextValue() / sampleRate;
  92. return 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. // 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 = MathConstants<NumericType>::twoPi / sampleRate;
  106. if (frequency.isSmoothing())
  107. {
  108. auto* buffer = rampBuffer.getRawDataPointer();
  109. for (size_t i = 0; i < len; ++i)
  110. buffer[i] = phase.advance (baseIncrement * frequency.getNextValue())
  111. - MathConstants<NumericType>::pi;
  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. auto p = phase;
  123. for (size_t ch = 0; ch < numChannels; ++ch)
  124. {
  125. p = phase;
  126. auto* dst = outBlock.getChannelPointer (ch);
  127. for (size_t i = 0; i < len; ++i)
  128. dst[i] = generator (p.advance (freq) - MathConstants<NumericType>::pi);
  129. }
  130. phase = p;
  131. }
  132. }
  133. private:
  134. //==============================================================================
  135. std::function<NumericType (NumericType)> generator;
  136. ScopedPointer<LookupTableTransform<NumericType>> lookupTable;
  137. Array<NumericType> rampBuffer;
  138. LinearSmoothedValue<NumericType> frequency { static_cast<NumericType> (440.0) };
  139. NumericType sampleRate = 48000.0;
  140. Phase<NumericType> phase;
  141. };
  142. } // namespace dsp
  143. } // namespace juce