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.2KB

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