Audacity  2.2.2
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Attributes | List of all members
SimpleBlockFile Class Reference

A BlockFile that reads and writes uncompressed data using libsndfile. More...

#include <SimpleBlockFile.h>

Inheritance diagram for SimpleBlockFile:
BlockFile ODDecodeBlockFile

Public Member Functions

 SimpleBlockFile (wxFileNameWrapper &&baseFileName, samplePtr sampleData, size_t sampleLen, sampleFormat format, bool allowDeferredWrite=false, bool bypassCache=false)
 Create a disk file and write summary and sample data to it. More...
 
 SimpleBlockFile (wxFileNameWrapper &&existingFile, size_t len, float min, float max, float rms)
 Create the memory structure to refer to the given block file. More...
 
virtual ~SimpleBlockFile ()
 
bool ReadSummary (ArrayOf< char > &data) override
 Read the summary section of the disk file. More...
 
size_t ReadData (samplePtr data, sampleFormat format, size_t start, size_t len, bool mayThrow) const override
 Read the data section of the disk file. More...
 
BlockFilePtr Copy (wxFileNameWrapper &&newFileName) override
 Create a NEW block file identical to this one. More...
 
void SaveXML (XMLWriter &xmlFile) override
 Write an XML representation of this file. More...
 
DiskByteCount GetSpaceUsage () const override
 
