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.

Time.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. # ifdef CLOCK_MONOTONIC_RAW
  69. clock_gettime (CLOCK_MONOTONIC_RAW, &t);
  70. # else
  71. clock_gettime (CLOCK_MONOTONIC, &t);
  72. # endif
  73. return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
  74. #endif
  75. }
  76. //==============================================================================
  77. Time::Time() noexcept : millisSinceEpoch (0)
  78. {
  79. }
  80. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  81. {
  82. }
  83. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  84. {
  85. }
  86. Time::~Time() noexcept
  87. {
  88. }
  89. Time& Time::operator= (const Time& other) noexcept
  90. {
  91. millisSinceEpoch = other.millisSinceEpoch;
  92. return *this;
  93. }
  94. //==============================================================================
  95. Time Time::getCurrentTime() noexcept
  96. {
  97. return Time (currentTimeMillis());
  98. }
  99. int64 Time::currentTimeMillis() noexcept
  100. {
  101. struct timeval tv;
  102. gettimeofday (&tv, nullptr);
  103. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  104. }
  105. //==============================================================================
  106. uint32 Time::getMillisecondCounter() noexcept
  107. {
  108. const uint32 now = water_millisecondsSinceStartup();
  109. if (now < TimeHelpers::lastMSCounterValue.get())
  110. {
  111. // in multi-threaded apps this might be called concurrently, so
  112. // make sure that our last counter value only increases and doesn't
  113. // go backwards..
  114. if (now < TimeHelpers::lastMSCounterValue.get() - 1000U)
  115. TimeHelpers::lastMSCounterValue = now;
  116. }
  117. else
  118. {
  119. TimeHelpers::lastMSCounterValue = now;
  120. }
  121. return now;
  122. }
  123. }