Audacity 3.2.0
TransportUtilities.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file TransportUtilities.cpp
6 @brief implements some UI related to starting and stopping play and record
7
8 Paul Licameli split from TransportMenus.cpp
9
10**********************************************************************/
11
12#include "TransportUtilities.h"
13
14#include <thread>
15#include "AudioIO.h"
17#include "Project.h"
18#include "ProjectAudioIO.h"
19#include "ProjectAudioManager.h"
20#include "ViewInfo.h"
23
25 const CommandContext &context,
26 bool newDefault,
27 bool cutpreview)
28{
29 auto &project = context.project;
30 auto &projectAudioManager = ProjectAudioManager::Get(project);
31
32 const auto &playRegion = ViewInfo::Get(project).playRegion;
33 double t0 = playRegion.GetStart();
34 double t1 = playRegion.GetEnd();
35
36 projectAudioManager.PlayCurrentRegion(newDefault, cutpreview);
37
38 if (project.mBatchMode > 0 && t0 != t1 && !newDefault) {
39 wxYieldIfNeeded();
40
41 /* i18n-hint: This title appears on a dialog that indicates the progress
42 in doing something.*/
43 ProgressDialog progress(XO("Progress"), XO("Playing"), pdlgHideCancelButton);
44 auto gAudioIO = AudioIO::Get();
45
46 while (projectAudioManager.Playing()) {
47 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
48 if (result != ProgressResult::Success) {
49 projectAudioManager.Stop();
50 if (result != ProgressResult::Stopped) {
51 context.Error(wxT("Playing interrupted"));
52 }
53 break;
54 }
55
56 using namespace std::chrono;
57 std::this_thread::sleep_for(100ms);
58 wxYieldIfNeeded();
59 }
60
61 projectAudioManager.Stop();
62 wxYieldIfNeeded();
63 }
64}
65
67 const CommandContext &context,
68 const SelectedRegion &selectedRegion,
69 const AudioIOStartStreamOptions &options,
70 PlayMode mode)
71{
72 auto &project = context.project;
73 auto &projectAudioManager = ProjectAudioManager::Get(project);
74
75 double t0 = selectedRegion.t0();
76 double t1 = selectedRegion.t1();
77
78 projectAudioManager.PlayPlayRegion(selectedRegion, options, mode);
79
80 if (project.mBatchMode > 0) {
81 wxYieldIfNeeded();
82
83 /* i18n-hint: This title appears on a dialog that indicates the progress
84 in doing something.*/
85 ProgressDialog progress(XO("Progress"), XO("Playing"), pdlgHideCancelButton);
86 auto gAudioIO = AudioIO::Get();
87
88 while (projectAudioManager.Playing()) {
89 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
90 if (result != ProgressResult::Success) {
91 projectAudioManager.Stop();
92 if (result != ProgressResult::Stopped) {
93 context.Error(wxT("Playing interrupted"));
94 }
95 break;
96 }
97
98 using namespace std::chrono;
99 std::this_thread::sleep_for(100ms);
100 wxYieldIfNeeded();
101 }
102
103 projectAudioManager.Stop();
104 wxYieldIfNeeded();
105 }
106}
107
109 const CommandContext &context, bool altAppearance)
110{
111 auto &project = context.project;
112 auto &projectAudioManager = ProjectAudioManager::Get(project);
113
114 const auto &selectedRegion = ViewInfo::Get(project).selectedRegion;
115 double t0 = selectedRegion.t0();
116 double t1 = selectedRegion.t1();
117
118 projectAudioManager.OnRecord(altAppearance);
119
120 if (project.mBatchMode > 0 && t1 != t0) {
121 wxYieldIfNeeded();
122
123 /* i18n-hint: This title appears on a dialog that indicates the progress
124 in doing something.*/
125 ProgressDialog progress(XO("Progress"), XO("Recording"), pdlgHideCancelButton);
126 auto gAudioIO = AudioIO::Get();
127
128 while (projectAudioManager.Recording()) {
129 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
130 if (result != ProgressResult::Success) {
131 projectAudioManager.Stop();
132 if (result != ProgressResult::Stopped) {
133 context.Error(wxT("Recording interrupted"));
134 }
135 break;
136 }
137
138 using namespace std::chrono;
139 std::this_thread::sleep_for(100ms);
140 wxYieldIfNeeded();
141 }
142
143 projectAudioManager.Stop();
144 wxYieldIfNeeded();
145 }
146}
147
148// Returns true if this project was stopped, otherwise false.
149// (it may though have stopped another project playing)
151{
152 auto &project = context.project;
153 auto &projectAudioManager = ProjectAudioManager::Get(project);
154 auto gAudioIO = AudioIOBase::Get();
155 auto &toolbar = ControlToolBar::Get(project);
156 auto token = ProjectAudioIO::Get(project).GetAudioIOToken();
157
158 //If this project is playing, stop playing, make sure everything is unpaused.
159 if (gAudioIO->IsStreamActive(token)) {
160 toolbar.SetStop(); //Pushes stop down
161 projectAudioManager.Stop();
162 // Playing project was stopped. All done.
163 return true;
164 }
165
166 // This project isn't playing.
167 // If some other project is playing, stop playing it
168 if (gAudioIO->IsStreamActive()) {
169
170 //find out which project we need;
171 auto start = AllProjects{}.begin(), finish = AllProjects{}.end(),
172 iter = std::find_if(start, finish,
173 [&](const AllProjects::value_type &ptr) {
174 return gAudioIO->IsStreamActive(
176
177 //stop playing the other project
178 if (iter != finish) {
179 auto otherProject = *iter;
180 auto &otherToolbar = ControlToolBar::Get(*otherProject);
181 auto &otherProjectAudioManager =
182 ProjectAudioManager::Get(*otherProject);
183 otherToolbar.SetStop(); //Pushes stop down
184 otherProjectAudioManager.Stop();
185 }
186 }
187 return false;
188}
189
191 const CommandContext &context, bool newDefault)
192{
193 auto &project = context.project;
194 auto &projectAudioManager = ProjectAudioManager::Get(project);
195 auto gAudioIO = AudioIOBase::Get();
196 //play the front project
197 if (!gAudioIO->IsBusy()) {
198 //Otherwise, start playing (assuming audio I/O isn't busy)
199
200 // Will automatically set mLastPlayMode
201 PlayCurrentRegionAndWait(context, newDefault);
202 }
203}
#define XO(s)
Definition: Internat.h:31
@ pdlgHideCancelButton
const_iterator end() const
Definition: Project.cpp:27
Container::value_type value_type
Definition: Project.h:56
const_iterator begin() const
Definition: Project.cpp:22
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:89
static AudioIO * Get()
Definition: AudioIO.cpp:140
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
virtual void Error(const wxString &message) const
AudacityProject & project
static ControlToolBar & Get(AudacityProject &project)
double t0() const
Definition: ViewInfo.h:34
double GetStart() const
Definition: ViewInfo.h:127
ProgressDialog Class.
ProgressResult Update(int value, const TranslatableString &message={})
int GetAudioIOToken() const
static ProjectAudioIO & Get(AudacityProject &project)
static ProjectAudioManager & Get(AudacityProject &project)
Defines a selected portion of a project.
double t1() const
double t0() const
PlayRegion playRegion
Definition: ViewInfo.h:217
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
ProgressResult
Definition: BasicUI.h:145
struct holding stream options, including a pointer to the time warp info and AudioIOListener and whet...
Definition: AudioIOBase.h:44
static void PlayPlayRegionAndWait(const CommandContext &context, const SelectedRegion &selectedRegion, const AudioIOStartStreamOptions &options, PlayMode mode)
static bool DoStopPlaying(const CommandContext &context)
static void PlayCurrentRegionAndWait(const CommandContext &context, bool newDefault=false, bool cutpreview=false)
static void RecordAndWait(const CommandContext &context, bool altAppearance)
static void DoStartPlaying(const CommandContext &context, bool newDefault=false)