Audacity  2.2.2
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:
ODTask ODDecodeFlacTask

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...
 
const wxChar * 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 (WaveTrack *oldTrack, WaveTrack *newTrack)
 Replaces all instances to a wavetrack with a NEW one, effectively transferring the task. More...
 
void AddWaveTrack (WaveTrack *track)
 Adds a WaveTrack to do the task for. More...
 
virtual int GetNumWaveTracks ()
 
virtual 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< 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 41 of file ODDecodeTask.h.

Constructor & Destructor Documentation

ODDecodeTask::ODDecodeTask ( )

Creates a NEW task that decodes files.

Definition at line 27 of file ODDecodeTask.cpp.

References mMaxBlockFiles.

28 {
29  mMaxBlockFiles = 0;
30 }
int mMaxBlockFiles
Definition: ODDecodeTask.h:95
virtual ODDecodeTask::~ODDecodeTask ( )
inlinevirtual

Definition at line 45 of file ODDecodeTask.h.

45 {};

Member Function Documentation

void ODDecodeTask::CalculatePercentComplete ( )
overrideprotectedvirtual

recalculates the percentage complete.

Implements ODTask.

Definition at line 114 of file ODDecodeTask.cpp.

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

Referenced by DoSomeInternal().

115 {
116  mPercentCompleteMutex.Lock();
117  mPercentComplete = (float) 1.0 - ((float)mBlockFiles.size() / (mMaxBlockFiles+1));
118  mPercentCompleteMutex.Unlock();
119 }
volatile float mPercentComplete
Definition: ODTask.h:150
int mMaxBlockFiles
Definition: ODDecodeTask.h:95
ODLock mPercentCompleteMutex
Definition: ODTask.h:151
std::vector< std::weak_ptr< ODDecodeBlockFile > > mBlockFiles
Definition: ODDecodeTask.h:92
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().

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 249 of file ODDecodeTask.cpp.

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

250 {
251  //only update if the subclass says we can seek.
252  if(SeekingAllowed())
253  ODTask::DemandTrackUpdate(track,seconds);
254 }
virtual bool SeekingAllowed()
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:324
void ODDecodeTask::DoSomeInternal ( )
overrideprotectedvirtual

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

Implements ODTask.

Definition at line 34 of file ODDecodeTask.cpp.

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

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

Definition at line 286 of file ODDecodeTask.cpp.

References mDecoders.

287 {
288  return mDecoders.size();
289 }
std::vector< std::unique_ptr< ODFileDecoder > > mDecoders
Definition: ODDecodeTask.h:93
unsigned int ODDecodeTask::GetODType ( )
inlineoverridevirtual

Subclasses should override to return respective type.

Reimplemented from ODTask.

Definition at line 60 of file ODDecodeTask.h.

References ODTask::eODNone.

Referenced by GetOrCreateMatchingFileDecoder(), and Update().

60 { return eODNone; }
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 262 of file ODDecodeTask.cpp.

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

Referenced by DoSomeInternal().

