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.

134 lines
5.7KB

  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. #pragma once
  18. //==============================================================================
  19. /**
  20. Makes repeated callbacks to a virtual method at a specified time interval.
  21. A Timer's timerCallback() method will be repeatedly called at a given
  22. interval. When you create a Timer object, it will do nothing until the
  23. startTimer() method is called, which will cause the message thread to
  24. start making callbacks at the specified interval, until stopTimer() is called
  25. or the object is deleted.
  26. The time interval isn't guaranteed to be precise to any more than maybe
  27. 10-20ms, and the intervals may end up being much longer than requested if the
  28. system is busy. Because the callbacks are made by the main message thread,
  29. anything that blocks the message queue for a period of time will also prevent
  30. any timers from running until it can carry on.
  31. If you need to have a single callback that is shared by multiple timers with
  32. different frequencies, then the MultiTimer class allows you to do that - its
  33. structure is very similar to the Timer class, but contains multiple timers
  34. internally, each one identified by an ID number.
  35. @see HighResolutionTimer, MultiTimer
  36. */
  37. class JUCE_API Timer
  38. {
  39. protected:
  40. //==============================================================================
  41. /** Creates a Timer.
  42. When created, the timer is stopped, so use startTimer() to get it going.
  43. */
  44. Timer() noexcept;
  45. /** Creates a copy of another timer.
  46. Note that this timer won't be started, even if the one you're copying
  47. is running.
  48. */
  49. Timer (const Timer&) noexcept;
  50. public:
  51. //==============================================================================
  52. /** Destructor. */
  53. virtual ~Timer();
  54. //==============================================================================
  55. /** The user-defined callback routine that actually gets called periodically.
  56. It's perfectly ok to call startTimer() or stopTimer() from within this
  57. callback to change the subsequent intervals.
  58. */
  59. virtual void timerCallback() = 0;
  60. //==============================================================================
  61. /** Starts the timer and sets the length of interval required.
  62. If the timer is already started, this will reset it, so the
  63. time between calling this method and the next timer callback
  64. will not be less than the interval length passed in.
  65. @param intervalInMilliseconds the interval to use (any value less
  66. than 1 will be rounded up to 1)
  67. */
  68. void startTimer (int intervalInMilliseconds) noexcept;
  69. /** Starts the timer with an interval specified in Hertz.
  70. This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
  71. */
  72. void startTimerHz (int timerFrequencyHz) noexcept;
  73. /** Stops the timer.
  74. No more timer callbacks will be triggered after this method returns.
  75. Note that if you call this from a background thread while the message-thread
  76. is already in the middle of your callback, then this method will cancel any
  77. future timer callbacks, but it will return without waiting for the current one
  78. to finish. The current callback will continue, possibly still running some of
  79. your timer code after this method has returned.
  80. */
  81. void stopTimer() noexcept;
  82. //==============================================================================
  83. /** Returns true if the timer is currently running. */
  84. bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
  85. /** Returns the timer's interval.
  86. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  87. */
  88. int getTimerInterval() const noexcept { return timerPeriodMs; }
  89. //==============================================================================
  90. /** Invokes a lambda after a given number of milliseconds. */
  91. static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function<void()> functionToCall);
  92. //==============================================================================
  93. /** For internal use only: invokes any timers that need callbacks.
  94. Don't call this unless you really know what you're doing!
  95. */
  96. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  97. private:
  98. class TimerThread;
  99. friend class TimerThread;
  100. int timerCountdownMs, timerPeriodMs; // NB: these member variable names are a little verbose
  101. Timer* previousTimer, *nextTimer; // to reduce risk of name-clashes with user subclasses
  102. Timer& operator= (const Timer&) JUCE_DELETED_FUNCTION;
  103. };