Audacity 3.2.0
TrackUtilities.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 TrackUtilities.cpp
6
7 Paul Licameli split from TrackMenus.cpp
8
9 **********************************************************************/
10
11#include "TrackUtilities.h"
12
13#include "PlayableTrack.h"
14#include "ProjectHistory.h"
15#include "ProjectWindow.h"
16#include "TrackPanelAx.h"
17#include "TrackPanel.h"
18
19namespace TrackUtilities {
20
22{
23 auto &tracks = TrackList::Get( project );
24 auto &trackPanel = TrackPanel::Get( project );
25
26 std::vector<Track*> toRemove;
27 for (auto track : tracks.Selected())
28 toRemove.push_back(track);
29
30 // Capture the track preceding the first removed track
31 Track *f{};
32 if (!toRemove.empty()) {
33 auto found = tracks.Find(toRemove[0]);
34 f = *--found;
35 }
36
37 for (auto track : toRemove)
38 tracks.Remove(track);
39
40 if (!f)
41 // try to use the last track
42 f = *tracks.Any().rbegin();
43 if (f) {
44 // Try to use the first track after the removal
45 auto found = tracks.FindLeader(f);
46 auto t = *++found;
47 if (t)
48 f = t;
49 }
50
51 // If we actually have something left, then set focus and make sure it's seen
52 if (f) {
53 TrackFocus::Get(project).Set(f);
54 f->EnsureVisible();
55 }
56
57 ProjectHistory::Get( project )
58 .PushState(XO("Removed audio track(s)"), XO("Remove Track"));
59
60 trackPanel.UpdateViewIfNoTracks();
61}
62
63void DoTrackMute(AudacityProject &project, Track *t, bool exclusive)
64{
65 auto &tracks = TrackList::Get( project );
66
67 // Whatever t is, replace with lead channel
68 t = *tracks.FindLeader(t);
69
70 // "exclusive" mute means mute the chosen track and unmute all others.
71 if (exclusive) {
72 for (auto leader : tracks.Leaders<PlayableTrack>()) {
73 bool chosen = (t == leader);
74 leader->SetMute(chosen);
75 leader->SetSolo(false);
76 }
77 }
78 else {
79 // Normal click toggles this track.
80 auto pt = dynamic_cast<PlayableTrack *>( t );
81 if (!pt)
82 return;
83
84 bool wasMute = pt->GetMute();
85 pt->SetMute(!wasMute);
86
87 if (auto value = TracksBehaviorsSolo.ReadEnum();
88 value == SoloBehaviorSimple || value == SoloBehaviorNone)
89 {
90 // We also set a solo indicator if we have just one track / stereo pair playing.
91 // in a group of more than one playable tracks.
92 // otherwise clear solo on everything.
93
94 auto range = tracks.Leaders<PlayableTrack>();
95 auto nPlayableTracks = range.size();
96 auto nPlaying = (range - &PlayableTrack::GetMute).size();
97 for (auto track : range)
98 track->SetSolo((nPlaying == 1) &&
99 (nPlayableTracks > 1) && !track->GetMute());
100 }
101 }
102 ProjectHistory::Get( project ).ModifyState(true);
103
104 TrackFocus::Get( project ).UpdateAccessibility();
105}
106
107void DoTrackSolo(AudacityProject &project, Track *t, bool exclusive)
108{
109 auto &tracks = TrackList::Get( project );
110
111 // Whatever t is, replace with lead channel
112 t = *tracks.FindLeader(t);
113
114 const auto pt = dynamic_cast<PlayableTrack *>( t );
115 if (!pt)
116 return;
117 bool bWasSolo = pt->GetSolo();
118
119 bool simple = (TracksBehaviorsSolo.ReadEnum() == SoloBehaviorSimple);
120 bool bSoloMultiple = !simple ^ exclusive;
121
122 // Standard and Simple solo have opposite defaults:
123 // Standard - Behaves as individual buttons, shift=radio buttons
124 // Simple - Behaves as radio buttons, shift=individual
125 // In addition, Simple solo will mute/unmute tracks
126 // when in standard radio button mode.
127 if (bSoloMultiple)
128 pt->SetSolo(!bWasSolo);
129 else {
130 // Normal click solo this track only, mute everything else.
131 // OR unmute and unsolo everything.
132 for (auto leader : tracks.Leaders<PlayableTrack>()) {
133 bool chosen = (t == leader);
134 if (chosen) {
135 leader->SetSolo(!bWasSolo);
136 if (simple)
137 leader->SetMute(false);
138 }
139 else {
140 leader->SetSolo(false);
141 if (simple)
142 leader->SetMute(!bWasSolo);
143 }
144 }
145 }
146 ProjectHistory::Get( project ).ModifyState(true);
147
148 TrackFocus::Get( project ).UpdateAccessibility();
149}
150
151void DoRemoveTrack(AudacityProject &project, Track * toRemove)
152{
153 auto &tracks = TrackList::Get( project );
154 auto &trackFocus = TrackFocus::Get( project );
155 auto &window = ProjectWindow::Get( project );
156
157 // If it was focused, then NEW focus is the next or, if
158 // unavailable, the previous track. (The NEW focus is set
159 // after the track has been removed.)
160 bool toRemoveWasFocused = trackFocus.Get() == toRemove;
161 Track* newFocus{};
162 if (toRemoveWasFocused) {
163 auto iterNext = tracks.FindLeader(toRemove), iterPrev = iterNext;
164 newFocus = *++iterNext;
165 if (!newFocus) {
166 newFocus = *--iterPrev;
167 }
168 }
169
170 wxString name = toRemove->GetName();
171
172 auto channels = TrackList::Channels(toRemove);
173 // Be careful to post-increment over positions that get erased!
174 auto &iter = channels.first;
175 while (iter != channels.end())
176 tracks.Remove( * iter++ );
177
178 if (toRemoveWasFocused)
179 trackFocus.Set( newFocus );
180
182 XO("Removed track '%s.'").Format( name ),
183 XO("Track Remove"));
184}
185
187(AudacityProject &project, Track* target, MoveChoice choice)
188{
189 auto &tracks = TrackList::Get( project );
190
191 TranslatableString longDesc, shortDesc;
192
193 switch (choice)
194 {
195 case OnMoveTopID:
196 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
197 longDesc = XO("Moved '%s' to Top");
198 shortDesc = XO("Move Track to Top");
199
200 // TODO: write TrackList::Rotate to do this in one step and avoid emitting
201 // an event for each swap
202 while (tracks.CanMoveUp(target))
203 tracks.Move(target, true);
204
205 break;
206 case OnMoveBottomID:
207 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
208 longDesc = XO("Moved '%s' to Bottom");
209 shortDesc = XO("Move Track to Bottom");
210
211 // TODO: write TrackList::Rotate to do this in one step and avoid emitting
212 // an event for each swap
213 while (tracks.CanMoveDown(target))
214 tracks.Move(target, false);
215
216 break;
217 default:
218 bool bUp = (OnMoveUpID == choice);
219
220 tracks.Move(target, bUp);
221 longDesc =
222 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
223 bUp? XO("Moved '%s' Up")
224 : XO("Moved '%s' Down");
225 shortDesc =
226 /* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
227 bUp? XO("Move Track Up")
228 : XO("Move Track Down");
229
230 }
231
232 longDesc.Format(target->GetName());
233
234 ProjectHistory::Get( project ).PushState(longDesc, shortDesc);
235}
236
237}
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
EnumSetting< SoloBehavior > TracksBehaviorsSolo
Extends Track with notions of mute and solo setting.
@ SoloBehaviorSimple
Definition: PlayableTrack.h:70
@ SoloBehaviorNone
Definition: PlayableTrack.h:72
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
size_t size() const
How many attachment pointers are in the Site.
Definition: ClientData.h:251
Subclass * Find(const RegisteredFactory &key)
Get a (bare) pointer to an attachment, or null, down-cast it to Subclass *; will not create on demand...
Definition: ClientData.h:333
Abstract base class used in importing a file.
AudioTrack subclass that can also be audibly replayed by the program.
Definition: PlayableTrack.h:40
bool GetSolo() const
Definition: PlayableTrack.h:48
bool GetMute() const
Definition: PlayableTrack.h:47
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
void ModifyState(bool bWantsAutoSave)
static ProjectHistory & Get(AudacityProject &project)
static ProjectWindow & Get(AudacityProject &project)
Track * Get()
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:162
void EnsureVisible(bool modifyState=false)
Definition: Track.cpp:98
const wxString & GetName() const
Name is always the same for all channels of a group.
Definition: Track.cpp:68
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:385
static auto Channels(TrackType *pTrack) -> TrackIterRange< TrackType >
Definition: Track.h:1417
static TrackPanel & Get(AudacityProject &project)
Definition: TrackPanel.cpp:231
Holds a msgid for the translation catalog; may also bind format arguments.
TranslatableString & Format(Args &&...args) &
Capture variadic format arguments (by copy) when there is no plural.
void DoTrackMute(AudacityProject &project, Track *t, bool exclusive)
void DoMoveTrack(AudacityProject &project, Track *target, MoveChoice choice)
Move a track up, down, to top or to bottom.
void DoTrackSolo(AudacityProject &project, Track *t, bool exclusive)
void DoRemoveTrack(AudacityProject &project, Track *toRemove)
void DoRemoveTracks(AudacityProject &project)