Audacity  2.2.0
Scrubbing.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 Scrubbing.h
6 
7 Paul Licameli split from TrackPanel.cpp
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_SCRUBBING__
12 #define __AUDACITY_SCRUBBING__
13 
14 #include "../../MemoryX.h"
15 #include <vector>
16 #include <wx/event.h>
17 #include <wx/longlong.h>
18 
19 #include "../../Experimental.h"
20 #include "../../widgets/Overlay.h"
21 #include "../../../include/audacity/Types.h"
22 
23 class AudacityProject;
24 
25 // Conditionally compile either a separate thead, or else use a timer in the main
26 // thread, to poll the mouse and update scrubbing speed and direction. The advantage of
27 // a thread may be immunity to choppy scrubbing in case redrawing takes too much time.
28 #ifdef __WXGTK__
29 // Unfortunately some things the thread needs to do are not thread safe
30 #else
31 #define USE_SCRUB_THREAD
32 #endif
33 
34 // For putting an increment of work in the scrubbing queue
37 
38  bool adjustStart {};
39 
40  // usually from TrackList::GetEndTime()
41  sampleCount maxSample {};
42  sampleCount minSample {};
43 
44  bool enqueueBySpeed {};
45 
46  double delay {};
47 
48  // Limiting values for the speed of a scrub interval:
49  double minSpeed { 0.0 };
50  double maxSpeed { 1.0 };
51 
52 
53  // When maximum speed scrubbing skips to follow the mouse,
54  // this is the minimum amount of playback allowed at the maximum speed:
55  long minStutter {};
56 
57  // Scrubbing needs the time of start of the mouse movement that began
58  // the scrub:
59  wxLongLong startClockTimeMillis { -1 };
60 
61  static double MaxAllowedScrubSpeed()
62  { return 32.0; } // Is five octaves enough for your amusement?
63  static double MinAllowedScrubSpeed()
64  { return 0.01; } // Mixer needs a lower bound speed. Scrub no slower than this.
65 };
66 
67 // Scrub state object
68 class Scrubber : public wxEvtHandler
69 {
70 public:
71  Scrubber(AudacityProject *project);
72  ~Scrubber();
73 
74  // Assume xx is relative to the left edge of TrackPanel!
75  void MarkScrubStart(wxCoord xx, bool smoothScrolling, bool seek);
76 
77  // Returns true iff the event should be considered consumed by this:
78  // Assume xx is relative to the left edge of TrackPanel!
79  bool MaybeStartScrubbing(wxCoord xx);
80 
81  void ContinueScrubbingUI();
82  void ContinueScrubbingPoll();
83 
84  // This is meant to be called only from ControlToolBar
85  void StopScrubbing();
86 
87  wxCoord GetScrubStartPosition() const
88  { return mScrubStartPosition; }
89 
90  // True iff the user has clicked to start scrub and not yet stopped,
91  // but IsScrubbing() may yet be false
92  bool HasStartedScrubbing() const
93  { return GetScrubStartPosition() >= 0; }
94  bool IsScrubbing() const;
95 
96  bool IsScrollScrubbing() const // If true, implies HasStartedScrubbing()
97  { return mSmoothScrollingScrub; }
98  void SetScrollScrubbing(bool value)
99  { mSmoothScrollingScrub = value; }
100 
101  bool ChoseSeeking() const;
102  bool MayDragToSeek() const;
103  bool TemporarilySeeks() const;
104  bool Seeks() const;
105  bool Scrubs() const;
106  bool ShowsBar() const;
107 
108  void Cancel()
109  { mCancelled = true; }
110 
111  bool ShouldDrawScrubSpeed();
112  double FindScrubSpeed(bool seeking, double time) const;
113  double GetMaxScrubSpeed() const { return mOptions.maxSpeed; }
114 
115  void HandleScrollWheel(int steps);
116 
117  // This returns the same as the enabled state of the menu items:
118  bool CanScrub() const;
119 
120  // For the toolbar
121  void AddMenuItems();
122  // For popup
123  void PopulatePopupMenu(wxMenu &menu);
124 
125  void OnScrubOrSeek(bool seek);
126  void OnScrub(wxCommandEvent&);
127  void OnSeek(wxCommandEvent&);
128  void OnToggleScrubRuler(wxCommandEvent&);
129 
130  // A string to put in the leftmost part of the status bar
131  // when scrub or seek is in progress, or else empty.
132  const wxString &GetUntranslatedStateString() const;
133  wxString StatusMessageForWave() const;
134 
135  // All possible status strings.
136  static std::vector<wxString> GetAllUntranslatedStatusStrings();
137 
138  void Pause(bool paused);
139  bool IsPaused() const;
140  void CheckMenuItems();
141  // Bug 1508
142  bool IsOneShotSeeking()const { return mInOneShotMode && IsScrubbing();};
143  bool mInOneShotMode;
144 
145 private:
146  void DoScrub(bool seek);
147  void OnActivateOrDeactivateApp(wxActivateEvent & event);
148 
149  // I need this because I can't push the scrubber as an event handler
150  // in two places at once.
151  struct Forwarder : public wxEvtHandler {
152  Forwarder(Scrubber &scrubber_) : scrubber( scrubber_ ) {}
153 
155 
156  void OnMouse(wxMouseEvent &event);
157  DECLARE_EVENT_TABLE()
158  };
160 
161 private:
163  bool mPaused;
166  wxCoord mLastScrubPosition {};
169 
170  bool mSeeking {};
171 
172  bool mDragging {};
173 
174  bool mCancelled {};
175 
176 #ifdef EXPERIMENTAL_SCRUBBING_SCROLL_WHEEL
178 #endif
179 
181 
182  DECLARE_EVENT_TABLE()
183 
184 #ifdef USE_SCRUB_THREAD
185  // Course corrections in playback are done in a helper thread, unhindered by
186  // the complications of the main event dispatch loop
187  class ScrubPollerThread;
189 #endif
190 
191  // Other periodic update of the UI must be done in the main thread,
192  // by this object which is driven by timer events.
193  class ScrubPoller;
194  std::unique_ptr<ScrubPoller> mPoller;
195 
197  double mMaxSpeed { 1.0 };
198 };
199 
200 // Specialist in drawing the scrub speed, and listening for certain events
201 class ScrubbingOverlay final : public wxEvtHandler, public Overlay
202 {
203 public:
205  virtual ~ScrubbingOverlay();
206 
207 private:
208  std::pair<wxRect, bool> DoGetRectangle(wxSize size) override;
209  void Draw(OverlayPanel &panel, wxDC &dc) override;
210 
211  void OnTimer(wxCommandEvent &event);
212 
213  const Scrubber &GetScrubber() const;
215 
217 
220 };
221 
222 #endif
Scrubber & scrubber
Definition: Scrubbing.h:154
Definition: Scrubbing.h:151
const Scrubber & GetScrubber() const
Definition: Scrubbing.cpp:866
bool ChoseSeeking() const
Definition: Scrubbing.cpp:593
virtual ~ScrubbingOverlay()
Definition: Scrubbing.cpp:739
bool HasStartedScrubbing() const
Definition: Scrubbing.h:92
sampleCount maxSample
Definition: Scrubbing.h:41
void Pause(bool paused)
Definition: Scrubbing.cpp:681
wxRect mNextScrubRect
Definition: Scrubbing.h:218
AudacityProject * mProject
Definition: Scrubbing.h:180
bool MayDragToSeek() const
Definition: Scrubbing.cpp:603
bool mScrubSeekPress
Definition: Scrubbing.h:167
wxString mNextScrubSpeedText
Definition: Scrubbing.h:219
wxString mLastScrubSpeedText
Definition: Scrubbing.h:219
bool mSeeking
Definition: Scrubbing.h:170
bool ShowsBar() const
Definition: Scrubbing.cpp:573
bool IsPaused() const
Definition: Scrubbing.cpp:686
AudacityProject * mProject
Definition: Scrubbing.h:216
bool mCancelled
Definition: Scrubbing.h:174
Definition: Scrubbing.h:68
void CheckMenuItems()
Definition: Scrubbing.cpp:1040
ScrubPollerThread * mpThread
Definition: Scrubbing.h:188
long minStutter
Definition: Scrubbing.h:55
void SetScrollScrubbing(bool value)
Definition: Scrubbing.h:98
bool mPaused
Definition: Scrubbing.h:163
ScrubbingOverlay(AudacityProject *project)
Definition: Scrubbing.cpp:726
bool IsOneShotSeeking() const
Definition: Scrubbing.h:142
int mScrubToken
Definition: Scrubbing.h:162
void OnScrub(wxCommandEvent &)
Definition: Scrubbing.cpp:924
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:161
void StopScrubbing()
Definition: Scrubbing.cpp:539
std::pair< wxRect, bool > DoGetRectangle(wxSize size) override
Definition: Scrubbing.cpp:747
void AddMenuItems()
Definition: Scrubbing.cpp:1003
static std::vector< wxString > GetAllUntranslatedStatusStrings()
Definition: Scrubbing.cpp:984
int mLogMaxScrubSpeed
Definition: Scrubbing.h:177
static double MaxAllowedScrubSpeed()
Definition: Scrubbing.h:61
void OnToggleScrubRuler(wxCommandEvent &)
Definition: Scrubbing.cpp:936
wxCoord mLastScrubPosition
Definition: Scrubbing.h:166
void OnSeek(wxCommandEvent &)
Definition: Scrubbing.cpp:930
bool IsScrubbing() const
Definition: Scrubbing.cpp:578
void ContinueScrubbingUI()
Definition: Scrubbing.cpp:502
void ContinueScrubbingPoll()
Definition: Scrubbing.cpp:438
bool TemporarilySeeks() const
Definition: Scrubbing.cpp:625
void Draw(OverlayPanel &panel, wxDC &dc) override
Definition: Scrubbing.cpp:760
bool MaybeStartScrubbing(wxCoord xx)
Definition: Scrubbing.cpp:306
void OnMouse(wxMouseEvent &event)
Definition: Scrubbing.cpp:701
wxString StatusMessageForWave() const
Definition: Scrubbing.cpp:970
double FindScrubSpeed(bool seeking, double time) const
Definition: Scrubbing.cpp:654
bool Seeks() const
Definition: Scrubbing.cpp:631
std::unique_ptr< ScrubPoller > mPoller
Definition: Scrubbing.h:193
bool ShouldDrawScrubSpeed()
Definition: Scrubbing.cpp:643
Forwarder mForwarder
Definition: Scrubbing.h:159
double mMaxSpeed
Definition: Scrubbing.h:197
Definition: OverlayPanel.h:17
wxCoord mScrubStartPosition
Definition: Scrubbing.h:165
bool CanScrub() const
Definition: Scrubbing.cpp:996
Definition: Scrubbing.cpp:158
double delay
Definition: Scrubbing.h:46
bool adjustStart
Definition: Scrubbing.h:38
Definition: Scrubbing.h:201
double maxSpeed
Definition: Scrubbing.h:50
bool Scrubs() const
Definition: Scrubbing.cpp:636
Definition: Scrubbing.cpp:134
bool mDragging
Definition: Scrubbing.h:172
wxLongLong startClockTimeMillis
Definition: Scrubbing.h:59
Definition: Overlay.h:19
double minSpeed
Definition: Scrubbing.h:49
Definition: Scrubbing.h:35
Scrubber(AudacityProject *project)
Definition: Scrubbing.cpp:184
void OnActivateOrDeactivateApp(wxActivateEvent &event)
Definition: Scrubbing.cpp:691
bool IsScrollScrubbing() const
Definition: Scrubbing.h:96
bool mInOneShotMode
Definition: Scrubbing.h:142
void OnTimer(wxCommandEvent &event)
Definition: Scrubbing.cpp:786
sampleCount minSample
Definition: Scrubbing.h:42
void HandleScrollWheel(int steps)
Definition: Scrubbing.cpp:662
void PopulatePopupMenu(wxMenu &menu)
Definition: Scrubbing.cpp:1024
bool mSmoothScrollingScrub
Definition: Scrubbing.h:168
void Cancel()
Definition: Scrubbing.h:108
bool enqueueBySpeed
Definition: Scrubbing.h:44
wxCoord GetScrubStartPosition() const
Definition: Scrubbing.h:87
~Scrubber()
Definition: Scrubbing.cpp:207
void DoScrub(bool seek)
Definition: Scrubbing.cpp:876
double GetMaxScrubSpeed() const
Definition: Scrubbing.h:113
wxRect mLastScrubRect
Definition: Scrubbing.h:218
const wxString & GetUntranslatedStateString() const
Definition: Scrubbing.cpp:958
void MarkScrubStart(wxCoord xx, bool smoothScrolling, bool seek)
Definition: Scrubbing.cpp:266
Forwarder(Scrubber &scrubber_)
Definition: Scrubbing.h:152
ScrubbingOptions mOptions
Definition: Scrubbing.h:196
int mScrubSpeedDisplayCountdown
Definition: Scrubbing.h:164
void OnScrubOrSeek(bool seek)
Definition: Scrubbing.cpp:901
static double MinAllowedScrubSpeed()
Definition: Scrubbing.h:63
ScrubbingOptions()
Definition: Scrubbing.h:36