Audacity  2.2.2
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 "../WaveTrack.h"
22 
23 #include "TimeWarper.h"
24 
25 #include "../MemoryX.h"
26 #include "../widgets/ErrorDialog.h"
27 
29 {
30  if (GetDuration() < 0.0)
31  return false;
32 
33 
34  // Set up mOutputTracks.
35  // This effect needs Track::All for sync-lock grouping.
37 
38  // Iterate over the tracks
39  bool bGoodResult = true;
40  int ntrack = 0;
41  TrackListIterator iter(mOutputTracks.get());
42  Track* t = iter.First();
43 
44  while (t != NULL)
45  {
46  if (t->GetKind() == Track::Wave && t->GetSelected()) {
47  WaveTrack* track = (WaveTrack*)t;
48 
49  bool editClipCanMove = gPrefs->GetEditClipsCanMove();
50 
51  //if we can't move clips, and we're generating into an empty space,
52  //make sure there's room.
53  if (!editClipCanMove &&
54  track->IsEmpty(mT0, mT1+1.0/track->GetRate()) &&
55  !track->IsEmpty(mT0, mT0+GetDuration()-(mT1-mT0)-1.0/track->GetRate()))
56  {
58  _("There is not enough room available to generate the audio"),
59  wxICON_STOP,
60  _("Error"));
61  Failure();
62  return false;
63  }
64 
65  if (GetDuration() > 0.0)
66  {
68  // Create a temporary track
71  track->GetRate())
72  );
73  BeforeTrack(*track);
75 
76  // Fill it with data
77  if (!GenerateTrack(&*tmp, *track, ntrack))
78  bGoodResult = false;
79  else {
80  // Transfer the data from the temporary track to the actual one
81  tmp->Flush();
82  StepTimeWarper warper{
84  track->ClearAndPaste(
85  p->GetSel0(), p->GetSel1(), &*tmp, true, false, &warper);
86  }
87 
88  if (!bGoodResult) {
89  Failure();
90  return false;
91  }
92  }
93  else
94  {
95  // If the duration is zero, there's no need to actually
96  // generate anything
97  track->Clear(mT0, mT1);
98  }
99 
100  ntrack++;
101  }
102  else if (t->IsSyncLockSelected()) {
103  t->SyncLockAdjust(mT1, mT0 + GetDuration());
104  }
105  // Move on to the next track
106  t = iter.Next();
107  }
108 
109  Success();
110 
111  this->ReplaceProcessedTracks(bGoodResult);
112 
113  mT1 = mT0 + GetDuration(); // Update selection.
114 
115  return true;
116 }
117 
119  const WaveTrack &track,
120  int ntrack)
121 {
122  bool bGoodResult = true;
123  numSamples = track.TimeToLongSamples(GetDuration());
124  decltype(numSamples) i = 0;
125  Floats data{ tmp->GetMaxBlockSize() };
126 
127  while ((i < numSamples) && bGoodResult) {
128  const auto block =
129  limitSampleBufferSize( tmp->GetBestBlockSize(i), numSamples - i );
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() /
140  numSamples.as_double()))
141  bGoodResult = false;
142  }
143  return bGoodResult;
144 }
Like identity but with a jump.
Definition: TimeWarper.h:180
double mT1
Definition: Effect.h:461
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
int MessageBox(const wxString &message, long style=DefaultMessageBoxStyle, const wxString &titleStr=wxString{})
Definition: Effect.cpp:2660
bool TrackProgress(int whichTrack, double frac, const wxString &=wxEmptyString)
Definition: Effect.cpp:1985
double GetSel0() const
Definition: Project.h:204
virtual void BeforeGenerate()
Definition: Generator.h:41
double GetDuration() override
Definition: Effect.cpp:722
void CopyInputTracks()
Definition: Effect.cpp:2036
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2162
void ClearAndPaste(double t0, double t1, const Track *src, bool preserve=true, bool merge=true, const TimeWarper *effectWarper=NULL)
Definition: WaveTrack.cpp:782
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:678
size_t GetBestBlockSize(sampleCount t) const
Definition: WaveTrack.cpp:1607
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: Types.h:178
Contains declarations for TimeWarper, IdentityTimeWarper, ShiftTimeWarper, LinearTimeWarper, LinearInputRateSlideTimeWarper, LinearOutputRateSlideTimeWarper, LinearInputInverseRateTimeWarper, GeometricInputRateTimeWarper, GeometricOutputRateTimeWarper classes.
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:176
std::unique_ptr< WaveTrack > Holder
Definition: WaveTrack.h:90
bool GenerateTrack(WaveTrack *tmp, const WaveTrack &track, int ntrack) override
Definition: Generator.cpp:118
char * samplePtr
Definition: Types.h:203
A Track that contains audio waveform data.
Definition: WaveTrack.h:60
Fundamental data object of Audacity, placed in the TrackPanel. Classes derived form it include the Wa...
Definition: Track.h:101
bool Process() override
Definition: Generator.cpp:28
virtual bool GenerateTrack(WaveTrack *tmp, const WaveTrack &track, int ntrack)=0
void Append(samplePtr buffer, sampleFormat format, size_t len, unsigned int stride=1, XMLWriter *blockFileLog=NULL)
Append the sample data to the WaveTrack. You must call Flush() after the last Append.
Definition: WaveTrack.cpp:1557
size_t GetMaxBlockSize() const
Definition: WaveTrack.cpp:1625
virtual void Success()
Definition: Generator.h:46
std::unique_ptr< WaveTrack > NewWaveTrack(sampleFormat format=(sampleFormat) 0, double rate=0)
Definition: WaveTrack.cpp:78
An iterator for a TrackList.
Definition: Track.h:401
virtual void Failure()
Definition: Generator.h:47
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
TrackFactory * mFactory
Definition: Effect.h:457
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1843
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
sampleFormat GetSampleFormat() const
Definition: WaveTrack.h:146
double GetRate() const
Definition: WaveTrack.cpp:398
double GetSel1() const
Definition: Project.h:205
virtual void BeforeTrack(const WaveTrack &WXUNUSED(track))
Definition: Generator.h:42
bool IsEmpty(double t0, double t1) const
Returns true if there are no WaveClips in the specified region.
Definition: WaveTrack.cpp:495
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:459
bool GetEditClipsCanMove()
Definition: Prefs.cpp:158
double mT0
Definition: Effect.h:460