Audacity 3.2.0
SampleBlock.h
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SampleBlock.h
6
7**********************************************************************/
8
9#ifndef __AUDACITY_SAMPLE_BLOCK__
10#define __AUDACITY_SAMPLE_BLOCK__
11
12#include "GlobalVariable.h"
13#include "SampleFormat.h"
14
15#include <functional>
16#include <memory>
17#include <unordered_set>
18
19#include "XMLTagHandler.h"
20
21class AudacityProject;
22class ProjectFileIO;
23class XMLWriter;
24
25class SampleBlock;
26using SampleBlockPtr = std::shared_ptr<SampleBlock>;
28using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
29
30using SampleBlockID = long long;
31
33{
34public:
35 float min = 0;
36 float max = 0;
37 float RMS = 0;
38};
39
44{
45public:
47 struct DeletionCallback : GlobalHook<DeletionCallback,
48 void(const SampleBlock&)
49 >{};
50
51 virtual ~SampleBlock();
52
53 virtual void CloseLock() = 0;
54
55 virtual SampleBlockID GetBlockID() const = 0;
56
57 // If !mayThrow and there is an error, ignores it and returns zero.
58 // That may be appropriate when only attempting to display samples, not edit.
59 size_t GetSamples(samplePtr dest,
60 sampleFormat destformat,
61 size_t sampleoffset,
62 size_t numsamples, bool mayThrow = true);
63
64 virtual size_t GetSampleCount() const = 0;
65
67 virtual bool
68 GetSummary256(float *dest, size_t frameoffset, size_t numframes) = 0;
70 virtual bool
71 GetSummary64k(float *dest, size_t frameoffset, size_t numframes) = 0;
72
74 // If !mayThrow and there is an error, ignores it and returns zeroes.
75 // That may be appropriate when only attempting to display samples, not edit.
77 size_t start, size_t len, bool mayThrow = true);
78
80 // If !mayThrow and there is an error, ignores it and returns zeroes.
81 // That may be appropriate when only attempting to display samples, not edit.
82 MinMaxRMS GetMinMaxRMS(bool mayThrow = true) const;
83
84 virtual size_t GetSpaceUsage() const = 0;
85
86 virtual void SaveXML(XMLWriter &xmlFile) = 0;
87
88protected:
89 virtual size_t DoGetSamples(samplePtr dest,
90 sampleFormat destformat,
91 size_t sampleoffset,
92 size_t numsamples) = 0;
93
94 virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len) = 0;
95
96 virtual MinMaxRMS DoGetMinMaxRMS() const = 0;
97};
98
99// Makes a useful function object
100inline std::function< void(const SampleBlock&) >
101BlockSpaceUsageAccumulator (unsigned long long &total)
102{
103 return [&total]( const SampleBlock &block ){
104 total += block.GetSpaceUsage();
105 };
106};
107
110{
111public:
113 struct AUDACITY_DLL_API Factory : GlobalHook<Factory,
114 SampleBlockFactoryPtr( AudacityProject& )
115 >{};
116
117 // Invoke the installed factory (throw an exception if none was installed)
118 static SampleBlockFactoryPtr New( AudacityProject &project );
119
121
122 // Returns a non-null pointer or else throws an exception
124 size_t numsamples,
125 sampleFormat srcformat);
126
127 // Returns a non-null pointer or else throws an exception
129 size_t numsamples,
130 sampleFormat srcformat);
131
132 // Returns a non-null pointer or else throws an exception
134 sampleFormat srcformat,
135 const AttributesList &attrs);
136
137 using SampleBlockIDs = std::unordered_set<SampleBlockID>;
140
141protected:
142 // The override should throw more informative exceptions on error than the
143 // default InconsistencyException thrown by Create
145 size_t numsamples,
146 sampleFormat srcformat) = 0;
147
148 // The override should throw more informative exceptions on error than the
149 // default InconsistencyException thrown by CreateSilent
151 size_t numsamples,
152 sampleFormat srcformat) = 0;
153
154 // The override should throw more informative exceptions on error than the
155 // default InconsistencyException thrown by CreateFromXML
157 sampleFormat srcformat,
158 const AttributesList &attrs) = 0;
159};
160
161#endif
long long SampleBlockID
Definition: ProjectFileIO.h:41
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: SampleBlock.h:26
std::function< void(const SampleBlock &) > BlockSpaceUsageAccumulator(unsigned long long &total)
Definition: SampleBlock.h:101
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:28
sampleFormat
Definition: SampleFormat.h:29
char * samplePtr
Definition: SampleFormat.h:49
const char * constSamplePtr
Definition: SampleFormat.h:50
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
Global function-valued variable, adding a convenient Call()
float min
Definition: SampleBlock.h:35
float max
Definition: SampleBlock.h:36
float RMS
Definition: SampleBlock.h:37
Object associated with a project that manages reading and writing of Audacity project file formats,...
Definition: ProjectFileIO.h:64
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:110
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:137
virtual SampleBlockPtr DoCreateFromXML(sampleFormat srcformat, const AttributesList &attrs)=0
static SampleBlockFactoryPtr New(AudacityProject &project)
Definition: SampleBlock.cpp:16
SampleBlockPtr CreateSilent(size_t numsamples, sampleFormat srcformat)
Definition: SampleBlock.cpp:36
virtual SampleBlockIDs GetActiveBlockIDs()=0
virtual ~SampleBlockFactory()
virtual SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat)=0
SampleBlockPtr Create(constSamplePtr src, size_t numsamples, sampleFormat srcformat)
Definition: SampleBlock.cpp:26
virtual SampleBlockPtr DoCreate(constSamplePtr src, size_t numsamples, sampleFormat srcformat)=0
SampleBlockPtr CreateFromXML(sampleFormat srcformat, const AttributesList &attrs)
Definition: SampleBlock.cpp:46
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:44
virtual size_t GetSpaceUsage() const =0
MinMaxRMS GetMinMaxRMS(size_t start, size_t len, bool mayThrow=true)
Gets extreme values for the specified region.
Definition: SampleBlock.cpp:72
virtual bool GetSummary64k(float *dest, size_t frameoffset, size_t numframes)=0
Non-throwing, should fill with zeroes on failure.
virtual MinMaxRMS DoGetMinMaxRMS() const =0
virtual void CloseLock()=0
virtual bool GetSummary256(float *dest, size_t frameoffset, size_t numframes)=0
Non-throwing, should fill with zeroes on failure.
virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len)=0
virtual size_t GetSampleCount() const =0
size_t GetSamples(samplePtr dest, sampleFormat destformat, size_t sampleoffset, size_t numsamples, bool mayThrow=true)
Definition: SampleBlock.cpp:58
virtual SampleBlockID GetBlockID() const =0
virtual size_t DoGetSamples(samplePtr dest, sampleFormat destformat, size_t sampleoffset, size_t numsamples)=0
virtual void SaveXML(XMLWriter &xmlFile)=0
virtual ~SampleBlock()
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
Type of function that is informed when a block is about to be deleted.
Definition: SampleBlock.h:49
Global factory of per-project factories of sample blocks.
Definition: SampleBlock.h:115