OpenShot Library | libopenshot-audio  0.1.9
juce_Timer.h
1 
2 /** @weakgroup juce_events-timers
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Makes repeated callbacks to a virtual method at a specified time interval.
33 
34  A Timer's timerCallback() method will be repeatedly called at a given
35  interval. When you create a Timer object, it will do nothing until the
36  startTimer() method is called, which will cause the message thread to
37  start making callbacks at the specified interval, until stopTimer() is called
38  or the object is deleted.
39 
40  The time interval isn't guaranteed to be precise to any more than maybe
41  10-20ms, and the intervals may end up being much longer than requested if the
42  system is busy. Because the callbacks are made by the main message thread,
43  anything that blocks the message queue for a period of time will also prevent
44  any timers from running until it can carry on.
45 
46  If you need to have a single callback that is shared by multiple timers with
47  different frequencies, then the MultiTimer class allows you to do that - its
48  structure is very similar to the Timer class, but contains multiple timers
49  internally, each one identified by an ID number.
50 
51  @see HighResolutionTimer, MultiTimer
52 
53  @tags{Events}
54 */
56 {
57 protected:
58  //==============================================================================
59  /** Creates a Timer.
60  When created, the timer is stopped, so use startTimer() to get it going.
61  */
62  Timer() noexcept;
63 
64  /** Creates a copy of another timer.
65 
66  Note that this timer won't be started, even if the one you're copying
67  is running.
68  */
69  Timer (const Timer&) noexcept;
70 
71 public:
72  //==============================================================================
73  /** Destructor. */
74  virtual ~Timer();
75 
76  //==============================================================================
77  /** The user-defined callback routine that actually gets called periodically.
78 
79  It's perfectly ok to call startTimer() or stopTimer() from within this
80  callback to change the subsequent intervals.
81  */
82  virtual void timerCallback() = 0;
83 
84  //==============================================================================
85  /** Starts the timer and sets the length of interval required.
86 
87  If the timer is already started, this will reset it, so the
88  time between calling this method and the next timer callback
89  will not be less than the interval length passed in.
90 
91  @param intervalInMilliseconds the interval to use (any value less
92  than 1 will be rounded up to 1)
93  */
94  void startTimer (int intervalInMilliseconds) noexcept;
95 
96  /** Starts the timer with an interval specified in Hertz.
97  This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
98  */
99  void startTimerHz (int timerFrequencyHz) noexcept;
100 
101  /** Stops the timer.
102 
103  No more timer callbacks will be triggered after this method returns.
104 
105  Note that if you call this from a background thread while the message-thread
106  is already in the middle of your callback, then this method will cancel any
107  future timer callbacks, but it will return without waiting for the current one
108  to finish. The current callback will continue, possibly still running some of
109  your timer code after this method has returned.
110  */
111  void stopTimer() noexcept;
112 
113  //==============================================================================
114  /** Returns true if the timer is currently running. */
115  bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
116 
117  /** Returns the timer's interval.
118  @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
119  */
120  int getTimerInterval() const noexcept { return timerPeriodMs; }
121 
122  //==============================================================================
123  /** Invokes a lambda after a given number of milliseconds. */
124  static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function<void()> functionToCall);
125 
126  //==============================================================================
127  /** For internal use only: invokes any timers that need callbacks.
128  Don't call this unless you really know what you're doing!
129  */
130  static void JUCE_CALLTYPE callPendingTimersSynchronously();
131 
132 private:
133  class TimerThread;
134  friend class TimerThread;
135  size_t positionInQueue = (size_t) -1;
136  int timerPeriodMs = 0;
137 
138  Timer& operator= (const Timer&) = delete;
139 };
140 
141 } // namespace juce
142 
143 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
int getTimerInterval() const noexcept
Returns the timer&#39;s interval.
Definition: juce_Timer.h:120
bool isTimerRunning() const noexcept
Returns true if the timer is currently running.
Definition: juce_Timer.h:115
Makes repeated callbacks to a virtual method at a specified time interval.
Definition: juce_Timer.h:55