Audacity  3.0.3
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ODDecodeTask Class Referenceabstract

A class representing a modular task to be used with the On-Demand structures. More...

#include <ODDecodeTask.h>

Inheritance diagram for ODDecodeTask:
[legend]
Collaboration diagram for ODDecodeTask:
[legend]

Public Member Functions

 ODDecodeTask ()
 Creates a NEW task that decodes files. More...
 
virtual ~ODDecodeTask ()
 
virtual bool SeekingAllowed ()
 
void DemandTrackUpdate (WaveTrack *track, double seconds) override
 
const char * GetTaskName () override
 Return the task name. More...
 
TranslatableString GetTip () override
 
unsigned int GetODType () override
 Subclasses should override to return respective type. More...
 
virtual ODFileDecoderCreateFileDecoder (const wxString &fileName)=0
 Creates an ODFileDecoder that decodes a file of filetype the subclass handles. More...
 
virtual ODFileDecoderGetOrCreateMatchingFileDecoder (ODDecodeBlockFile *blockFile)
 
virtual int GetNumFileDecoders ()
 
- Public Member Functions inherited from ODTask
 ODTask ()
 Constructs an ODTask. More...
 
virtual ~ODTask ()
 
virtual std::unique_ptr< ODTaskClone () const =0
 
void DoSome (float amountWork=0.0)
 
void DoAll ()
 Call DoSome until PercentComplete >= 1.0. More...
 
virtual float PercentComplete ()
 return the amount of the task that has been completed. 0.0 to 1.0 More...
 
virtual bool UsesCustomWorkUntilPercentage ()
 
virtual float ComputeNextWorkUntilPercentageComplete ()
 
virtual bool CanMergeWith (ODTask *otherTask)
 
virtual void StopUsingWaveTrack (WaveTrack *track)
 
virtual void ReplaceWaveTrack (Track *oldTrack, const std::shared_ptr< Track > &newTrack)
 Replaces all instances to a wavetrack with a NEW one, effectively transferring the task. More...
 
void AddWaveTrack (const std::shared_ptr< WaveTrack > &track)
 Adds a WaveTrack to do the task for. More...
 
virtual int GetNumWaveTracks ()
 
virtual std::shared_ptr< WaveTrackGetWaveTrack (int i)
 
bool IsComplete ()
 return More...
 
void TerminateAndBlock ()
 
virtual void Terminate ()
 releases memory that the ODTask owns. Subclasses should override. More...
 
virtual sampleCount GetDemandSample () const
 
virtual void SetDemandSample (sampleCount sample)
 
virtual void RecalculatePercentComplete ()
 does an od update and then recalculates the data. More...
 
int GetTaskNumber ()
 returns the number of tasks created before this instance. More...
 
void SetNeedsODUpdate ()
 
bool GetNeedsODUpdate ()
 
void ResetNeedsODUpdate ()
 
virtual bool IsTaskAssociatedWithProject (AudacityProject *proj)
 returns true if the task is associated with the project. More...
 
bool IsRunning ()
 

Protected Member Functions

void CalculatePercentComplete () override
 recalculates the percentage complete. More...
 
void DoSomeInternal () override
 Computes and writes the data for one BlockFile if it still has a refcount. More...
 
void Update () override
 by default creates the order of the wavetrack to load. More...
 
void OrderBlockFiles (std::vector< std::weak_ptr< ODDecodeBlockFile > > &unorderedBlocks)
 Orders the input as either On-Demand or default layered order. More...
 
- Protected Member Functions inherited from ODTask
virtual void ODUpdate ()
 
void SetIsRunning (bool value)
 

Protected Attributes

std::vector< std::weak_ptr< ODDecodeBlockFile > > mBlockFiles
 
std::vector< std::unique_ptr< ODFileDecoder > > mDecoders
 
int mMaxBlockFiles
 
- Protected Attributes inherited from ODTask
int mTaskNumber
 
volatile float mPercentComplete
 
ODLock mPercentCompleteMutex
 
volatile bool mDoingTask
 
volatile bool mTaskStarted
 
volatile bool mTerminate
 
ODLock mTerminateMutex
 
ODLock mBlockUntilTerminateMutex
 
std::vector< std::weak_ptr< WaveTrack > > mWaveTracks
 
