Convolver.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Convolver.h
  19. * @ingroup fx
  20. * The Convolver class.
  21. */
  22. #include "FFTConvolver.h"
  23. #include "util/ThreadPool.h"
  24. #include "util/FFTPlan.h"
  25. #include <memory>
  26. #include <vector>
  27. #include <mutex>
  28. #include <future>
  29. #include <atomic>
  30. #include <deque>
  31. AUD_NAMESPACE_BEGIN
  32. /**
  33. * This class allows to convolve a sound with a very large impulse response.
  34. */
  35. class AUD_API Convolver
  36. {
  37. private:
  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 parts.
  44. */
  45. int m_M;
  46. /**
  47. * The max length of the input slices.
  48. */
  49. int m_L;
  50. /**
  51. * The impulse response divided in parts.
  52. */
  53. std::shared_ptr<std::vector<std::shared_ptr<std::vector<std::complex<sample_t>>>>> m_irBuffers;
  54. /**
  55. * Accumulation buffers for the threads.
  56. */
  57. std::vector<fftwf_complex*> m_threadAccBuffers;
  58. /**
  59. * A vector of FFTConvolvers used to calculate the partial convolutions.
  60. */
  61. std::vector<std::unique_ptr<FFTConvolver>> m_fftConvolvers;
  62. /**
  63. * The actual number of threads being used.
  64. */
  65. int m_numThreads;
  66. /**
  67. * A pool of threads that will be used for convolution.
  68. */
  69. std::shared_ptr<ThreadPool> m_threadPool;
  70. /**
  71. * A vector of futures used for thread sync
  72. */
  73. std::vector<std::future<bool>> m_futures;
  74. /**
  75. * A mutex for the sum of thread accumulators.
  76. */
  77. std::mutex m_sumMutex;
  78. /**
  79. * A flag to control thread execution when a reset is scheduled.
  80. */
  81. std::atomic_bool m_resetFlag;
  82. /**
  83. * Global accumulation buffer.
  84. */
  85. fftwf_complex* m_accBuffer;
  86. /**
  87. * Delay line.
  88. */
  89. std::deque<fftwf_complex*> m_delayLine;
  90. /**
  91. * The complete length of the impulse response.
  92. */
  93. int m_irLength;
  94. /**
  95. * Counter for the tail;
  96. */
  97. int m_tailCounter;
  98. /**
  99. * Flag end of sound;
  100. */
  101. bool m_eos;
  102. // delete copy constructor and operator=
  103. Convolver(const Convolver&) = delete;
  104. Convolver& operator=(const Convolver&) = delete;
  105. public:
  106. /**
  107. * Creates a new FFTConvolver.
  108. * \param ir A shared pointer to a vector with the data of the various impulse response parts in the frequency domain (see ImpulseResponse class for an easy way to obtain it).
  109. * \param irLength The length of the full impulse response.
  110. * \param threadPool A shared pointer to a ThreadPool object with 1 or more threads.
  111. * \param plan A shared pointer to a FFT plan that will be used for convolution.
  112. */
  113. Convolver(std::shared_ptr<std::vector<std::shared_ptr<std::vector<std::complex<sample_t>>>>> ir, int irLength, std::shared_ptr<ThreadPool> threadPool, std::shared_ptr<FFTPlan> plan);
  114. virtual ~Convolver();
  115. /**
  116. * Convolves the data that is provided with the inpulse response.
  117. * Given a plan of size N, the amount of samples convolved by one call to this method will be N/2.
  118. * \param[in] inBuffer A buffer with the input data to be convolved, nullptr if the source sound has ended (the convolved sound is larger than the source sound).
  119. * \param[in] outBuffer A buffer in which the convolved data will be written. Its size must be at least N/2.
  120. * \param[in,out] length The number of samples you wish to obtain. If an inBuffer is provided this argument must match its length.
  121. * When this method returns, the value of length represents the number of samples written into the outBuffer.
  122. * \param[out] eos True if the end of the sound is reached, false otherwise.
  123. */
  124. void getNext(sample_t* inBuffer, sample_t* outBuffer, int& length, bool& eos);
  125. /**
  126. * Resets all the internally stored data so the convolution of a new sound can be started.
  127. */
  128. void reset();
  129. /**
  130. * Retrieves the current impulse response being used.
  131. * \return The current impulse response.
  132. */
  133. std::shared_ptr<std::vector<std::shared_ptr<std::vector<std::complex<sample_t>>>>> getImpulseResponse();
  134. /**
  135. * Changes the impulse response and resets the convolver.
  136. * \param ir A shared pointer to a vector with the data of the various impulse response parts in the frequency domain (see ImpulseResponse class for an easy way to obtain it).
  137. */
  138. void setImpulseResponse(std::shared_ptr<std::vector<std::shared_ptr<std::vector<std::complex<sample_t>>>>> ir);
  139. private:
  140. /**
  141. * This function will be enqueued into the thread pool, and will process the input signal with a subset of the impulse response parts.
  142. * \param id The id of the thread, starting with 0.
  143. */
  144. bool threadFunction(int id);
  145. };
  146. AUD_NAMESPACE_END