Link.ipp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/link/Phase.hpp>
  21. namespace ableton
  22. {
  23. namespace detail
  24. {
  25. template <typename Clock>
  26. inline typename BasicLink<Clock>::SessionState toSessionState(
  27. const link::ClientState& state, const bool isConnected)
  28. {
  29. return {{state.timeline,
  30. state.timelineSessionId,
  31. {state.startStopState.isPlaying, state.startStopState.time}},
  32. isConnected};
  33. }
  34. inline link::IncomingClientState toIncomingClientState(
  35. const link::ApiState& state,
  36. const link::ApiState& originalState,
  37. const std::chrono::microseconds timestamp)
  38. {
  39. const auto timeline = originalState.timeline != state.timeline
  40. ? link::OptionalTimeline{state.timeline}
  41. : link::OptionalTimeline{};
  42. const auto startStopState =
  43. originalState.startStopState != state.startStopState
  44. ? link::OptionalClientStartStopState{{state.startStopState.isPlaying,
  45. state.startStopState.time,
  46. timestamp}}
  47. : link::OptionalClientStartStopState{};
  48. return {timeline, startStopState, timestamp};
  49. }
  50. } // namespace detail
  51. template <typename Clock>
  52. inline BasicLink<Clock>::BasicLink(const double bpm)
  53. : mController(
  54. link::Tempo(bpm),
  55. [this](const std::size_t peers)
  56. {
  57. std::lock_guard<std::mutex> lock(mCallbackMutex);
  58. mPeerCountCallback(peers);
  59. },
  60. [this](const link::Tempo tempo)
  61. {
  62. std::lock_guard<std::mutex> lock(mCallbackMutex);
  63. mTempoCallback(tempo);
  64. },
  65. [this](const bool isPlaying)
  66. {
  67. std::lock_guard<std::mutex> lock(mCallbackMutex);
  68. mStartStopCallback(isPlaying);
  69. },
  70. mClock)
  71. {
  72. }
  73. template <typename Clock>
  74. inline bool BasicLink<Clock>::isEnabled() const
  75. {
  76. return mController.isEnabled();
  77. }
  78. template <typename Clock>
  79. inline void BasicLink<Clock>::enable(const bool bEnable)
  80. {
  81. mController.enable(bEnable);
  82. }
  83. template <typename Clock>
  84. inline bool BasicLink<Clock>::isStartStopSyncEnabled() const
  85. {
  86. return mController.isStartStopSyncEnabled();
  87. }
  88. template <typename Clock>
  89. inline void BasicLink<Clock>::enableStartStopSync(bool bEnable)
  90. {
  91. mController.enableStartStopSync(bEnable);
  92. }
  93. template <typename Clock>
  94. inline std::size_t BasicLink<Clock>::numPeers() const
  95. {
  96. return mController.numPeers();
  97. }
  98. template <typename Clock>
  99. template <typename Callback>
  100. void BasicLink<Clock>::setNumPeersCallback(Callback callback)
  101. {
  102. std::lock_guard<std::mutex> lock(mCallbackMutex);
  103. mPeerCountCallback = [callback](const std::size_t numPeers) { callback(numPeers); };
  104. }
  105. template <typename Clock>
  106. template <typename Callback>
  107. void BasicLink<Clock>::setTempoCallback(Callback callback)
  108. {
  109. std::lock_guard<std::mutex> lock(mCallbackMutex);
  110. mTempoCallback = [callback](const link::Tempo tempo) { callback(tempo.bpm()); };
  111. }
  112. template <typename Clock>
  113. template <typename Callback>
  114. void BasicLink<Clock>::setStartStopCallback(Callback callback)
  115. {
  116. std::lock_guard<std::mutex> lock(mCallbackMutex);
  117. mStartStopCallback = callback;
  118. }
  119. template <typename Clock>
  120. inline Clock BasicLink<Clock>::clock() const
  121. {
  122. return mClock;
  123. }
  124. template <typename Clock>
  125. inline typename BasicLink<Clock>::SessionState BasicLink<
  126. Clock>::captureAudioSessionState() const
  127. {
  128. return detail::toSessionState<Clock>(mController.clientStateRtSafe(), numPeers() > 0);
  129. }
  130. template <typename Clock>
  131. inline void BasicLink<Clock>::commitAudioSessionState(
  132. const typename BasicLink<Clock>::SessionState state)
  133. {
  134. mController.setClientStateRtSafe(
  135. detail::toIncomingClientState(state.mState, state.mOriginalState, mClock.micros()));
  136. }
  137. template <typename Clock>
  138. inline typename BasicLink<Clock>::SessionState BasicLink<Clock>::captureAppSessionState()
  139. const
  140. {
  141. return detail::toSessionState<Clock>(mController.clientState(), numPeers() > 0);
  142. }
  143. template <typename Clock>
  144. inline void BasicLink<Clock>::commitAppSessionState(
  145. const typename BasicLink<Clock>::SessionState state)
  146. {
  147. mController.setClientState(
  148. detail::toIncomingClientState(state.mState, state.mOriginalState, mClock.micros()));
  149. }
  150. // Link::SessionState
  151. template <typename Clock>
  152. inline BasicLink<Clock>::SessionState::SessionState(const link::ApiState state,
  153. const bool bRespectQuantum)
  154. : mOriginalState(state)
  155. , mState(state)
  156. , mbRespectQuantum(bRespectQuantum)
  157. {
  158. }
  159. template <typename Clock>
  160. inline bool BasicLink<Clock>::SessionState::operator==(const SessionState& other) const
  161. {
  162. return std::tie(mOriginalState, mState, mbRespectQuantum)
  163. == std::tie(other.mOriginalState, other.mState, other.mbRespectQuantum);
  164. }
  165. template <typename Clock>
  166. inline bool BasicLink<Clock>::SessionState::operator!=(const SessionState& other) const
  167. {
  168. return !(operator==(other));
  169. }
  170. template <typename Clock>
  171. inline double BasicLink<Clock>::SessionState::tempo() const
  172. {
  173. return mState.timeline.tempo.bpm();
  174. }
  175. template <typename Clock>
  176. inline void BasicLink<Clock>::SessionState::setTempo(
  177. const double bpm, const std::chrono::microseconds atTime)
  178. {
  179. const auto desiredTl = link::clampTempo(
  180. link::Timeline{link::Tempo(bpm), mState.timeline.toBeats(atTime), atTime});
  181. mState.timeline.tempo = desiredTl.tempo;
  182. mState.timeline.timeOrigin = desiredTl.fromBeats(mState.timeline.beatOrigin);
  183. }
  184. template <typename Clock>
  185. inline double BasicLink<Clock>::SessionState::beatAtTime(
  186. const std::chrono::microseconds time, const double quantum) const
  187. {
  188. return link::toPhaseEncodedBeats(mState.timeline, time, link::Beats{quantum})
  189. .floating();
  190. }
  191. template <typename Clock>
  192. inline double BasicLink<Clock>::SessionState::phaseAtTime(
  193. const std::chrono::microseconds time, const double quantum) const
  194. {
  195. return link::phase(link::Beats{beatAtTime(time, quantum)}, link::Beats{quantum})
  196. .floating();
  197. }
  198. template <typename Clock>
  199. inline std::chrono::microseconds BasicLink<Clock>::SessionState::timeAtBeat(
  200. const double beat, const double quantum) const
  201. {
  202. return link::fromPhaseEncodedBeats(
  203. mState.timeline, link::Beats{beat}, link::Beats{quantum});
  204. }
  205. template <typename Clock>
  206. inline void BasicLink<Clock>::SessionState::requestBeatAtTime(
  207. const double beat, std::chrono::microseconds time, const double quantum)
  208. {
  209. if (mbRespectQuantum)
  210. {
  211. time = timeAtBeat(
  212. link::nextPhaseMatch(
  213. link::Beats{beatAtTime(time, quantum)}, link::Beats{beat}, link::Beats{quantum})
  214. .floating(),
  215. quantum);
  216. }
  217. forceBeatAtTime(beat, time, quantum);
  218. }
  219. inline void forceBeatAtTimeImpl(link::Timeline& timeline,
  220. const link::Beats beat,
  221. const std::chrono::microseconds time,
  222. const link::Beats quantum)
  223. {
  224. // There are two components to the beat adjustment: a phase shift
  225. // and a beat magnitude adjustment.
  226. const auto curBeatAtTime = link::toPhaseEncodedBeats(timeline, time, quantum);
  227. const auto closestInPhase = link::closestPhaseMatch(curBeatAtTime, beat, quantum);
  228. timeline = shiftClientTimeline(timeline, closestInPhase - curBeatAtTime);
  229. // Now adjust the magnitude
  230. timeline.beatOrigin = timeline.beatOrigin + beat - closestInPhase;
  231. }
  232. template <typename Clock>
  233. inline void BasicLink<Clock>::SessionState::forceBeatAtTime(
  234. const double beat, std::chrono::microseconds time, const double quantum)
  235. {
  236. forceBeatAtTimeImpl(mState.timeline, link::Beats{beat}, time, link::Beats{quantum});
  237. // Due to quantization errors the resulting BeatTime at 'time' after forcing can be
  238. // bigger than 'beat' which then violates intended behavior of the API and can lead
  239. // i.e. to missing a downbeat. Thus we have to shift the timeline forwards.
  240. if (beatAtTime(time, quantum) > beat)
  241. {
  242. forceBeatAtTimeImpl(mState.timeline, link::Beats{beat}, ++time, link::Beats{quantum});
  243. }
  244. }
  245. template <typename Clock>
  246. inline void BasicLink<Clock>::SessionState::setIsPlaying(
  247. const bool isPlaying, const std::chrono::microseconds time)
  248. {
  249. mState.startStopState = {isPlaying, time};
  250. }
  251. template <typename Clock>
  252. inline bool BasicLink<Clock>::SessionState::isPlaying() const
  253. {
  254. return mState.startStopState.isPlaying;
  255. }
  256. template <typename Clock>
  257. inline std::chrono::microseconds BasicLink<Clock>::SessionState::timeForIsPlaying() const
  258. {
  259. return mState.startStopState.time;
  260. }
  261. template <typename Clock>
  262. inline void BasicLink<Clock>::SessionState::requestBeatAtStartPlayingTime(
  263. const double beat, const double quantum)
  264. {
  265. if (isPlaying())
  266. {
  267. requestBeatAtTime(beat, mState.startStopState.time, quantum);
  268. }
  269. }
  270. template <typename Clock>
  271. inline void BasicLink<Clock>::SessionState::setIsPlayingAndRequestBeatAtTime(
  272. bool isPlaying, std::chrono::microseconds time, double beat, double quantum)
  273. {
  274. mState.startStopState = {isPlaying, time};
  275. requestBeatAtStartPlayingTime(beat, quantum);
  276. }
  277. } // namespace ableton