Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.8KB

  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. template <typename Clock>
  24. inline BasicLink<Clock>::BasicLink(const double bpm)
  25. : mPeerCountCallback([](std::size_t) {})
  26. , mTempoCallback([](link::Tempo) {})
  27. , mController(link::Tempo(bpm),
  28. [this](const std::size_t peers) {
  29. std::lock_guard<std::mutex> lock(mCallbackMutex);
  30. mPeerCountCallback(peers);
  31. },
  32. [this](const link::Tempo tempo) {
  33. std::lock_guard<std::mutex> lock(mCallbackMutex);
  34. mTempoCallback(tempo);
  35. },
  36. mClock,
  37. util::injectVal(link::platform::IoContext{}))
  38. {
  39. }
  40. template <typename Clock>
  41. inline bool BasicLink<Clock>::isEnabled() const
  42. {
  43. return mController.isEnabled();
  44. }
  45. template <typename Clock>
  46. inline void BasicLink<Clock>::enable(const bool bEnable)
  47. {
  48. mController.enable(bEnable);
  49. }
  50. template <typename Clock>
  51. inline std::size_t BasicLink<Clock>::numPeers() const
  52. {
  53. return mController.numPeers();
  54. }
  55. template <typename Clock>
  56. template <typename Callback>
  57. void BasicLink<Clock>::setNumPeersCallback(Callback callback)
  58. {
  59. std::lock_guard<std::mutex> lock(mCallbackMutex);
  60. mPeerCountCallback = [callback](const std::size_t numPeers) { callback(numPeers); };
  61. }
  62. template <typename Clock>
  63. template <typename Callback>
  64. void BasicLink<Clock>::setTempoCallback(Callback callback)
  65. {
  66. std::lock_guard<std::mutex> lock(mCallbackMutex);
  67. mTempoCallback = [callback](const link::Tempo tempo) { callback(tempo.bpm()); };
  68. }
  69. template <typename Clock>
  70. inline Clock BasicLink<Clock>::clock() const
  71. {
  72. return mClock;
  73. }
  74. template <typename Clock>
  75. inline typename BasicLink<Clock>::Timeline BasicLink<Clock>::captureAudioTimeline() const
  76. {
  77. return BasicLink<Clock>::Timeline{mController.timelineRtSafe(), numPeers() > 0};
  78. }
  79. template <typename Clock>
  80. inline void BasicLink<Clock>::commitAudioTimeline(const Timeline timeline)
  81. {
  82. if (timeline.mOriginalTimeline != timeline.mTimeline)
  83. {
  84. mController.setTimelineRtSafe(timeline.mTimeline, mClock.micros());
  85. }
  86. }
  87. template <typename Clock>
  88. inline typename BasicLink<Clock>::Timeline BasicLink<Clock>::captureAppTimeline() const
  89. {
  90. return Timeline{mController.timeline(), numPeers() > 0};
  91. }
  92. template <typename Clock>
  93. inline void BasicLink<Clock>::commitAppTimeline(const Timeline timeline)
  94. {
  95. if (timeline.mOriginalTimeline != timeline.mTimeline)
  96. {
  97. mController.setTimeline(timeline.mTimeline, mClock.micros());
  98. }
  99. }
  100. ////////////////////
  101. // Link::Timeline //
  102. ////////////////////
  103. template <typename Clock>
  104. inline BasicLink<Clock>::Timeline::Timeline(
  105. const link::Timeline timeline, const bool bRespectQuantum)
  106. : mOriginalTimeline(timeline)
  107. , mbRespectQuantum(bRespectQuantum)
  108. , mTimeline(timeline)
  109. {
  110. }
  111. template <typename Clock>
  112. inline double BasicLink<Clock>::Timeline::tempo() const
  113. {
  114. return mTimeline.tempo.bpm();
  115. }
  116. template <typename Clock>
  117. inline void BasicLink<Clock>::Timeline::setTempo(
  118. const double bpm, const std::chrono::microseconds atTime)
  119. {
  120. const auto desiredTl =
  121. link::clampTempo(link::Timeline{link::Tempo(bpm), mTimeline.toBeats(atTime), atTime});
  122. mTimeline.tempo = desiredTl.tempo;
  123. mTimeline.timeOrigin = desiredTl.fromBeats(mTimeline.beatOrigin);
  124. }
  125. template <typename Clock>
  126. inline double BasicLink<Clock>::Timeline::beatAtTime(
  127. const std::chrono::microseconds time, const double quantum) const
  128. {
  129. return link::toPhaseEncodedBeats(mTimeline, time, link::Beats{quantum}).floating();
  130. }
  131. template <typename Clock>
  132. inline double BasicLink<Clock>::Timeline::phaseAtTime(
  133. const std::chrono::microseconds time, const double quantum) const
  134. {
  135. return link::phase(link::Beats{beatAtTime(time, quantum)}, link::Beats{quantum})
  136. .floating();
  137. }
  138. template <typename Clock>
  139. inline std::chrono::microseconds BasicLink<Clock>::Timeline::timeAtBeat(
  140. const double beat, const double quantum) const
  141. {
  142. return link::fromPhaseEncodedBeats(mTimeline, link::Beats{beat}, link::Beats{quantum});
  143. }
  144. template <typename Clock>
  145. inline void BasicLink<Clock>::Timeline::requestBeatAtTime(
  146. const double beat, std::chrono::microseconds time, const double quantum)
  147. {
  148. if (mbRespectQuantum)
  149. {
  150. time = timeAtBeat(link::nextPhaseMatch(link::Beats{beatAtTime(time, quantum)},
  151. link::Beats{beat}, link::Beats{quantum})
  152. .floating(),
  153. quantum);
  154. }
  155. forceBeatAtTime(beat, time, quantum);
  156. }
  157. template <typename Clock>
  158. inline void BasicLink<Clock>::Timeline::forceBeatAtTime(
  159. const double beat, const std::chrono::microseconds time, const double quantum)
  160. {
  161. // There are two components to the beat adjustment: a phase shift
  162. // and a beat magnitude adjustment.
  163. const auto curBeatAtTime = link::Beats{beatAtTime(time, quantum)};
  164. const auto closestInPhase =
  165. link::closestPhaseMatch(curBeatAtTime, link::Beats{beat}, link::Beats{quantum});
  166. mTimeline = shiftClientTimeline(mTimeline, closestInPhase - curBeatAtTime);
  167. // Now adjust the magnitude
  168. mTimeline.beatOrigin = mTimeline.beatOrigin + (link::Beats{beat} - closestInPhase);
  169. }
  170. } // ableton