forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackUtilities.cpp
249 lines (202 loc) · 7.23 KB
/
TrackUtilities.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**********************************************************************
Sneedacity: A Digital Audio Editor
TrackUtilities.cpp
Paul Licameli split from TrackMenus.cpp
**********************************************************************/
#include "TrackUtilities.h"
#include "ProjectHistory.h"
#include "ProjectSettings.h"
#include "ProjectWindow.h"
#include "Track.h"
#include "TrackPanelAx.h"
#include "TrackPanel.h"
namespace TrackUtilities {
void DoRemoveTracks( SneedacityProject &project )
{
auto &tracks = TrackList::Get( project );
auto &trackPanel = TrackPanel::Get( project );
std::vector<Track*> toRemove;
for (auto track : tracks.Selected())
toRemove.push_back(track);
// Capture the track preceding the first removed track
Track *f{};
if (!toRemove.empty()) {
auto found = tracks.Find(toRemove[0]);
f = *--found;
}
for (auto track : toRemove)
tracks.Remove(track);
if (!f)
// try to use the last track
f = *tracks.Any().rbegin();
if (f) {
// Try to use the first track after the removal
auto found = tracks.FindLeader(f);
auto t = *++found;
if (t)
f = t;
}
// If we actually have something left, then set focus and make sure it's seen
if (f) {
TrackFocus::Get(project).Set(f);
f->EnsureVisible();
}
ProjectHistory::Get( project )
.PushState(XO("Removed audio track(s)"), XO("Remove Track"));
trackPanel.UpdateViewIfNoTracks();
}
void DoTrackMute(SneedacityProject &project, Track *t, bool exclusive)
{
const auto &settings = ProjectSettings::Get( project );
auto &tracks = TrackList::Get( project );
// Whatever t is, replace with lead channel
t = *tracks.FindLeader(t);
// "exclusive" mute means mute the chosen track and unmute all others.
if (exclusive) {
for (auto leader : tracks.Leaders<PlayableTrack>()) {
const auto group = TrackList::Channels(leader);
bool chosen = (t == leader);
for (auto channel : group)
channel->SetMute( chosen ),
channel->SetSolo( false );
}
}
else {
// Normal click toggles this track.
auto pt = dynamic_cast<PlayableTrack *>( t );
if (!pt)
return;
bool wasMute = pt->GetMute();
for (auto channel : TrackList::Channels(pt))
channel->SetMute( !wasMute );
if (settings.IsSoloSimple() || settings.IsSoloNone())
{
// We also set a solo indicator if we have just one track / stereo pair playing.
// in a group of more than one playable tracks.
// otherwise clear solo on everything.
auto range = tracks.Leaders<PlayableTrack>();
auto nPlayableTracks = range.size();
auto nPlaying = (range - &PlayableTrack::GetMute).size();
for (auto track : tracks.Any<PlayableTrack>())
// will set both of a stereo pair
track->SetSolo( (nPlaying==1) && (nPlayableTracks > 1 ) && !track->GetMute() );
}
}
ProjectHistory::Get( project ).ModifyState(true);
TrackFocus::Get( project ).UpdateAccessibility();
}
void DoTrackSolo(SneedacityProject &project, Track *t, bool exclusive)
{
const auto &settings = ProjectSettings::Get( project );
auto &tracks = TrackList::Get( project );
// Whatever t is, replace with lead channel
t = *tracks.FindLeader(t);
const auto pt = dynamic_cast<PlayableTrack *>( t );
if (!pt)
return;
bool bWasSolo = pt->GetSolo();
bool bSoloMultiple = !settings.IsSoloSimple() ^ exclusive;
// Standard and Simple solo have opposite defaults:
// Standard - Behaves as individual buttons, shift=radio buttons
// Simple - Behaves as radio buttons, shift=individual
// In addition, Simple solo will mute/unmute tracks
// when in standard radio button mode.
if ( bSoloMultiple )
{
for (auto channel : TrackList::Channels(pt))
channel->SetSolo( !bWasSolo );
}
else
{
// Normal click solo this track only, mute everything else.
// OR unmute and unsolo everything.
for (auto leader : tracks.Leaders<PlayableTrack>()) {
const auto group = TrackList::Channels(leader);
bool chosen = (t == leader);
for (auto channel : group) {
if (chosen) {
channel->SetSolo( !bWasSolo );
if( settings.IsSoloSimple() )
channel->SetMute( false );
}
else {
channel->SetSolo( false );
if( settings.IsSoloSimple() )
channel->SetMute( !bWasSolo );
}
}
}
}
ProjectHistory::Get( project ).ModifyState(true);
TrackFocus::Get( project ).UpdateAccessibility();
}
void DoRemoveTrack(SneedacityProject &project, Track * toRemove)
{
auto &tracks = TrackList::Get( project );
auto &trackFocus = TrackFocus::Get( project );
auto &window = ProjectWindow::Get( project );
// If it was focused, then NEW focus is the next or, if
// unavailable, the previous track. (The NEW focus is set
// after the track has been removed.)
bool toRemoveWasFocused = trackFocus.Get() == toRemove;
Track* newFocus{};
if (toRemoveWasFocused) {
auto iterNext = tracks.FindLeader(toRemove), iterPrev = iterNext;
newFocus = *++iterNext;
if (!newFocus) {
newFocus = *--iterPrev;
}
}
wxString name = toRemove->GetName();
auto channels = TrackList::Channels(toRemove);
// Be careful to post-increment over positions that get erased!
auto &iter = channels.first;
while (iter != channels.end())
tracks.Remove( * iter++ );
if (toRemoveWasFocused)
trackFocus.Set( newFocus );
ProjectHistory::Get( project ).PushState(
XO("Removed track '%s.'").Format( name ),
XO("Track Remove"));
}
void DoMoveTrack
(SneedacityProject &project, Track* target, MoveChoice choice)
{
auto &tracks = TrackList::Get( project );
TranslatableString longDesc, shortDesc;
switch (choice)
{
case OnMoveTopID:
/* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
longDesc = XO("Moved '%s' to Top");
shortDesc = XO("Move Track to Top");
// TODO: write TrackList::Rotate to do this in one step and avoid emitting
// an event for each swap
while (tracks.CanMoveUp(target))
tracks.Move(target, true);
break;
case OnMoveBottomID:
/* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
longDesc = XO("Moved '%s' to Bottom");
shortDesc = XO("Move Track to Bottom");
// TODO: write TrackList::Rotate to do this in one step and avoid emitting
// an event for each swap
while (tracks.CanMoveDown(target))
tracks.Move(target, false);
break;
default:
bool bUp = (OnMoveUpID == choice);
tracks.Move(target, bUp);
longDesc =
/* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
bUp? XO("Moved '%s' Up")
: XO("Moved '%s' Down");
shortDesc =
/* i18n-hint: Past tense of 'to move', as in 'moved audio track up'.*/
bUp? XO("Move Track Up")
: XO("Move Track Down");
}
longDesc.Format(target->GetName());
ProjectHistory::Get( project ).PushState(longDesc, shortDesc);
}
}