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.

201 lines
8.7KB

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