263 {
264  ODFileDecoder* ret=NULL;
265  //see if the filename matches any of our decoders, if so, return it.
266  for(int i=0;i<(int)mDecoders.size();i++)
267  {
268  //we check filename and decode type, since two types of ODDecoders might work with the same filetype
269  //e.g., FFmpeg and LibMad import both do MP3s. TODO: is this necessary? in theory we filter this when
270  //updating our list of blockfiles.
271  if(mDecoders[i]->GetFileName()==blockFile->GetAudioFileName().GetFullPath() &&
272  GetODType() == blockFile->GetDecodeType() )
273  {
274  ret = mDecoders[i].get();
275  break;
276  }
277  }
278 
279  //otherwise, create and add one, and return it.
280  if(!ret)
281  {
282  ret=CreateFileDecoder(blockFile->GetAudioFileName().GetFullPath());
283  }
284  return ret;
285 }
const wxFileName & GetAudioFileName()
class to decode a particular file (one per file). Saves info such as filename and length (after the h...
Definition: ODDecodeTask.h:100
std::vector< std::unique_ptr< ODFileDecoder > > mDecoders
Definition: ODDecodeTask.h:93
unsigned int GetDecodeType() const
Returns the type of audiofile this blockfile is loaded from.
unsigned int GetODType() override
Subclasses should override to return respective type.
Definition: ODDecodeTask.h:60
virtual ODFileDecoder * CreateFileDecoder(const wxString &fileName)=0
Creates an ODFileDecoder that decodes a file of filetype the subclass handles.
const char* ODDecodeTask::GetTaskName ( )
inlineoverridevirtual

Return the task name.

Reimplemented from ODTask.

Definition at line 55 of file ODDecodeTask.h.

55 { return "ODDecodeTask"; }
const wxChar* ODDecodeTask::GetTip ( )
inlineoverridevirtual

Implements ODTask.

Definition at line 57 of file ODDecodeTask.h.

References _().

57 { return _("Decoding Waveform"); }
_("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
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 199 of file ODDecodeTask.cpp.

Referenced by Update().

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

Definition at line 121 of file ODDecodeTask.cpp.

References mDecoders.

Referenced by DemandTrackUpdate().

122 {
123  for (unsigned int i = 0; i < mDecoders.size(); i++) {
124  if(!mDecoders[i]->SeekingAllowed())
125  return false;
126  }
127  return true;
128 }
std::vector< std::unique_ptr< ODFileDecoder > > mDecoders
Definition: ODDecodeTask.h:93
virtual bool SeekingAllowed()
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 131 of file ODDecodeTask.cpp.

References SeqBlock::f, GetODType(), Sequence::LockDeleteUpdateMutex(), ODTask::mWaveTrackMutex, ODTask::mWaveTracks, OrderBlockFiles(), SeqBlock::start, and Sequence::UnlockDeleteUpdateMutex().

132 {
133  std::vector< std::weak_ptr< ODDecodeBlockFile > > tempBlocks;
134 
135  mWaveTrackMutex.Lock();
136 
137  for(size_t j=0;j<mWaveTracks.size();j++)
138  {
139  if(mWaveTracks[j])
140  {
141  BlockArray *blocks;
142  Sequence *seq;
143 
144  //gather all the blockfiles that we should process in the wavetrack.
145  for (const auto &clip : mWaveTracks[j]->GetAllClips()) {
146  seq = clip->GetSequence();
147  //TODO:this lock is way to big since the whole file is one sequence. find a way to break it down.
148  seq->LockDeleteUpdateMutex();
149 
150  //See Sequence::Delete() for why need this for now..
151  blocks = clip->GetSequenceBlockArray();
152  int i;
153  int insertCursor;
154 
155  insertCursor =0;//OD TODO:see if this works, removed from inner loop (bfore was n*n)
156  for (i = 0; i<(int)blocks->size(); i++)
157  {
158  //since we have more than one ODDecodeBlockFile, we will need type flags to cast.
159  SeqBlock &block = (*blocks)[i];
160  const auto &file = block.f;
161  std::shared_ptr<ODDecodeBlockFile> oddbFile;
162  if (!file->IsDataAvailable() &&
163  (oddbFile =
164  std::static_pointer_cast<ODDecodeBlockFile>(file))->GetDecodeType() == this->GetODType())
165  {
166  oddbFile->SetStart(block.start);
167  oddbFile->SetClipOffset(sampleCount(
168  clip->GetStartTime()*clip->GetRate()
169  ));
170 
171  //these will always be linear within a sequence-lets take advantage of this by keeping a cursor.
172  {
173  std::shared_ptr< ODDecodeBlockFile > ptr;
174  while(insertCursor < (int)tempBlocks.size() &&
175  (!(ptr = tempBlocks[insertCursor].lock()) ||
176  ptr->GetStart() + ptr->GetClipOffset() <
177  oddbFile->GetStart() + oddbFile->GetClipOffset()))
178  insertCursor++;
179  }
180 
181  tempBlocks.insert(tempBlocks.begin() + insertCursor++, oddbFile);
182  }
183  }
184 
186  }
187  }
188  }
189  mWaveTrackMutex.Unlock();
190 
191  //get the NEW order.
192  OrderBlockFiles(tempBlocks);
193 }
Data structure containing pointer to a BlockFile and a start time. Element of a BlockArray.
Definition: Sequence.h:31
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:54
ODLock mWaveTrackMutex
Definition: ODTask.h:160
sampleCount start
the sample in the global wavetrack that this block starts at.
Definition: Sequence.h:35
An AliasBlockFile that references uncompressed data in an existing file.
std::vector< WaveTrack * > mWaveTracks
Definition: ODTask.h:159
void OrderBlockFiles(std::vector< std::weak_ptr< ODDecodeBlockFile > > &unorderedBlocks)
Orders the input as either On-Demand or default layered order.
unsigned int GetODType() override
Subclasses should override to return respective type.
Definition: ODDecodeTask.h:60
void UnlockDeleteUpdateMutex()
Definition: Sequence.h:198
BlockFilePtr f
Definition: Sequence.h:33
void LockDeleteUpdateMutex()
Definition: Sequence.h:197

Member Data Documentation

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

Definition at line 92 of file ODDecodeTask.h.

Referenced by CalculatePercentComplete(), and DoSomeInternal().

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

Definition at line 95 of file ODDecodeTask.h.

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


The documentation for this class was generated from the following files: