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.

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