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.

103 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. /**
  20. A high-resolution periodic timer.
  21. This provides accurately-timed regular callbacks. Unlike the normal Timer
  22. class, this one uses a dedicated thread, not the message thread, so is
  23. far more stable and precise.
  24. You should only use this class in situations where you really need accuracy,
  25. because unlike the normal Timer class, which is very lightweight and cheap
  26. to start/stop, the HighResolutionTimer will use far more resources, and
  27. starting/stopping it may involve launching and killing threads.
  28. @see Timer
  29. @tags{Core}
  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. std::unique_ptr<Pimpl> pimpl;
  75. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HighResolutionTimer)
  76. };
  77. } // namespace juce