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.

193 lines
8.4KB

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