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.

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