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

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