ODLock mWaveTrackMutex
 
sampleCount mDemandSample
 
ODLock mDemandSampleMutex
 
volatile bool mIsRunning
 
ODLock mIsRunningMutex
 

Additional Inherited Members

- Public Types inherited from ODTask
enum  {
  eODNone = 0x00000000, eODFLAC = 0x00000001, eODMP3 = 0x00000002, eODFFMPEG = 0x00000004,
  eODPCMSummary = 0x00001000, eODOTHER = 0x10000000
}
 
- Public Attributes inherited from ODTask
enum ODTask:: { ... }  ODTypeEnum
 

Detailed Description

A class representing a modular task to be used with the On-Demand structures.

Decodes a file into a simpleBlockFile, but not immediately.

Decodes files and writes block files in .au format, updating the ODDecodeBlockFile and the GUI of the newly available data.

This is an abstract class that subclasses will have to derive the types from. For any type there should only be one ODDecodeTask associated with a given track. There could be the ODDecodeBlockFiles of several FLACs in one track (after copy and pasting), so things aren't as simple as they seem - the implementation needs to be robust enough to allow all the user changes such as copy/paste, DELETE, and so on.

Definition at line 40 of file ODDecodeTask.h.

Constructor & Destructor Documentation

◆ ODDecodeTask()

ODDecodeTask::ODDecodeTask ( )

Creates a NEW task that decodes files.

Definition at line 29 of file ODDecodeTask.cpp.

30 {
31  mMaxBlockFiles = 0;
32 }

References mMaxBlockFiles.

◆ ~ODDecodeTask()

virtual ODDecodeTask::~ODDecodeTask ( )
inlinevirtual

Definition at line 44 of file ODDecodeTask.h.

44 {};

Member Function Documentation

◆ CalculatePercentComplete()

void ODDecodeTask::CalculatePercentComplete ( )
overrideprotectedvirtual

recalculates the percentage complete.

Implements ODTask.

Definition at line 121 of file ODDecodeTask.cpp.

122 {
123  mPercentCompleteMutex.Lock();
124  mPercentComplete = (float) 1.0 - ((float)mBlockFiles.size() / (mMaxBlockFiles+1));
125  mPercentCompleteMutex.Unlock();
126 }

References mBlockFiles, mMaxBlockFiles, ODTask::mPercentComplete, and ODTask::mPercentCompleteMutex.

Referenced by DoSomeInternal().

Here is the caller graph for this function:

◆ CreateFileDecoder()

virtual ODFileDecoder* ODDecodeTask::CreateFileDecoder ( const wxString &  fileName)
pure virtual

Creates an ODFileDecoder that decodes a file of filetype the subclass handles.

Implemented in ODDecodeFlacTask.

Referenced by GetOrCreateMatchingFileDecoder().

Here is the caller graph for this function:

◆ DemandTrackUpdate()

void ODDecodeTask::DemandTrackUpdate ( WaveTrack track,
double  seconds 
)
overridevirtual

changes the tasks associated with this Waveform to process the task from a different point in the track this is overridden from ODTask because certain classes don't allow users to seek sometimes, or not at all.

Reimplemented from ODTask.

Definition at line 257 of file ODDecodeTask.cpp.

258 {
259  //only update if the subclass says we can seek.
260  if(SeekingAllowed())
261  ODTask::DemandTrackUpdate(track,seconds);
262 }

References ODTask::DemandTrackUpdate(), and SeekingAllowed().

Here is the call graph for this function:

◆ DoSomeInternal()

void ODDecodeTask::DoSomeInternal ( )
overrideprotectedvirtual

Computes and writes the data for one BlockFile if it still has a refcount.

Implements ODTask.

Definition at line 36 of file ODDecodeTask.cpp.

