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.

138 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Makes repeated callbacks to a virtual method at a specified time interval.
  22. A Timer's timerCallback() method will be repeatedly called at a given
  23. interval. When you create a Timer object, it will do nothing until the
  24. startTimer() method is called, which will cause the message thread to
  25. start making callbacks at the specified interval, until stopTimer() is called
  26. or the object is deleted.
  27. The time interval isn't guaranteed to be precise to any more than maybe
  28. 10-20ms, and the intervals may end up being much longer than requested if the
  29. system is busy. Because the callbacks are made by the main message thread,
  30. anything that blocks the message queue for a period of time will also prevent
  31. any timers from running until it can carry on.
  32. If you need to have a single callback that is shared by multiple timers with
  33. different frequencies, then the MultiTimer class allows you to do that - its
  34. structure is very similar to the Timer class, but contains multiple timers
  35. internally, each one identified by an ID number.
  36. @see HighResolutionTimer, MultiTimer
  37. @tags{Events}
  38. */
  39. class JUCE_API Timer
  40. {
  41. protected:
  42. //==============================================================================
  43. /** Creates a Timer.
  44. When created, the timer is stopped, so use startTimer() to get it going.
  45. */
  46. Timer() noexcept;
  47. /** Creates a copy of another timer.
  48. Note that this timer won't be started, even if the one you're copying
  49. is running.
  50. */
  51. Timer (const Timer&) noexcept;
  52. public:
  53. //==============================================================================
  54. /** Destructor. */
  55. virtual ~Timer();
  56. //==============================================================================
  57. /** The user-defined callback routine that actually gets called periodically.
  58. It's perfectly ok to call startTimer() or stopTimer() from within this
  59. callback to change the subsequent intervals.
  60. */
  61. virtual void timerCallback() = 0;
  62. //==============================================================================
  63. /** Starts the timer and sets the length of interval required.
  64. If the timer is already started, this will reset it, so the
  65. time between calling this method and the next timer callback
  66. will not be less than the interval length passed in.
  67. @param intervalInMilliseconds the interval to use (any value less
  68. than 1 will be rounded up to 1)
  69. */
  70. void startTimer (int intervalInMilliseconds) noexcept;
  71. /** Starts the timer with an interval specified in Hertz.
  72. This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
  73. */
  74. void startTimerHz (int timerFrequencyHz) noexcept;
  75. /** Stops the timer.
  76. No more timer callbacks will be triggered after this method returns.
  77. Note that if you call this from a background thread while the message-thread
  78. is already in the middle of your callback, then this method will cancel any
  79. future timer callbacks, but it will return without waiting for the current one
  80. to finish. The current callback will continue, possibly still running some of
  81. your timer code after this method has returned.
  82. */
  83. void stopTimer() noexcept;
  84. //==============================================================================
  85. /** Returns true if the timer is currently running. */
  86. bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
  87. /** Returns the timer's interval.
  88. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  89. */
  90. int getTimerInterval() const noexcept { return timerPeriodMs; }
  91. //==============================================================================
  92. /** Invokes a lambda after a given number of milliseconds. */
  93. static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function<void()> functionToCall);
  94. //==============================================================================
  95. /** For internal use only: invokes any timers that need callbacks.
  96. Don't call this unless you really know what you're doing!
  97. */
  98. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  99. private:
  100. class TimerThread;
  101. friend class TimerThread;
  102. size_t positionInQueue = (size_t) -1;
  103. int timerPeriodMs = 0;
  104. Timer& operator= (const Timer&) = delete;
  105. };
  106. } // namespace juce