Audacity  3.0.3
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 "ViewInfo.h"
22 #include "../WaveTrack.h"
23 #include "../prefs/TracksBehaviorsPrefs.h"
24 
25 #include "TimeWarper.h"
26 
27 #include "../widgets/AudacityMessageBox.h"
28 
30 {
31  if (GetDuration() < 0.0)
32  return false;
33 
34 
35  // Set up mOutputTracks.
36  // This effect needs all for sync-lock grouping.
37  this->CopyInputTracks(true);
38 
39  // Iterate over the tracks
40  bool bGoodResult = true;
41  int ntrack = 0;
42 
43  mOutputTracks->Any().VisitWhile( bGoodResult,
44  [&](WaveTrack *track, const Track::Fallthrough &fallthrough) {
45  if (!track->GetSelected())
46  return fallthrough();
47  bool editClipCanMove = GetEditClipsCanMove();
48 
49  //if we can't move clips, and we're generating into an empty space,
50  //make sure there's room.
51  if (!editClipCanMove &&
52  track->IsEmpty(mT0, mT1+1.0/track->GetRate()) &&
53  !track->IsEmpty(mT0, mT0+GetDuration()-(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 (GetDuration() > 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(&*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+GetDuration() };
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) {
101  if (t->IsSyncLockSelected()) {
102  t->SyncLockAdjust(mT1, mT0 + GetDuration());
103  }
104  }
105  );
106 
107  if (bGoodResult) {
108  Success();
109 
110  this->ReplaceProcessedTracks(bGoodResult);
111 
112  mT1 = mT0 + GetDuration(); // Update selection.
113  }
114 
115  return bGoodResult;
116 }
117 
119  const WaveTrack &track,
120  int ntrack)
121 {
122  bool bGoodResult = true;
124  decltype(numSamples) i = 0;
125  Floats data{ tmp->GetMaxBlockSize() };
126 
127  while ((i < numSamples) && bGoodResult) {
128  const auto block =
130 
131  GenerateBlock(data.get(), track, block);
132 
133  // Add the generated data to the temporary track
134  tmp->Append((samplePtr)data.get(), floatSample, block);
135  i += block;
136 
137  // Update the progress meter
138  if (TrackProgress(ntrack,
139  i.as_double() /
141  bGoodResult = false;
142  }
143  return bGoodResult;
144 }
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:161
Generator::Failure
virtual void Failure()
Definition: Generator.h:47
Effect::FindProject
const AudacityProject * FindProject() const
Definition: Effect.cpp:2272
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:69
Effect::GetDuration
double GetDuration() override
Definition: Effect.cpp:816
BlockGenerator::GenerateTrack
bool GenerateTrack(WaveTrack *tmp, const WaveTrack &track, int ntrack) override
Definition: Generator.cpp:118
Effect::MessageBox
int MessageBox(const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={})
Definition: Effect.cpp:2482
Effect::CopyInputTracks
void CopyInputTracks(bool allSyncLockSelected=false)
Definition: Effect.cpp:2071
Project.h
TimeWarper.h
Contains declarations for TimeWarper, IdentityTimeWarper, ShiftTimeWarper, LinearTimeWarper,...
Generator::GenerateTrack
virtual bool GenerateTrack(WaveTrack *tmp, const WaveTrack &track, int ntrack)=0
Effect::mT1
double mT1
Definition: Effect.h:467
BlockGenerator::GenerateBlock
virtual void GenerateBlock(float *data, const WaveTrack &track, size_t block)=0
GetEditClipsCanMove
bool GetEditClipsCanMove()
Definition: TracksBehaviorsPrefs.cpp:143
XO
#define XO(s)
Definition: Internat.h:31
BlockGenerator::numSamples
sampleCount numSamples
Definition: Generator.h:64
WaveTrack::ClearAndPaste
void ClearAndPaste(double t0, double t1, const Track *src, bool preserve=true, bool merge=true, const TimeWarper *effectWarper=NULL)
Definition: WaveTrack.cpp:847
Generator::Success
virtual void Success()
Definition: Generator.h:46
WaveTrack::EmptyCopy
Holder EmptyCopy(const SampleBlockFactoryPtr &pFactory={}) const
Definition: WaveTrack.cpp:666
Generator::BeforeTrack
virtual void BeforeTrack(const WaveTrack &WXUNUSED(track))
Definition: Generator.h:42
floatSample
@ floatSample
Definition: SampleFormat.h:34
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:196
WaveTrack::Clear
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:751
Effect::mT0
double mT0
Definition: Effect.h:466
sampleCount::as_double
double as_double() const
Definition: SampleCount.h:45
Generator::BeforeGenerate
virtual void BeforeGenerate()
Definition: Generator.h:41
WaveTrack::IsEmpty
bool IsEmpty(double t0, double t1) const
Returns true if there are no WaveClips in the specified region.
Definition: WaveTrack.cpp:556
Effect::ReplaceProcessedTracks
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2193
Track::Fallthrough
Continuation<> Fallthrough
Type of arguments passed as optional second parameter to TypeSwitch<void>() cases.
Definition: Track.h:504
WaveTrack::Append
bool Append(constSamplePtr buffer, sampleFormat format, size_t len, unsigned int stride=1)
Append the sample data to the WaveTrack. You must call Flush() after the last Append.
Definition: WaveTrack.cpp:1632
Effect::mOutputTracks
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:465
ViewInfo.h
Track::GetSelected
bool GetSelected() const
Definition: Track.h:431
samplePtr
char * samplePtr
Definition: SampleFormat.h:49
WaveTrack::GetMaxBlockSize
size_t GetMaxBlockSize() const
Definition: WaveTrack.cpp:1669
WaveTrack::TimeToLongSamples
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1865
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:239
WaveTrack::GetBestBlockSize
size_t GetBestBlockSize(sampleCount t) const
Definition: WaveTrack.cpp:1651
Prefs.h
limitSampleBufferSize
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
PasteTimeWarper
Unit slope but with either a jump (pasting more) or a flat interval (pasting less)
Definition: TimeWarper.h:181
Generator.h
Effect::TrackProgress
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={})
Definition: Effect.cpp:2025
ArrayOf< float >
Generator::Process
AUDACITY_DLL_API bool Process() override
Definition: Generator.cpp:29
WaveTrack::GetRate
double GetRate() const
Definition: WaveTrack.cpp:452