ARATimelineConversion.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //------------------------------------------------------------------------------
  2. //! \file ARATimelineConversion.h
  3. //! classes to effectively iterate over tempo and signature content readers
  4. //! and convert between in seconds or quarters and between quarters and beats
  5. //! \project ARA SDK Library
  6. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  7. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  8. //! you may not use this file except in compliance with the License.
  9. //! You may obtain a copy of the License at
  10. //!
  11. //! http://www.apache.org/licenses/LICENSE-2.0
  12. //!
  13. //! Unless required by applicable law or agreed to in writing, software
  14. //! distributed under the License is distributed on an "AS IS" BASIS,
  15. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. //! See the License for the specific language governing permissions and
  17. //! limitations under the License.
  18. //------------------------------------------------------------------------------
  19. #ifndef ARATimelineConversion_h
  20. #define ARATimelineConversion_h
  21. #include "ARA_Library/Dispatch/ARAContentReader.h"
  22. #include "ARA_Library/Utilities/ARASamplePositionConversion.h"
  23. #include <algorithm>
  24. #include <iterator>
  25. #include <cmath>
  26. namespace ARA {
  27. //! @addtogroup ARA_Library_Utility_Timeline_Conversion
  28. //! @{
  29. /*******************************************************************************/
  30. // TempoConverter
  31. /** Efficient conversion between physical time in seconds and musical time in quarters.
  32. Utilizes internal cache of last access to optimize next access if "close" to last access,
  33. which is the typical case during playback, when drawing a time line, etc.
  34. templated so it can be used in both hosts and plug-ins with content readers,
  35. or with any other data structure that provides a matching interface.
  36. */
  37. /*******************************************************************************/
  38. template <typename TempoContentReader>
  39. class TempoConverter
  40. {
  41. public:
  42. //! Construct using a host or plug-in ::kARAContentTypeTempoEntries reader.
  43. TempoConverter (const TempoContentReader& contentReader)
  44. : _contentReader { contentReader },
  45. _leftEntryCache { this->_contentReader.begin () },
  46. _rightEntryCache { std::next (this->_leftEntryCache) }
  47. {}
  48. //! Convert a position in time to a quarter position.
  49. ARAQuarterPosition getQuarterForTime (const ARATimePosition timePosition) const
  50. {
  51. this->updateCacheByPosition (timePosition, [] (ARATimePosition position, const ARAContentTempoEntry& tempoEntry)
  52. {
  53. return position < tempoEntry.timePosition;
  54. });
  55. const auto quartersPerSecond { (this->_rightEntryCache->quarterPosition - this->_leftEntryCache->quarterPosition) / (this->_rightEntryCache->timePosition - this->_leftEntryCache->timePosition) };
  56. return this->_leftEntryCache->quarterPosition + (timePosition - this->_leftEntryCache->timePosition) * quartersPerSecond;
  57. }
  58. //! Convert a quarter position to a position in time.
  59. ARATimePosition getTimeForQuarter (const ARAQuarterPosition quarterPosition) const
  60. {
  61. this->updateCacheByPosition (quarterPosition, [] (ARAQuarterPosition position, const ARAContentTempoEntry& tempoEntry)
  62. {
  63. return position < tempoEntry.quarterPosition;
  64. });
  65. const auto secondsPerQuarter { (this->_rightEntryCache->timePosition - this->_leftEntryCache->timePosition) / (this->_rightEntryCache->quarterPosition - this->_leftEntryCache->quarterPosition) };
  66. return this->_leftEntryCache->timePosition + (quarterPosition - this->_leftEntryCache->quarterPosition) * secondsPerQuarter;
  67. }
  68. private:
  69. template <typename T, typename Func>
  70. void updateCacheByPosition (const T position, Func findByPosition) const
  71. {
  72. if (findByPosition (position, *this->_leftEntryCache))
  73. {
  74. if (this->_leftEntryCache != this->_contentReader.begin ())
  75. {
  76. // test if we're hitting the entries pair right before the current entries pair
  77. const auto prevLeft { std::prev (this->_leftEntryCache) };
  78. if ((prevLeft == this->_contentReader.begin ()) || !findByPosition (position, *prevLeft))
  79. {
  80. this->_rightEntryCache = this->_leftEntryCache;
  81. this->_leftEntryCache = prevLeft;
  82. }
  83. else
  84. {
  85. // find the entry after position, then pick left and right entry based on position being before or after first entry
  86. const auto it { std::upper_bound (this->_contentReader.begin (), prevLeft, position, findByPosition) };
  87. if (it == this->_contentReader.begin ())
  88. {
  89. this->_leftEntryCache = it;
  90. this->_rightEntryCache = std::next (it);
  91. }
  92. else
  93. {
  94. this->_leftEntryCache = std::prev (it);
  95. this->_rightEntryCache = it;
  96. }
  97. }
  98. }
  99. }
  100. else if (!findByPosition (position, *this->_rightEntryCache))
  101. {
  102. const auto nextRight { std::next (this->_rightEntryCache) };
  103. if (nextRight != this->_contentReader.end ())
  104. {
  105. // test if we're hitting the entries pair right after the current entries pair
  106. const auto last { std::prev (this->_contentReader.end ()) };
  107. if ((nextRight == last) || findByPosition (position, *nextRight))
  108. {
  109. this->_leftEntryCache = this->_rightEntryCache;
  110. this->_rightEntryCache = nextRight;
  111. }
  112. else
  113. {
  114. // find the entry after position (or last entry)
  115. this->_rightEntryCache = std::upper_bound (std::next (nextRight), last, position, findByPosition);
  116. this->_leftEntryCache = std::prev (this->_rightEntryCache);
  117. }
  118. }
  119. }
  120. ARA_INTERNAL_ASSERT (!findByPosition (position, *this->_leftEntryCache) || this->_leftEntryCache == this->_contentReader.begin ());
  121. ARA_INTERNAL_ASSERT (findByPosition (position, *this->_rightEntryCache) || std::next (this->_rightEntryCache) == this->_contentReader.end ());
  122. ARA_INTERNAL_ASSERT (this->_leftEntryCache == std::prev (this->_rightEntryCache));
  123. }
  124. private:
  125. const TempoContentReader& _contentReader;
  126. mutable typename TempoContentReader::const_iterator _leftEntryCache;
  127. mutable typename TempoContentReader::const_iterator _rightEntryCache;
  128. };
  129. /*******************************************************************************/
  130. // BarSignaturesConverter
  131. /** Mapping between a given quarter position and its associated bar signature and beat position.
  132. Utilizes internal cache of last access to optimize next access if "close" to last access,
  133. which is the typical case during playback, when drawing a time line, etc.
  134. templated so it can be used in both hosts and plug-ins with content readers,
  135. or with any other data structure that provides a matching interface.
  136. */
  137. /*******************************************************************************/
  138. template <typename BarSignaturesContentReader>
  139. class BarSignaturesConverter
  140. {
  141. public:
  142. //! Construct using a host or plug-in ::kARAContentTypeBarSignatures reader.
  143. inline BarSignaturesConverter (const BarSignaturesContentReader& reader) noexcept
  144. : _contentReader { reader },
  145. _entryCache { this->_contentReader.begin () }
  146. {}
  147. //! Look up the bar signature at a particular quarter position.
  148. inline const ARAContentBarSignature getBarSignatureForQuarter (const ARAQuarterPosition quarterPosition) const noexcept
  149. {
  150. this->updateCacheByQuarterPosition (quarterPosition);
  151. return *this->_entryCache;
  152. }
  153. //! Look up the bar signature at a particular beat position.
  154. inline const ARAContentBarSignature getBarSignatureForBeat (const double beatPosition) const noexcept
  155. {
  156. this->updateCacheByBeatPosition (beatPosition);
  157. return *this->_entryCache;
  158. }
  159. //! Get the beats per quarter for \p barSignature.
  160. static inline double getBeatsPerQuarter (const ARAContentBarSignature& barSignature) noexcept
  161. {
  162. return static_cast<double> (barSignature.denominator) / 4.0;
  163. }
  164. //! Get the quarters per bar for \p barSignature.
  165. static inline double getQuartersPerBar (const ARAContentBarSignature& barSignature) noexcept
  166. {
  167. return static_cast<double> (barSignature.numerator) / BarSignaturesConverter::getBeatsPerQuarter (barSignature);
  168. }
  169. //! Get the beat position for a particular quarter position.
  170. inline double getBeatForQuarter (const ARAQuarterPosition quarterPosition) const noexcept
  171. {
  172. this->updateCacheByQuarterPosition (quarterPosition);
  173. return this->_entryStartBeatCache + BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, quarterPosition);
  174. }
  175. //! Get the quarter position for a particular beat position.
  176. inline ARAQuarterPosition getQuarterForBeat (const double beatPosition) const noexcept
  177. {
  178. this->updateCacheByBeatPosition (beatPosition);
  179. return this->_entryCache->position + (beatPosition - this->_entryStartBeatCache) / BarSignaturesConverter::getBeatsPerQuarter (*this->_entryCache);
  180. }
  181. //! Get the distance in beats from the start of the bar signature at \p quarterPosition.
  182. inline double getBeatDistanceFromBarStartForQuarter (const ARAQuarterPosition quarterPosition) const noexcept
  183. {
  184. this->updateCacheByQuarterPosition (quarterPosition);
  185. const auto beatDistance { BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, quarterPosition) };
  186. const auto beatsPerBar { static_cast<double> (this->_entryCache->numerator) };
  187. const auto remainder { fmod (beatDistance, beatsPerBar) };
  188. return (beatDistance >= 0) ? remainder : beatsPerBar + remainder;
  189. }
  190. //! Get the index of the bar at \p quarterPosition.
  191. //! Relatively expensive since bar indices aren't cached in the current implementation.
  192. inline int getBarIndexForQuarter (const ARAQuarterPosition quarterPosition) const noexcept
  193. {
  194. this->updateCacheByQuarterPosition (quarterPosition);
  195. auto bars { floor ((quarterPosition - this->_entryCache->position) / BarSignaturesConverter::getQuartersPerBar (*this->_entryCache)) };
  196. auto it { this->_entryCache };
  197. while (it != this->_contentReader.begin ())
  198. {
  199. const auto prevEndQuarter { it->position };
  200. --it;
  201. bars += (prevEndQuarter - it->position) / BarSignaturesConverter::getQuartersPerBar (*it);
  202. }
  203. return (int) (bars + 0.5);
  204. }
  205. //! Get the quarter position of the start of the bar at \p barIndex.
  206. //! Relatively expensive since bar indices aren't cached in the current implementation.
  207. inline ARAQuarterPosition getQuarterForBarIndex (const int barIndex) const noexcept
  208. {
  209. this->_entryCache = this->_contentReader.begin ();
  210. this->_entryStartBeatCache = 0.0;
  211. bool didUpdateEntryStartBeatCache { false };
  212. int startBar { 0 };
  213. while (true)
  214. {
  215. const auto next { std::next (this->_entryCache) };
  216. if (next == this->_contentReader.end ())
  217. break;
  218. const auto nextStartBar { startBar + roundSamplePosition<int> ((next->position - this->_entryCache->position) / BarSignaturesConverter::getQuartersPerBar (*this->_entryCache)) };
  219. if (nextStartBar > barIndex)
  220. break;
  221. startBar = nextStartBar;
  222. this->_entryStartBeatCache = this->_entryStartBeatCache + BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, next->position);
  223. didUpdateEntryStartBeatCache = true;
  224. this->_entryCache = next;
  225. }
  226. // to avoid errors adding up over time, we round the cache to an integer value after modification
  227. if (didUpdateEntryStartBeatCache)
  228. this->_entryStartBeatCache = std::round (this->_entryStartBeatCache);
  229. return this->_entryCache->position + (barIndex - startBar) * BarSignaturesConverter::getQuartersPerBar (*this->_entryCache);
  230. }
  231. private:
  232. static inline double getBeatDistanceFromQuarterPosition (const typename BarSignaturesContentReader::const_iterator& it, const ARAQuarterPosition quarterPosition) noexcept
  233. {
  234. return (quarterPosition - it->position) * BarSignaturesConverter::getBeatsPerQuarter (*it);
  235. }
  236. void updateCacheByQuarterPosition (const ARAQuarterPosition quarterPosition) const noexcept
  237. {
  238. bool didUpdateEntryStartBeatCache { false };
  239. if (quarterPosition < this->_entryCache->position)
  240. {
  241. // before our entry - go back until first entry or entry before quarter
  242. while (this->_entryCache != this->_contentReader.begin ())
  243. {
  244. const auto prevEndQuarter { this->_entryCache->position };
  245. --this->_entryCache;
  246. this->_entryStartBeatCache -= BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, prevEndQuarter);
  247. didUpdateEntryStartBeatCache = true;
  248. if (this->_entryCache->position <= quarterPosition)
  249. break;
  250. }
  251. }
  252. else
  253. {
  254. // at or after our entry - go forward until last entry or entry before quarter
  255. while (true)
  256. {
  257. const auto next { std::next (this->_entryCache) };
  258. if ((next == this->_contentReader.end ()) || (next->position > quarterPosition))
  259. break;
  260. this->_entryStartBeatCache += BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, next->position);
  261. didUpdateEntryStartBeatCache = true;
  262. this->_entryCache = next;
  263. }
  264. }
  265. // to avoid errors adding up over time, we round the cache to an integer value after modification
  266. if (didUpdateEntryStartBeatCache)
  267. this->_entryStartBeatCache = std::round (this->_entryStartBeatCache);
  268. }
  269. void updateCacheByBeatPosition (const double beatPosition) const noexcept
  270. {
  271. bool didUpdateEntryStartBeatCache { false };
  272. if (beatPosition < this->_entryStartBeatCache)
  273. {
  274. // before our entry - go back until first entry or entry before beat
  275. while (this->_entryCache != this->_contentReader.begin ())
  276. {
  277. const auto prevEndQuarter { this->_entryCache->position };
  278. --this->_entryCache;
  279. this->_entryStartBeatCache -= BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, prevEndQuarter);
  280. didUpdateEntryStartBeatCache = true;
  281. if (this->_entryStartBeatCache <= beatPosition)
  282. break;
  283. }
  284. }
  285. else
  286. {
  287. // at or after our entry - go forward until last entry or entry before beat
  288. while (true)
  289. {
  290. const auto next { std::next (this->_entryCache) };
  291. if (next == this->_contentReader.end ())
  292. break;
  293. const auto nextStartBeat { this->_entryStartBeatCache + BarSignaturesConverter::getBeatDistanceFromQuarterPosition (this->_entryCache, next->position) };
  294. if (nextStartBeat > beatPosition)
  295. break;
  296. this->_entryStartBeatCache = nextStartBeat;
  297. didUpdateEntryStartBeatCache = true;
  298. this->_entryCache = next;
  299. }
  300. }
  301. // to avoid errors adding up over time, we round the cache to an integer value after modification
  302. if (didUpdateEntryStartBeatCache)
  303. this->_entryStartBeatCache = std::round (this->_entryStartBeatCache);
  304. }
  305. private:
  306. const BarSignaturesContentReader& _contentReader;
  307. mutable typename BarSignaturesContentReader::const_iterator _entryCache;
  308. mutable double _entryStartBeatCache { 0.0 };
  309. };
  310. //! @} ARA_Library_Utility_Timeline_Conversion
  311. } // namespace ARA
  312. #endif // ARATimelineConversion_h