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

7 years ago
9 years ago
9 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. */
  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 timer callbacks will be triggered after this method returns.
  76. Note that if you call this from a background thread while the message-thread
  77. is already in the middle of your callback, then this method will cancel any
  78. future timer callbacks, but it will return without waiting for the current one
  79. to finish. The current callback will continue, possibly still running some of
  80. your timer code after this method has returned.
  81. */
  82. void stopTimer() noexcept;
  83. //==============================================================================
  84. /** Returns true if the timer is currently running. */
  85. bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
  86. /** Returns the timer's interval.
  87. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  88. */
  89. int getTimerInterval() const noexcept { return timerPeriodMs; }
  90. //==============================================================================
  91. /** Invokes a lambda after a given number of milliseconds. */
  92. static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function<void()> functionToCall);
  93. //==============================================================================
  94. /** For internal use only: invokes any timers that need callbacks.
  95. Don't call this unless you really know what you're doing!
  96. */
  97. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  98. private:
  99. class TimerThread;
  100. friend class TimerThread;
  101. int timerCountdownMs, timerPeriodMs; // NB: these member variable names are a little verbose
  102. Timer* previousTimer, *nextTimer; // to reduce risk of name-clashes with user subclasses
  103. Timer& operator= (const Timer&) JUCE_DELETED_FUNCTION;
  104. };
  105. } // namespace juce