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