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.

151 lines
6.0KB

  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. #ifndef DOXYGEN
  24. template <typename NumericType>
  25. class OversamplingEngine;
  26. #endif
  27. //===============================================================================
  28. /**
  29. A processing class performing multi-channel oversampling.
  30. It can be configured to do 2 times, 4 times, 8 times or 16 times oversampling
  31. using a multi-stage approach, either polyphase allpass IIR filters or FIR
  32. filters for the filtering, and reports successfully the latency added by the
  33. filter stages.
  34. The principle of oversampling is to increase the sample rate of a given
  35. non-linear process, to prevent it from creating aliasing. Oversampling works
  36. by upsampling N times the input signal, processing the upsampling signal
  37. with the increased internal sample rate, and downsample the result to get
  38. back the original processing sample rate.
  39. Choose between FIR or IIR filtering depending on your needs in term of
  40. latency and phase distortion. With FIR filters, the phase is linear but the
  41. latency is maximum. With IIR filtering, the phase is compromised around the
  42. Nyquist frequency but the latency is minimum.
  43. @see FilterDesign.
  44. @tags{DSP}
  45. */
  46. template <typename SampleType>
  47. class JUCE_API Oversampling
  48. {
  49. public:
  50. /** The type of filter that can be used for the oversampling processing. */
  51. enum FilterType
  52. {
  53. filterHalfBandFIREquiripple = 0,
  54. filterHalfBandPolyphaseIIR,
  55. numFilterTypes
  56. };
  57. //===============================================================================
  58. /**
  59. Constructor of the oversampling class. All the processing parameters must be
  60. provided at the creation of the oversampling object.
  61. Note : you might want to create a class heriting from Oversampling with a
  62. different constructor if you need more control on what happens in the process.
  63. @param numChannels the number of channels to process with this object
  64. @param factor the processing will perform 2 ^ factor times oversampling
  65. @param type the type of filter design employed for filtering during
  66. oversampling
  67. @param isMaxQuality if the oversampling is done using the maximum quality,
  68. the filters will be more efficient, but the CPU load will
  69. increase as well
  70. */
  71. Oversampling (size_t numChannels, size_t factor, FilterType type, bool isMaxQuality = true);
  72. /** Destructor. */
  73. ~Oversampling();
  74. //===============================================================================
  75. /** Returns the latency in samples of the whole processing. Use this information
  76. in your main processor to compensate the additional latency involved with
  77. the oversampling, for example with a dry / wet functionality, and to report
  78. the latency to the DAW.
  79. Note : the latency might not be integer, so you might need to round its value
  80. or to compensate it properly in your processing code.
  81. */
  82. SampleType getLatencyInSamples() noexcept;
  83. /** Returns the current oversampling factor. */
  84. size_t getOversamplingFactor() noexcept;
  85. //===============================================================================
  86. /** Must be called before any processing, to set the buffer sizes of the internal
  87. buffers of the oversampling processing.
  88. */
  89. void initProcessing (size_t maximumNumberOfSamplesBeforeOversampling);
  90. /** Resets the processing pipeline, ready to oversample a new stream of data. */
  91. void reset() noexcept;
  92. /** Must be called to perform the upsampling, prior to any oversampled processing.
  93. Returns an AudioBlock referencing the oversampled input signal, which must be
  94. used to perform the non-linear processing which needs the higher sample rate.
  95. Don't forget to set the sample rate of that processing to N times the original
  96. sample rate.
  97. */
  98. dsp::AudioBlock<SampleType> processSamplesUp (const dsp::AudioBlock<SampleType> &inputBlock) noexcept;
  99. /** Must be called to perform the downsampling, after the upsampling and the
  100. non-linear processing. The output signal is probably delayed by the internal
  101. latency of the whole oversampling behaviour, so don't forget to take this
  102. into account.
  103. */
  104. void processSamplesDown (dsp::AudioBlock<SampleType> &outputBlock) noexcept;
  105. private:
  106. //===============================================================================
  107. bool isMaximumQuality;
  108. size_t factorOversampling, numStages;
  109. FilterType type;
  110. size_t numChannels;
  111. //===============================================================================
  112. bool isReady = false;
  113. OwnedArray<OversamplingEngine<SampleType>> engines;
  114. //===============================================================================
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oversampling)
  116. };
  117. } // namespace dsp
  118. } // namespace juce