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
6.5KB

  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. /**
  20. Performs stereo uniform-partitioned convolution of an input signal with an
  21. impulse response in the frequency domain, using the juce FFT class.
  22. It provides some thread-safe functions to load impulse responses as well,
  23. from audio files or memory on the fly without any noticeable artefacts,
  24. performing resampling and trimming if necessary.
  25. The processing is equivalent to the time domain convolution done in the
  26. class FIRFilter, with a FIRFilter::Coefficients object having as
  27. coefficients the samples of the impulse response. However, it is more
  28. efficient in general to do frequency domain convolution when the size of
  29. the impulse response is higher than 64 samples.
  30. @see FIRFilter, FIRFilter::Coefficients, FFT
  31. */
  32. class JUCE_API Convolution
  33. {
  34. public:
  35. //==============================================================================
  36. /** Initialises an object for performing convolution in the frequency domain. */
  37. Convolution();
  38. /** Destructor. */
  39. ~Convolution();
  40. //==============================================================================
  41. /** Must be called before loading any impulse response, to provide to the
  42. convolution the maximumBufferSize to handle, and the sample rate useful for
  43. optional resampling.
  44. */
  45. void prepare (const ProcessSpec&);
  46. /** Resets the processing pipeline, ready to start a new stream of data. */
  47. void reset() noexcept;
  48. /** Performs the filter operation on the given set of samples, with optional
  49. stereo processing.
  50. */
  51. template <typename ProcessContext>
  52. void process (const ProcessContext& context) noexcept
  53. {
  54. static_assert (std::is_same<typename ProcessContext::SampleType, float>::value,
  55. "Convolution engine only supports single precision floating point data");
  56. processSamples (context.getInputBlock(), context.getOutputBlock(), context.isBypassed);
  57. }
  58. //==============================================================================
  59. /** This function loads an impulse response audio file from memory, added in a
  60. JUCE project with the Projucer as binary data. It can load any of the audio
  61. formats registered in JUCE, and performs some resampling and pre-processing
  62. as well if needed.
  63. Note : obviously, don't try to use this function on float samples, since the
  64. data is supposed to be an audio file in its binary format, and be sure that
  65. the original data is not going to move at all its memory location during the
  66. process !!
  67. @param sourceData the block of data to use as the stream's source
  68. @param sourceDataSize the number of bytes in the source data block
  69. @param wantsStereo requests to load both stereo channels or only one mono channel
  70. @param wantsTrimming requests to trim the start and the end of the impulse response
  71. @param size the expected size for the impulse response after loading
  72. */
  73. void loadImpulseResponse (const void* sourceData, size_t sourceDataSize,
  74. bool wantsStereo, bool wantsTrimming, size_t size);
  75. /** This function loads an impulse response from an audio file on any drive. It
  76. can load any of the audio formats registered in JUCE, and performs some
  77. resampling and pre-processing as well if needed.
  78. @param fileImpulseResponse the location of the audio file
  79. @param wantsStereo requests to load both stereo channels or only one mono channel
  80. @param wantsTrimming requests to trim the start and the end of the impulse response
  81. @param size the expected size for the impulse response after loading
  82. */
  83. void loadImpulseResponse (const File& fileImpulseResponse,
  84. bool wantsStereo, bool wantsTrimming, size_t size);
  85. /** This function loads an impulse response from an audio buffer, which is
  86. copied before doing anything else. Performs some resampling and
  87. pre-processing as well if needed.
  88. @param buffer the AudioBuffer to use
  89. @param bufferSampleRate the sampleRate of the data in the AudioBuffer
  90. @param wantsStereo requests to load both stereo channels or only one mono channel
  91. @param wantsTrimming requests to trim the start and the end of the impulse response
  92. @param size the expected size for the impulse response after loading
  93. */
  94. void copyAndLoadImpulseResponseFromBuffer (const AudioBuffer<float>& buffer, double bufferSampleRate,
  95. bool wantsStereo, bool wantsTrimming, size_t size);
  96. private:
  97. //==============================================================================
  98. struct Pimpl;
  99. ScopedPointer<Pimpl> pimpl;
  100. //==============================================================================
  101. void processSamples (const AudioBlock<float>&, AudioBlock<float>&, bool isBypassed) noexcept;
  102. //==============================================================================
  103. double sampleRate;
  104. bool currentIsBypassed = false;
  105. LinearSmoothedValue<float> volumeDry[2], volumeWet[2];
  106. AudioBlock<float> dryBuffer;
  107. HeapBlock<char> dryBufferStorage;
  108. //==============================================================================
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Convolution)
  110. };