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.

163 lines
5.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 oscillator with a periodic input function (-pi..pi).
  35. If lookup table is not zero, then the function will be approximated
  36. with a lookup table.
  37. */
  38. Oscillator (const std::function<NumericType (NumericType)>& function, size_t lookupTableNumPoints = 0)
  39. : generator (function), frequency (440.0f)
  40. {
  41. if (lookupTableNumPoints != 0)
  42. {
  43. auto table = new LookupTableTransform<NumericType> (generator, static_cast <NumericType> (-1.0 * double_Pi),
  44. static_cast<NumericType> (double_Pi), lookupTableNumPoints);
  45. lookupTable = table;
  46. generator = [table] (NumericType x) { return (*table) (x); };
  47. }
  48. }
  49. //==============================================================================
  50. /** Sets the frequency of the oscillator. */
  51. void setFrequency (NumericType newGain) noexcept { frequency.setValue (newGain); }
  52. /** Returns the current frequency of the oscillator. */
  53. NumericType getFrequency() const noexcept { return frequency.getTargetValue(); }
  54. //==============================================================================
  55. /** Called before processing starts. */
  56. void prepare (const ProcessSpec& spec) noexcept
  57. {
  58. sampleRate = static_cast<NumericType> (spec.sampleRate);
  59. rampBuffer.resize ((int) spec.maximumBlockSize);
  60. reset();
  61. }
  62. /** Resets the internal state of the oscillator */
  63. void reset() noexcept
  64. {
  65. pos = 0.0;
  66. if (sampleRate > 0)
  67. frequency.reset (sampleRate, 0.05);
  68. }
  69. //==============================================================================
  70. /** Returns the result of processing a single sample. */
  71. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType) noexcept
  72. {
  73. auto increment = static_cast<NumericType> (2.0 * double_Pi) * frequency.getNextValue() / sampleRate;
  74. auto value = generator (pos - static_cast<NumericType> (double_Pi));
  75. pos = std::fmod (pos + increment, static_cast<NumericType> (2.0 * double_Pi));
  76. return value;
  77. }
  78. /** Processes the input and output buffers supplied in the processing context. */
  79. template <typename ProcessContext>
  80. void process (const ProcessContext& context) noexcept
  81. {
  82. auto&& outBlock = context.getOutputBlock();
  83. // this is an output-only processory
  84. jassert (context.getInputBlock().getNumChannels() == 0 || (! context.usesSeparateInputAndOutputBlocks()));
  85. jassert (outBlock.getNumSamples() <= static_cast<size_t> (rampBuffer.size()));
  86. auto len = outBlock.getNumSamples();
  87. auto numChannels = outBlock.getNumChannels();
  88. auto baseIncrement = static_cast<NumericType> (2.0 * double_Pi) / sampleRate;
  89. if (frequency.isSmoothing())
  90. {
  91. auto* buffer = rampBuffer.getRawDataPointer();
  92. for (size_t i = 0; i < len; ++i)
  93. {
  94. buffer[i] = pos - static_cast<NumericType> (double_Pi);
  95. pos = std::fmod (pos + (baseIncrement * frequency.getNextValue()), static_cast<NumericType> (2.0 * double_Pi));
  96. }
  97. for (size_t ch = 0; ch < numChannels; ++ch)
  98. {
  99. auto* dst = outBlock.getChannelPointer (ch);
  100. for (size_t i = 0; i < len; ++i)
  101. dst[i] = generator (buffer[i]);
  102. }
  103. }
  104. else
  105. {
  106. auto freq = baseIncrement * frequency.getNextValue();
  107. for (size_t ch = 0; ch < numChannels; ++ch)
  108. {
  109. auto p = pos;
  110. auto* dst = outBlock.getChannelPointer (ch);
  111. for (size_t i = 0; i < len; ++i)
  112. {
  113. dst[i] = generator (p - static_cast<NumericType> (double_Pi));
  114. p = std::fmod (p + freq, static_cast<NumericType> (2.0 * double_Pi));
  115. }
  116. }
  117. pos = std::fmod (pos + freq * static_cast<NumericType> (len), static_cast<NumericType> (2.0 * double_Pi));
  118. }
  119. }
  120. private:
  121. //==============================================================================
  122. std::function<NumericType (NumericType)> generator;
  123. ScopedPointer<LookupTableTransform<NumericType>> lookupTable;
  124. Array<NumericType> rampBuffer;
  125. LinearSmoothedValue<NumericType> frequency {static_cast<NumericType> (440.0)};
  126. NumericType sampleRate = 48000.0, pos = 0.0;
  127. };
  128. } // namespace dsp
  129. } // namespace juce