37 {
38  if(mBlockFiles.size()<=0)
39  {
40  mPercentCompleteMutex.Lock();
41  mPercentComplete = 1.0;
42  mPercentCompleteMutex.Unlock();
43  return;
44  }
45 
46  ODFileDecoder* decoder;
47 
48  for(size_t j=0; j < mWaveTracks.size() && mBlockFiles.size();j++)
49  {
50  const auto bf = mBlockFiles[0].lock();
51  sampleCount blockStartSample = 0;
52  sampleCount blockEndSample = 0;
53  bool success =false;
54 
55  int ret = 1;
56 
57  if(bf)
58  {
59  //OD TODO: somehow pass the bf a reference to the decoder that manages its file.
60  //we need to ensure that the filename won't change or be moved. We do this by calling LockRead(),
61  //which the dirmanager::EnsureSafeFilename also does.
62  {
63  auto locker = bf->LockForRead();
64  //Get the decoder. If the file was moved, we need to create another one and init it.
65  decoder = GetOrCreateMatchingFileDecoder( &*bf );
66  if(!decoder->IsInitialized())
67  decoder->Init();
68  bf->SetODFileDecoder(decoder);
69  // Does not throw:
70  ret = bf->DoWriteBlockFile();
71  }
72 
73  if(ret >= 0) {
74  success = true;
75  blockStartSample = bf->GetStart();
76  blockEndSample = blockStartSample + bf->GetLength();
77  }
78  }
79  else
80  {
81  success = true;
82  // The block file disappeared.
83  //the waveform in the wavetrack now is shorter, so we need to update mMaxBlockFiles
84  //because now there is less work to do.
86  }
87 
88  if (success)
89  {
90  //take it out of the array - we are done with it.
91  mBlockFiles.erase(mBlockFiles.begin());
92  }
93  else
94  // The task does not make progress
95  ;
96 
97  //Release the refcount we placed on it if we are successful
98  if( bf && success ) {
99  //upddate the gui for all associated blocks. It doesn't matter that we're hitting more wavetracks then we should
100  //because this loop runs a number of times equal to the number of tracks, they probably are getting processed in
101  //the next iteration at the same sample window.
102  mWaveTrackMutex.Lock();
103  for(size_t i=0;i<mWaveTracks.size();i++)
104  {
105  auto waveTrack = mWaveTracks[i].lock();
106  if(waveTrack)
107  {
108 #if 0 // LLL: Commented out while removing OD files handling
109  waveTrack->AddInvalidRegion(blockStartSample,blockEndSample);
110 #endif
111  }
112  }
113  mWaveTrackMutex.Unlock();
114  }
115  }
116 
117  //update percentage complete.
119 }

References CalculatePercentComplete(), GetOrCreateMatchingFileDecoder(), ODFileDecoder::Init(), ODFileDecoder::IsInitialized(), mBlockFiles, mMaxBlockFiles, ODTask::mPercentComplete, ODTask::mPercentCompleteMutex, ODTask::mWaveTrackMutex, and ODTask::mWaveTracks.

Here is the call graph for this function:

◆ GetNumFileDecoders()

int ODDecodeTask::GetNumFileDecoders ( )
virtual

Definition at line 294 of file ODDecodeTask.cpp.

295 {
296  return mDecoders.size();
297 }

References mDecoders.

◆ GetODType()

unsigned int ODDecodeTask::GetODType ( )
inlineoverridevirtual

Subclasses should override to return respective type.

Reimplemented from ODTask.

Definition at line 59 of file ODDecodeTask.h.

59 { return eODNone; }

References ODTask::eODNone.

Referenced by GetOrCreateMatchingFileDecoder(), and Update().

Here is the caller graph for this function:

◆ GetOrCreateMatchingFileDecoder()

ODFileDecoder * ODDecodeTask::GetOrCreateMatchingFileDecoder ( ODDecodeBlockFile blockFile)
virtual

there could be the ODDecodeBlockFiles of several FLACs in one track (after copy and pasting) so we keep a list of decoders that keep track of the file names, etc, and check the blocks against them. Blocks that have IsDataAvailable()==false are blockfiles to be decoded. if BlockFile::GetDecodeType()==ODDecodeTask::GetODType() then this decoder should handle it. Decoders are accessible with the methods below. These aren't thread-safe and should only be called from the decoding thread.

Definition at line 270 of file ODDecodeTask.cpp.

