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.

156 lines
4.5KB

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