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.

122 lines
4.8KB

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