271 {
272  ODFileDecoder* ret=NULL;
273  //see if the filename matches any of our decoders, if so, return it.
274  for(int i=0;i<(int)mDecoders.size();i++)
275  {
276  //we check filename and decode type, since two types of ODDecoders might work with the same filetype
277  //e.g., FFmpeg and LibMad import both do MP3s. TODO: is this necessary? in theory we filter this when
278  //updating our list of blockfiles.
279  if(mDecoders[i]->GetFileName()==blockFile->GetAudioFileName().GetFullPath() &&
280  GetODType() == blockFile->GetDecodeType() )
281  {
282  ret = mDecoders[i].get();
283  break;
284  }
285  }
286 
287  //otherwise, create and add one, and return it.
288  if(!ret)
289  {
290  ret=CreateFileDecoder(blockFile->GetAudioFileName().GetFullPath());
291  }
292  return ret;
293 }

References CreateFileDecoder(), ODDecodeBlockFile::GetAudioFileName(), ODDecodeBlockFile::GetDecodeType(), GetODType(), and mDecoders.

Referenced by DoSomeInternal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTaskName()

const char* ODDecodeTask::GetTaskName ( )
inlineoverridevirtual

Return the task name.

Reimplemented from ODTask.

Definition at line 54 of file ODDecodeTask.h.

54 { return "ODDecodeTask"; }

◆ GetTip()

TranslatableString ODDecodeTask::GetTip ( )
inlineoverridevirtual

Implements ODTask.

Definition at line 56 of file ODDecodeTask.h.

56 { return XO("Decoding Waveform"); }

References XO.

◆ OrderBlockFiles()

void ODDecodeTask::OrderBlockFiles ( std::vector< std::weak_ptr< ODDecodeBlockFile > > &  unorderedBlocks)
protected

Orders the input as either On-Demand or default layered order.

Definition at line 206 of file ODDecodeTask.cpp.

208 {
209  mBlockFiles.clear();
210  //TODO:order the blockfiles into our queue in a fancy convenient way. (this could be user-prefs)
211  //for now just put them in linear. We start the order from the first block that includes the ondemand sample
212  //(which the user sets by clicking.) note that this code is pretty hacky - it assumes that the array is sorted in time.
213 
214  //find the startpoint
215  auto processStartSample = GetDemandSample();
216  std::shared_ptr< ODDecodeBlockFile > firstBlock;
217  for(auto i = unorderedBlocks.size(); i--; )
218  {
219  auto ptr = unorderedBlocks[i].lock();
220  if(ptr)
221  {
222  //test if the blockfiles are near the task cursor. we use the last mBlockFiles[0] as our point of reference
223  //and add ones that are closer.
224  //since the order is linear right to left, this will add blocks so that the ones on the right side of the target
225  //are processed first, with the ones closer being processed earlier. Then the ones on the left side get processed.
226  if(mBlockFiles.size() &&
227  ptr->GetGlobalEnd() >= processStartSample &&
228  ( firstBlock->GetGlobalEnd() < processStartSample ||
229  ptr->GetGlobalStart() <= firstBlock->GetGlobalStart()) )
230  {
231  //insert at the front of the list if we get blockfiles that are after the demand sample
232  firstBlock = ptr;
233  mBlockFiles.insert(mBlockFiles.begin(), unorderedBlocks[i]);
234  }
235  else
236  {
237  //otherwise no priority
238  if ( !firstBlock )
239  firstBlock = ptr;
240  mBlockFiles.push_back(unorderedBlocks[i]);
241  }
242  if(mMaxBlockFiles< (int) mBlockFiles.size())
243  mMaxBlockFiles = mBlockFiles.size();
244  }
245  else
246  {
247  // The block file disappeared.
248  }
249  }
250 
251 }

References ODTask::GetDemandSample(), ODDecodeBlockFile::GetGlobalEnd(), ODDecodeBlockFile::GetGlobalStart(), mBlockFiles, and mMaxBlockFiles.

Referenced by Update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SeekingAllowed()

bool ODDecodeTask::SeekingAllowed ( )
virtual

Definition at line 128 of file ODDecodeTask.cpp.

129 {
130  for (unsigned int i = 0; i < mDecoders.size(); i++) {
131  if(!mDecoders[i]->SeekingAllowed())
132  return false;
133  }
134  return true;
135 }

References mDecoders.

Referenced by DemandTrackUpdate().

Here is the caller graph for this function:

◆ Update()

void ODDecodeTask::Update ( )
overrideprotectedvirtual

