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.

104 lines
2.6KB

  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/discovery/NetworkByteStreamSerializable.hpp>
  21. #include <cmath>
  22. #include <cstdint>
  23. #include <tuple>
  24. namespace ableton
  25. {
  26. namespace link
  27. {
  28. struct Beats : std::tuple<std::int64_t>
  29. {
  30. Beats() = default;
  31. explicit Beats(const double beats)
  32. : std::tuple<std::int64_t>(llround(beats * 1e6))
  33. {
  34. }
  35. explicit Beats(const std::int64_t microBeats)
  36. : std::tuple<std::int64_t>(microBeats)
  37. {
  38. }
  39. double floating() const
  40. {
  41. return microBeats() / 1e6;
  42. }
  43. std::int64_t microBeats() const
  44. {
  45. return std::get<0>(*this);
  46. }
  47. Beats operator-() const
  48. {
  49. return Beats{-microBeats()};
  50. }
  51. friend Beats abs(const Beats b)
  52. {
  53. return Beats{std::abs(b.microBeats())};
  54. }
  55. friend Beats operator+(const Beats lhs, const Beats rhs)
  56. {
  57. return Beats{lhs.microBeats() + rhs.microBeats()};
  58. }
  59. friend Beats operator-(const Beats lhs, const Beats rhs)
  60. {
  61. return Beats{lhs.microBeats() - rhs.microBeats()};
  62. }
  63. friend Beats operator%(const Beats lhs, const Beats rhs)
  64. {
  65. return rhs == Beats{0.} ? Beats{0.} : Beats{lhs.microBeats() % rhs.microBeats()};
  66. }
  67. // Model the NetworkByteStreamSerializable concept
  68. friend std::uint32_t sizeInByteStream(const Beats beats)
  69. {
  70. return discovery::sizeInByteStream(beats.microBeats());
  71. }
  72. template <typename It>
  73. friend It toNetworkByteStream(const Beats beats, It out)
  74. {
  75. return discovery::toNetworkByteStream(beats.microBeats(), std::move(out));
  76. }
  77. template <typename It>
  78. static std::pair<Beats, It> fromNetworkByteStream(It begin, It end)
  79. {
  80. auto result = discovery::Deserialize<std::int64_t>::fromNetworkByteStream(
  81. std::move(begin), std::move(end));
  82. return std::make_pair(Beats{result.first}, std::move(result.second));
  83. }
  84. };
  85. } // namespace link
  86. } // namespace ableton