The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

131 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MULTITIMER_JUCEHEADER__
  19. #define __JUCE_MULTITIMER_JUCEHEADER__
  20. #include "juce_Timer.h"
  21. //==============================================================================
  22. /**
  23. A type of timer class that can run multiple timers with different frequencies,
  24. all of which share a single callback.
  25. This class is very similar to the Timer class, but allows you run multiple
  26. separate timers, where each one has a unique ID number. The methods in this
  27. class are exactly equivalent to those in Timer, but with the addition of
  28. this ID number.
  29. To use it, you need to create a subclass of MultiTimer, implementing the
  30. timerCallback() method. Then you can start timers with startTimer(), and
  31. each time the callback is triggered, it passes in the ID of the timer that
  32. caused it.
  33. @see Timer
  34. */
  35. class JUCE_API MultiTimer
  36. {
  37. protected:
  38. //==============================================================================
  39. /** Creates a MultiTimer.
  40. When created, no timers are running, so use startTimer() to start things off.
  41. */
  42. MultiTimer() noexcept;
  43. /** Creates a copy of another timer.
  44. Note that this timer will not contain any running timers, even if the one you're
  45. copying from was running.
  46. */
  47. MultiTimer (const MultiTimer& other) noexcept;
  48. public:
  49. //==============================================================================
  50. /** Destructor. */
  51. virtual ~MultiTimer();
  52. //==============================================================================
  53. /** The user-defined callback routine that actually gets called by each of the
  54. timers that are running.
  55. It's perfectly ok to call startTimer() or stopTimer() from within this
  56. callback to change the subsequent intervals.
  57. */
  58. virtual void timerCallback (int timerId) = 0;
  59. //==============================================================================
  60. /** Starts a timer and sets the length of interval required.
  61. If the timer is already started, this will reset it, so the
  62. time between calling this method and the next timer callback
  63. will not be less than the interval length passed in.
  64. @param timerId a unique Id number that identifies the timer to
  65. start. This is the id that will be passed back
  66. to the timerCallback() method when this timer is
  67. triggered
  68. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  69. rounded up to 1)
  70. */
  71. void startTimer (int timerId, int intervalInMilliseconds) noexcept;
  72. /** Stops a timer.
  73. If a timer has been started with the given ID number, it will be cancelled.
  74. No more callbacks will be made for the specified timer after this method returns.
  75. If this is called from a different thread, any callbacks that may
  76. be currently executing may be allowed to finish before the method
  77. returns.
  78. */
  79. void stopTimer (int timerId) noexcept;
  80. //==============================================================================
  81. /** Checks whether a timer has been started for a specified ID.
  82. @returns true if a timer with the given ID is running.
  83. */
  84. bool isTimerRunning (int timerId) const noexcept;
  85. /** Returns the interval for a specified timer ID.
  86. @returns the timer's interval in milliseconds if it's running, or 0 if it's no timer
  87. is running for the ID number specified.
  88. */
  89. int getTimerInterval (int timerId) const noexcept;
  90. //==============================================================================
  91. private:
  92. class MultiTimerCallback;
  93. SpinLock timerListLock;
  94. OwnedArray <MultiTimerCallback> timers;
  95. MultiTimer& operator= (const MultiTimer&);
  96. };
  97. #endif // __JUCE_MULTITIMER_JUCEHEADER__