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.

170 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2023 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "Time.h"
  21. #include "../memory/Atomic.h"
  22. #include <ctime>
  23. #if defined(CARLA_OS_MAC)
  24. # include <mach/mach_time.h>
  25. #elif defined(CARLA_OS_WIN)
  26. # include <mmsystem.h>
  27. #endif
  28. #ifdef _MSC_VER
  29. # ifndef NOMINMAX
  30. # define NOMINMAX
  31. # endif
  32. # define WIN32_LEAN_AND_MEAN 1
  33. # include <winsock2.h>
  34. # include <windows.h>
  35. #else
  36. # include <sys/time.h>
  37. #endif
  38. namespace water {
  39. namespace TimeHelpers
  40. {
  41. static Atomic<uint32> lastMSCounterValue;
  42. #ifdef CARLA_OS_MAC
  43. /* NB: these are kept outside the HiResCounterInfo struct and initialised to 1 to avoid
  44. division-by-zero errors if some other static constructor calls us before this file's
  45. static constructors have had a chance to fill them in correctly..
  46. */
  47. static uint64 hiResCounterNumerator = 0, hiResCounterDenominator = 1;
  48. struct HiResCounterInfo {
  49. HiResCounterInfo()
  50. {
  51. mach_timebase_info_data_t timebase;
  52. (void) mach_timebase_info (&timebase);
  53. if (timebase.numer % 1000000 == 0)
  54. {
  55. hiResCounterNumerator = timebase.numer / 1000000;
  56. hiResCounterDenominator = timebase.denom;
  57. }
  58. else
  59. {
  60. hiResCounterNumerator = timebase.numer;
  61. hiResCounterDenominator = timebase.denom * (uint64) 1000000;
  62. }
  63. }
  64. };
  65. static HiResCounterInfo hiResCounterInfo;
  66. #endif
  67. }
  68. //==============================================================================
  69. static uint32 water_millisecondsSinceStartup() noexcept
  70. {
  71. #if defined(CARLA_OS_MAC)
  72. return (uint32) ((mach_absolute_time() * TimeHelpers::hiResCounterNumerator) / TimeHelpers::hiResCounterDenominator);
  73. #elif defined(CARLA_OS_WIN)
  74. return (uint32) timeGetTime();
  75. #else
  76. timespec t;
  77. #ifdef CLOCK_MONOTONIC_RAW
  78. clock_gettime (CLOCK_MONOTONIC_RAW, &t);
  79. #else
  80. clock_gettime (CLOCK_MONOTONIC, &t);
  81. #endif
  82. return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
  83. #endif
  84. }
  85. //==============================================================================
  86. Time::Time() noexcept : millisSinceEpoch (0)
  87. {
  88. }
  89. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  90. {
  91. }
  92. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  93. {
  94. }
  95. Time::~Time() noexcept
  96. {
  97. }
  98. Time& Time::operator= (const Time& other) noexcept
  99. {
  100. millisSinceEpoch = other.millisSinceEpoch;
  101. return *this;
  102. }
  103. //==============================================================================
  104. Time Time::getCurrentTime() noexcept
  105. {
  106. return Time (currentTimeMillis());
  107. }
  108. int64 Time::currentTimeMillis() noexcept
  109. {
  110. #ifdef _MSC_VER
  111. struct _timeb t;
  112. _ftime_s (&t);
  113. return ((int64) t.time) * 1000 + t.millitm;
  114. #else
  115. struct timeval tv;
  116. gettimeofday (&tv, nullptr);
  117. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  118. #endif
  119. }
  120. //==============================================================================
  121. uint32 Time::getMillisecondCounter() noexcept
  122. {
  123. const uint32 now = water_millisecondsSinceStartup();
  124. if (now < TimeHelpers::lastMSCounterValue.get())
  125. {
  126. // in multi-threaded apps this might be called concurrently, so
  127. // make sure that our last counter value only increases and doesn't
  128. // go backwards..
  129. if (now < TimeHelpers::lastMSCounterValue.get() - 1000U)
  130. TimeHelpers::lastMSCounterValue = now;
  131. }
  132. else
  133. {
  134. TimeHelpers::lastMSCounterValue = now;
  135. }
  136. return now;
  137. }
  138. }