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.

101 lines
3.9KB

  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/Beats.hpp>
  21. #include <ableton/link/Timeline.hpp>
  22. #include <chrono>
  23. namespace ableton
  24. {
  25. namespace link
  26. {
  27. // Returns a value in the range [0,quantum) corresponding to beats %
  28. // quantum except that negative beat values are handled correctly.
  29. // If the given quantum is zero, returns zero.
  30. inline Beats phase(const Beats beats, const Beats quantum)
  31. {
  32. if (quantum == Beats{INT64_C(0)})
  33. {
  34. return Beats{INT64_C(0)};
  35. }
  36. else
  37. {
  38. // Handle negative beat values by doing the computation relative to an
  39. // origin that is on the nearest quantum boundary less than -(abs(x))
  40. const auto quantumMicros = quantum.microBeats();
  41. const auto quantumBins = (llabs(beats.microBeats()) + quantumMicros) / quantumMicros;
  42. const std::int64_t quantumBeats{quantumBins * quantumMicros};
  43. return (beats + Beats{quantumBeats}) % quantum;
  44. }
  45. }
  46. // Return the least value greater than x that matches the phase of
  47. // target with respect to the given quantum. If the given quantum
  48. // quantum is 0, x is returned.
  49. inline Beats nextPhaseMatch(const Beats x, const Beats target, const Beats quantum)
  50. {
  51. const auto desiredPhase = phase(target, quantum);
  52. const auto xPhase = phase(x, quantum);
  53. const auto phaseDiff = (desiredPhase - xPhase + quantum) % quantum;
  54. return x + phaseDiff;
  55. }
  56. // Return the closest value to x that matches the phase of the target
  57. // with respect to the given quantum. The result deviates from x by at
  58. // most quantum/2, but may be less than x.
  59. inline Beats closestPhaseMatch(const Beats x, const Beats target, const Beats quantum)
  60. {
  61. return nextPhaseMatch(x - Beats{0.5 * quantum.floating()}, target, quantum);
  62. }
  63. // Interprets the given timeline as encoding a quantum boundary at its
  64. // origin. Given such a timeline, returns a phase-encoded beat value
  65. // relative to the given quantum that corresponds to the given
  66. // time. The phase of the resulting beat value can be calculated with
  67. // phase(beats, quantum). The result will deviate by up to +-
  68. // (quantum/2) beats compared to the result of tl.toBeats(time).
  69. inline Beats toPhaseEncodedBeats(
  70. const Timeline& tl, const std::chrono::microseconds time, const Beats quantum)
  71. {
  72. const auto beat = tl.toBeats(time);
  73. return closestPhaseMatch(beat, beat - tl.beatOrigin, quantum);
  74. }
  75. // The inverse of toPhaseEncodedBeats. Given a phase encoded beat
  76. // value from the given timeline and quantum, find the time value that
  77. // it maps to.
  78. inline std::chrono::microseconds fromPhaseEncodedBeats(
  79. const Timeline& tl, const Beats beat, const Beats quantum)
  80. {
  81. const auto fromOrigin = beat - tl.beatOrigin;
  82. const auto originOffset = fromOrigin - phase(fromOrigin, quantum);
  83. // invert the phase calculation so that it always rounds up in the
  84. // middle instead of down like closestPhaseMatch. Otherwise we'll
  85. // end up rounding down twice when a value is at phase quantum/2.
  86. const auto inversePhaseOffset = closestPhaseMatch(
  87. quantum - phase(fromOrigin, quantum), quantum - phase(beat, quantum), quantum);
  88. return tl.fromBeats(tl.beatOrigin + originOffset + quantum - inversePhaseOffset);
  89. }
  90. } // link
  91. } // ableton