The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

107 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_HIGHRESOLUTIONTIMER_JUCEHEADER__
  19. #define __JUCE_HIGHRESOLUTIONTIMER_JUCEHEADER__
  20. /**
  21. A high-resolution periodic timer.
  22. This provides accurately-timed regular callbacks. Unlike the normal Timer
  23. class, this one uses a dedicated thread, not the message thread, so is
  24. far more stable and precise.
  25. You should only use this class in situations where you really need accuracy,
  26. because unlike the normal Timer class, which is very lightweight and cheap
  27. to start/stop, the HighResolutionTimer will use far more resources, and
  28. starting/stopping it may involve launching and killing threads.
  29. @see Timer
  30. */
  31. class JUCE_API HighResolutionTimer
  32. {
  33. protected:
  34. /** Creates a HighResolutionTimer.
  35. When created, the timer is stopped, so use startTimer() to get it going.
  36. */
  37. HighResolutionTimer();
  38. public:
  39. /** Destructor. */
  40. virtual ~HighResolutionTimer();
  41. //==============================================================================
  42. /** The user-defined callback routine that actually gets called periodically.
  43. This will be called on a dedicated timer thread, so make sure your
  44. implementation is thread-safe!
  45. It's perfectly ok to call startTimer() or stopTimer() from within this
  46. callback to change the subsequent intervals.
  47. */
  48. virtual void hiResTimerCallback() = 0;
  49. //==============================================================================
  50. /** Starts the timer and sets the length of interval required.
  51. If the timer is already started, this will reset its counter, so the
  52. time between calling this method and the next timer callback will not be
  53. less than the interval length passed in.
  54. @param intervalInMilliseconds the interval to use (any values less than 1 will be
  55. rounded up to 1)
  56. */
  57. void startTimer (int intervalInMilliseconds);
  58. /** Stops the timer.
  59. This method may block while it waits for pending callbacks to complete. Once it
  60. returns, no more callbacks will be made. If it is called from the timer's own thread,
  61. it will cancel the timer after the current callback returns.
  62. */
  63. void stopTimer();
  64. /** Checks if the timer has been started.
  65. @returns true if the timer is running.
  66. */
  67. bool isTimerRunning() const noexcept;
  68. /** Returns the timer's interval.
  69. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  70. */
  71. int getTimerInterval() const noexcept;
  72. private:
  73. struct Pimpl;
  74. friend struct Pimpl;
  75. friend class ScopedPointer<Pimpl>;
  76. ScopedPointer<Pimpl> pimpl;
  77. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HighResolutionTimer)
  78. };
  79. #endif // __JUCE_HIGHRESOLUTIONTIMER_JUCEHEADER__