by default creates the order of the wavetrack to load.

Readjusts the blockfile order in the default manner. If we have had an ODRequest Then it updates in the OD manner.

Reimplemented from ODTask.

Definition at line 138 of file ODDecodeTask.cpp.

139 {
140  std::vector< std::weak_ptr< ODDecodeBlockFile > > tempBlocks;
141 
142  mWaveTrackMutex.Lock();
143 
144  for(size_t j=0;j<mWaveTracks.size();j++)
145  {
146  auto waveTrack = mWaveTracks[j].lock();
147  if(waveTrack)
148  {
149  BlockArray *blocks;
150  Sequence *seq;
151 
152  //gather all the blockfiles that we should process in the wavetrack.
153  for (const auto &clip : waveTrack->GetAllClips()) {
154  seq = clip->GetSequence();
155  //TODO:this lock is way to big since the whole file is one sequence. find a way to break it down.
156  seq->LockDeleteUpdateMutex();
157 
158  //See Sequence::Delete() for why need this for now..
159  blocks = clip->GetSequenceBlockArray();
160  int i;
161  int insertCursor;
162 
163  insertCursor =0;//OD TODO:see if this works, removed from inner loop (bfore was n*n)
164  for (i = 0; i<(int)blocks->size(); i++)
165  {
166  //since we have more than one ODDecodeBlockFile, we will need type flags to cast.
167  SeqBlock &block = (*blocks)[i];
168  const auto &file = block.f;
169  std::shared_ptr<ODDecodeBlockFile> oddbFile;
170  if (!file->IsDataAvailable() &&
171  (oddbFile =
172  std::static_pointer_cast<ODDecodeBlockFile>(file))->GetDecodeType() == this->GetODType())
173  {
174  oddbFile->SetStart(block.start);
175  oddbFile->SetClipOffset(sampleCount(
176  clip->GetStartTime()*clip->GetRate()
177  ));
178 
179  //these will always be linear within a sequence-lets take advantage of this by keeping a cursor.
180  {
181  std::shared_ptr< ODDecodeBlockFile > ptr;
182  while(insertCursor < (int)tempBlocks.size() &&
183  (!(ptr = tempBlocks[insertCursor].lock()) ||
184  ptr->GetStart() + ptr->GetClipOffset() <
185  oddbFile->GetStart() + oddbFile->GetClipOffset()))
186  insertCursor++;
187  }
188 
189  tempBlocks.insert(tempBlocks.begin() + insertCursor++, oddbFile);
190  }
191  }
192 
193  seq->UnlockDeleteUpdateMutex();
194  }
195  }
196  }
197  mWaveTrackMutex.Unlock();
198 
199  //get the NEW order.
200  OrderBlockFiles(tempBlocks);
201 }

References ODDecodeBlockFile::GetClipOffset(), GetODType(), ODDecodeBlockFile::GetStart(), ODTask::mWaveTrackMutex, ODTask::mWaveTracks, OrderBlockFiles(), ODDecodeBlockFile::SetClipOffset(), ODDecodeBlockFile::SetStart(), and SeqBlock::start.

Here is the call graph for this function:

Member Data Documentation

◆ mBlockFiles

std::vector<std::weak_ptr<ODDecodeBlockFile> > ODDecodeTask::mBlockFiles
protected

Definition at line 91 of file ODDecodeTask.h.

Referenced by CalculatePercentComplete(), DoSomeInternal(), and OrderBlockFiles().

◆ mDecoders

std::vector<std::unique_ptr<ODFileDecoder> > ODDecodeTask::mDecoders
protected

◆ mMaxBlockFiles

int ODDecodeTask::mMaxBlockFiles
protected