void Recover () override
 if the on-disk state disappeared, either recover it (if it was More...
 
bool GetNeedWriteCacheToDisk () override
 
void WriteCacheToDisk () override
 
bool GetNeedFillCache () override
 
void FillCache () override
 
- Public Member Functions inherited from BlockFile
 BlockFile (wxFileNameWrapper &&fileName, size_t samples)
 Construct a BlockFile. More...
 
virtual ~BlockFile ()
 
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...
 
void SilenceLog () const
 if we've detected an on-disk problem, the user opted to More...
 
virtual void CloseLock ()
 
ReadLock LockForRead () const
 

Static Public Member Functions

static BlockFilePtr BuildFromXML (DirManager &dm, const wxChar **attrs)
 static More...
 

Protected Member Functions

bool WriteSimpleBlockFile (samplePtr sampleData, size_t sampleLen, sampleFormat format, void *summaryData)
 
void ReadIntoCache ()
 
- Protected Member Functions inherited from BlockFile
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 void FixSummary (void *data)
 

Static Protected Member Functions

static bool GetCache ()
 
- Static Protected Member Functions inherited from BlockFile
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

SimpleBlockFileCache mCache
 
- Protected Attributes inherited from BlockFile
wxFileNameWrapper mFileName
 
size_t mLen
 
SummaryInfo mSummaryInfo
 
float mMin
 
float mMax
 
float mRMS
 
bool mSilentLog
 

Private Attributes

sampleFormat mFormat
 

Additional Inherited Members

- Public Types inherited from BlockFile
using DiskByteCount = unsigned long long
 
- Static Public Attributes inherited from BlockFile
static unsigned long gBlockFileDestructionCount { 0 }
 
- Protected Types inherited from BlockFile
using ReadLockBase = std::unique_ptr< const BlockFile, ReadUnlocker >
 

Detailed Description

A BlockFile that reads and writes uncompressed data using libsndfile.

A block file that writes the audio data to an .au file and reads it back using libsndfile.

There are two ways to construct a simple block file. One is to supply data and have the constructor write the file. The other is for when the file already exists and we simply want to create the data structure to refer to it.

The block file can be cached in two ways. Caching is enabled if the preference "/Directories/CacheBlockFiles" is set, otherwise disabled. The default is to disable caching.

Read-caching: If caching is enabled, all block files will always be read-cached. Block files on disk will be read as soon as they are created and held in memory. New block files will be written to disk, but held in memory, so they are never read from disk in the current session.

Write-caching: If caching is enabled and the parameter allowDeferredWrite is enabled at the block file constructor, NEW block files are held in memory and written to disk only when WriteCacheToDisk() is called. This is used during recording to prevent disk access. After recording, WriteCacheToDisk() will be called on all block files and they will be written to disk. During normal editing, no write cache is active, that is, any block files will be written to disk instantly.

Even with write cache, auto recovery during normal editing will work as expected. However, auto recovery during recording will not work (not even manual auto recovery, because the files are never written physically to disk).

Definition at line 46 of file SimpleBlockFile.h.

Constructor & Destructor Documentation

SimpleBlockFile::SimpleBlockFile ( wxFileNameWrapper &&  baseFileName,
samplePtr  sampleData,
size_t  sampleLen,
sampleFormat  format,
bool  allowDeferredWrite = false,
bool  bypassCache = false 
)

Create a disk file and write summary and sample data to it.

Constructs a SimpleBlockFile based on sample data and writes it to disk.

Parameters
baseFileNameThe filename to use, but without an extension. This constructor will add the appropriate extension (.au in this case).
sampleDataThe sample data to be written to this block.
sampleLenThe number of samples to be written to this block.
formatThe format of the given samples.
allowDeferredWriteAllow deferred write-caching

Definition at line 101 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, BlockFile::CalcSummary(), SimpleBlockFileCache::format, format, GetCache(), BlockFile::GetFileName(), mCache, mFormat, BlockFile::mSummaryInfo, BlockFile::GetFileNameResult::name, SimpleBlockFileCache::needWrite, ArrayOf< X >::reinit(), SAMPLE_SIZE, SimpleBlockFileCache::sampleData, SimpleBlockFileCache::summaryData, SummaryInfo::totalSummaryBytes, FileException::Write, and WriteSimpleBlockFile().

105  :
106  BlockFile {
107  (baseFileName.SetExt(wxT("au")), std::move(baseFileName)),
108  sampleLen
109  }
110 {
111  mFormat = format;
112 
113  mCache.active = false;
114 
115  bool useCache = GetCache() && (!bypassCache);
116 
117  if (!(allowDeferredWrite && useCache) && !bypassCache)
118  {
119  bool bSuccess = WriteSimpleBlockFile(sampleData, sampleLen, format, NULL);
120  if (!bSuccess)
121  throw FileException{
123  }
124 
125  if (useCache) {
126  //wxLogDebug("SimpleBlockFile::SimpleBlockFile(): Caching block file data.");
127  mCache.active = true;
128  mCache.needWrite = true;
129  mCache.format = format;
130  const auto sampleDataSize = sampleLen * SAMPLE_SIZE(format);
131  mCache.sampleData.reinit(sampleDataSize);
132  memcpy(mCache.sampleData.get(), sampleData, sampleDataSize);
133  ArrayOf<char> cleanup;
134  void* summaryData = BlockFile::CalcSummary(sampleData, sampleLen,
135  format, cleanup);
137  memcpy(mCache.summaryData.get(), summaryData,
139  }
140 }
A BlockFile is a chunk of immutable audio data.
Definition: BlockFile.h:56
sampleFormat mFormat
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:117
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
int format
Definition: ExportPCM.cpp:56
size_t totalSummaryBytes
Definition: BlockFile.h:41
ArrayOf< char > sampleData
SimpleBlockFileCache mCache
virtual void * CalcSummary(samplePtr buffer, size_t len, sampleFormat format, ArrayOf< char > &cleanup)
Definition: BlockFile.cpp:181
bool WriteSimpleBlockFile(samplePtr sampleData, size_t sampleLen, sampleFormat format, void *summaryData)
virtual GetFileNameResult GetFileName() const
Definition: BlockFile.cpp:127
const wxFileName & name
Definition: BlockFile.h:99
ArrayOf< char > summaryData
static bool GetCache()
SimpleBlockFile::SimpleBlockFile ( wxFileNameWrapper &&  existingFile,
size_t  len,
float  min,
float  max,
float  rms 
)

Create the memory structure to refer to the given block file.

Construct a SimpleBlockFile memory structure that will point to an existing block file. This file must exist and be a valid block file.

Parameters
existingFileThe disk file this SimpleBlockFile should use.

Definition at line 146 of file SimpleBlockFile.cpp.

147  :
148  BlockFile{ std::move(existingFile), len }
149 {
150  // Set an invalid format to force GetSpaceUsage() to read it from the file.
151  mFormat = (sampleFormat) 0;
152 
153  mMin = min;
154  mMax = max;
155  mRMS = rms;
156 
157  mCache.active = false;
158 }
A BlockFile is a chunk of immutable audio data.
Definition: BlockFile.h:56
sampleFormat mFormat
float mMin
Definition: BlockFile.h:238
sampleFormat
Definition: Types.h:188
float mRMS
Definition: BlockFile.h:238
int min(int a, int b)
SimpleBlockFileCache mCache
float mMax
Definition: BlockFile.h:238
SimpleBlockFile::~SimpleBlockFile ( )
virtual

Definition at line 160 of file SimpleBlockFile.cpp.

161 {
162 }

Member Function Documentation

BlockFilePtr SimpleBlockFile::BuildFromXML ( DirManager dm,
const wxChar **  attrs 
)
static

static

Definition at line 440 of file SimpleBlockFile.cpp.

References DirManager::AssignFile(), Internat::CompatibleToDouble(), DirManager::GetProjectDataDir(), XMLValueChecker::IsGoodFileString(), XMLValueChecker::IsGoodInt(), XMLValueChecker::IsGoodString(), min(), and PLATFORM_MAX_PATH.

Referenced by DirManager::HandleXMLTag().

441 {
442  wxFileNameWrapper fileName;
443  float min = 0.0f, max = 0.0f, rms = 0.0f;
444  size_t len = 0;
445  double dblValue;
446  long nValue;
447 
448  while(*attrs)
449  {
450  const wxChar *attr = *attrs++;
451  const wxChar *value = *attrs++;
452  if (!value)
453  break;
454 
455  const wxString strValue = value;
456  if (!wxStricmp(attr, wxT("filename")) &&
457  // Can't use XMLValueChecker::IsGoodFileName here, but do part of its test.
459  (strValue.Length() + 1 + dm.GetProjectDataDir().Length() <= PLATFORM_MAX_PATH))
460  {
461  if (!dm.AssignFile(fileName, strValue, false))
462  // Make sure fileName is back to uninitialized state so we can detect problem later.
463  fileName.Clear();
464  }
465  else if (!wxStrcmp(attr, wxT("len")) &&
466  XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue) &&
467  nValue > 0)
468  len = nValue;
469  else if (XMLValueChecker::IsGoodString(strValue) && Internat::CompatibleToDouble(strValue, &dblValue))
470  { // double parameters
471  if (!wxStricmp(attr, wxT("min")))
472  min = dblValue;
473  else if (!wxStricmp(attr, wxT("max")))
474  max = dblValue;
475  else if (!wxStricmp(attr, wxT("rms")) && (dblValue >= 0.0))
476  rms = dblValue;
477  }
478  }
479 
480  return make_blockfile<SimpleBlockFile>
481  (std::move(fileName), len, min, max, rms);
482 }
static bool IsGoodInt(const wxString &strInt)
Check that the supplied string can be converted to a long (32bit) integer.
#define PLATFORM_MAX_PATH
Definition: Audacity.h:118
static bool IsGoodString(const wxString &str)
static bool CompatibleToDouble(const wxString &stringToConvert, double *result)
Convert a string to a number.
Definition: Internat.cpp:122
int min(int a, int b)
wxString GetProjectDataDir()
Definition: DirManager.cpp:679
static bool IsGoodFileString(const wxString &str)
bool AssignFile(wxFileNameWrapper &filename, const wxString &value, bool check)
Definition: DirManager.cpp:752
BlockFilePtr SimpleBlockFile::Copy ( wxFileNameWrapper &&  newFileName)
overridevirtual

Create a NEW block file identical to this one.

Create a copy of this BlockFile, but using a different disk file.

Parameters
newFileNameThe name of the NEW file to use.

Implements BlockFile.

Definition at line 487 of file SimpleBlockFile.cpp.

References BlockFile::mLen, BlockFile::mMax, BlockFile::mMin, and BlockFile::mRMS.

Referenced by ODDecodeBlockFile::Copy().

488 {
489  auto newBlockFile = make_blockfile<SimpleBlockFile>
490  (std::move(newFileName), mLen, mMin, mMax, mRMS);
491 
492  return newBlockFile;
493 }
float mMin
Definition: BlockFile.h:238
float mRMS
Definition: BlockFile.h:238
size_t mLen
Definition: BlockFile.h:236
float mMax
Definition: BlockFile.h:238
void SimpleBlockFile::FillCache ( )
overridevirtual

Reimplemented from BlockFile.

Definition at line 276 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, AU_SAMPLE_FORMAT_16, AU_SAMPLE_FORMAT_24, auHeader::encoding, floatSample, SimpleBlockFileCache::format, int16Sample, int24Sample, auHeader::magic, mCache, BlockFile::mFileName, BlockFile::mLen, SimpleBlockFileCache::needWrite, ReadData(), ReadSummary(), ArrayOf< X >::reinit(), SAMPLE_SIZE, SimpleBlockFileCache::sampleData, SimpleBlockFileCache::summaryData, and SwapUintEndianess().

277 {
278  if (mCache.active)
279  return; // cache is already filled
280 
281  // Check sample format
282  wxFFile file(mFileName.GetFullPath(), wxT("rb"));
283  if (!file.IsOpened())
284  {
285  // Don't read into cache if file not available
286  return;
287  }
288 
289  auHeader header;
290 
291  if (file.Read(&header, sizeof(header)) != sizeof(header))
292  {
293  // Corrupt file
294  return;
295  }
296 
297  wxUint32 encoding;
298 
299  if (header.magic == 0x2e736e64)
300  encoding = header.encoding; // correct endianness
301  else
302  encoding = SwapUintEndianess(header.encoding);
303 
304  switch (encoding)
305  {
306  case AU_SAMPLE_FORMAT_16:
308  break;
309  case AU_SAMPLE_FORMAT_24:
311  break;
312  default:
313  // floatSample is a safe default (we will never loose data)
315  break;
316  }
317 
318  file.Close();
319 
320  // Read samples into cache
322  if (ReadData(mCache.sampleData.get(), mCache.format, 0, mLen,
323  // no exceptions!
324  false) != mLen)
325  {
326  // Could not read all samples
327  mCache.sampleData.reset();
328  return;
329  }
330 
331  // Read summary data into cache
332  // Fills with zeroes in case of failure:
334 
335  // Cache is active but already on disk
336  mCache.active = true;
337  mCache.needWrite = false;
338 
339  //wxLogDebug("SimpleBlockFile::FillCache(): Succesfully read simple block file into cache.");
340 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:117
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
wxUint32 magic
bool ReadSummary(ArrayOf< char > &data) override
Read the summary section of the disk file.
static wxUint32 SwapUintEndianess(wxUint32 in)
wxUint32 encoding
size_t ReadData(samplePtr data, sampleFormat format, size_t start, size_t len, bool mayThrow) const override
Read the data section of the disk file.
ArrayOf< char > sampleData
SimpleBlockFileCache mCache
size_t mLen
Definition: BlockFile.h:236
The auHeader is a structure used by SimpleBlockFile for .au file format. There probably is an 'offici...
ArrayOf< char > summaryData
bool SimpleBlockFile::GetCache ( )
staticprotected

Definition at line 596 of file SimpleBlockFile.cpp.

References GetFreeMemory(), and gPrefs.

Referenced by SimpleBlockFile().

597 {
598 #ifdef DEPRECATED_AUDIO_CACHE
599  // See http://bugzilla.audacityteam.org/show_bug.cgi?id=545.
600  bool cacheBlockFiles = false;
601  gPrefs->Read(wxT("/Directories/CacheBlockFiles"), &cacheBlockFiles);
602  if (!cacheBlockFiles)
603  return false;
604 
605  int lowMem = gPrefs->Read(wxT("/Directories/CacheLowMem"), 16l);
606  if (lowMem < 16) {
607  lowMem = 16;
608  }
609  lowMem <<= 20;
610  return (GetFreeMemory() > lowMem);
611 #else
612  return false;
613 #endif
614 }
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
wxMemorySize GetFreeMemory()
Definition: DirManager.cpp:120
bool SimpleBlockFile::GetNeedFillCache ( )
inlineoverridevirtual

Reimplemented from BlockFile.

Definition at line 84 of file SimpleBlockFile.h.

84 { return !mCache.active; }
SimpleBlockFileCache mCache
bool SimpleBlockFile::GetNeedWriteCacheToDisk ( )
overridevirtual

Reimplemented from BlockFile.

Definition at line 591 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, mCache, and SimpleBlockFileCache::needWrite.

Referenced by WriteCacheToDisk().

592 {
593  return mCache.active && mCache.needWrite;
594 }
SimpleBlockFileCache mCache
auto SimpleBlockFile::GetSpaceUsage ( ) const
overridevirtual

Implements BlockFile.

Definition at line 495 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, AU_SAMPLE_FORMAT_16, AU_SAMPLE_FORMAT_24, auHeader::encoding, floatSample, BlockFile::GetLength(), int16Sample, int24Sample, auHeader::magic, mCache, BlockFile::mFileName, mFormat, BlockFile::mSummaryInfo, SimpleBlockFileCache::needWrite, SAMPLE_SIZE_DISK, SwapUintEndianess(), and SummaryInfo::totalSummaryBytes.

496 {
497  if (mCache.active && mCache.needWrite)
498  {
499  // We don't know space usage yet
500  return 0;
501  }
502 
503  // Don't know the format, so it must be read from the file
504  if (mFormat == (sampleFormat) 0)
505  {
506  // Check sample format
507  wxFFile file(mFileName.GetFullPath(), wxT("rb"));
508  if (!file.IsOpened())
509  {
510  // Don't read into cache if file not available
511  return 0;
512  }
513 
514  auHeader header;
515 
516  if (file.Read(&header, sizeof(header)) != sizeof(header))
517  {
518  // Corrupt file
519  return 0;
520  }
521 
522  wxUint32 encoding;
523 
524  if (header.magic == 0x2e736e64)
525  encoding = header.encoding; // correct endianness
526  else
527  encoding = SwapUintEndianess(header.encoding);
528 
529  switch (encoding)
530  {
531  case AU_SAMPLE_FORMAT_16:
533  break;
534  case AU_SAMPLE_FORMAT_24:
536  break;
537  default:
538  // floatSample is a safe default (we will never loose data)
540  break;
541  }
542 
543  file.Close();
544  }
545 
546  return (
547  sizeof(auHeader) +
550  );
551 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
sampleFormat mFormat
wxUint32 magic
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
static wxUint32 SwapUintEndianess(wxUint32 in)
size_t GetLength() const
Definition: BlockFile.h:114
wxUint32 encoding
#define SAMPLE_SIZE_DISK(SampleFormat)
Return the size on disk of one uncompressed sample (bytes)
Definition: SampleFormat.h:43
sampleFormat
Definition: Types.h:188
size_t totalSummaryBytes
Definition: BlockFile.h:41
SimpleBlockFileCache mCache
The auHeader is a structure used by SimpleBlockFile for .au file format. There probably is an 'offici...
size_t SimpleBlockFile::ReadData ( samplePtr  data,
sampleFormat  format,
size_t  start,
size_t  len,
bool  mayThrow 
) const
overridevirtual

Read the data section of the disk file.

Read the data portion of the block file using libsndfile. Convert it to the given format if it is not already.

Parameters
dataThe buffer where the data will be stored
formatThe format the data will be stored in
startThe offset in this block file
lenThe number of samples to read

Implements BlockFile.

Definition at line 395 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, ClearSamples(), BlockFile::CommonReadData(), CopySamples(), SimpleBlockFileCache::format, mCache, BlockFile::mFileName, min(), BlockFile::mLen, BlockFile::mSilentLog, FileException::Read, SAMPLE_SIZE, and SimpleBlockFileCache::sampleData.

Referenced by FillCache(), and ODDecodeBlockFile::ReadData().

397 {
398  if (mCache.active)
399  {
400  //wxLogDebug("SimpleBlockFile::ReadData(): Data are already in cache.");
401 
402  auto framesRead = std::min(len, std::max(start, mLen) - start);
403  CopySamples(
404  (samplePtr)(mCache.sampleData.get() +
405  start * SAMPLE_SIZE(mCache.format)),
406  mCache.format, data, format, framesRead);
407 
408  if ( framesRead < len ) {
409  if (mayThrow)
410  // Not the best exception class?
412  ClearSamples(data, format, framesRead, len - framesRead);
413  }
414 
415  return framesRead;
416  }
417  else
418  return CommonReadData( mayThrow,
419  mFileName, mSilentLog, nullptr, 0, 0, data, format, start, len);
420 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
bool mSilentLog
Definition: BlockFile.h:239
int format
Definition: ExportPCM.cpp:56
char * samplePtr
Definition: Types.h:203
ArrayOf< char > sampleData
int min(int a, int b)
SimpleBlockFileCache mCache
size_t mLen
Definition: BlockFile.h:236
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)
Definition: BlockFile.cpp:483
void ClearSamples(samplePtr dst, sampleFormat format, size_t start, size_t len)
void SimpleBlockFile::ReadIntoCache ( )
protected
bool SimpleBlockFile::ReadSummary ( ArrayOf< char > &  data)
overridevirtual

Read the summary section of the disk file.

Read the summary section of the disk file.

Parameters
*dataThe buffer to write the data to. It must be at least mSummaryinfo.totalSummaryBytes long.

Implements BlockFile.

Definition at line 346 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::active, Maybe< X >::create(), BlockFile::FixSummary(), mCache, BlockFile::mFileName, BlockFile::mSilentLog, BlockFile::mSummaryInfo, ArrayOf< X >::reinit(), SimpleBlockFileCache::summaryData, and SummaryInfo::totalSummaryBytes.

Referenced by FillCache(), and ODDecodeBlockFile::ReadSummary().

347 {
349  if (mCache.active) {
350  //wxLogDebug("SimpleBlockFile::ReadSummary(): Summary is already in cache.");
351  memcpy(data.get(), mCache.summaryData.get(), mSummaryInfo.totalSummaryBytes);
352  return true;
353  }
354  else
355  {
356  //wxLogDebug("SimpleBlockFile::ReadSummary(): Reading summary from disk.");
357 
358  wxFFile file(mFileName.GetFullPath(), wxT("rb"));
359 
360  {
361  Maybe<wxLogNull> silence{};
362  if (mSilentLog)
363  silence.create();
364  // FIXME: TRAP_ERR no report to user of absent summary files?
365  // filled with zero instead.
366  if (!file.IsOpened()){
367  memset(data.get(), 0, mSummaryInfo.totalSummaryBytes);
368  mSilentLog = TRUE;
369  return false;
370  }
371  }
372  mSilentLog = FALSE;
373 
374  // The offset is just past the au header
375  if( !file.Seek(sizeof(auHeader)) ||
376  file.Read(data.get(), mSummaryInfo.totalSummaryBytes) !=
378  memset(data.get(), 0, mSummaryInfo.totalSummaryBytes);
379  return false;
380  }
381 
382  FixSummary(data.get());
383 
384  return true;
385  }
386 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:117
bool mSilentLog
Definition: BlockFile.h:239
Definition: MemoryX.h:204
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
size_t totalSummaryBytes
Definition: BlockFile.h:41
SimpleBlockFileCache mCache
The auHeader is a structure used by SimpleBlockFile for .au file format. There probably is an 'offici...
ArrayOf< char > summaryData
virtual void FixSummary(void *data)
Definition: BlockFile.cpp:331
void create(Args &&...args)
Definition: MemoryX.h:251
void SimpleBlockFile::Recover ( )
overridevirtual

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

Implements BlockFile.

Definition at line 553 of file SimpleBlockFile.cpp.

References AU_SAMPLE_FORMAT_16, auHeader::channels, auHeader::dataOffset, auHeader::dataSize, auHeader::encoding, auHeader::magic, BlockFile::mFileName, BlockFile::mLen, BlockFile::mSummaryInfo, auHeader::sampleRate, and SummaryInfo::totalSummaryBytes.

553  {
554  wxFFile file(mFileName.GetFullPath(), wxT("wb"));
555 
556  if( !file.IsOpened() ){
557  // Can't do anything else.
558  return;
559  }
560 
561  auHeader header;
562  header.magic = 0x2e736e64;
564 
565  // dataSize is optional, and we opt out
566  header.dataSize = 0xffffffff;
567  header.encoding = AU_SAMPLE_FORMAT_16;
568  header.sampleRate = 44100;
569  header.channels = 1;
570  file.Write(&header, sizeof(header));
571 
572  for(decltype(mSummaryInfo.totalSummaryBytes) i = 0;
574  file.Write(wxT("\0"),1);
575 
576  for(decltype(mLen) i = 0; i < mLen * 2; i++)
577  file.Write(wxT("\0"),1);
578 
579 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
wxUint32 magic
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
wxUint32 encoding
size_t totalSummaryBytes
Definition: BlockFile.h:41
wxUint32 dataOffset
wxUint32 dataSize
size_t mLen
Definition: BlockFile.h:236
wxUint32 channels
wxUint32 sampleRate
The auHeader is a structure used by SimpleBlockFile for .au file format. There probably is an 'offici...
void SimpleBlockFile::SaveXML ( XMLWriter xmlFile)
overridevirtual

Write an XML representation of this file.

Implements BlockFile.

Definition at line 422 of file SimpleBlockFile.cpp.

References XMLWriter::EndTag(), BlockFile::mFileName, BlockFile::mLen, BlockFile::mMax, BlockFile::mMin, BlockFile::mRMS, XMLWriter::StartTag(), and XMLWriter::WriteAttr().

Referenced by ODDecodeBlockFile::SaveXML().

424 {
425  xmlFile.StartTag(wxT("simpleblockfile"));
426 
427  xmlFile.WriteAttr(wxT("filename"), mFileName.GetFullName());
428  xmlFile.WriteAttr(wxT("len"), mLen);
429  xmlFile.WriteAttr(wxT("min"), mMin);
430  xmlFile.WriteAttr(wxT("max"), mMax);
431  xmlFile.WriteAttr(wxT("rms"), mRMS);
432 
433  xmlFile.EndTag(wxT("simpleblockfile"));
434 }
virtual void StartTag(const wxString &name)
Definition: XMLWriter.cpp:78
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
virtual void WriteAttr(const wxString &name, const wxString &value)
Definition: XMLWriter.cpp:131
virtual void EndTag(const wxString &name)
Definition: XMLWriter.cpp:101
float mMin
Definition: BlockFile.h:238
float mRMS
Definition: BlockFile.h:238
size_t mLen
Definition: BlockFile.h:236
float mMax
Definition: BlockFile.h:238
void SimpleBlockFile::WriteCacheToDisk ( )
overridevirtual

Reimplemented from BlockFile.

Definition at line 581 of file SimpleBlockFile.cpp.

References SimpleBlockFileCache::format, GetNeedWriteCacheToDisk(), mCache, BlockFile::mLen, SimpleBlockFileCache::needWrite, SimpleBlockFileCache::sampleData, SimpleBlockFileCache::summaryData, and WriteSimpleBlockFile().

582 {
584  return;
585 
587  mCache.summaryData.get()))
588  mCache.needWrite = false;
589 }
ArrayOf< char > sampleData
SimpleBlockFileCache mCache
size_t mLen
Definition: BlockFile.h:236
bool GetNeedWriteCacheToDisk() override
bool WriteSimpleBlockFile(samplePtr sampleData, size_t sampleLen, sampleFormat format, void *summaryData)
ArrayOf< char > summaryData
bool SimpleBlockFile::WriteSimpleBlockFile ( samplePtr  sampleData,
size_t  sampleLen,
sampleFormat  format,
void *  summaryData 
)
protected

Definition at line 164 of file SimpleBlockFile.cpp.

References AU_SAMPLE_FORMAT_16, AU_SAMPLE_FORMAT_24, AU_SAMPLE_FORMAT_FLOAT, BlockFile::CalcSummary(), auHeader::channels, auHeader::dataOffset, auHeader::dataSize, auHeader::encoding, floatSample, int16Sample, int24Sample, auHeader::magic, BlockFile::mFileName, BlockFile::mSummaryInfo, SAMPLE_SIZE, auHeader::sampleRate, and SummaryInfo::totalSummaryBytes.

Referenced by SimpleBlockFile(), WriteCacheToDisk(), and ODDecodeBlockFile::WriteODDecodeBlockFile().

169 {
170  wxFFile file(mFileName.GetFullPath(), wxT("wb"));
171  if( !file.IsOpened() ){
172  // Can't do anything else.
173  return false;
174  }
175 
176  auHeader header;
177 
178  // AU files can be either big or little endian. Which it is is
179  // determined implicitly by the byte-order of the magic 0x2e736e64
180  // (.snd). We want it to be native-endian, so we write the magic
181  // to memory and then let it write that to a file in native
182  // endianness
183  header.magic = 0x2e736e64;
184 
185  // We store the summary data at the end of the header, so the data
186  // offset is the length of the summary data plus the length of the header
188 
189  // dataSize is optional, and we opt out
190  header.dataSize = 0xffffffff;
191 
192  switch(format) {
193  case int16Sample:
194  header.encoding = AU_SAMPLE_FORMAT_16;
195  break;
196 
197  case int24Sample:
198  header.encoding = AU_SAMPLE_FORMAT_24;
199  break;
200 
201  case floatSample:
203  break;
204  }
205 
206  // TODO: don't fabricate
207  header.sampleRate = 44100;
208 
209  // BlockFiles are always mono
210  header.channels = 1;
211 
212  // Write the file
213  ArrayOf<char> cleanup;
214  if (!summaryData)
215  summaryData = /*BlockFile::*/CalcSummary(sampleData, sampleLen, format, cleanup);
216  //mchinen:allowing virtual override of calc summary for ODDecodeBlockFile.
217  // PRL: cleanup fixes a possible memory leak!
218 
219  size_t nBytesToWrite = sizeof(header);
220  size_t nBytesWritten = file.Write(&header, nBytesToWrite);
221  if (nBytesWritten != nBytesToWrite)
222  {
223  wxLogDebug(wxT("Wrote %lld bytes, expected %lld."), (long long) nBytesWritten, (long long) nBytesToWrite);
224  return false;
225  }
226 
227  nBytesToWrite = mSummaryInfo.totalSummaryBytes;
228  nBytesWritten = file.Write(summaryData, nBytesToWrite);
229  if (nBytesWritten != nBytesToWrite)
230  {
231  wxLogDebug(wxT("Wrote %lld bytes, expected %lld."), (long long) nBytesWritten, (long long) nBytesToWrite);
232  return false;
233  }
234 
235  if( format == int24Sample )
236  {
237  // we can't write the buffer directly to disk, because 24-bit samples
238  // on disk need to be packed, not padded to 32 bits like they are in
239  // memory
240  int *int24sampleData = (int*)sampleData;
241 
242  for( size_t i = 0; i < sampleLen; i++ )
243  {
244  nBytesToWrite = 3;
245  nBytesWritten =
246  #if wxBYTE_ORDER == wxBIG_ENDIAN
247  file.Write((char*)&int24sampleData[i] + 1, nBytesToWrite);
248  #else
249  file.Write((char*)&int24sampleData[i], nBytesToWrite);
250  #endif
251  if (nBytesWritten != nBytesToWrite)
252  {
253  wxLogDebug(wxT("Wrote %lld bytes, expected %lld."), (long long) nBytesWritten, (long long) nBytesToWrite);
254  return false;
255  }
256  }
257  }
258  else
259  {
260  // for all other sample formats we can write straight from the buffer
261  // to disk
262  nBytesToWrite = sampleLen * SAMPLE_SIZE(format);
263  nBytesWritten = file.Write(sampleData, nBytesToWrite);
264  if (nBytesWritten != nBytesToWrite)
265  {
266  wxLogDebug(wxT("Wrote %lld bytes, expected %lld."), (long long) nBytesWritten, (long long) nBytesToWrite);
267  return false;
268  }
269  }
270 
271  return true;
272 }
wxFileNameWrapper mFileName
Definition: BlockFile.h:235
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
wxUint32 magic
SummaryInfo mSummaryInfo
Definition: BlockFile.h:237
wxUint32 encoding
int format
Definition: ExportPCM.cpp:56
size_t totalSummaryBytes
Definition: BlockFile.h:41
wxUint32 dataOffset
wxUint32 dataSize
virtual void * CalcSummary(samplePtr buffer, size_t len, sampleFormat format, ArrayOf< char > &cleanup)
Definition: BlockFile.cpp:181
wxUint32 channels
wxUint32 sampleRate
The auHeader is a structure used by SimpleBlockFile for .au file format. There probably is an 'offici...

Member Data Documentation

SimpleBlockFileCache SimpleBlockFile::mCache
protected
sampleFormat SimpleBlockFile::mFormat
mutableprivate

Definition at line 98 of file SimpleBlockFile.h.

Referenced by GetSpaceUsage(), and SimpleBlockFile().


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