Audacity  2.2.2
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Attributes | Static Private Attributes | List of all members
BlockFile Class Referenceabstract

A BlockFile is a chunk of immutable audio data. More...

#include <BlockFile.h>

Inheritance diagram for BlockFile:
AliasBlockFile LegacyBlockFile SilentBlockFile SimpleBlockFile PCMAliasBlockFile ODDecodeBlockFile LegacyAliasBlockFile ODPCMAliasBlockFile

Classes

struct  GetFileNameResult
 
struct  MinMaxRMS
 
class  ReadLock
 
struct  ReadLocker
 
struct  ReadUnlocker
 

Public Types

using DiskByteCount = unsigned long long
 

Public Member Functions

 BlockFile (wxFileNameWrapper &&fileName, size_t samples)
 Construct a BlockFile. More...
 
virtual ~BlockFile ()
 
virtual size_t ReadData (samplePtr data, sampleFormat format, size_t start, size_t len, bool mayThrow=true) const =0
 
virtual bool GetNeedWriteCacheToDisk ()
 
virtual void WriteCacheToDisk ()
 
virtual bool GetNeedFillCache ()
 
virtual void FillCache ()
 
virtual void SaveXML (XMLWriter &xmlFile)=0
 Stores a representation of this file in XML. More...
 
virtual GetFileNameResult GetFileName () const
 
virtual void SetFileName (wxFileNameWrapper &&name)
 sets the file name the summary info will be saved in. threadsafe. More...
 
size_t GetLength () const
 
void SetLength (size_t newLen)
 
virtual void Lock ()
 Locks this BlockFile, to prevent it from being moved. More...
 
virtual void Unlock ()
 Unlock this BlockFile, allowing it to be moved. More...
 
virtual bool IsLocked ()
 Returns TRUE if this BlockFile is locked. More...
 
virtual MinMaxRMS GetMinMaxRMS (size_t start, size_t len, bool mayThrow=true) const
 Gets extreme values for the specified region. More...
 
virtual MinMaxRMS GetMinMaxRMS (bool mayThrow=true) const
 Gets extreme values for the entire block. More...
 
virtual bool Read256 (float *buffer, size_t start, size_t len)
 Returns the 256 byte summary data block. More...
 
virtual bool Read64K (float *buffer, size_t start, size_t len)
 Returns the 64K summary data block. More...
 
virtual bool IsAlias () const
 Returns TRUE if this block references another disk file. More...
 
virtual bool IsSummaryAvailable () const
 Returns TRUE if this block's complete summary has been computed and is ready (for OD) More...
 
virtual bool IsDataAvailable () const
 Returns TRUE if this block's complete data is ready to be accessed by Read() More...
 
virtual bool IsSummaryBeingComputed ()
 Returns TRUE if the summary has not yet been written, but is actively being computed and written to disk. More...
 
virtual BlockFilePtr Copy (wxFileNameWrapper &&newFileName)=0
 Create a NEW BlockFile identical to this, using the given filename. More...
 
virtual DiskByteCount GetSpaceUsage () const =0
 