The documentation for this class was generated from the following files:
ODDecodeTask::CreateFileDecoder
virtual ODFileDecoder * CreateFileDecoder(const wxString &fileName)=0
Creates an ODFileDecoder that decodes a file of filetype the subclass handles.
ODTask::DemandTrackUpdate
virtual void DemandTrackUpdate(WaveTrack *track, double seconds)
changes the tasks associated with this Waveform to process the task from a different point in the tra...
Definition: ODTask.cpp:318
ODDecodeTask::CalculatePercentComplete
void CalculatePercentComplete() override
recalculates the percentage complete.
Definition: ODDecodeTask.cpp:121
ODFileDecoder::Init
virtual bool Init()
Definition: ODDecodeBlockFile.h:203
ODDecodeBlockFile::SetStart
void SetStart(sampleCount startSample)
Sets the value that indicates where the first sample in this block corresponds to the global sequence...
Definition: ODDecodeBlockFile.h:97
ODTask::eODNone
@ eODNone
Definition: ODTask.h:43
XO
#define XO(s)
Definition: Internat.h:31
ODDecodeBlockFile::GetGlobalStart
sampleCount GetGlobalStart() const
Definition: ODDecodeBlockFile.h:103
Sequence
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:61
ODDecodeBlockFile::SetClipOffset
void SetClipOffset(sampleCount numSamples)
sets the amount of samples the clip associated with this blockfile is offset in the wavetrack (non ef...
Definition: ODDecodeBlockFile.h:122
ODTask::mPercentCompleteMutex
ODLock mPercentCompleteMutex
Definition: ODTask.h:152
ODTask::mWaveTracks
std::vector< std::weak_ptr< WaveTrack > > mWaveTracks
Definition: ODTask.h:160
ODFileDecoder::IsInitialized
bool IsInitialized()
Definition: ODDecodeBlockFile.cpp:548
ODTask::mWaveTrackMutex
ODLock mWaveTrackMutex
Definition: ODTask.h:161
ODDecodeTask::mDecoders
std::vector< std::unique_ptr< ODFileDecoder > > mDecoders
Definition: ODDecodeTask.h:92
ODDecodeBlockFile::GetDecodeType
unsigned int GetDecodeType() const
Returns the type of audiofile this blockfile is loaded from.
Definition: ODDecodeBlockFile.h:118
ODDecodeTask::OrderBlockFiles
void OrderBlockFiles(std::vector< std::weak_ptr< ODDecodeBlockFile > > &unorderedBlocks)
Orders the input as either On-Demand or default layered order.
Definition: ODDecodeTask.cpp:207
ODDecodeTask::GetOrCreateMatchingFileDecoder
virtual ODFileDecoder * GetOrCreateMatchingFileDecoder(ODDecodeBlockFile *blockFile)
Definition: ODDecodeTask.cpp:270
ODDecodeBlockFile::GetGlobalEnd
sampleCount GetGlobalEnd() const
Definition: ODDecodeBlockFile.h:106
ODDecodeBlockFile::GetStart
sampleCount GetStart() const
Gets the value that indicates where the first sample in this block corresponds to the global sequence...
Definition: ODDecodeBlockFile.h:100
SeqBlock
Data structure containing pointer to a sample block and a start time. Element of a BlockArray.
Definition: Sequence.h:28
ODFileDecoder
class to decode a particular file (one per file). Saves info such as filename and length (after the h...
Definition: ODDecodeBlockFile.h:194
ODDecodeTask::mMaxBlockFiles
int mMaxBlockFiles
Definition: ODDecodeTask.h:94
BlockArray
Definition: Sequence.h:49
sampleCount
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
ODTask::mPercentComplete
volatile float mPercentComplete
Definition: ODTask.h:151
ODTask::GetDemandSample
virtual sampleCount GetDemandSample() const
Definition: ODTask.cpp:221
ODDecodeTask::SeekingAllowed
virtual bool SeekingAllowed()
Definition: ODDecodeTask.cpp:128
ODDecodeBlockFile::GetAudioFileName
const wxFileName & GetAudioFileName()
Definition: ODDecodeBlockFile.h:132
ODDecodeTask::mBlockFiles
std::vector< std::weak_ptr< ODDecodeBlockFile > > mBlockFiles
Definition: ODDecodeTask.h:91
ODDecodeTask::GetODType
unsigned int GetODType() override
Subclasses should override to return respective type.
Definition: ODDecodeTask.h:59
ODDecodeBlockFile::GetClipOffset
sampleCount GetClipOffset() const
Gets the number of samples the clip associated with this blockfile is offset by.
Definition: ODDecodeBlockFile.h:125
SeqBlock::start
sampleCount start
the sample in the global wavetrack that this block starts at.
Definition: Sequence.h:33