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.

212 lines
9.5KB

  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::dsp
  19. {
  20. //==============================================================================
  21. /**
  22. A processor that performs multi-channel oversampling.
  23. This class can be configured to do a factor of 2, 4, 8 or 16 times
  24. oversampling, using multiple stages, with polyphase allpass IIR filters or FIR
  25. filters, and latency compensation.
  26. The principle of oversampling is to increase the sample rate of a given
  27. non-linear process to prevent it from creating aliasing. Oversampling works
  28. by upsampling the input signal N times, processing the upsampled signal
  29. with the increased internal sample rate, then downsampling the result to get
  30. back to the original sample rate.
  31. Choose between FIR or IIR filtering depending on your needs in terms of
  32. latency and phase distortion. With FIR filters the phase is linear but the
  33. latency is maximised. With IIR filtering the phase is compromised around the
  34. Nyquist frequency but the latency is minimised.
  35. @see FilterDesign.
  36. @tags{DSP}
  37. */
  38. template <typename SampleType>
  39. class JUCE_API Oversampling
  40. {
  41. public:
  42. /** The type of filter that can be used for the oversampling processing. */
  43. enum FilterType
  44. {
  45. filterHalfBandFIREquiripple = 0,
  46. filterHalfBandPolyphaseIIR,
  47. numFilterTypes
  48. };
  49. //==============================================================================
  50. /** The default constructor.
  51. Note: This creates a "dummy" oversampling stage, which needs to be removed
  52. before adding proper oversampling stages.
  53. @param numChannels the number of channels to process with this object
  54. @see clearOversamplingStages, addOversamplingStage
  55. */
  56. explicit Oversampling (size_t numChannels = 1);
  57. /** Constructor.
  58. @param numChannels the number of channels to process with this object
  59. @param factor the processing will perform 2 ^ factor times oversampling
  60. @param type the type of filter design employed for filtering during
  61. oversampling
  62. @param isMaxQuality if the oversampling is done using the maximum quality, where
  63. the filters will be more efficient but the CPU load will
  64. increase as well
  65. @param useIntegerLatency if true this processor will add some fractional delay at the
  66. end of the signal path to ensure that the overall latency of
  67. the oversampling is an integer
  68. */
  69. Oversampling (size_t numChannels,
  70. size_t factor,
  71. FilterType type,
  72. bool isMaxQuality = true,
  73. bool useIntegerLatency = false);
  74. /** Destructor. */
  75. ~Oversampling();
  76. //==============================================================================
  77. /* Sets if this processor should add some fractional delay at the end of the signal
  78. path to ensure that the overall latency of the oversampling is an integer.
  79. */
  80. void setUsingIntegerLatency (bool shouldUseIntegerLatency) noexcept;
  81. /** Returns the latency in samples of the overall processing. You can use this
  82. information in your main processor to compensate the additional latency
  83. involved with the oversampling, for example with a dry / wet mixer, and to
  84. report the latency to the DAW.
  85. Note: If you have not opted to use an integer latency then the latency may not be
  86. integer, so you might need to round its value or to compensate it properly in
  87. your processing code since plug-ins can only report integer latency values in
  88. samples to the DAW.
  89. */
  90. SampleType getLatencyInSamples() const noexcept;
  91. /** Returns the current oversampling factor. */
  92. size_t getOversamplingFactor() const noexcept;
  93. //==============================================================================
  94. /** Must be called before any processing, to set the buffer sizes of the internal
  95. buffers of the oversampling processing.
  96. */
  97. void initProcessing (size_t maximumNumberOfSamplesBeforeOversampling);
  98. /** Resets the processing pipeline, ready to oversample a new stream of data. */
  99. void reset() noexcept;
  100. /** Must be called to perform the upsampling, prior to any oversampled processing.
  101. Returns an AudioBlock referencing the oversampled input signal, which must be
  102. used to perform the non-linear processing which needs the higher sample rate.
  103. Don't forget to set the sample rate of that processing to N times the original
  104. sample rate.
  105. */
  106. AudioBlock<SampleType> processSamplesUp (const AudioBlock<const SampleType>& inputBlock) noexcept;
  107. /** Must be called to perform the downsampling, after the upsampling and the
  108. non-linear processing. The output signal is probably delayed by the internal
  109. latency of the whole oversampling behaviour, so don't forget to take this
  110. into account.
  111. */
  112. void processSamplesDown (AudioBlock<SampleType>& outputBlock) noexcept;
  113. //==============================================================================
  114. /** Adds a new oversampling stage to the Oversampling class, multiplying the
  115. current oversampling factor by two. This is used with the default constructor
  116. to create custom oversampling chains, requiring a call to the
  117. clearOversamplingStages before any addition.
  118. Note: Upsampling and downsampling filtering have different purposes, the
  119. former removes upsampling artefacts while the latter removes useless frequency
  120. content created by the oversampled process, so usually the attenuation is
  121. increased when upsampling compared to downsampling.
  122. @param normalisedTransitionWidthUp a value between 0 and 0.5 which specifies how much
  123. the transition between passband and stopband is
  124. steep, for upsampling filtering (the lower the better)
  125. @param stopbandAmplitudedBUp the amplitude in dB in the stopband for upsampling
  126. filtering, must be negative
  127. @param normalisedTransitionWidthDown a value between 0 and 0.5 which specifies how much
  128. the transition between passband and stopband is
  129. steep, for downsampling filtering (the lower the better)
  130. @param stopbandAmplitudedBDown the amplitude in dB in the stopband for downsampling
  131. filtering, must be negative
  132. @see clearOversamplingStages
  133. */
  134. void addOversamplingStage (FilterType,
  135. float normalisedTransitionWidthUp, float stopbandAmplitudedBUp,
  136. float normalisedTransitionWidthDown, float stopbandAmplitudedBDown);
  137. /** Adds a new "dummy" oversampling stage, which does nothing to the signal. Using
  138. one can be useful if your application features a customisable oversampling factor
  139. and if you want to select the current one from an OwnedArray without changing
  140. anything in the processing code.
  141. @see OwnedArray, clearOversamplingStages, addOversamplingStage
  142. */
  143. void addDummyOversamplingStage();
  144. /** Removes all the previously registered oversampling stages, so you can add
  145. your own from scratch.
  146. @see addOversamplingStage, addDummyOversamplingStage
  147. */
  148. void clearOversamplingStages();
  149. //==============================================================================
  150. size_t factorOversampling = 1;
  151. size_t numChannels = 1;
  152. #ifndef DOXYGEN
  153. struct OversamplingStage;
  154. #endif
  155. private:
  156. //==============================================================================
  157. void updateDelayLine();
  158. SampleType getUncompensatedLatency() const noexcept;
  159. //==============================================================================
  160. OwnedArray<OversamplingStage> stages;
  161. bool isReady = false, shouldUseIntegerLatency = false;
  162. DelayLine<SampleType, DelayLineInterpolationTypes::Thiran> delay { 8 };
  163. SampleType fractionalDelay = 0;
  164. //==============================================================================
  165. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oversampling)
  166. };
  167. } // namespace juce::dsp