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.

147 lines
5.9KB

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