forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cpp
More file actions
144 lines (115 loc) · 3.94 KB
/
Copy pathGenerator.cpp
File metadata and controls
144 lines (115 loc) · 3.94 KB
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
/**********************************************************************
Audacity: A Digital Audio Editor
Generator.h
Two Abstract classes, Generator, and BlockGenerator, that effects which
generate audio should derive from.
Block Generator breaks the synthesis task up into smaller parts.
Dominic Mazzoni
Vaughan Johnson
**********************************************************************/
#include "Generator.h"
#include "../Project.h"
#include "../Prefs.h"
#include "../ViewInfo.h"
#include "../WaveTrack.h"
#include "../prefs/TracksBehaviorsPrefs.h"
#include "TimeWarper.h"
#include "../widgets/AudacityMessageBox.h"
bool Generator::Process()
{
if (GetDuration() < 0.0)
return false;
// Set up mOutputTracks.
// This effect needs all for sync-lock grouping.
this->CopyInputTracks(true);
// Iterate over the tracks
bool bGoodResult = true;
int ntrack = 0;
mOutputTracks->Any().VisitWhile( bGoodResult,
[&](WaveTrack *track, const Track::Fallthrough &fallthrough) {
if (!track->GetSelected())
return fallthrough();
bool editClipCanMove = GetEditClipsCanMove();
//if we can't move clips, and we're generating into an empty space,
//make sure there's room.
if (!editClipCanMove &&
track->IsEmpty(mT0, mT1+1.0/track->GetRate()) &&
!track->IsEmpty(mT0, mT0+GetDuration()-(mT1-mT0)-1.0/track->GetRate()))
{
Effect::MessageBox(
XO("There is not enough room available to generate the audio"),
wxICON_STOP,
XO("Error") );
Failure();
bGoodResult = false;
return;
}
if (GetDuration() > 0.0)
{
auto pProject = FindProject();
// Create a temporary track
auto tmp = track->EmptyCopy();
BeforeTrack(*track);
BeforeGenerate();
// Fill it with data
if (!GenerateTrack(&*tmp, *track, ntrack))
bGoodResult = false;
else {
// Transfer the data from the temporary track to the actual one
tmp->Flush();
PasteTimeWarper warper{ mT1, mT0+GetDuration() };
const auto &selectedRegion =
ViewInfo::Get( *pProject ).selectedRegion;
track->ClearAndPaste(
selectedRegion.t0(), selectedRegion.t1(),
&*tmp, true, false, &warper);
}
if (!bGoodResult) {
Failure();
return;
}
}
else
{
// If the duration is zero, there's no need to actually
// generate anything
track->Clear(mT0, mT1);
}
ntrack++;
},
[&](Track *t) {
if (t->IsSyncLockSelected()) {
t->SyncLockAdjust(mT1, mT0 + GetDuration());
}
}
);
if (bGoodResult) {
Success();
this->ReplaceProcessedTracks(bGoodResult);
mT1 = mT0 + GetDuration(); // Update selection.
}
return bGoodResult;
}
bool BlockGenerator::GenerateTrack(WaveTrack *tmp,
const WaveTrack &track,
int ntrack)
{
bool bGoodResult = true;
numSamples = track.TimeToLongSamples(GetDuration());
decltype(numSamples) i = 0;
Floats data{ tmp->GetMaxBlockSize() };
while ((i < numSamples) && bGoodResult) {
const auto block =
limitSampleBufferSize( tmp->GetBestBlockSize(i), numSamples - i );
GenerateBlock(data.get(), track, block);
// Add the generated data to the temporary track
tmp->Append((samplePtr)data.get(), floatSample, block);
i += block;
// Update the progress meter
if (TrackProgress(ntrack,
i.as_double() /
numSamples.as_double()))
bGoodResult = false;
}
return bGoodResult;
}