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.

208 lines
9.3KB

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