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"
15
16#include <functional>
17#include <memory>
18#include <unordered_set>
19
20#include "Observer.h"
21#include "XMLTagHandler.h"
22
23class AudacityProject;
24class ProjectFileIO;
25class XMLWriter;
26
27class SampleBlock;
28using SampleBlockPtr = std::shared_ptr<SampleBlock>;
29using SampleBlockConstPtr = std::shared_ptr<const SampleBlock>;
31using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
32
33using SampleBlockID = long long;
34
36{
37public:
38 float min = 0;
39 float max = 0;
40 float RMS = 0;
41};
42
46class WAVE_TRACK_API SampleBlock
47{
48public:
49 virtual ~SampleBlock();
50
51 virtual void CloseLock() noexcept = 0;
52
53 virtual SampleBlockID GetBlockID() const = 0;
54
55 // If !mayThrow and there is an error, ignores it and returns zero.
56 // That may be appropriate when only attempting to display samples, not edit.
57 size_t GetSamples(samplePtr dest,
58 sampleFormat destformat,
59 size_t sampleoffset,
60 size_t numsamples, bool mayThrow = true);
61
62 virtual BlockSampleView GetFloatSampleView(bool mayThrow) = 0;
63
64 virtual sampleFormat GetSampleFormat() const = 0;
65
66 virtual size_t GetSampleCount() const = 0;
67
69 virtual bool
70 GetSummary256(float *dest, size_t frameoffset, size_t numframes) = 0;
72 virtual bool
73 GetSummary64k(float *dest, size_t frameoffset, size_t numframes) = 0;
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(
79 size_t start, size_t len, bool mayThrow = true);
80
82 // If !mayThrow and there is an error, ignores it and returns zeroes.
83 // That may be appropriate when only attempting to display samples, not edit.
84 MinMaxRMS GetMinMaxRMS(bool mayThrow = true) const;
85
86 virtual size_t GetSpaceUsage() const = 0;
87
88 virtual void SaveXML(XMLWriter &xmlFile) = 0;
89
90protected:
91 virtual size_t DoGetSamples(samplePtr dest,
92 sampleFormat destformat,
93 size_t sampleoffset,
94 size_t numsamples) = 0;
95
96 virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len) = 0;
97
98 virtual MinMaxRMS DoGetMinMaxRMS() const = 0;
99};
100
101// Makes a useful function object
102inline std::function< void(SampleBlockConstPtr) >
103BlockSpaceUsageAccumulator (unsigned long long &total)
104{
105 return [&total](SampleBlockConstPtr pBlock){
106 total += pBlock->GetSpaceUsage();
107 };
108};
109
111
113class WAVE_TRACK_API SampleBlockFactory
114 : public Observer::Publisher<SampleBlockCreateMessage>
115{
116public:
118 struct WAVE_TRACK_API Factory : GlobalHook<Factory,
119 SampleBlockFactoryPtr( AudacityProject& )
120 >{};
121
122 // Invoke the installed factory (throw an exception if none was installed)
124
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
133 SampleBlockPtr CreateSilent(
134 size_t numsamples,
135 sampleFormat srcformat);
136
137 // Returns a non-null pointer or else throws an exception
138 SampleBlockPtr CreateFromXML(
139 sampleFormat srcformat,
140 const AttributesList &attrs);
141
142 // Potentially returns a null pointer
143 SampleBlockPtr CreateFromId(sampleFormat srcformat, SampleBlockID id);
144
145 using SampleBlockIDs = std::unordered_set<SampleBlockID>;
148
149protected:
150 // The override should throw more informative exceptions on error than the
151 // default InconsistencyException thrown by Create
153 size_t numsamples,
154 sampleFormat srcformat) = 0;
155
156 // The override should throw more informative exceptions on error than the
157 // default InconsistencyException thrown by CreateSilent
159 size_t numsamples,
160 sampleFormat srcformat) = 0;
161
162 // The override should throw more informative exceptions on error than the
163 // default InconsistencyException thrown by CreateFromXML
165 sampleFormat srcformat,
166 const AttributesList &attrs) = 0;
167
168 virtual SampleBlockPtr
170};
171
172#endif
An audio segment is either a whole clip or the silence between clips. Views allow shared references t...
std::shared_ptr< std::vector< float > > BlockSampleView
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: CloudSyncDTO.h:25
long long SampleBlockID
Definition: CloudSyncDTO.h:26
std::function< void(SampleBlockConstPtr) > BlockSpaceUsageAccumulator(unsigned long long &total)
Definition: SampleBlock.h:103
std::shared_ptr< const SampleBlock > SampleBlockConstPtr
Definition: SampleBlock.h:29
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:31
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:57
const char * constSamplePtr
Definition: SampleFormat.h:58
const auto project
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:38
float max
Definition: SampleBlock.h:39
float RMS
Definition: SampleBlock.h:40
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:66
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:115
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:145
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
virtual SampleBlockPtr DoCreateFromId(sampleFormat srcformat, SampleBlockID id)=0
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:47
virtual void CloseLock() noexcept=0
virtual ~SampleBlock()
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
STL namespace.
Global factory of per-project factories of sample blocks.
Definition: SampleBlock.h:120