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.

CarlaTimeUtils.hpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Carla time utils
  3. * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_TIME_UTILS_HPP_INCLUDED
  18. #define CARLA_TIME_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <ctime>
  21. #ifdef CARLA_OS_WIN
  22. # include <mmsystem.h>
  23. #endif
  24. // --------------------------------------------------------------------------------------------------------------------
  25. // carla_*sleep
  26. /*
  27. * Sleep for 'secs' seconds.
  28. */
  29. static inline
  30. void carla_sleep(const uint secs) noexcept
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  33. try {
  34. #ifdef CARLA_OS_WIN
  35. ::Sleep(secs * 1000);
  36. #else
  37. ::sleep(secs);
  38. #endif
  39. } CARLA_SAFE_EXCEPTION("carla_sleep");
  40. }
  41. /*
  42. * Sleep for 'msecs' milliseconds.
  43. */
  44. static inline
  45. void carla_msleep(const uint msecs) noexcept
  46. {
  47. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  48. try {
  49. #ifdef CARLA_OS_WIN
  50. ::Sleep(msecs);
  51. #else
  52. ::usleep(msecs * 1000);
  53. #endif
  54. } CARLA_SAFE_EXCEPTION("carla_msleep");
  55. }
  56. // --------------------------------------------------------------------------------------------------------------------
  57. // carla_gettime_*
  58. /*
  59. * Get a monotonically-increasing time in milliseconds.
  60. */
  61. static inline
  62. uint32_t carla_gettime_ms() noexcept
  63. {
  64. #if defined(CARLA_OS_MAC)
  65. static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
  66. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s;
  67. #elif defined(CARLA_OS_WIN)
  68. return static_cast<uint32_t>(timeGetTime());
  69. #else
  70. static struct {
  71. timespec ts;
  72. int r;
  73. uint32_t ms;
  74. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint32_t>(s.ts.tv_sec * 1000 +
  75. s.ts.tv_nsec / 1000000) };
  76. timespec ts;
  77. clock_gettime(CLOCK_MONOTONIC, &ts);
  78. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms;
  79. #endif
  80. }
  81. /*
  82. * Get a monotonically-increasing time in microseconds.
  83. */
  84. static inline
  85. uint64_t carla_gettime_us() noexcept
  86. {
  87. #if defined(CARLA_OS_MAC)
  88. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
  89. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s;
  90. #elif defined(CARLA_OS_WIN)
  91. static struct {
  92. LARGE_INTEGER freq;
  93. LARGE_INTEGER counter;
  94. BOOL r1, r2;
  95. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  96. LARGE_INTEGER counter;
  97. QueryPerformanceCounter(&counter);
  98. return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart;
  99. #else
  100. static struct {
  101. timespec ts;
  102. int r;
  103. uint64_t us;
  104. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000 +
  105. s.ts.tv_nsec / 1000) };
  106. timespec ts;
  107. clock_gettime(CLOCK_MONOTONIC, &ts);
  108. return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us;
  109. #endif
  110. }
  111. /*
  112. * Get a monotonically-increasing time in nanoseconds.
  113. */
  114. static inline
  115. uint64_t carla_gettime_ns() noexcept
  116. {
  117. #if defined(CARLA_OS_MAC)
  118. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  119. return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s;
  120. #elif defined(CARLA_OS_WIN)
  121. static struct {
  122. LARGE_INTEGER freq;
  123. LARGE_INTEGER counter;
  124. BOOL r1, r2;
  125. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  126. LARGE_INTEGER counter;
  127. QueryPerformanceCounter(&counter);
  128. return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart;
  129. #else
  130. static struct {
  131. timespec ts;
  132. int r;
  133. uint64_t ns;
  134. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000000ULL +
  135. s.ts.tv_nsec) };
  136. timespec ts;
  137. clock_gettime(CLOCK_MONOTONIC, &ts);
  138. return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns;
  139. #endif
  140. }
  141. // --------------------------------------------------------------------------------------------------------------------
  142. #endif // CARLA_TIME_UTILS_HPP_INCLUDED