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;
50  gPrefs->Read(wxT("/GUI/EditClipCanMove"), &editClipCanMove, true);
51 
52  //if we can't move clips, and we're generating into an empty space,
53  //make sure there's room.
54  if (!editClipCanMove &&
55  track->IsEmpty(mT0, mT1+1.0/track->GetRate()) &&
56  !track->IsEmpty(mT0, mT0+GetDuration()-(mT1-mT0)-1.0/track->GetRate()))
57  {
59  _("There is not enough room available to generate the audio"),
60  wxICON_STOP,
61  _("Error"));
62  Failure();
63  return false;
64  }
65 
66  if (GetDuration() > 0.0)
67  {
69  // Create a temporary track
72  track->GetRate())
73  );
74  BeforeTrack(*track);
76 
77  // Fill it with data
78  if (!GenerateTrack(&*tmp, *track, ntrack))
79  bGoodResult = false;
80  else {
81  // Transfer the data from the temporary track to the actual one
82  tmp->Flush();
83  StepTimeWarper warper{
85  track->ClearAndPaste(
86  p->GetSel0(), p->GetSel1(), &*tmp, true, false, &warper);
87  }
88 
89  if (!bGoodResult) {
90  Failure();
91  return false;
92  }
93  }
94  else
95  {
96  // If the duration is zero, there's no need to actually
97  // generate anything
98  track->Clear(mT0, mT1);
99  }
100 
101  ntrack++;
102  }
103  else if (t->IsSyncLockSelected()) {
104  t->SyncLockAdjust(mT1, mT0 + GetDuration());
105  }
106  // Move on to the next track
107  t = iter.Next();
108  }
109 
110  Success();
111 
112  this->ReplaceProcessedTracks(bGoodResult);
113 
114  mT1 = mT0 + GetDuration(); // Update selection.
115 
116  return true;
117 }
118 
120  const WaveTrack &track,
121  int ntrack)
122 {
123  bool bGoodResult = true;
124  numSamples = track.TimeToLongSamples(GetDuration());
125  decltype(numSamples) i = 0;
126  Floats data{ tmp->GetMaxBlockSize() };
127 
128  while ((i < numSamples) && bGoodResult) {
129  const auto block =
130  limitSampleBufferSize( tmp->GetBestBlockSize(i), numSamples - i );
131 
132  GenerateBlock(data.get(), track, block);
133 
134  // Add the generated data to the temporary track
135  tmp->Append((samplePtr)data.get(), floatSample, block);
136  i += block;
137 
138  // Update the progress meter
139  if (TrackProgress(ntrack,
140  i.as_double() /
141  numSamples.as_double()))
142  bGoodResult = false;
143  }
144  return bGoodResult;
145 }
Like identity but with a jump.
Definition: TimeWarper.h:180
double mT1
Definition: Effect.h:464
int MessageBox(const wxString &message, long style=DefaultMessageBoxStyle, const wxString &titleStr=wxString{})
Definition: Effect.cpp:2665
bool TrackProgress(int whichTrack, double frac, const wxString &=wxEmptyString)
Definition: Effect.cpp:1988
double GetSel0() const
Definition: Project.h:186
virtual void BeforeGenerate()
Definition: Generator.h:41
double GetDuration() override
Definition: Effect.cpp:747
void CopyInputTracks()
Definition: Effect.cpp:2039
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2170
void ClearAndPaste(double t0, double t1, const Track *src, bool preserve=true, bool merge=true, const TimeWarper *effectWarper=NULL)
Definition: WaveTrack.cpp:786
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:699
size_t GetBestBlockSize(sampleCount t) const
Definition: WaveTrack.cpp:1613
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:158
wxFileConfig * gPrefs
Definition: Prefs.cpp:72
std::unique_ptr< WaveTrack > Holder
Definition: WaveTrack.h:90
bool GenerateTrack(WaveTrack *tmp, const WaveTrack &track, int ntrack) override
Definition: Generator.cpp:119
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:94
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:1563
size_t GetMaxBlockSize() const
Definition: WaveTrack.cpp:1631
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:394
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
virtual void Failure()
Definition: Generator.h:47
TrackFactory * mFactory
Definition: Effect.h:460
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1849
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:300
sampleFormat GetSampleFormat() const
Definition: WaveTrack.h:146
double GetRate() const
Definition: WaveTrack.cpp:424
double GetSel1() const
Definition: Project.h:187
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:516
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:462
double mT0
Definition: Effect.h:463