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 using DeletionCallback = std::function<void(const SampleBlock &)>;
50
51 virtual ~SampleBlock();
52
53 virtual void CloseLock() noexcept = 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 BlockSampleView GetFloatSampleView(bool mayThrow) = 0;
65
66 virtual sampleFormat GetSampleFormat() const = 0;
67
68 virtual size_t GetSampleCount() const = 0;
69
71 virtual bool
72 GetSummary256(float *dest, size_t frameoffset, size_t numframes) = 0;
74 virtual bool
75 GetSummary64k(float *dest, size_t frameoffset, size_t numframes) = 0;
76
78 // If !mayThrow and there is an error, ignores it and returns zeroes.
79 // That may be appropriate when only attempting to display samples, not edit.
80 MinMaxRMS GetMinMaxRMS(
81 size_t start, size_t len, bool mayThrow = true);
82
84 // If !mayThrow and there is an error, ignores it and returns zeroes.
85 // That may be appropriate when only attempting to display samples, not edit.
86 MinMaxRMS GetMinMaxRMS(bool mayThrow = true) const;
87
88 virtual size_t GetSpaceUsage() const = 0;
89
90 virtual void SaveXML(XMLWriter &xmlFile) = 0;
91
92protected:
93 virtual size_t DoGetSamples(samplePtr dest,
94 sampleFormat destformat,
95 size_t sampleoffset,
96 size_t numsamples) = 0;
97
98 virtual MinMaxRMS DoGetMinMaxRMS(size_t start, size_t len) = 0;
99
100 virtual MinMaxRMS DoGetMinMaxRMS() const = 0;
101};
102
103// Makes a useful function object
104inline std::function< void(SampleBlockConstPtr) >
105BlockSpaceUsageAccumulator (unsigned long long &total)
106{
107 return [&total](SampleBlockConstPtr pBlock){
108 total += pBlock->GetSpaceUsage();
109 };
110};
111
113
115class WAVE_TRACK_API SampleBlockFactory
116 : public Observer::Publisher<SampleBlockCreateMessage>
117{
118public:
120 struct WAVE_TRACK_API Factory : GlobalHook<Factory,
121 SampleBlockFactoryPtr( AudacityProject& )
122 >{};
123
124 // Invoke the installed factory (throw an exception if none was installed)
126
128
129 // Returns a non-null pointer or else throws an exception
131 size_t numsamples,
132 sampleFormat srcformat);
133
134 // Returns a non-null pointer or else throws an exception
135 SampleBlockPtr CreateSilent(
136 size_t numsamples,
137 sampleFormat srcformat);
138
139 // Returns a non-null pointer or else throws an exception
140 SampleBlockPtr CreateFromXML(
141 sampleFormat srcformat,
142 const AttributesList &attrs);
143
144 // Potentially returns a null pointer
145 SampleBlockPtr CreateFromId(sampleFormat srcformat, SampleBlockID id);
146
147 using SampleBlockIDs = std::unordered_set<SampleBlockID>;
150
151protected:
152 // The override should throw more informative exceptions on error than the
153 // default InconsistencyException thrown by Create
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 CreateSilent
161 size_t numsamples,
162 sampleFormat srcformat) = 0;
163
164 // The override should throw more informative exceptions on error than the
165 // default InconsistencyException thrown by CreateFromXML
167 sampleFormat srcformat,
168 const AttributesList &attrs) = 0;
169
170 virtual SampleBlockPtr
172};
173
174#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:105
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:117
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:147
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
std::function< void(const SampleBlock &)> DeletionCallback
Definition: SampleBlock.h:49
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:122