virtual void Recover ()=0
 if the on-disk state disappeared, either recover it (if it was More...
 
void SilenceLog () const
 if we've detected an on-disk problem, the user opted to More...
 
virtual void CloseLock ()
 
ReadLock LockForRead () const
 

Static Public Attributes

static unsigned long gBlockFileDestructionCount { 0 }
 

Protected Types

using ReadLockBase = std::unique_ptr< const BlockFile, ReadUnlocker >
 

Protected Member Functions

virtual void LockRead () const
 Prevents a read on other threads. The basic blockfile runs on only one thread, so does nothing. More...
 
virtual void UnlockRead () const
 Allows reading on other threads. More...
 
virtual void * CalcSummary (samplePtr buffer, size_t len, sampleFormat format, ArrayOf< char > &cleanup)
 
void CalcSummaryFromBuffer (const float *fbuffer, size_t len, float *summary256, float *summary64K)
 
virtual bool ReadSummary (ArrayOf< char > &data)=0
 Read the summary section of the file. Derived classes implement. More...
 
virtual void FixSummary (void *data)
 

Static Protected Member Functions

static size_t CommonReadData (bool mayThrow, const wxFileName &fileName, bool &mSilentLog, const AliasBlockFile *pAliasFile, sampleCount origin, unsigned channel, samplePtr data, sampleFormat format, size_t start, size_t len, const sampleFormat *pLegacyFormat=nullptr, size_t legacyLen=0)
 

Protected Attributes

wxFileNameWrapper mFileName
 
size_t mLen
 
SummaryInfo mSummaryInfo
 
float mMin
 
float mMax
 
float mRMS
 
bool mSilentLog
 

Private Attributes

int mLockCount
 

Static Private Attributes

static ArrayOf< char > fullSummary
 

Detailed Description

A BlockFile is a chunk of immutable audio data.

A BlockFile represents a chunk of audio data. These chunks are assembled into sequences by the class Sequence. These classes are at the heart of how Audacity stores audio data.

BlockFile is an abstract base class that can be implemented in many different ways. However it does have a fairly large amount of shared code that deals with the physical file and manipulating the summary data.

BlockFile should be thought of as an immutable class. After it is constructed, it is essentially never changed (though there are a few exceptions). Most notably, the audio data and summary data are never altered once it is constructed. This is important to some of the derived classes that are actually aliases to audio data stored in existing files.

BlockFiles are managed by std::shared_ptr, so they are reference-counted.

Definition at line 56 of file BlockFile.h.

Member Typedef Documentation

using BlockFile::DiskByteCount = unsigned long long

Definition at line 152 of file BlockFile.h.

using BlockFile::ReadLockBase = std::unique_ptr< const BlockFile, ReadUnlocker >
protected

Definition at line 183 of file BlockFile.h.

Constructor & Destructor Documentation

BlockFile::BlockFile ( wxFileNameWrapper &&  fileName,
size_t  samples 
)

Construct a BlockFile.

Initializes the base BlockFile data. The block is initially unlocked and its reference count is 1.

Parameters
fileNameThe name of the disk file associated with this BlockFile. Not all BlockFiles will store their sample data here (for example, AliasBlockFiles read their data from elsewhere), but all BlockFiles will store at least the summary data here.
samplesThe number of samples this BlockFile contains.

Definition at line 101 of file BlockFile.cpp.

References mSilentLog.

101  :
102  mLockCount(0),
103  mFileName(std::move(fileName)),
104  mLen(samples),
105  mSummaryInfo(samples)
106 {
107  mSilentLog=FALSE;
108 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
bool mSilentLog
Definition: BlockFile.h:239
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
size_t mLen
Definition: BlockFile.h:236
int mLockCount
Definition: BlockFile.h:230
BlockFile::~BlockFile ( )
virtual

Definition at line 113 of file BlockFile.cpp.

References gBlockFileDestructionCount, IsLocked(), and mFileName.

114 {
115  if (!IsLocked() && mFileName.HasName())
116  // PRL: what should be done if this fails?
117  wxRemoveFile(mFileName.GetFullPath());
118 
120 }
static unsigned long gBlockFileDestructionCount
Definition: BlockFile.h:65
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
virtual bool IsLocked()
Returns TRUE if this BlockFile is locked.
Definition: BlockFile.cpp:162

Member Function Documentation

void * BlockFile::CalcSummary ( samplePtr  buffer,
size_t  len,
sampleFormat  format,
ArrayOf< char > &  cleanup 
)
protectedvirtual

Calculate summary data for the given sample data Overrides have differing details of memory management

Get a buffer containing a summary block describing this sample data. This must be called by derived classes when they are constructed, to allow them to construct their summary data, after which they should write that data to their disk file.

This method also has the side effect of setting the mMin, mMax, and mRMS members of this class.

You must not DELETE the returned buffer; it is static to this method.

Parameters
bufferA buffer containing the sample data to be analyzed
lenThe length of the sample data
formatThe format of the sample data.

Reimplemented in ODDecodeBlockFile, and ODPCMAliasBlockFile.

Definition at line 181 of file BlockFile.cpp.

References CalcSummaryFromBuffer(), CopySamples(), floatSample, fullSummary, headerTag, headerTagLen, mSummaryInfo, SummaryInfo::offset256, SummaryInfo::offset64K, ArrayOf< X >::reinit(), and SummaryInfo::totalSummaryBytes.

Referenced by SimpleBlockFile::SimpleBlockFile(), SimpleBlockFile::WriteSimpleBlockFile(), and AliasBlockFile::WriteSummary().

183 {
184  // Caller has nothing to deallocate
185  cleanup.reset();
186 
188 
189  memcpy(fullSummary.get(), headerTag, headerTagLen);
190 
191  float *summary64K = (float *)(fullSummary.get() + mSummaryInfo.offset64K);
192  float *summary256 = (float *)(fullSummary.get() + mSummaryInfo.offset256);
193 
194  Floats fbuffer{ len };
195  CopySamples(buffer, format,
196  (samplePtr)fbuffer.get(), floatSample, len);
197 
198  CalcSummaryFromBuffer(fbuffer.get(), len, summary256, summary64K);
199 
200  return fullSummary.get();
201 }
void CalcSummaryFromBuffer(const float *fbuffer, size_t len, float *summary256, float *summary64K)
Definition: BlockFile.cpp:203
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:117
int offset64K
Definition: BlockFile.h:38
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
static const int headerTagLen
Definition: BlockFile.cpp:70
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
static char headerTag[headerTagLen+1]
Definition: BlockFile.cpp:71
int format
Definition: ExportPCM.cpp:56
char * samplePtr
Definition: Types.h:203
size_t totalSummaryBytes
Definition: BlockFile.h:41
static ArrayOf< char > fullSummary
Definition: BlockFile.h:232
int offset256
Definition: BlockFile.h:40
void BlockFile::CalcSummaryFromBuffer ( const float *  fbuffer,
size_t  len,
float *  summary256,
float *  summary64K 
)
protected

Definition at line 203 of file BlockFile.cpp.

References SummaryInfo::frames256, SummaryInfo::frames64K, min(), mMax, mMin, mRMS, and mSummaryInfo.

Referenced by ODPCMAliasBlockFile::CalcSummary(), ODDecodeBlockFile::CalcSummary(), and CalcSummary().

205 {
206  decltype(len) sumLen;
207 
208  float min, max;
209  float sumsq;
210  double totalSquares = 0.0;
211  double fraction { 0.0 };
212 
213  // Recalc 256 summaries
214  sumLen = (len + 255) / 256;
215  int summaries = 256;
216 
217  for (decltype(sumLen) i = 0; i < sumLen; i++) {
218  min = fbuffer[i * 256];
219  max = fbuffer[i * 256];
220  sumsq = ((float)min) * ((float)min);
221  decltype(len) jcount = 256;
222  if (jcount > len - i * 256) {
223  jcount = len - i * 256;
224  fraction = 1.0 - (jcount / 256.0);
225  }
226  for (decltype(jcount) j = 1; j < jcount; j++) {
227  float f1 = fbuffer[i * 256 + j];
228  sumsq += ((float)f1) * ((float)f1);
229  if (f1 < min)
230  min = f1;
231  else if (f1 > max)
232  max = f1;
233  }
234 
235  totalSquares += sumsq;
236  float rms = (float)sqrt(sumsq / jcount);
237 
238  summary256[i * 3] = min;
239  summary256[i * 3 + 1] = max;
240  summary256[i * 3 + 2] = rms; // The rms is correct, but this may be for less than 256 samples in last loop.
241  }
242  for (auto i = sumLen; i < mSummaryInfo.frames256; i++) {
243  // filling in the remaining bits with non-harming/contributing values
244  // rms values are not "non-harming", so keep count of them:
245  summaries--;
246  summary256[i * 3] = FLT_MAX; // min
247  summary256[i * 3 + 1] = -FLT_MAX; // max
248  summary256[i * 3 + 2] = 0.0f; // rms
249  }
250 
251  // Calculate now while we can do it accurately
252  mRMS = sqrt(totalSquares/len);
253 
254  // Recalc 64K summaries
255  sumLen = (len + 65535) / 65536;
256 
257  for (decltype(sumLen) i = 0; i < sumLen; i++) {
258  min = summary256[3 * i * 256];
259  max = summary256[3 * i * 256 + 1];
260  sumsq = (float)summary256[3 * i * 256 + 2];
261  sumsq *= sumsq;
262  for (decltype(len) j = 1; j < 256; j++) { // we can overflow the useful summary256 values here, but have put non-harmful values in them
263  if (summary256[3 * (i * 256 + j)] < min)
264  min = summary256[3 * (i * 256 + j)];
265  if (summary256[3 * (i * 256 + j) + 1] > max)
266  max = summary256[3 * (i * 256 + j) + 1];
267  float r1 = summary256[3 * (i * 256 + j) + 2];
268  sumsq += r1*r1;
269  }
270 
271  double denom = (i < sumLen - 1) ? 256.0 : summaries - fraction;
272  float rms = (float)sqrt(sumsq / denom);
273 
274  summary64K[i * 3] = min;
275  summary64K[i * 3 + 1] = max;
276  summary64K[i * 3 + 2] = rms;
277  }
278  for (auto i = sumLen; i < mSummaryInfo.frames64K; i++) {
279  wxASSERT_MSG(false, wxT("Out of data for mSummaryInfo")); // Do we ever get here?
280  summary64K[i * 3] = 0.0f; // probably should be FLT_MAX, need a test case
281  summary64K[i * 3 + 1] = 0.0f; // probably should be -FLT_MAX, need a test case
282  summary64K[i * 3 + 2] = 0.0f; // just padding
283  }
284 
285  // Recalc block-level summary (mRMS already calculated)
286  min = summary64K[0];
287  max = summary64K[1];
288 
289  for (decltype(sumLen) i = 1; i < sumLen; i++) {
290  if (summary64K[3*i] < min)
291  min = summary64K[3*i];
292  if (summary64K[3*i+1] > max)
293  max = summary64K[3*i+1];
294  }
295 
296  mMin = min;
297  mMax = max;
298 }
size_t frames256
Definition: BlockFile.h:39
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
float mMin
Definition: BlockFile.h:238
float mRMS
Definition: BlockFile.h:238
if(pTrack &&pTrack->GetDisplay()!=WaveTrack::Spectrum)
int min(int a, int b)
size_t frames64K
Definition: BlockFile.h:37
float mMax
Definition: BlockFile.h:238
virtual void BlockFile::CloseLock ( )
inlinevirtual

when the project closes, it locks the blockfiles. Override this in case it needs special treatment.

Reimplemented in ODPCMAliasBlockFile.

Definition at line 170 of file BlockFile.h.

170 {Lock();}
virtual void Lock()
Locks this BlockFile, to prevent it from being moved.
Definition: BlockFile.cpp:148
size_t BlockFile::CommonReadData ( bool  mayThrow,
const wxFileName &  fileName,
bool &  mSilentLog,
const AliasBlockFile pAliasFile,
sampleCount  origin,
unsigned  channel,
samplePtr  data,
sampleFormat  format,
size_t  start,
size_t  len,
const sampleFormat pLegacyFormat = nullptr,
size_t  legacyLen = 0 
)
staticprotected

Definition at line 483 of file BlockFile.cpp.

References sampleCount::as_long_long(), ClearSamples(), CopySamples(), Maybe< X >::create(), floatSample, int16Sample, int24Sample, AudacityApp::MarkAliasedFilesMissingWarning(), SampleBuffer::ptr(), FileException::Read, SAMPLE_SIZE, sf_subtype_is_integer(), sf_subtype_more_than_16_bits(), and wxGetApp().

Referenced by PCMAliasBlockFile::ReadData(), LegacyBlockFile::ReadData(), SimpleBlockFile::ReadData(), and ODPCMAliasBlockFile::ReadData().

489 {
490  // Third party library has its own type alias, check it before
491  // adding origin + size_t
492  static_assert(sizeof(sampleCount::type) <= sizeof(sf_count_t),
493  "Type sf_count_t is too narrow to hold a sampleCount");
494 
495  SF_INFO info;
496  memset(&info, 0, sizeof(info));
497 
498  if ( pLegacyFormat ) {
499  switch( *pLegacyFormat ) {
500  case int16Sample:
501  info.format =
502  SF_FORMAT_RAW | SF_FORMAT_PCM_16 | SF_ENDIAN_CPU;
503  break;
504  default:
505  case floatSample:
506  info.format =
507  SF_FORMAT_RAW | SF_FORMAT_FLOAT | SF_ENDIAN_CPU;
508  break;
509  case int24Sample:
510  info.format = SF_FORMAT_RAW | SF_FORMAT_PCM_32 | SF_ENDIAN_CPU;
511  break;
512  }
513  info.samplerate = 44100; // Doesn't matter
514  info.channels = 1;
515  info.frames = legacyLen + origin.as_long_long();
516  }
517 
518 
519  wxFile f; // will be closed when it goes out of scope
520  SFFile sf;
521 
522  {
523  Maybe<wxLogNull> silence{};
524  if (mSilentLog)
525  silence.create();
526 
527  const auto fullPath = fileName.GetFullPath();
528  if (wxFile::Exists(fullPath) && f.Open(fullPath)) {
529  // Even though there is an sf_open() that takes a filename, use the one that
530  // takes a file descriptor since wxWidgets can open a file with a Unicode name and
531  // libsndfile can't (under Windows).
532  sf.reset(SFCall<SNDFILE*>(sf_open_fd, f.fd(), SFM_READ, &info, FALSE));
533  }
534 
535  if (!sf) {
536 
537  memset(data, 0, SAMPLE_SIZE(format)*len);
538 
539  if (pAliasFile) {
540  // Set a marker to display an error message for the silence
541  if (!wxGetApp().ShouldShowMissingAliasedFileWarning())
543  }
544  }
545  }
546  mSilentLog = !sf;
547 
548  size_t framesRead = 0;
549  if (sf) {
550  auto seek_result = SFCall<sf_count_t>(
551  sf_seek, sf.get(), ( origin + start ).as_long_long(), SEEK_SET);
552 
553  if (seek_result < 0)
554  // error
555  ;
556  else {
557  auto channels = info.channels;
558  wxASSERT(channels >= 1);
559  wxASSERT((int)channel < channels);
560 
561  if (channels == 1 &&
562  format == int16Sample &&
563  sf_subtype_is_integer(info.format)) {
564  // If both the src and dest formats are integer formats,
565  // read integers directly from the file, comversions not needed
566  framesRead = SFCall<sf_count_t>(
567  sf_readf_short, sf.get(), (short *)data, len);
568  }
569  else if (channels == 1 &&
570  format == int24Sample &&
571  sf_subtype_is_integer(info.format)) {
572  framesRead = SFCall<sf_count_t>(
573  sf_readf_int, sf.get(), (int *)data, len);
574 
575  // libsndfile gave us the 3 byte sample in the 3 most
576  // significant bytes -- we want it in the 3 least
577  // significant bytes.
578  int *intPtr = (int *)data;
579  for( size_t i = 0; i < framesRead; i++ )
580  intPtr[i] = intPtr[i] >> 8;
581  }
582  else if (format == int16Sample &&
583  !sf_subtype_more_than_16_bits(info.format)) {
584  // Special case: if the file is in 16-bit (or less) format,
585  // and the calling method wants 16-bit data, go ahead and
586  // read 16-bit data directly. This is a pretty common
587  // case, as most audio files are 16-bit.
588  SampleBuffer buffer(len * channels, int16Sample);
589  framesRead = SFCall<sf_count_t>(
590  sf_readf_short, sf.get(), (short *)buffer.ptr(), len);
591  for (size_t i = 0; i < framesRead; i++)
592  ((short *)data)[i] =
593  ((short *)buffer.ptr())[(channels * i) + channel];
594  }
595  else {
596  // Otherwise, let libsndfile handle the conversion and
597  // scaling, and pass us normalized data as floats. We can
598  // then convert to whatever format we want.
599  SampleBuffer buffer(len * channels, floatSample);
600  framesRead = SFCall<sf_count_t>(
601  sf_readf_float, sf.get(), (float *)buffer.ptr(), len);
602  auto bufferPtr = (samplePtr)((float *)buffer.ptr() + channel);
603  CopySamples(bufferPtr, floatSample,
604  (samplePtr)data, format,
605  framesRead,
606  true /* high quality by default */,
607  channels /* source stride */);
608  }
609  }
610  }
611 
612  if ( framesRead < len ) {
613  if (mayThrow)
614  throw FileException{ FileException::Cause::Read, fileName };
615  ClearSamples(data, format, framesRead, len - framesRead);
616  }
617 
618  return framesRead;
619 }
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
bool sf_subtype_more_than_16_bits(unsigned int format)
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
bool mSilentLog
Definition: BlockFile.h:239
Definition: MemoryX.h:204
long long type
Definition: Types.h:63
int format
Definition: ExportPCM.cpp:56
bool sf_subtype_is_integer(unsigned int format)
char * samplePtr
Definition: Types.h:203
void MarkAliasedFilesMissingWarning(const AliasBlockFile *b)
Mark playback as having missing aliased blockfiles.
void ClearSamples(samplePtr dst, sampleFormat format, size_t start, size_t len)
long long as_long_long() const
Definition: Types.h:90
AudacityApp & wxGetApp()
void create(Args &&...args)
Definition: MemoryX.h:251
virtual BlockFilePtr BlockFile::Copy ( wxFileNameWrapper &&  newFileName)
pure virtual

Create a NEW BlockFile identical to this, using the given filename.

Implemented in ODDecodeBlockFile, ODPCMAliasBlockFile, SimpleBlockFile, LegacyBlockFile, PCMAliasBlockFile, SilentBlockFile, and LegacyAliasBlockFile.

virtual void BlockFile::FillCache ( )
inlinevirtual

Reimplemented in SimpleBlockFile.

Definition at line 86 of file BlockFile.h.

86 { /* no cache by default */ }
void BlockFile::FixSummary ( void *  data)
protectedvirtual

Byte-swap the summary data, in case it was saved by a system on a different platform

Definition at line 331 of file BlockFile.cpp.

References ComputeMinMax256(), SummaryInfo::fields, floatSample, SummaryInfo::format, min(), mSummaryInfo, SummaryInfo::offset256, SummaryInfo::offset64K, and SummaryInfo::totalSummaryBytes.

Referenced by SimpleBlockFile::ReadSummary(), ODPCMAliasBlockFile::ReadSummary(), and AliasBlockFile::ReadSummary().

332 {
334  mSummaryInfo.fields != 3)
335  return;
336 
337  float *summary64K = (float *)((char *)data + mSummaryInfo.offset64K);
338  float *summary256 = (float *)((char *)data + mSummaryInfo.offset256);
339 
340  float min, max;
341  int bad;
342 
343  ComputeMinMax256(summary256, &min, &max, &bad);
344 
345  if (min != summary64K[0] || max != summary64K[1] || bad > 0) {
346  unsigned int *buffer = (unsigned int *)data;
347  auto len = mSummaryInfo.totalSummaryBytes / 4;
348 
349  for(unsigned int i=0; i<len; i++)
350  buffer[i] = wxUINT32_SWAP_ALWAYS(buffer[i]);
351 
352  ComputeMinMax256(summary256, &min, &max, &bad);
353  if (min == summary64K[0] && max == summary64K[1] && bad == 0) {
354  // The byte-swapping worked!
355  return;
356  }
357 
358  // Hmmm, no better, we should swap back
359  for(unsigned i=0; i<len; i++)
360  buffer[i] = wxUINT32_SWAP_ALWAYS(buffer[i]);
361  }
362 }
sampleFormat format
Definition: BlockFile.h:35
int offset64K
Definition: BlockFile.h:38
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
size_t totalSummaryBytes
Definition: BlockFile.h:41
int min(int a, int b)
int fields
Definition: BlockFile.h:34
static void ComputeMinMax256(float *summary256, float *outMin, float *outMax, int *outBads)
Definition: BlockFile.cpp:300
int offset256
Definition: BlockFile.h:40
auto BlockFile::GetFileName ( ) const
virtual

Returns the file name of the disk file associated with this BlockFile. Not all BlockFiles store their sample data here, but most BlockFiles have at least their summary data here. (some, i.e. SilentBlockFiles, do not correspond to a file on disk and have empty file names)

Reimplemented in ODDecodeBlockFile, and ODPCMAliasBlockFile.

Definition at line 127 of file BlockFile.cpp.

References mFileName.

Referenced by DirManager::ContainsBlockFile(), DirManager::CopyToNewProjectDirectory(), and SimpleBlockFile::SimpleBlockFile().

128 {
129  return { mFileName };
130 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
size_t BlockFile::GetLength ( ) const
inline
auto BlockFile::GetMinMaxRMS ( size_t  start,
size_t  len,
bool  mayThrow = true 
) const
virtual

Gets extreme values for the specified region.

Retrieves the minimum, maximum, and maximum RMS of the specified sample data in this block.

Parameters
startThe offset in this block where the region should begin
lenThe number of samples to include in the region

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 369 of file BlockFile.cpp.

References floatSample, min(), and SampleBuffer::ptr().

Referenced by ODDecodeBlockFile::GetMinMaxRMS(), and ODPCMAliasBlockFile::GetMinMaxRMS().

371 {
372  // TODO: actually use summaries
373  SampleBuffer blockData(len, floatSample);
374 
375  this->ReadData(blockData.ptr(), floatSample, start, len, mayThrow);
376 
377  float min = FLT_MAX;
378  float max = -FLT_MAX;
379  float sumsq = 0;
380 
381  for( decltype(len) i = 0; i < len; i++ )
382  {
383  float sample = ((float*)blockData.ptr())[i];
384 
385  if( sample > max )
386  max = sample;
387  if( sample < min )
388  min = sample;
389  sumsq += (sample*sample);
390  }
391 
392  return { min, max, (float)sqrt(sumsq/len) };
393 }
int min(int a, int b)
virtual size_t ReadData(samplePtr data, sampleFormat format, size_t start, size_t len, bool mayThrow=true) const =0
auto BlockFile::GetMinMaxRMS ( bool  mayThrow = true) const
virtual

Gets extreme values for the entire block.

Retrieves the minimum, maximum, and maximum RMS of this entire block. This is faster than the other GetMinMax function since these values are already computed.

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 398 of file BlockFile.cpp.

References mMax, mMin, and mRMS.

400 {
401  return { mMin, mMax, mRMS };
402 }
float mMin
Definition: BlockFile.h:238
float mRMS
Definition: BlockFile.h:238
float mMax
Definition: BlockFile.h:238
virtual bool BlockFile::GetNeedFillCache ( )
inlinevirtual

Reimplemented in SimpleBlockFile.

Definition at line 84 of file BlockFile.h.

84 { return false; }
virtual bool BlockFile::GetNeedWriteCacheToDisk ( )
inlinevirtual

Reimplemented in SimpleBlockFile.

Definition at line 80 of file BlockFile.h.

80 { return false; }
virtual DiskByteCount BlockFile::GetSpaceUsage ( ) const
pure virtual
virtual bool BlockFile::IsAlias ( ) const
inlinevirtual

Returns TRUE if this block references another disk file.

Reimplemented in AliasBlockFile.

Definition at line 137 of file BlockFile.h.

137 { return false; }
virtual bool BlockFile::IsDataAvailable ( ) const
inlinevirtual

Returns TRUE if this block's complete data is ready to be accessed by Read()

Reimplemented in ODDecodeBlockFile.

Definition at line 143 of file BlockFile.h.

143 {return true;}
bool BlockFile::IsLocked ( )
virtual

Returns TRUE if this BlockFile is locked.

Returns true if the block is locked.

Definition at line 162 of file BlockFile.cpp.

References mLockCount.

Referenced by ODPCMAliasBlockFile::Unlock(), and ~BlockFile().

163 {
164  return mLockCount > 0;
165 }
int mLockCount
Definition: BlockFile.h:230
virtual bool BlockFile::IsSummaryAvailable ( ) const
inlinevirtual

Returns TRUE if this block's complete summary has been computed and is ready (for OD)

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 140 of file BlockFile.h.

Referenced by DirManager::CopyToNewProjectDirectory().

140 {return true;}
virtual bool BlockFile::IsSummaryBeingComputed ( )
inlinevirtual

Returns TRUE if the summary has not yet been written, but is actively being computed and written to disk.

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 146 of file BlockFile.h.

Referenced by DirManager::CopyToNewProjectDirectory().

146 {return false;}
void BlockFile::Lock ( )
virtual

Locks this BlockFile, to prevent it from being moved.

Marks this BlockFile as "locked." A locked BlockFile may not be moved or deleted, only copied. Locking a BlockFile prevents it from disappearing if the project is saved in a different location. When doing a "Save As," Audacity locks all blocks belonging to the already-existing project, to ensure that the existing project remains valid with all the blocks it needs. Audacity also locks the blocks of the last saved version of a project when the project is deleted so that the files aren't deleted when their refcount hits zero.

Reimplemented in ODPCMAliasBlockFile.

Definition at line 148 of file BlockFile.cpp.

References BLOCKFILE_DEBUG_OUTPUT, and mLockCount.

Referenced by ODPCMAliasBlockFile::CloseLock(), and ODPCMAliasBlockFile::Lock().

149 {
150  mLockCount++;
152 }
#define BLOCKFILE_DEBUG_OUTPUT(op, i)
Definition: BlockFile.cpp:67
int mLockCount
Definition: BlockFile.h:230
ReadLock BlockFile::LockForRead ( ) const
inline

Definition at line 200 of file BlockFile.h.

Referenced by ODPCMAliasBlockFile::Copy(), ODDecodeBlockFile::Copy(), ODDecodeBlockFile::ReadData(), ODPCMAliasBlockFile::ReadData(), ODPCMAliasBlockFile::SaveXML(), and ODDecodeBlockFile::SaveXML().

200 { LockRead(); return { this, ReadUnlocker{} }; }
virtual void LockRead() const
Prevents a read on other threads. The basic blockfile runs on only one thread, so does nothing...
Definition: BlockFile.h:174
virtual void BlockFile::LockRead ( ) const
inlineprotectedvirtual

Prevents a read on other threads. The basic blockfile runs on only one thread, so does nothing.

Reimplemented in ODDecodeBlockFile, and ODPCMAliasBlockFile.

Definition at line 174 of file BlockFile.h.

Referenced by BlockFile::ReadLocker::operator()().

174 {}
bool BlockFile::Read256 ( float *  buffer,
size_t  start,
size_t  len 
)
virtual

Returns the 256 byte summary data block.

Retrieves a portion of the 256-byte summary buffer from this BlockFile. This data provides information about the minimum value, the maximum value, and the maximum RMS value for every group of 256 samples in the file. Fill with zeroes and return false if data are unavailable for any reason.

Parameters
*bufferThe area where the summary information will be written. It must be at least len*3 long.
startThe offset in 256-sample increments
lenThe number of 256-sample summary frames to read

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 415 of file BlockFile.cpp.

References SummaryInfo::bytesPerFrame, CopySamples(), SummaryInfo::fields, floatSample, SummaryInfo::format, SummaryInfo::frames256, min(), mSummaryInfo, SummaryInfo::offset256, and ReadSummary().

Referenced by ODDecodeBlockFile::Read256(), and ODPCMAliasBlockFile::Read256().

417 {
418  wxASSERT(start >= 0);
419 
420  ArrayOf< char > summary;
421  // In case of failure, summary is filled with zeroes
422  auto result = this->ReadSummary(summary);
423 
424  start = std::min( start, mSummaryInfo.frames256 );
425  len = std::min( len, mSummaryInfo.frames256 - start );
426 
427  CopySamples(summary.get() + mSummaryInfo.offset256 +
428  (start * mSummaryInfo.bytesPerFrame),
430  (samplePtr)buffer, floatSample, len * mSummaryInfo.fields);
431 
432  if (mSummaryInfo.fields == 2) {
433  // No RMS info; make guess
434  for(auto i = len; i--;) {
435  buffer[3*i+2] = (fabs(buffer[2*i]) + fabs(buffer[2*i+1]))/4.0;
436  buffer[3*i+1] = buffer[2*i+1];
437  buffer[3*i] = buffer[2*i];
438  }
439  }
440 
441  return result;
442 }
int bytesPerFrame
Definition: BlockFile.h:36
sampleFormat format
Definition: BlockFile.h:35
size_t frames256
Definition: BlockFile.h:39
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
virtual bool ReadSummary(ArrayOf< char > &data)=0
Read the summary section of the file. Derived classes implement.
char * samplePtr
Definition: Types.h:203
int min(int a, int b)
int fields
Definition: BlockFile.h:34
int offset256
Definition: BlockFile.h:40
bool BlockFile::Read64K ( float *  buffer,
size_t  start,
size_t  len 
)
virtual

Returns the 64K summary data block.

Retrieves a portion of the 64K summary buffer from this BlockFile. This data provides information about the minimum value, the maximum value, and the maximum RMS value for every group of 64K samples in the file. Fill with zeroes and return false if data are unavailable for any reason.

Parameters
*bufferThe area where the summary information will be written. It must be at least len*3 long.
startThe offset in 64K-sample increments
lenThe number of 64K-sample summary frames to read

Reimplemented in ODPCMAliasBlockFile, and ODDecodeBlockFile.

Definition at line 454 of file BlockFile.cpp.

References SummaryInfo::bytesPerFrame, CopySamples(), SummaryInfo::fields, floatSample, SummaryInfo::format, SummaryInfo::frames64K, min(), mSummaryInfo, SummaryInfo::offset64K, and ReadSummary().

Referenced by ODDecodeBlockFile::Read64K(), and ODPCMAliasBlockFile::Read64K().

456 {
457  wxASSERT(start >= 0);
458 
459  ArrayOf< char > summary;
460  // In case of failure, summary is filled with zeroes
461  auto result = this->ReadSummary(summary);
462 
463  start = std::min( start, mSummaryInfo.frames64K );
464  len = std::min( len, mSummaryInfo.frames64K - start );
465 
466  CopySamples(summary.get() + mSummaryInfo.offset64K +
467  (start * mSummaryInfo.bytesPerFrame),
469  (samplePtr)buffer, floatSample, len * mSummaryInfo.fields);
470 
471  if (mSummaryInfo.fields == 2) {
472  // No RMS info; make guess
473  for(auto i = len; i--;) {
474  buffer[3*i+2] = (fabs(buffer[2*i]) + fabs(buffer[2*i+1]))/4.0;
475  buffer[3*i+1] = buffer[2*i+1];
476  buffer[3*i] = buffer[2*i];
477  }
478  }
479 
480  return result;
481 }
int bytesPerFrame
Definition: BlockFile.h:36
sampleFormat format
Definition: BlockFile.h:35
int offset64K
Definition: BlockFile.h:38
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
virtual bool ReadSummary(ArrayOf< char > &data)=0
Read the summary section of the file. Derived classes implement.
char * samplePtr
Definition: Types.h:203
int min(int a, int b)
int fields
Definition: BlockFile.h:34
size_t frames64K
Definition: BlockFile.h:37
virtual size_t BlockFile::ReadData ( samplePtr  data,
sampleFormat  format,
size_t  start,
size_t  len,
bool  mayThrow = true 
) const
pure virtual

Retrieves audio data from this BlockFile Returns the number of samples really read, not more than len If fewer can be read than len, throws an exception if mayThrow is true, otherwise fills the remainder of data with zeroes.

Implemented in ODPCMAliasBlockFile, ODDecodeBlockFile, SimpleBlockFile, LegacyBlockFile, PCMAliasBlockFile, and SilentBlockFile.

Referenced by AliasBlockFile::WriteSummary().

virtual bool BlockFile::ReadSummary ( ArrayOf< char > &  data)
protectedpure virtual

Read the summary section of the file. Derived classes implement.

Implemented in AliasBlockFile, ODPCMAliasBlockFile, ODDecodeBlockFile, SimpleBlockFile, LegacyBlockFile, and SilentBlockFile.

Referenced by Read256(), and Read64K().

virtual void BlockFile::Recover ( )
pure virtual

if the on-disk state disappeared, either recover it (if it was

Implemented in ODDecodeBlockFile, ODPCMAliasBlockFile, SimpleBlockFile, LegacyBlockFile, SilentBlockFile, PCMAliasBlockFile, and LegacyAliasBlockFile.

virtual void BlockFile::SaveXML ( XMLWriter xmlFile)
pure virtual

Stores a representation of this file in XML.

Implemented in ODDecodeBlockFile, ODPCMAliasBlockFile, SimpleBlockFile, LegacyBlockFile, SilentBlockFile, PCMAliasBlockFile, and LegacyAliasBlockFile.

void BlockFile::SetFileName ( wxFileNameWrapper &&  name)
virtual

sets the file name the summary info will be saved in. threadsafe.

Reimplemented in ODDecodeBlockFile, and ODPCMAliasBlockFile.

Definition at line 133 of file BlockFile.cpp.

References mFileName, and name.

Referenced by DirManager::CopyToNewProjectDirectory().

134 {
135  mFileName=std::move(name);
136 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
void BlockFile::SetLength ( size_t  newLen)
inline

Definition at line 115 of file BlockFile.h.

115 { mLen = newLen; }
size_t mLen
Definition: BlockFile.h:236
void BlockFile::SilenceLog ( ) const
inline

if we've detected an on-disk problem, the user opted to

Definition at line 165 of file BlockFile.h.

165 { mSilentLog = TRUE; }
bool mSilentLog
Definition: BlockFile.h:239
void BlockFile::Unlock ( )
virtual

Unlock this BlockFile, allowing it to be moved.

Marks this BlockFile as "unlocked.".

Reimplemented in ODPCMAliasBlockFile.

Definition at line 155 of file BlockFile.cpp.

References BLOCKFILE_DEBUG_OUTPUT, and mLockCount.

Referenced by ODPCMAliasBlockFile::Unlock().

156 {
157  mLockCount--;
159 }
#define BLOCKFILE_DEBUG_OUTPUT(op, i)
Definition: BlockFile.cpp:67
int mLockCount
Definition: BlockFile.h:230
virtual void BlockFile::UnlockRead ( ) const
inlineprotectedvirtual

Allows reading on other threads.

Reimplemented in ODDecodeBlockFile, and ODPCMAliasBlockFile.

Definition at line 176 of file BlockFile.h.

Referenced by BlockFile::ReadUnlocker::operator()().

176 {}
virtual void BlockFile::WriteCacheToDisk ( )
inlinevirtual

Reimplemented in SimpleBlockFile.

Definition at line 81 of file BlockFile.h.

81 { /* no cache by default */ }

Member Data Documentation

ArrayOf< char > BlockFile::fullSummary
staticprivate

Definition at line 232 of file BlockFile.h.

Referenced by CalcSummary().

unsigned long BlockFile::gBlockFileDestructionCount { 0 }
static

Definition at line 65 of file BlockFile.h.

Referenced by DirManager::DirManager(), DirManager::GetBalanceInfo(), and ~BlockFile().

wxFileNameWrapper BlockFile::mFileName
protected
size_t BlockFile::mLen
protected
int BlockFile::mLockCount
private

Definition at line 230 of file BlockFile.h.

Referenced by IsLocked(), Lock(), and Unlock().

float BlockFile::mMax
protected
float BlockFile::mMin
protected
float BlockFile::mRMS
protected
bool BlockFile::mSilentLog
mutableprotected
SummaryInfo BlockFile::mSummaryInfo
protected

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