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.

167 lines
8.2KB

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