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

9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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&) 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 value less
  67. than 1 will be rounded up to 1)
  68. */
  69. void startTimer (int intervalInMilliseconds) noexcept;
  70. /** Starts the timer with an interval specified in Hertz.
  71. This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
  72. */
  73. void startTimerHz (int timerFrequencyHz) noexcept;
  74. /** Stops the timer.
  75. No more callbacks will be made after this method returns.
  76. If this is called from a different thread, any callbacks that may
  77. be currently executing may be allowed to finish before the method
  78. returns.
  79. */
  80. void stopTimer() noexcept;
  81. //==============================================================================
  82. /** Returns true if the timer is currently running. */
  83. bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
  84. /** Returns the timer's interval.
  85. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  86. */
  87. int getTimerInterval() const noexcept { return timerPeriodMs; }
  88. //==============================================================================
  89. /** For internal use only: invokes any timers that need callbacks.
  90. Don't call this unless you really know what you're doing!
  91. */
  92. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  93. private:
  94. class TimerThread;
  95. friend class TimerThread;
  96. int timerCountdownMs, timerPeriodMs; // NB: these member variable names are a little verbose
  97. Timer* previousTimer, *nextTimer; // to reduce risk of name-clashes with user subclasses
  98. Timer& operator= (const Timer&) JUCE_DELETED_FUNCTION;
  99. };
  100. #endif // JUCE_TIMER_H_INCLUDED