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_MultiTimer.h 4.8KB

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. 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. } // namespace juce