BinauralReader.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 BinauralReader.h
  19. * @ingroup fx
  20. * The BinauralReader class.
  21. */
  22. #include "IReader.h"
  23. #include "ISound.h"
  24. #include "Convolver.h"
  25. #include "HRTF.h"
  26. #include "Source.h"
  27. #include "util/FFTPlan.h"
  28. #include "util/ThreadPool.h"
  29. #include <memory>
  30. #include <vector>
  31. #include <future>
  32. AUD_NAMESPACE_BEGIN
  33. /**
  34. * This class represents a reader for a sound that can sound different depending on its realtive position with the listener.
  35. */
  36. class AUD_API BinauralReader : public IReader
  37. {
  38. private:
  39. /**
  40. * The current position.
  41. */
  42. int m_position;
  43. /**
  44. * The reader of the input sound.
  45. */
  46. std::shared_ptr<IReader> m_reader;
  47. /**
  48. * The HRTF set.
  49. */
  50. std::shared_ptr<HRTF> m_hrtfs;
  51. /**
  52. * A Source object that will be used to change the source position of the sound.
  53. */
  54. std::shared_ptr<Source> m_source;
  55. /**
  56. * The intended azimuth.
  57. */
  58. float m_Azimuth;
  59. /**
  60. * The intended elevation.
  61. */
  62. float m_Elevation;
  63. /**
  64. * The real azimuth being used.
  65. */
  66. float m_RealAzimuth;
  67. /**
  68. * The real elevation being used.
  69. */
  70. float m_RealElevation;
  71. /**
  72. * The FFT size, given by the FFTPlan.
  73. */
  74. int m_N;
  75. /**
  76. * The length of the impulse response fragments, m_N/2 will be used.
  77. */
  78. int m_M;
  79. /**
  80. * The max length of the input slices, m_N/2 will be used.
  81. */
  82. int m_L;
  83. /**
  84. * The array of convolvers that will be used, one per channel.
  85. */
  86. std::vector<std::unique_ptr<Convolver>> m_convolvers;
  87. /**
  88. * True if a transition is happening.
  89. */
  90. bool m_transition;
  91. /**
  92. * The position of the current transition (decreasing)
  93. */
  94. int m_transPos;
  95. /**
  96. * The output buffer in which the convolved data will be written and from which the reader will read.
  97. */
  98. sample_t* m_outBuffer;
  99. /**
  100. * The input buffer that will hold the data to be convolved.
  101. */
  102. sample_t* m_inBuffer;
  103. /**
  104. * Current position in which the m_outBuffer is being read.
  105. */
  106. int m_outBufferPos;
  107. /**
  108. * Length of rhe m_outBuffer.
  109. */
  110. int m_outBufLen;
  111. /**
  112. * Effective length of rhe m_outBuffer.
  113. */
  114. int m_eOutBufLen;
  115. /**
  116. * Flag indicating whether the end of the sound has been reached or not.
  117. */
  118. bool m_eosReader;
  119. /**
  120. * Flag indicating whether the end of the extra data generated in the convolution has been reached or not.
  121. */
  122. bool m_eosTail;
  123. /**
  124. * A vector of buffers (one per channel) on which the audio signal will be separated per channel so it can be convolved.
  125. */
  126. std::vector<sample_t*> m_vecOut;
  127. /**
  128. * A shared ptr to a thread pool.
  129. */
  130. std::shared_ptr<ThreadPool> m_threadPool;
  131. /**
  132. * Length of the input data to be used by the channel threads.
  133. */
  134. int m_lastLengthIn;
  135. /**
  136. * A vector of futures to sync tasks.
  137. */
  138. std::vector<std::future<int>> m_futures;
  139. // delete copy constructor and operator=
  140. BinauralReader(const BinauralReader&) = delete;
  141. BinauralReader& operator=(const BinauralReader&) = delete;
  142. public:
  143. /**
  144. * Creates a new convolver reader.
  145. * \param reader A reader of the input sound to be assigned to this reader. It must have one channel.
  146. * \param hrtfs A shared pointer to an HRTF object that will be used to get a particular impulse response depending on the source.
  147. * \param source A shared pointer to a Source object that will be used to change the source position of the sound.
  148. * \param threadPool A shared pointer to a ThreadPool object with 1 or more threads.
  149. * \param plan A shared pointer to and FFT plan that will be used for convolution.
  150. * \exception Exception thrown if the specs of the HRTFs and the sound don't match or if the provided HRTF object is empty.
  151. */
  152. BinauralReader(std::shared_ptr<IReader> reader, std::shared_ptr<HRTF> hrtfs, std::shared_ptr<Source> source, std::shared_ptr<ThreadPool> threadPool, std::shared_ptr<FFTPlan> plan);
  153. virtual ~BinauralReader();
  154. virtual bool isSeekable() const;
  155. virtual void seek(int position);
  156. virtual int getLength() const;
  157. virtual int getPosition() const;
  158. virtual Specs getSpecs() const;
  159. virtual void read(int& length, bool& eos, sample_t* buffer);
  160. private:
  161. /**
  162. * Joins several buffers (one per channel) into the m_outBuffer.
  163. * \param start The starting position from which the m_outBuffer will be written.
  164. * \param len The amout of samples that will be joined.
  165. * \param nConvolvers The number of convolvers that have been used. Only use 2 or 4 as possible values.
  166. If the value is 4 the result will be interpolated.
  167. */
  168. void joinByChannel(int start, int len, int nConvolvers);
  169. /**
  170. * Loads the m_outBuffer with data.
  171. * \param nConvolvers The number of convolver objects that will be used. Only 2 or 4 should be used.
  172. */
  173. void loadBuffer(int nConvolvers);
  174. /**
  175. * The function that the threads will run. It will process a subset of channels.
  176. * \param id An id number that will determine which subset of channels will be processed.
  177. * \param input A flag that will indicate if thare is input data.
  178. * -If true there is new input data.
  179. * -If false there isn't new input data.
  180. * \return The number of samples obtained.
  181. */
  182. int threadFunction(int id, bool input);
  183. bool checkSource();
  184. };
  185. AUD_NAMESPACE_END