Collection of DPF-based plugins for packaging
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.

128 lines
4.2KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_TIME_HPP_INCLUDED
  17. #define DISTRHO_TIME_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #ifdef DISTRHO_OS_WINDOWS
  20. # include <winsock2.h>
  21. # include <windows.h>
  22. # include <mmsystem.h>
  23. #else
  24. # include <ctime>
  25. #endif
  26. START_NAMESPACE_DISTRHO
  27. // -----------------------------------------------------------------------------------------------------------
  28. // d_gettime_*
  29. /*
  30. * Get a monotonically-increasing time in milliseconds.
  31. */
  32. static inline
  33. uint32_t d_gettime_ms() noexcept
  34. {
  35. #if defined(DISTRHO_OS_MAC)
  36. static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
  37. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s;
  38. #elif defined(DISTRHO_OS_WINDOWS)
  39. return static_cast<uint32_t>(timeGetTime());
  40. #else
  41. static struct {
  42. timespec ts;
  43. int r;
  44. uint32_t ms;
  45. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint32_t>(s.ts.tv_sec * 1000 +
  46. s.ts.tv_nsec / 1000000) };
  47. timespec ts;
  48. clock_gettime(CLOCK_MONOTONIC, &ts);
  49. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms;
  50. #endif
  51. }
  52. /*
  53. * Get a monotonically-increasing time in microseconds.
  54. */
  55. static inline
  56. uint64_t d_gettime_us() noexcept
  57. {
  58. #if defined(DISTRHO_OS_MAC)
  59. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
  60. return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s;
  61. #elif defined(DISTRHO_OS_WINDOWS)
  62. static struct {
  63. LARGE_INTEGER freq;
  64. LARGE_INTEGER counter;
  65. BOOL r1, r2;
  66. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  67. LARGE_INTEGER counter;
  68. QueryPerformanceCounter(&counter);
  69. return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart;
  70. #else
  71. static struct {
  72. timespec ts;
  73. int r;
  74. uint64_t us;
  75. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000 +
  76. s.ts.tv_nsec / 1000) };
  77. timespec ts;
  78. clock_gettime(CLOCK_MONOTONIC, &ts);
  79. return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us;
  80. #endif
  81. }
  82. /*
  83. * Get a monotonically-increasing time in nanoseconds.
  84. */
  85. static inline
  86. uint64_t d_gettime_ns() noexcept
  87. {
  88. #if defined(DISTRHO_OS_MAC)
  89. static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
  90. return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s;
  91. #elif defined(DISTRHO_OS_WINDOWS)
  92. static struct {
  93. LARGE_INTEGER freq;
  94. LARGE_INTEGER counter;
  95. BOOL r1, r2;
  96. } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
  97. LARGE_INTEGER counter;
  98. QueryPerformanceCounter(&counter);
  99. return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart;
  100. #else
  101. static struct {
  102. timespec ts;
  103. int r;
  104. uint64_t ns;
  105. } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000000ULL +
  106. s.ts.tv_nsec) };
  107. timespec ts;
  108. clock_gettime(CLOCK_MONOTONIC, &ts);
  109. return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns;
  110. #endif
  111. }
  112. // -----------------------------------------------------------------------------------------------------------
  113. END_NAMESPACE_DISTRHO
  114. #endif // DISTRHO_TIME_HPP_INCLUDED