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.

127 lines
4.9KB

  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_MULTITIMER_H_INCLUDED
  18. #define JUCE_MULTITIMER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A type of timer class that can run multiple timers with different frequencies,
  22. all of which share a single callback.
  23. This class is very similar to the Timer class, but allows you run multiple
  24. separate timers, where each one has a unique ID number. The methods in this
  25. class are exactly equivalent to those in Timer, but with the addition of
  26. this ID number.
  27. To use it, you need to create a subclass of MultiTimer, implementing the
  28. timerCallback() method. Then you can start timers with startTimer(), and
  29. each time the callback is triggered, it passes in the ID of the timer that
  30. caused it.
  31. @see Timer
  32. */
  33. class JUCE_API MultiTimer
  34. {
  35. protected:
  36. //==============================================================================
  37. /** Creates a MultiTimer.
  38. When created, no timers are running, so use startTimer() to start things off.
  39. */
  40. MultiTimer() noexcept;
  41. /** Creates a copy of another timer.
  42. Note that this timer will not contain any running timers, even if the one you're
  43. copying from was running.
  44. */
  45. MultiTimer (const MultiTimer&) noexcept;
  46. public:
  47. //==============================================================================
  48. /** Destructor. */
  49. virtual ~MultiTimer();
  50. //==============================================================================
  51. /** The user-defined callback routine that actually gets called by each of the
  52. timers that are running.
  53. It's perfectly ok to call startTimer() or stopTimer() from within this
  54. callback to change the subsequent intervals.
  55. */
  56. virtual void timerCallback (int timerID) = 0;
  57. //==============================================================================
  58. /** Starts a timer and sets the length of interval required.
  59. If the timer is already started, this will reset it, so the
  60. time between calling this method and the next timer callback
  61. will not be less than the interval length passed in.
  62. @param timerID a unique Id number that identifies the timer to
  63. start. This is the id that will be passed back
  64. to the timerCallback() method when this timer is
  65. triggered
  66. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  67. rounded up to 1)
  68. */
  69. void startTimer (int timerID, int intervalInMilliseconds) noexcept;
  70. /** Stops a timer.
  71. If a timer has been started with the given ID number, it will be cancelled.
  72. No more callbacks will be made for the specified timer after this method returns.
  73. If this is called from a different thread, any callbacks that may
  74. be currently executing may be allowed to finish before the method
  75. returns.
  76. */
  77. void stopTimer (int timerID) noexcept;
  78. //==============================================================================
  79. /** Checks whether a timer has been started for a specified ID.
  80. @returns true if a timer with the given ID is running.
  81. */
  82. bool isTimerRunning (int timerID) const noexcept;
  83. /** Returns the interval for a specified timer ID.
  84. @returns the timer's interval in milliseconds if it's running, or 0 if no
  85. timer was running for the ID number specified.
  86. */
  87. int getTimerInterval (int timerID) const noexcept;
  88. //==============================================================================
  89. private:
  90. SpinLock timerListLock;
  91. OwnedArray<Timer> timers;
  92. Timer* getCallback (int) const noexcept;
  93. MultiTimer& operator= (const MultiTimer&);
  94. };
  95. #endif // JUCE_MULTITIMER_H_INCLUDED