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.

71 lines
1.7KB

  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 <chrono>
  21. #include <mach/mach_time.h>
  22. namespace ableton
  23. {
  24. namespace platforms
  25. {
  26. namespace darwin
  27. {
  28. struct Clock
  29. {
  30. using Ticks = std::uint64_t;
  31. using Micros = std::chrono::microseconds;
  32. Clock()
  33. {
  34. mach_timebase_info_data_t timeInfo;
  35. mach_timebase_info(&timeInfo);
  36. // numer / denom gives nanoseconds, we want microseconds
  37. mTicksToMicros = timeInfo.numer / (timeInfo.denom * 1000.);
  38. }
  39. Micros ticksToMicros(const Ticks ticks) const
  40. {
  41. return Micros{llround(mTicksToMicros * ticks)};
  42. }
  43. Ticks microsToTicks(const Micros micros) const
  44. {
  45. return static_cast<Ticks>(micros.count() / mTicksToMicros);
  46. }
  47. Ticks ticks() const
  48. {
  49. return mach_absolute_time();
  50. }
  51. std::chrono::microseconds micros() const
  52. {
  53. return ticksToMicros(ticks());
  54. }
  55. double mTicksToMicros;
  56. };
  57. } // namespace darwin
  58. } // namespace platforms
  59. } // namespace ableton