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.

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