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