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.

122 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #include "Time.h"
  24. namespace water {
  25. namespace TimeHelpers
  26. {
  27. static uint32 lastMSCounterValue = 0;
  28. }
  29. //==============================================================================
  30. static uint32 juce_millisecondsSinceStartup() noexcept
  31. {
  32. #ifdef CARLA_OS_WIN
  33. return (uint32) timeGetTime();
  34. #else
  35. timespec t;
  36. clock_gettime (CLOCK_MONOTONIC, &t);
  37. return (uint32) (t.tv_sec * 1000 + t.tv_nsec / 1000000);
  38. #endif
  39. }
  40. //==============================================================================
  41. Time::Time() noexcept : millisSinceEpoch (0)
  42. {
  43. }
  44. Time::Time (const Time& other) noexcept : millisSinceEpoch (other.millisSinceEpoch)
  45. {
  46. }
  47. Time::Time (const int64 ms) noexcept : millisSinceEpoch (ms)
  48. {
  49. }
  50. Time::~Time() noexcept
  51. {
  52. }
  53. Time& Time::operator= (const Time& other) noexcept
  54. {
  55. millisSinceEpoch = other.millisSinceEpoch;
  56. return *this;
  57. }
  58. //==============================================================================
  59. Time Time::getCurrentTime() noexcept
  60. {
  61. return Time (currentTimeMillis());
  62. }
  63. int64 Time::currentTimeMillis() noexcept
  64. {
  65. struct timeval tv;
  66. gettimeofday (&tv, nullptr);
  67. return ((int64) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
  68. }
  69. //==============================================================================
  70. uint32 Time::getMillisecondCounter() noexcept
  71. {
  72. const uint32 now = juce_millisecondsSinceStartup();
  73. if (now < TimeHelpers::lastMSCounterValue)
  74. {
  75. // in multi-threaded apps this might be called concurrently, so
  76. // make sure that our last counter value only increases and doesn't
  77. // go backwards..
  78. if (now < TimeHelpers::lastMSCounterValue - 1000)
  79. TimeHelpers::lastMSCounterValue = now;
  80. }
  81. else
  82. {
  83. TimeHelpers::lastMSCounterValue = now;
  84. }
  85. return now;
  86. }
  87. uint32 Time::getApproximateMillisecondCounter() noexcept
  88. {
  89. if (TimeHelpers::lastMSCounterValue == 0)
  90. getMillisecondCounter();
  91. return TimeHelpers::lastMSCounterValue;
  92. }
  93. }