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.

164 lines
6.6KB

  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 upsampling signal
  33. with the increased internal sample rate, and downsample 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 maximum. With IIR filtering, the phase is compromised around the
  38. Nyquist frequency but the latency is minimum.
  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. Note: You might want to create a class inheriting from Oversampling with a
  58. different constructor if you need more control on what happens in the process.
  59. @param numChannels the number of channels to process with this object
  60. @param factor the processing will perform 2 ^ factor times oversampling
  61. @param type the type of filter design employed for filtering during
  62. oversampling
  63. @param isMaxQuality if the oversampling is done using the maximum quality,
  64. the filters will be more efficient, but the CPU load will
  65. increase as well
  66. */
  67. Oversampling (size_t numChannels,
  68. size_t factor,
  69. FilterType type,
  70. bool isMaxQuality = true);
  71. /** Default constructor of the oversampling class, which can be used to create an
  72. empty object and then add the appropriate stages.
  73. */
  74. explicit Oversampling (size_t numChannels = 1);
  75. /** Destructor. */
  76. ~Oversampling();
  77. //===============================================================================
  78. /** Returns the latency in samples of the whole processing. Use this information
  79. in your main processor to compensate the additional latency involved with
  80. the oversampling, for example with a dry / wet functionality, and to report
  81. the latency to the DAW.
  82. Note: The latency might not be integer, so you might need to round its value
  83. or to compensate it properly in your processing code.
  84. */
  85. SampleType getLatencyInSamples() noexcept;
  86. /** Returns the current oversampling factor. */
  87. size_t getOversamplingFactor() noexcept;
  88. //===============================================================================
  89. /** Must be called before any processing, to set the buffer sizes of the internal
  90. buffers of the oversampling processing.
  91. */
  92. void initProcessing (size_t maximumNumberOfSamplesBeforeOversampling);
  93. /** Resets the processing pipeline, ready to oversample a new stream of data. */
  94. void reset() noexcept;
  95. /** Must be called to perform the upsampling, prior to any oversampled processing.
  96. Returns an AudioBlock referencing the oversampled input signal, which must be
  97. used to perform the non-linear processing which needs the higher sample rate.
  98. Don't forget to set the sample rate of that processing to N times the original
  99. sample rate.
  100. */
  101. dsp::AudioBlock<SampleType> processSamplesUp (const dsp::AudioBlock<SampleType>& inputBlock) noexcept;
  102. /** Must be called to perform the downsampling, after the upsampling and the
  103. non-linear processing. The output signal is probably delayed by the internal
  104. latency of the whole oversampling behaviour, so don't forget to take this
  105. into account.
  106. */
  107. void processSamplesDown (dsp::AudioBlock<SampleType>& outputBlock) noexcept;
  108. //===============================================================================
  109. void addOversamplingStage (FilterType,
  110. float normalizedTransitionWidthUp, float stopbandAttenuationdBUp,
  111. float normalizedTransitionWidthDown, float stopbandAttenuationdBDown);
  112. void addDummyOversamplingStage();
  113. void clearOversamplingStages();
  114. //===============================================================================
  115. size_t factorOversampling = 1;
  116. size_t numChannels = 1;
  117. #ifndef DOXYGEN
  118. struct OversamplingStage;
  119. #endif
  120. private:
  121. //===============================================================================
  122. OwnedArray<OversamplingStage> stages;
  123. bool isReady = false;
  124. //===============================================================================
  125. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oversampling)
  126. };
  127. } // namespace dsp
  128. } // namespace juce