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.

124 lines
3.3KB

  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 <ctime>
  22. #include <sys/time.h>
  23. #ifdef CARLA_OS_WIN
  24. # include <mmsystem.h>
  25. #endif
  26. namespace water {
  27. namespace TimeHelpers
  28. {
  29. static uint32 lastMSCounterValue = 0;
  30. }
  31. //==============================================================================
  32. static uint32 water_millisecondsSinceStartup() noexcept
  33. {
  34. #ifdef CARLA_OS_WIN
  35. return (uint32) timeGetTime();
  36. #else
  37. timespec t;
  38. clock_gettime (CLOCK_MONOTONIC, &t);
  39. return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
  40. #endif
  41. }
  42. //==============================================================================
  43. Time::Time() noexcept : millisSinceEpoch (0)
  44. {
  45. }
  46. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  47. {
  48. }
  49. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  50. {
  51. }
  52. Time::~Time() noexcept
  53. {
  54. }
  55. Time& Time::operator= (const Time& other) noexcept
  56. {
  57. millisSinceEpoch = other.millisSinceEpoch;
  58. return *this;
  59. }
  60. //==============================================================================
  61. Time Time::getCurrentTime() noexcept
  62. {
  63. return Time (currentTimeMillis());
  64. }
  65. int64 Time::currentTimeMillis() noexcept
  66. {
  67. struct timeval tv;
  68. gettimeofday (&tv, nullptr);
  69. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  70. }
  71. //==============================================================================
  72. uint32 Time::getMillisecondCounter() noexcept
  73. {
  74. const uint32 now = water_millisecondsSinceStartup();
  75. if (now < TimeHelpers::lastMSCounterValue)
  76. {
  77. // in multi-threaded apps this might be called concurrently, so
  78. // make sure that our last counter value only increases and doesn't
  79. // go backwards..
  80. if (now < TimeHelpers::lastMSCounterValue - 1000)
  81. TimeHelpers::lastMSCounterValue = now;
  82. }
  83. else
  84. {
  85. TimeHelpers::lastMSCounterValue = now;
  86. }
  87. return now;
  88. }
  89. uint32 Time::getApproximateMillisecondCounter() noexcept
  90. {
  91. if (TimeHelpers::lastMSCounterValue == 0)
  92. getMillisecondCounter();
  93. return TimeHelpers::lastMSCounterValue;
  94. }
  95. }