ConvolverReader.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ConvolverReader.h
  19. * @ingroup fx
  20. * The ConvolverReader class.
  21. */
  22. #include "IReader.h"
  23. #include "ISound.h"
  24. #include "Convolver.h"
  25. #include "ImpulseResponse.h"
  26. #include "util/FFTPlan.h"
  27. #include "util/ThreadPool.h"
  28. #include <memory>
  29. #include <vector>
  30. #include <future>
  31. AUD_NAMESPACE_BEGIN
  32. /**
  33. * This class represents a reader for a sound that can be modified depending on a given impulse response.
  34. */
  35. class AUD_API ConvolverReader : public IReader
  36. {
  37. private:
  38. /**
  39. * The current position.
  40. */
  41. int m_position;
  42. /**
  43. * The reader of the input sound.
  44. */
  45. std::shared_ptr<IReader> m_reader;
  46. /**
  47. * The impulse response in the frequency domain.
  48. */
  49. std::shared_ptr<ImpulseResponse> m_ir;
  50. /**
  51. * The FFT size, given by the FFTPlan.
  52. */
  53. int m_N;
  54. /**
  55. * The length of the impulse response fragments, m_N/2 will be used.
  56. */
  57. int m_M;
  58. /**
  59. * The max length of the input slices, m_N/2 will be used.
  60. */
  61. int m_L;
  62. /**
  63. * The array of convolvers that will be used, one per channel.
  64. */
  65. std::vector<std::unique_ptr<Convolver>> m_convolvers;
  66. /**
  67. * The output buffer in which the convolved data will be written and from which the reader will read.
  68. */
  69. sample_t* m_outBuffer;
  70. /**
  71. * A vector of buffers (one per channel) on which the audio signal will be separated per channel so it can be convolved.
  72. */
  73. std::vector<sample_t*> m_vecInOut;
  74. /**
  75. * Current position in which the m_outBuffer is being read.
  76. */
  77. int m_outBufferPos;
  78. /**
  79. * Effective length of the m_outBuffer.
  80. */
  81. int m_eOutBufLen;
  82. /**
  83. * Real length of the m_outBuffer.
  84. */
  85. int m_outBufLen;
  86. /**
  87. * Flag indicating whether the end of the sound has been reached or not.
  88. */
  89. bool m_eosReader;
  90. /**
  91. * Flag indicating whether the end of the extra data generated in the convolution has been reached or not.
  92. */
  93. bool m_eosTail;
  94. /**
  95. * The number of channels of the sound to be convolved.
  96. */
  97. int m_inChannels;
  98. /**
  99. * The number of channels of the impulse response.
  100. */
  101. int m_irChannels;
  102. /**
  103. * The number of threads used for channels.
  104. */
  105. int m_nChannelThreads;
  106. /**
  107. * Length of the input data to be used by the channel threads.
  108. */
  109. int m_lastLengthIn;
  110. /**
  111. * A shared ptr to a thread pool.
  112. */
  113. std::shared_ptr<ThreadPool> m_threadPool;
  114. /**
  115. * A vector of futures to sync tasks.
  116. */
  117. std::vector<std::future<int>> m_futures;
  118. // delete copy constructor and operator=
  119. ConvolverReader(const ConvolverReader&) = delete;
  120. ConvolverReader& operator=(const ConvolverReader&) = delete;
  121. public:
  122. /**
  123. * Creates a new convolver reader.
  124. * \param reader A reader of the input sound to be assigned to this reader.
  125. * \param ir A shared pointer to an impulseResponse object that will be used to convolve the sound.
  126. * \param threadPool A shared pointer to a ThreadPool object with 1 or more threads.
  127. * \param plan A shared pointer to and FFT plan that will be used for convolution.
  128. * \exception Exception thrown if impulse response doesn't match the specs (number fo channels and rate) of the input reader.
  129. */
  130. ConvolverReader(std::shared_ptr<IReader> reader, std::shared_ptr<ImpulseResponse> ir, std::shared_ptr<ThreadPool> threadPool, std::shared_ptr<FFTPlan> plan);
  131. virtual ~ConvolverReader();
  132. virtual bool isSeekable() const;
  133. virtual void seek(int position);
  134. virtual int getLength() const;
  135. virtual int getPosition() const;
  136. virtual Specs getSpecs() const;
  137. virtual void read(int& length, bool& eos, sample_t* buffer);
  138. private:
  139. /**
  140. * Divides a sound buffer in several buffers, one per channel.
  141. * \param buffer The buffer that will be divided.
  142. * \param len The length of the buffer.
  143. */
  144. void divideByChannel(const sample_t* buffer, int len);
  145. /**
  146. * Joins several buffers (one per channel) into the m_outBuffer.
  147. * \param start The starting position from which the m_outBuffer will be written.
  148. * \param len The amout of samples that will be joined.
  149. */
  150. void joinByChannel(int start, int len);
  151. /**
  152. * Loads the m_outBuffer with data.
  153. */
  154. void loadBuffer();
  155. /**
  156. * The function that the threads will run. It will process a subset of channels.
  157. * \param id An id number that will determine which subset of channels will be processed.
  158. * \param input A flag that will indicate if thare is input data.
  159. * -If true there is new input data.
  160. * -If false there isn't new input data.
  161. * \return The number of samples obtained.
  162. */
  163. int threadFunction(int id, bool input);
  164. };
  165. AUD_NAMESPACE_END