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 "Observer.h"
20#include "XMLTagHandler.h"
21
22class AudacityProject;
23class ProjectFileIO;
24class XMLWriter;
25
26class SampleBlock;
27using SampleBlockPtr = std::shared_ptr<SampleBlock>;
29using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
30
31using SampleBlockID = long long;
32
34{
35public:
36 float min = 0;
37 float max = 0;
38 float RMS = 0;
39};
40
44class WAVE_TRACK_API SampleBlock
45{
46public:
48 struct DeletionCallback : GlobalHook<DeletionCallback,
49 void(const SampleBlock&)
50 >{};
51
52 virtual ~SampleBlock();
53
54 virtual void CloseLock() = 0;
55
56 virtual SampleBlockID GetBlockID() const = 0;
57
58 // If !mayThrow and there is an error, ignores it and returns zero.
59 // That may be appropriate when only attempting to display samples, not edit.
60 size_t GetSamples(samplePtr dest,
61 sampleFormat destformat,
62 size_t sampleoffset,
63 size_t numsamples, bool mayThrow = true);
64
65 virtual size_t GetSampleCount() const = 0;
66
68 virtual bool
69 GetSummary256(float *dest, size_t frameoffset, size_t numframes) = 0;
71 virtual bool
72 GetSummary64k(float *dest, size_t frameoffset, size_t numframes) = 0;
73
75 // If !mayThrow and there is an error, ignores it and returns zeroes.
76 // That may be appropriate when only attempting to display samples, not edit.
77 MinMaxRMS GetMinMaxRMS(
78 size_t start, size_t len, bool mayThrow = true);
79
81 // If !mayThrow and there is an error, ignores it and returns zeroes.
82 // That may be appropriate when only attempting to display samples, not edit.
83 MinMaxRMS GetMinMaxRMS(bool mayThrow = true) const;
84
85 virtual size_t GetSpaceUsage() const = 0;
86
87 virtual void SaveXML(XMLWriter &xmlFile) = 0;
88
89protected:
90 virtual size_t DoGetSamples(samplePtr dest,
91 sampleFormat destformat,
92 size_t sampleoffset,
93 size_t numsamples) = 0;
94
95 virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len) = 0;
96
97 virtual MinMaxRMS DoGetMinMaxRMS() const = 0;
98};
99
100// Makes a useful function object
101inline std::function< void(const SampleBlock&) >
102BlockSpaceUsageAccumulator (unsigned long long &total)
103{
104 return [&total]( const SampleBlock &block ){
105 total += block.GetSpaceUsage();
106 };
107};
108
110
112class WAVE_TRACK_API SampleBlockFactory
113 : public Observer::Publisher<SampleBlockCreateMessage>
114{
115public:
117 struct WAVE_TRACK_API Factory : GlobalHook<Factory,
118 SampleBlockFactoryPtr( AudacityProject& )
119 >{};
120
121 // Invoke the installed factory (throw an exception if none was installed)
122 static SampleBlockFactoryPtr New( AudacityProject &project );
123
125
126 // Returns a non-null pointer or else throws an exception
128 size_t numsamples,
129 sampleFormat srcformat);
130
131 // Returns a non-null pointer or else throws an exception
132 SampleBlockPtr CreateSilent(
133 size_t numsamples,
134 sampleFormat srcformat);
135
136 // Returns a non-null pointer or else throws an exception
137 SampleBlockPtr CreateFromXML(
138 sampleFormat srcformat,
139 const AttributesList &attrs);
140
141 using SampleBlockIDs = std::unordered_set<SampleBlockID>;
144
145protected:
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 AttributesList &attrs) = 0;
163};
164
165#endif
long long SampleBlockID
Definition: ProjectFileIO.h:42
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: SampleBlock.h:27
std::function< void(const SampleBlock &) > BlockSpaceUsageAccumulator(unsigned long long &total)
Definition: SampleBlock.h:102
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:29
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
char * samplePtr
Definition: SampleFormat.h:55
const char * constSamplePtr
Definition: SampleFormat.h:56
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:90
Global function-valued variable, adding a convenient Call()
float min
Definition: SampleBlock.h:36
float max
Definition: SampleBlock.h:37
float RMS
Definition: SampleBlock.h:38
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
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:114
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:141
virtual SampleBlockPtr DoCreateFromXML(sampleFormat srcformat, const AttributesList &attrs)=0
virtual SampleBlockIDs GetActiveBlockIDs()=0
virtual ~SampleBlockFactory()
virtual SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat)=0
virtual SampleBlockPtr DoCreate(constSamplePtr src, size_t numsamples, sampleFormat srcformat)=0
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:45
virtual size_t GetSpaceUsage() const =0
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
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:25
Type of function that is informed when a block is about to be deleted.
Definition: SampleBlock.h:50
Global factory of per-project factories of sample blocks.
Definition: SampleBlock.h:119