Audacity 3.2.0
src/effects/Generator.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Generator.h
6
7 Two Abstract classes, Generator, and BlockGenerator, that effects which
8 generate audio should derive from.
9
10 Block Generator breaks the synthesis task up into smaller parts.
11
12 Dominic Mazzoni
13 Vaughan Johnson
14
15**********************************************************************/
16
17#include "Generator.h"
18
19#include "Project.h"
20#include "Prefs.h"
21#include "SyncLock.h"
22#include "ViewInfo.h"
23#include "../WaveTrack.h"
24#include "../prefs/TracksBehaviorsPrefs.h"
25
26#include "TimeWarper.h"
27
28#include "../widgets/AudacityMessageBox.h"
29
31{
32 const auto duration = settings.extra.GetDuration();
33
34 // Set up mOutputTracks.
35 // This effect needs all for sync-lock grouping.
36 this->CopyInputTracks(true);
37
38 // Iterate over the tracks
39 bool bGoodResult = true;
40 int ntrack = 0;
41
42 mOutputTracks->Any().VisitWhile( bGoodResult,
43 [&](WaveTrack *track, const Track::Fallthrough &fallthrough) {
44 if (!track->GetSelected())
45 return fallthrough();
46 bool editClipCanMove = GetEditClipsCanMove();
47
48 //if we can't move clips, and we're generating into an empty space,
49 //make sure there's room.
50 if (!editClipCanMove &&
51 track->IsEmpty(mT0, mT1+1.0/track->GetRate()) &&
52 !track->IsEmpty(mT0,
53 mT0 + duration - (mT1 - mT0) - 1.0 / track->GetRate()))
54 {
56 XO("There is not enough room available to generate the audio"),
57 wxICON_STOP,
58 XO("Error") );
59 Failure();
60 bGoodResult = false;
61 return;
62 }
63
64 if (duration > 0.0)
65 {
66 auto pProject = FindProject();
67 // Create a temporary track
68 auto tmp = track->EmptyCopy();
69 BeforeTrack(*track);
71
72 // Fill it with data
73 if (!GenerateTrack(settings, &*tmp, *track, ntrack))
74 bGoodResult = false;
75 else {
76 // Transfer the data from the temporary track to the actual one
77 tmp->Flush();
78 PasteTimeWarper warper{ mT1, mT0 + duration };
79 const auto &selectedRegion =
80 ViewInfo::Get( *pProject ).selectedRegion;
81 track->ClearAndPaste(
82 selectedRegion.t0(), selectedRegion.t1(),
83 &*tmp, true, false, &warper);
84 }
85
86 if (!bGoodResult) {
87 Failure();
88 return;
89 }
90 }
91 else
92 {
93 // If the duration is zero, there's no need to actually
94 // generate anything
95 track->Clear(mT0, mT1);
96 }
97
98 ntrack++;
99 },
100 [&](Track *t) {
102 t->SyncLockAdjust(mT1, mT0 + duration);
103 }
104 }
105 );
106
107 if (bGoodResult) {
108 Success();
109
110 this->ReplaceProcessedTracks(bGoodResult);
111
112 mT1 = mT0 + duration; // Update selection.
113 }
114
115 return bGoodResult;
116}
117
119 WaveTrack *tmp, const WaveTrack &track, int ntrack)
120{
121 bool bGoodResult = true;
122 numSamples = track.TimeToLongSamples(settings.extra.GetDuration());
123 decltype(numSamples) i = 0;
124 Floats data{ tmp->GetMaxBlockSize() };
125
126 while ((i < numSamples) && bGoodResult) {
127 const auto block =
129
130 GenerateBlock(data.get(), track, block);
131
132 // Add the generated data to the temporary track
133 tmp->Append((samplePtr)data.get(), floatSample, block);
134 i += block;
135
136 // Update the progress meter
137 if (TrackProgress(ntrack,
138 i.as_double() /
140 bGoodResult = false;
141 }
142 return bGoodResult;
143}
#define XO(s)
Definition: Internat.h:31
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
@ floatSample
Definition: SampleFormat.h:34
char * samplePtr
Definition: SampleFormat.h:49
Contains declarations for TimeWarper, IdentityTimeWarper, ShiftTimeWarper, LinearTimeWarper,...
static Settings & settings()
Definition: TrackInfo.cpp:87
bool GetEditClipsCanMove()
Definition: WaveTrack.cpp:2852
virtual void GenerateBlock(float *data, const WaveTrack &track, size_t block)=0
bool GenerateTrack(EffectSettings &settings, WaveTrack *tmp, const WaveTrack &track, int ntrack) override
sampleCount numSamples
Definition: Generator.h:61
double mT1
Definition: EffectBase.h:107
std::shared_ptr< TrackList > mOutputTracks
Definition: EffectBase.h:105
double mT0
Definition: EffectBase.h:106
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: EffectBase.cpp:236
const AudacityProject * FindProject() const
Definition: EffectBase.cpp:315
void CopyInputTracks(bool allSyncLockSelected=false)
Definition: Effect.cpp:739
int MessageBox(const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={}) const
Definition: Effect.cpp:869
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:691
Performs effect computation.
virtual void BeforeTrack(const WaveTrack &WXUNUSED(track))
Definition: Generator.h:39
virtual bool GenerateTrack(EffectSettings &settings, WaveTrack *tmp, const WaveTrack &track, int ntrack)=0
virtual void BeforeGenerate()
Definition: Generator.h:38
virtual void Failure()
Definition: Generator.h:44
AUDACITY_DLL_API bool Process(EffectInstance &instance, EffectSettings &settings) override
Actually do the effect here.
virtual void Success()
Definition: Generator.h:43
Unit slope but with either a jump (pasting more) or a flat interval (pasting less)
Definition: TimeWarper.h:181
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: SampleTrack.cpp:35
static bool IsSyncLockSelected(const Track *pTrack)
Definition: SyncLock.cpp:43
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:225
bool GetSelected() const
Definition: Track.h:461
Continuation<> Fallthrough
Type of arguments passed as optional second parameter to TypeSwitch<void>() cases.
Definition: Track.h:533
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
A Track that contains audio waveform data.
Definition: WaveTrack.h:57
size_t GetMaxBlockSize() const override
This returns a nonnegative number of samples meant to size a memory buffer.
Definition: WaveTrack.cpp:1808
size_t GetBestBlockSize(sampleCount t) const override
This returns a nonnegative number of samples meant to size a memory buffer.
Definition: WaveTrack.cpp:1790
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:790
bool IsEmpty(double t0, double t1) const
Returns true if there are no WaveClips in the specified region.
Definition: WaveTrack.cpp:607
bool Append(constSamplePtr buffer, sampleFormat format, size_t len, unsigned int stride=1) override
Definition: WaveTrack.cpp:1768
double GetRate() const override
Definition: WaveTrack.cpp:481
void ClearAndPaste(double t0, double t1, const Track *src, bool preserve=true, bool merge=true, const TimeWarper *effectWarper=NULL)
Definition: WaveTrack.cpp:917
Holder EmptyCopy(const SampleBlockFactoryPtr &pFactory={}, bool keepLink=true) const
Definition: WaveTrack.cpp:707
double as_double() const
Definition: SampleCount.h:45
Externalized state of a plug-in.