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"
16#include "AudioIOSequences.h"
17#include "CommandContext.h"
18#include "Project.h"
19#include "ProjectAudioIO.h"
20#include "ProjectAudioManager.h"
21#include "SampleTrack.h"
22#include "StretchingSequence.h"
23#include "ViewInfo.h"
25#include "ProgressDialog.h"
26#include "WaveTrack.h"
27
29 const CommandContext &context,
30 bool newDefault,
31 bool cutpreview)
32{
33 auto &project = context.project;
34 auto &projectAudioManager = ProjectAudioManager::Get(project);
35
36 const auto &playRegion = ViewInfo::Get(project).playRegion;
37 double t0 = playRegion.GetStart();
38 double t1 = playRegion.GetEnd();
39
40 projectAudioManager.PlayCurrentRegion(newDefault, cutpreview);
41
42 if (project.mBatchMode > 0 && t0 != t1 && !newDefault) {
43 wxYieldIfNeeded();
44
45 /* i18n-hint: This title appears on a dialog that indicates the progress
46 in doing something.*/
47 ProgressDialog progress(XO("Progress"), XO("Playing"), pdlgHideCancelButton);
48 auto gAudioIO = AudioIO::Get();
49
50 while (projectAudioManager.Playing()) {
51 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
52 if (result != ProgressResult::Success) {
53 projectAudioManager.Stop();
54 if (result != ProgressResult::Stopped) {
55 context.Error(wxT("Playing interrupted"));
56 }
57 break;
58 }
59
60 using namespace std::chrono;
61 std::this_thread::sleep_for(100ms);
62 wxYieldIfNeeded();
63 }
64
65 projectAudioManager.Stop();
66 wxYieldIfNeeded();
67 }
68}
69
71 const CommandContext &context,
72 const SelectedRegion &selectedRegion,
73 const AudioIOStartStreamOptions &options,
74 PlayMode mode)
75{
76 auto &project = context.project;
77 auto &projectAudioManager = ProjectAudioManager::Get(project);
78
79 double t0 = selectedRegion.t0();
80 double t1 = selectedRegion.t1();
81
82 projectAudioManager.PlayPlayRegion(selectedRegion, options, mode);
83
84 if (project.mBatchMode > 0) {
85 wxYieldIfNeeded();
86
87 /* i18n-hint: This title appears on a dialog that indicates the progress
88 in doing something.*/
89 ProgressDialog progress(XO("Progress"), XO("Playing"), pdlgHideCancelButton);
90 auto gAudioIO = AudioIO::Get();
91
92 while (projectAudioManager.Playing()) {
93 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
94 if (result != ProgressResult::Success) {
95 projectAudioManager.Stop();
96 if (result != ProgressResult::Stopped) {
97 context.Error(wxT("Playing interrupted"));
98 }
99 break;
100 }
101
102 using namespace std::chrono;
103 std::this_thread::sleep_for(100ms);
104 wxYieldIfNeeded();
105 }
106
107 projectAudioManager.Stop();
108 wxYieldIfNeeded();
109 }
110}
111
113 const CommandContext &context, bool altAppearance)
114{
115 auto &project = context.project;
116 auto &projectAudioManager = ProjectAudioManager::Get(project);
117
118 const auto &selectedRegion = ViewInfo::Get(project).selectedRegion;
119 double t0 = selectedRegion.t0();
120 double t1 = selectedRegion.t1();
121
122 projectAudioManager.OnRecord(altAppearance);
123
124 if (project.mBatchMode > 0 && t1 != t0) {
125 wxYieldIfNeeded();
126
127 /* i18n-hint: This title appears on a dialog that indicates the progress
128 in doing something.*/
129 ProgressDialog progress(XO("Progress"), XO("Recording"), pdlgHideCancelButton);
130 auto gAudioIO = AudioIO::Get();
131
132 while (projectAudioManager.Recording()) {
133 ProgressResult result = progress.Update(gAudioIO->GetStreamTime() - t0, t1 - t0);
134 if (result != ProgressResult::Success) {
135 projectAudioManager.Stop();
136 if (result != ProgressResult::Stopped) {
137 context.Error(wxT("Recording interrupted"));
138 }
139 break;
140 }
141
142 using namespace std::chrono;
143 std::this_thread::sleep_for(100ms);
144 wxYieldIfNeeded();
145 }
146
147 projectAudioManager.Stop();
148 wxYieldIfNeeded();
149 }
150}
151
152// Returns true if this project was stopped, otherwise false.
153// (it may though have stopped another project playing)
155{
156 auto &project = context.project;
157 auto &projectAudioManager = ProjectAudioManager::Get(project);
158 auto gAudioIO = AudioIOBase::Get();
159 auto &toolbar = ControlToolBar::Get(project);
161
162 //If this project is playing, stop playing, make sure everything is unpaused.
163 if (gAudioIO->IsStreamActive(token)) {
164 toolbar.SetStop(); //Pushes stop down
165 projectAudioManager.Stop();
166 // Playing project was stopped. All done.
167 return true;
168 }
169
170 // This project isn't playing.
171 // If some other project is playing, stop playing it
172 if (gAudioIO->IsStreamActive()) {
173
174 //find out which project we need;
175 auto start = AllProjects{}.begin(), finish = AllProjects{}.end(),
176 iter = std::find_if(start, finish,
177 [&](const AllProjects::value_type &ptr) {
178 return gAudioIO->IsStreamActive(
180
181 //stop playing the other project
182 if (iter != finish) {
183 auto otherProject = *iter;
184 auto &otherToolbar = ControlToolBar::Get(*otherProject);
185 auto &otherProjectAudioManager =
186 ProjectAudioManager::Get(*otherProject);
187 otherToolbar.SetStop(); //Pushes stop down
188 otherProjectAudioManager.Stop();
189 }
190 }
191 return false;
192}
193
195 const CommandContext &context, bool newDefault)
196{
197 auto &project = context.project;
198 auto gAudioIO = AudioIOBase::Get();
199 //play the front project
200 if (!gAudioIO->IsBusy()) {
201 //Otherwise, start playing (assuming audio I/O isn't busy)
202
203 // Will automatically set mLastPlayMode
204 PlayCurrentRegionAndWait(context, newDefault);
205 }
206}
207
209 TrackList &trackList, bool selectedOnly, bool nonWaveToo)
210{
211 TransportSequences result;
212 {
213 const auto range = trackList.Any<WaveTrack>()
214 + (selectedOnly ? &Track::IsSelected : &Track::Any);
215 for (auto pTrack : range)
216 result.playbackSequences.push_back(
217 StretchingSequence::Create(*pTrack, pTrack->GetClipInterfaces()));
218 }
219 if (nonWaveToo) {
220 const auto range = trackList.Any<const PlayableTrack>() +
221 (selectedOnly ? &Track::IsSelected : &Track::Any);
222 for (auto pTrack : range)
223 if (!track_cast<const SampleTrack *>(pTrack))
224 if (auto pSequence =
225 std::dynamic_pointer_cast<const OtherPlayableSequence>(
226 pTrack->shared_from_this())
227 )
228 result.otherPlayableSequences.push_back(pSequence);
229 }
230 return result;
231}
wxT("CloseDown"))
XO("Cut/Copy/Paste")
@ pdlgHideCancelButton
const auto project
TransportSequences MakeTransportTracks(TrackList &trackList, bool selectedOnly, bool nonWaveToo)
const_iterator end() const
Definition: Project.cpp:27
Container::value_type value_type
Definition: Project.h:57
const_iterator begin() const
Definition: Project.cpp:22
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:94
static AudioIO * Get()
Definition: AudioIO.cpp:126
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:35
double GetStart() const
Definition: ViewInfo.h:128
AudioTrack subclass that can also be audibly replayed by the program.
Definition: PlayableTrack.h:40
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
static std::shared_ptr< StretchingSequence > Create(const PlayableSequence &, const ClipConstHolders &clips)
bool IsSelected() const
Definition: Track.cpp:258
bool Any() const
Definition: Track.cpp:255
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:850
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:950
PlayRegion playRegion
Definition: ViewInfo.h:216
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:215
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
ProgressResult
Definition: BasicUI.h:148
struct holding stream options, including a pointer to the time warp info and AudioIOListener and whet...
Definition: AudioIOBase.h:44
std::vector< std::shared_ptr< const OtherPlayableSequence > > otherPlayableSequences
Definition: AudioIO.h:73
ConstPlayableSequences playbackSequences
Definition: AudioIO.h:70
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)