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.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. struct MultiTimerCallback : public Timer
  18. {
  19. MultiTimerCallback (const int tid, MultiTimer& mt) noexcept
  20. : owner (mt), timerID (tid)
  21. {
  22. }
  23. void timerCallback() override
  24. {
  25. owner.timerCallback (timerID);
  26. }
  27. MultiTimer& owner;
  28. const int timerID;
  29. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiTimerCallback)
  30. };
  31. //==============================================================================
  32. MultiTimer::MultiTimer() noexcept {}
  33. MultiTimer::MultiTimer (const MultiTimer&) noexcept {}
  34. MultiTimer::~MultiTimer()
  35. {
  36. const SpinLock::ScopedLockType sl (timerListLock);
  37. timers.clear();
  38. }
  39. //==============================================================================
  40. Timer* MultiTimer::getCallback (int timerID) const noexcept
  41. {
  42. for (int i = timers.size(); --i >= 0;)
  43. {
  44. MultiTimerCallback* const t = static_cast<MultiTimerCallback*> (timers.getUnchecked(i));
  45. if (t->timerID == timerID)
  46. return t;
  47. }
  48. return nullptr;
  49. }
  50. void MultiTimer::startTimer (const int timerID, const int intervalInMilliseconds) noexcept
  51. {
  52. const SpinLock::ScopedLockType sl (timerListLock);
  53. Timer* timer = getCallback (timerID);
  54. if (timer == nullptr)
  55. timers.add (timer = new MultiTimerCallback (timerID, *this));
  56. timer->startTimer (intervalInMilliseconds);
  57. }
  58. void MultiTimer::stopTimer (const int timerID) noexcept
  59. {
  60. const SpinLock::ScopedLockType sl (timerListLock);
  61. if (Timer* const t = getCallback (timerID))
  62. t->stopTimer();
  63. }
  64. bool MultiTimer::isTimerRunning (const int timerID) const noexcept
  65. {
  66. const SpinLock::ScopedLockType sl (timerListLock);
  67. if (Timer* const t = getCallback (timerID))
  68. return t->isTimerRunning();
  69. return false;
  70. }
  71. int MultiTimer::getTimerInterval (const int timerID) const noexcept
  72. {
  73. const SpinLock::ScopedLockType sl (timerListLock);
  74. if (Timer* const t = getCallback (timerID))
  75. return t->getTimerInterval();
  76. return 0;
  77. }