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.

juce_Timer.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_TIMER_H_INCLUDED
  18. #define JUCE_TIMER_H_INCLUDED
  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. */
  38. class JUCE_API Timer
  39. {
  40. protected:
  41. //==============================================================================
  42. /** Creates a Timer.
  43. When created, the timer is stopped, so use startTimer() to get it going.
  44. */
  45. Timer() noexcept;
  46. /** Creates a copy of another timer.
  47. Note that this timer won't be started, even if the one you're copying
  48. is running.
  49. */
  50. Timer (const Timer& other) noexcept;
  51. public:
  52. //==============================================================================
  53. /** Destructor. */
  54. virtual ~Timer();
  55. //==============================================================================
  56. /** The user-defined callback routine that actually gets called periodically.
  57. It's perfectly ok to call startTimer() or stopTimer() from within this
  58. callback to change the subsequent intervals.
  59. */
  60. virtual void timerCallback() = 0;
  61. //==============================================================================
  62. /** Starts the timer and sets the length of interval required.
  63. If the timer is already started, this will reset it, so the
  64. time between calling this method and the next timer callback
  65. will not be less than the interval length passed in.
  66. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  67. rounded up to 1)
  68. */
  69. void startTimer (int intervalInMilliseconds) noexcept;
  70. /** Stops the timer.
  71. No more callbacks will be made after this method returns.
  72. If this is called from a different thread, any callbacks that may
  73. be currently executing may be allowed to finish before the method
  74. returns.
  75. */
  76. void stopTimer() noexcept;
  77. //==============================================================================
  78. /** Checks if the timer has been started.
  79. @returns true if the timer is running.
  80. */
  81. bool isTimerRunning() const noexcept { return periodMs > 0; }
  82. /** Returns the timer's interval.
  83. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  84. */
  85. int getTimerInterval() const noexcept { return periodMs; }
  86. //==============================================================================
  87. /** For internal use only: invokes any timers that need callbacks.
  88. Don't call this unless you really know what you're doing!
  89. */
  90. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  91. private:
  92. class TimerThread;
  93. friend class TimerThread;
  94. int countdownMs, periodMs;
  95. Timer* previous;
  96. Timer* next;
  97. Timer& operator= (const Timer&);
  98. };
  99. #endif // JUCE_TIMER_H_INCLUDED