FFTConvolver.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*******************************************************************************
  2. * Copyright 2015-2016 Juan Francisco Crespo Galán
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. #pragma once
  17. /**
  18. * @file FFTConvolver.h
  19. * @ingroup fx
  20. * The FFTConvolver class.
  21. */
  22. #include "IReader.h"
  23. #include "ISound.h"
  24. #include "util/FFTPlan.h"
  25. #include <memory>
  26. #include <vector>
  27. AUD_NAMESPACE_BEGIN
  28. /**
  29. * This class allows to easily convolve a sound using the Fourier transform.
  30. */
  31. class AUD_API FFTConvolver
  32. {
  33. private:
  34. /**
  35. * A shared pointer to an FFT plan.
  36. */
  37. std::shared_ptr<FFTPlan> m_plan;
  38. /**
  39. * The FFT size, must be at least M+L-1.
  40. */
  41. int m_N;
  42. /**
  43. * The length of the impulse response.
  44. */
  45. int m_M;
  46. /**
  47. * The max length of the input slices.
  48. */
  49. int m_L;
  50. /**
  51. * The real length of the internal buffer in fftwf_complex elements.
  52. */
  53. int m_realBufLen;
  54. /**
  55. * The internal buffer for the FFTS.
  56. */
  57. std::complex<sample_t>* m_inBuffer;
  58. /**
  59. * A shift buffer for the FDL method
  60. */
  61. sample_t* m_shiftBuffer;
  62. /**
  63. * A buffer to store the extra data obtained after each partial convolution.
  64. */
  65. float* m_tail;
  66. /**
  67. * The provided impulse response.
  68. */
  69. std::shared_ptr<std::vector<std::complex<sample_t>>> m_irBuffer;
  70. /**
  71. * If the tail is being read, this marks the current position.
  72. */
  73. int m_tailPos;
  74. // delete copy constructor and operator=
  75. FFTConvolver(const FFTConvolver&) = delete;
  76. FFTConvolver& operator=(const FFTConvolver&) = delete;
  77. public:
  78. /**
  79. * Creates a new FFTConvolver.
  80. * \param ir A shared pointer to a vector with the impulse response data in the frequency domain (see ImpulseResponse class for an easy way to obtain it).
  81. * \param plan A shared pointer to and FFT plan.
  82. */
  83. FFTConvolver(std::shared_ptr<std::vector<std::complex<sample_t>>> ir, std::shared_ptr<FFTPlan> plan);
  84. virtual ~FFTConvolver();
  85. /**
  86. * Convolves the data that is provided with the inpulse response.
  87. * \param[in] inBuffer A buffer with the input data to be convolved.
  88. * \param[in] outBuffer A pointer to the buffer in which the convolution result will be written.
  89. * \param[in,out] length The number of samples to be convolved (the length of both the inBuffer and the outBuffer).
  90. * The convolution output should be larger than the input, but since this class uses the overlap
  91. * add method, the extra length will be saved internally.
  92. * It must be equal or lower than N/2 (N=size of the FFTPlan) or the call will fail, setting this variable to 0 since no data would be
  93. * written in the outBuffer.
  94. */
  95. void getNext(const sample_t* inBuffer, sample_t* outBuffer, int& length);
  96. /**
  97. * Convolves the data that is provided with the inpulse response.
  98. * \param[in] inBuffer A buffer with the input data to be convolved.
  99. * \param[in] outBuffer A pointer to the buffer in which the convolution result will be written.
  100. * \param[in,out] length The number of samples to be convolved (the length of both the inBuffer and the outBuffer).
  101. * The convolution output should be larger than the input, but since this class uses the overlap
  102. * add method, the extra length will be saved internally.
  103. * It must be equal or lower than N/2 (N=size of the FFTPlan) or the call will fail, setting this variable to 0 since no data would be
  104. * written in the outBuffer.
  105. * \param[in] transformedData A pointer to a buffer in which the Fourier transform of the input will be written.
  106. */
  107. void getNext(const sample_t* inBuffer, sample_t* outBuffer, int& length, fftwf_complex* transformedData);
  108. /**
  109. * Convolves the data that is provided with the inpulse response.
  110. * \param[in] inBuffer A buffer with the input data to be convolved. Its length must be N/2 + 1
  111. * \param[in] outBuffer A pointer to the buffer in which the convolution result will be written.
  112. * \param[in,out] length The number of samples to be convolved and the length of the outBuffer.
  113. * The convolution output should be larger than the input, but since this class uses the overlap
  114. * add method, the extra length will be saved internally.
  115. * It must be equal or lower than N/2 (N=size of the FFTPlan) or the call will fail and set the value of length to 0 since no data would be
  116. * written in the outBuffer.
  117. */
  118. void getNext(const fftwf_complex* inBuffer, sample_t* outBuffer, int& length);
  119. /**
  120. * Gets the internally stored extra data which is result of the convolution.
  121. * \param[in,out] length The count of samples that should be read. Shall
  122. * contain the real count of samples after reading, in case
  123. * there were only fewer samples available.
  124. * A smaller value also indicates the end of the data.
  125. * \param[out] eos End of stream, whether the end is reached or not.
  126. * \param[in] buffer The pointer to the buffer to read into.
  127. */
  128. void getTail(int& length, bool& eos, sample_t* buffer);
  129. /**
  130. * Resets the internally stored data so a new convolution can be started.
  131. */
  132. void clear();
  133. /**
  134. * Calculates the Inverse Fast Fourier Transform of the input array.
  135. * \param[in] inBuffer A buffer with the input data to be transformed. Its length must be N/2 + 1
  136. * \param[in] outBuffer A pointer to the buffer in which the transform result will be written.
  137. * \param[in,out] length The number of samples to be transformed and the length of the outBuffer.
  138. * It must be equal or lower than N, but tipically N/2 should be used (N=size of the FFTPlan) or the call will fail and the value
  139. * of length will be setted to 0, since no data would be written in the outBuffer.
  140. */
  141. void IFFT_FDL(const fftwf_complex* inBuffer, sample_t* outBuffer, int& length);
  142. /**
  143. * Multiplicates a frequency domain input by the impulse response and accumulates the result to a buffer.
  144. * \param[in] inBuffer A buffer of complex numbers, samples in the frequency domain, that will be multiplied by the impulse response. Its length must be N/2 + 1
  145. * \param[in] accBuffer A pointer to the buffer into which the result of the multiplication will be summed. Its length must be N/2 + 1
  146. */
  147. void getNextFDL(const std::complex<sample_t>* inBuffer, std::complex<sample_t>* accBuffer);
  148. /**
  149. * Transforms an input array of real data to the frequency domain and multiplies it by the impulse response. The result is accumulated to a buffer.
  150. * \param[in] inBuffer A buffer of real numbers, samples in the time domain, that will be multiplied by the impulse response.
  151. * \param[in] accBuffer A pointer to the buffer into which the result of the multiplication will be summed. Its length must be N/2 + 1.
  152. * \param[in,out] length The number of samples to be transformed and the length of the inBuffer.
  153. * It must be equal or lower than N/2 (N=size of the FFTPlan) or the call will fail and the value
  154. * of length will be setted to 0, since no data would be written in the outBuffer.
  155. * \param[in] transformedData A pointer to a buffer in which the Fourier transform of the input will be written.
  156. */
  157. void getNextFDL(const sample_t* inBuffer, std::complex<sample_t>* accBuffer, int& length, fftwf_complex* transformedData);
  158. /**
  159. * Changes the impulse response and resets the FFTConvolver.
  160. * \param ir A shared pointer to a vector with the data of the impulse response in the frequency domain.
  161. */
  162. void setImpulseResponse(std::shared_ptr<std::vector<std::complex<sample_t>>> ir);
  163. /**
  164. * Retrieves the current impulse response being used.
  165. * \return The current impulse response.
  166. */
  167. std::shared_ptr<std::vector<std::complex<sample_t>>> getImpulseResponse();
  168. };
  169. AUD_NAMESPACE_END