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.

163 lines
4.6KB

  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. #ifndef _MSC_VER
  29. # include <sys/time.h>
  30. #endif
  31. namespace water {
  32. namespace TimeHelpers
  33. {
  34. static Atomic<uint32> lastMSCounterValue;
  35. #ifdef CARLA_OS_MAC
  36. /* NB: these are kept outside the HiResCounterInfo struct and initialised to 1 to avoid
  37. division-by-zero errors if some other static constructor calls us before this file's
  38. static constructors have had a chance to fill them in correctly..
  39. */
  40. static uint64 hiResCounterNumerator = 0, hiResCounterDenominator = 1;
  41. struct HiResCounterInfo {
  42. HiResCounterInfo()
  43. {
  44. mach_timebase_info_data_t timebase;
  45. (void) mach_timebase_info (&timebase);
  46. if (timebase.numer % 1000000 == 0)
  47. {
  48. hiResCounterNumerator = timebase.numer / 1000000;
  49. hiResCounterDenominator = timebase.denom;
  50. }
  51. else
  52. {
  53. hiResCounterNumerator = timebase.numer;
  54. hiResCounterDenominator = timebase.denom * (uint64) 1000000;
  55. }
  56. }
  57. };
  58. static HiResCounterInfo hiResCounterInfo;
  59. #endif
  60. }
  61. //==============================================================================
  62. static uint32 water_millisecondsSinceStartup() noexcept
  63. {
  64. #if defined(CARLA_OS_MAC)
  65. return (uint32) ((mach_absolute_time() * TimeHelpers::hiResCounterNumerator) / TimeHelpers::hiResCounterDenominator);
  66. #elif defined(CARLA_OS_WIN)
  67. return (uint32) timeGetTime();
  68. #else
  69. timespec t;
  70. #ifdef CLOCK_MONOTONIC_RAW
  71. clock_gettime (CLOCK_MONOTONIC_RAW, &t);
  72. #else
  73. clock_gettime (CLOCK_MONOTONIC, &t);
  74. #endif
  75. return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
  76. #endif
  77. }
  78. //==============================================================================
  79. Time::Time() noexcept : millisSinceEpoch (0)
  80. {
  81. }
  82. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  83. {
  84. }
  85. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  86. {
  87. }
  88. Time::~Time() noexcept
  89. {
  90. }
  91. Time& Time::operator= (const Time& other) noexcept
  92. {
  93. millisSinceEpoch = other.millisSinceEpoch;
  94. return *this;
  95. }
  96. //==============================================================================
  97. Time Time::getCurrentTime() noexcept
  98. {
  99. return Time (currentTimeMillis());
  100. }
  101. int64 Time::currentTimeMillis() noexcept
  102. {
  103. #ifdef _MSC_VER
  104. struct _timeb t;
  105. _ftime_s (&t);
  106. return ((int64) t.time) * 1000 + t.millitm;
  107. #else
  108. struct timeval tv;
  109. gettimeofday (&tv, nullptr);
  110. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  111. #endif
  112. }
  113. //==============================================================================
  114. uint32 Time::getMillisecondCounter() noexcept
  115. {
  116. const uint32 now = water_millisecondsSinceStartup();
  117. if (now < TimeHelpers::lastMSCounterValue.get())
  118. {
  119. // in multi-threaded apps this might be called concurrently, so
  120. // make sure that our last counter value only increases and doesn't
  121. // go backwards..
  122. if (now < TimeHelpers::lastMSCounterValue.get() - 1000U)
  123. TimeHelpers::lastMSCounterValue = now;
  124. }
  125. else
  126. {
  127. TimeHelpers::lastMSCounterValue = now;
  128. }
  129. return now;
  130. }
  131. }