Audacity  3.0.3
SampleBlock.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 SampleBlock.h
6 
7 **********************************************************************/
8 
9 #ifndef __AUDACITY_SAMPLE_BLOCK__
10 #define __AUDACITY_SAMPLE_BLOCK__
11 
12 #include "audacity/Types.h"
13 
14 #include <functional>
15 #include <memory>
16 #include <unordered_set>
17 
18 class AudacityProject;
19 class ProjectFileIO;
20 class XMLWriter;
21 
22 class SampleBlock;
23 using SampleBlockPtr = std::shared_ptr<SampleBlock>;
24 class SampleBlockFactory;
25 using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
27  std::function< SampleBlockFactoryPtr( AudacityProject& ) >;
28 
29 using SampleBlockID = long long;
30 
31 class MinMaxRMS
32 {
33 public:
34  float min = 0;
35  float max = 0;
36  float RMS = 0;
37 };
38 
40 
45 {
46 public:
47  virtual ~SampleBlock();
48 
49  virtual void CloseLock() = 0;
50 
51  virtual SampleBlockID GetBlockID() const = 0;
52 
53  // If !mayThrow and there is an error, ignores it and returns zero.
54  // That may be appropriate when only attempting to display samples, not edit.
55  size_t GetSamples(samplePtr dest,
56  sampleFormat destformat,
57  size_t sampleoffset,
58  size_t numsamples, bool mayThrow = true);
59 
60  virtual size_t GetSampleCount() const = 0;
61 
63  virtual bool
64  GetSummary256(float *dest, size_t frameoffset, size_t numframes) = 0;
66  virtual bool
67  GetSummary64k(float *dest, size_t frameoffset, size_t numframes) = 0;
68 
70  // If !mayThrow and there is an error, ignores it and returns zeroes.
71  // That may be appropriate when only attempting to display samples, not edit.
73  size_t start, size_t len, bool mayThrow = true);
74 
76  // If !mayThrow and there is an error, ignores it and returns zeroes.
77  // That may be appropriate when only attempting to display samples, not edit.
78  MinMaxRMS GetMinMaxRMS(bool mayThrow = true) const;
79 
80  virtual size_t GetSpaceUsage() const = 0;
81 
82  virtual void SaveXML(XMLWriter &xmlFile) = 0;
83 
84 protected:
85  virtual size_t DoGetSamples(samplePtr dest,
86  sampleFormat destformat,
87  size_t sampleoffset,
88  size_t numsamples) = 0;
89 
90  virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len) = 0;
91 
92  virtual MinMaxRMS DoGetMinMaxRMS() const = 0;
93 };
94 
95 // Makes a useful function object
96 inline std::function< void(const SampleBlock&) >
97 BlockSpaceUsageAccumulator (unsigned long long &total)
98 {
99  return [&total]( const SampleBlock &block ){
100  total += block.GetSpaceUsage();
101  };
102 };
103 
106 {
107 public:
108  // Install global function that produces a sample block factory object for
109  // a given project; the factory has methods that later make sample blocks.
110  // Return the previously installed factory.
112  SampleBlockFactoryFactory newFactory );
113 
114  // Invoke the installed factory (throw an exception if none was installed)
115  static SampleBlockFactoryPtr New( AudacityProject &project );
116 
118 
119  // Returns a non-null pointer or else throws an exception
121  size_t numsamples,
122  sampleFormat srcformat);
123 
124  // Returns a non-null pointer or else throws an exception
126  size_t numsamples,
127  sampleFormat srcformat);
128 
129  // Returns a non-null pointer or else throws an exception
131  sampleFormat srcformat,
132  const wxChar **attrs);
133 
134  using SampleBlockIDs = std::unordered_set<SampleBlockID>;
137 
139  using BlockDeletionCallback = std::function< void(const SampleBlock&) >;
140 
143  BlockDeletionCallback callback ) = 0;
144 
145 protected:
146  // The override should throw more informative exceptions on error than the
147  // default InconsistencyException thrown by Create
149  size_t numsamples,
150  sampleFormat srcformat) = 0;
151 
152  // The override should throw more informative exceptions on error than the
153  // default InconsistencyException thrown by CreateSilent
155  size_t numsamples,
156  sampleFormat srcformat) = 0;
157 
158  // The override should throw more informative exceptions on error than the
159  // default InconsistencyException thrown by CreateFromXML
161  sampleFormat srcformat,
162  const wxChar **attrs) = 0;
163 };
164 
165 #endif
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
SampleBlockFactory::BlockDeletionCallback
std::function< void(const SampleBlock &) > BlockDeletionCallback
Type of function that is informed when a block is about to be deleted.
Definition: SampleBlock.h:139
SampleBlock::GetSamples
size_t GetSamples(samplePtr dest, sampleFormat destformat, size_t sampleoffset, size_t numsamples, bool mayThrow=true)
Definition: SampleBlock.cpp:73
SampleBlock::GetBlockID
virtual SampleBlockID GetBlockID() const =0
SampleBlockFactory::DoCreate
virtual SampleBlockPtr DoCreate(constSamplePtr src, size_t numsamples, sampleFormat srcformat)=0
SampleBlockFactory::~SampleBlockFactory
virtual ~SampleBlockFactory()
constSamplePtr
const char * constSamplePtr
Definition: Types.h:215
MinMaxRMS::RMS
float RMS
Definition: SampleBlock.h:36
SampleBlockPtr
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: SampleBlock.h:23
SampleBlockFactory::DoCreateFromXML
virtual SampleBlockPtr DoCreateFromXML(sampleFormat srcformat, const wxChar **attrs)=0
MinMaxRMS
Definition: SampleBlock.h:32
SampleBlockFactory::DoCreateSilent
virtual SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat)=0
SampleBlock::CloseLock
virtual void CloseLock()=0
SampleBlockFactory::GetActiveBlockIDs
virtual SampleBlockIDs GetActiveBlockIDs()=0
SampleBlockID
long long SampleBlockID
Definition: ProjectFileIO.h:37
SqliteSampleBlockFactory
Implementation of SampleBlockFactory using Sqlite database.
Definition: SqliteSampleBlock.cpp:134
SampleBlockFactory::Create
SampleBlockPtr Create(constSamplePtr src, size_t numsamples, sampleFormat srcformat)
Definition: SampleBlock.cpp:41
samplePtr
char * samplePtr
Definition: Types.h:214
sampleFormat
sampleFormat
Definition: Types.h:194
SampleBlock::DoGetMinMaxRMS
virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len)=0
SampleBlock::DoGetSamples
virtual size_t DoGetSamples(samplePtr dest, sampleFormat destformat, size_t sampleoffset, size_t numsamples)=0
MinMaxRMS::max
float max
Definition: SampleBlock.h:35
SampleBlock::~SampleBlock
virtual ~SampleBlock()
SampleBlockFactory::New
static SampleBlockFactoryPtr New(AudacityProject &project)
Definition: SampleBlock.cpp:31
SampleBlockFactory::RegisterFactoryFactory
static SampleBlockFactoryFactory RegisterFactoryFactory(SampleBlockFactoryFactory newFactory)
Definition: SampleBlock.cpp:22
MinMaxRMS::min
float min
Definition: SampleBlock.h:34
SampleBlock::GetSampleCount
virtual size_t GetSampleCount() const =0
SampleBlock::GetSummary64k
virtual bool GetSummary64k(float *dest, size_t frameoffset, size_t numframes)=0
Non-throwing, should fill with zeroes on failure.
SampleBlockFactory::SampleBlockIDs
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:134
Types.h
SampleBlockFactory::CreateFromXML
SampleBlockPtr CreateFromXML(sampleFormat srcformat, const wxChar **attrs)
Definition: SampleBlock.cpp:61
SampleBlock::SaveXML
virtual void SaveXML(XMLWriter &xmlFile)=0
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:112
SampleBlock::GetSpaceUsage
virtual size_t GetSpaceUsage() const =0
SampleBlock
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:45
SampleBlockFactory
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:106
SampleBlockFactory::SetBlockDeletionCallback
virtual BlockDeletionCallback SetBlockDeletionCallback(BlockDeletionCallback callback)=0
Install a callback, returning the previously installed callback.
SampleBlockFactory::CreateSilent
SampleBlockPtr CreateSilent(size_t numsamples, sampleFormat srcformat)
Definition: SampleBlock.cpp:51
SampleBlockFactoryPtr
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:25
SampleBlock::DoGetMinMaxRMS
virtual MinMaxRMS DoGetMinMaxRMS() const =0
SampleBlock::GetMinMaxRMS
MinMaxRMS GetMinMaxRMS(size_t start, size_t len, bool mayThrow=true)
Gets extreme values for the specified region.
Definition: SampleBlock.cpp:87
SampleBlock::GetSummary256
virtual bool GetSummary256(float *dest, size_t frameoffset, size_t numframes)=0
Non-throwing, should fill with zeroes on failure.
BlockSpaceUsageAccumulator
std::function< void(const SampleBlock &) > BlockSpaceUsageAccumulator(unsigned long long &total)
Definition: SampleBlock.h:97
SampleBlockFactoryFactory
std::function< SampleBlockFactoryPtr(AudacityProject &) > SampleBlockFactoryFactory
Definition: SampleBlock.h:27
ProjectFileIO
Object associated with a project that manages reading and writing of Audacity project file formats,...
Definition: ProjectFileIO.h:60