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.

112 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. struct MultiTimerCallback : public Timer
  24. {
  25. MultiTimerCallback (const int tid, MultiTimer& mt) noexcept
  26. : owner (mt), timerID (tid)
  27. {
  28. }
  29. void timerCallback() override
  30. {
  31. owner.timerCallback (timerID);
  32. }
  33. MultiTimer& owner;
  34. const int timerID;
  35. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiTimerCallback)
  36. };
  37. //==============================================================================
  38. MultiTimer::MultiTimer() noexcept {}
  39. MultiTimer::MultiTimer (const MultiTimer&) noexcept {}
  40. MultiTimer::~MultiTimer()
  41. {
  42. const SpinLock::ScopedLockType sl (timerListLock);
  43. timers.clear();
  44. }
  45. //==============================================================================
  46. Timer* MultiTimer::getCallback (int timerID) const noexcept
  47. {
  48. for (int i = timers.size(); --i >= 0;)
  49. {
  50. MultiTimerCallback* const t = static_cast<MultiTimerCallback*> (timers.getUnchecked(i));
  51. if (t->timerID == timerID)
  52. return t;
  53. }
  54. return nullptr;
  55. }
  56. void MultiTimer::startTimer (const int timerID, const int intervalInMilliseconds) noexcept
  57. {
  58. const SpinLock::ScopedLockType sl (timerListLock);
  59. Timer* timer = getCallback (timerID);
  60. if (timer == nullptr)
  61. timers.add (timer = new MultiTimerCallback (timerID, *this));
  62. timer->startTimer (intervalInMilliseconds);
  63. }
  64. void MultiTimer::stopTimer (const int timerID) noexcept
  65. {
  66. const SpinLock::ScopedLockType sl (timerListLock);
  67. if (Timer* const t = getCallback (timerID))
  68. t->stopTimer();
  69. }
  70. bool MultiTimer::isTimerRunning (const int timerID) const noexcept
  71. {
  72. const SpinLock::ScopedLockType sl (timerListLock);
  73. if (Timer* const t = getCallback (timerID))
  74. return t->isTimerRunning();
  75. return false;
  76. }
  77. int MultiTimer::getTimerInterval (const int timerID) const noexcept
  78. {
  79. const SpinLock::ScopedLockType sl (timerListLock);
  80. if (Timer* const t = getCallback (timerID))
  81. return t->getTimerInterval();
  82. return 0;
  83. }