Audio plugin host https://kx.studio/carla
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.

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