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>;
30using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
31
32using SampleBlockID = long long;
33
35{
36public:
37 float min = 0;
38 float max = 0;
39 float RMS = 0;
40};
41
45class WAVE_TRACK_API SampleBlock
46{
47public:
49 struct DeletionCallback : GlobalHook<DeletionCallback,
50 void(const SampleBlock&)
51 >{};
52
53 virtual ~SampleBlock();
54
55 virtual void CloseLock() noexcept = 0;
56
57 virtual SampleBlockID GetBlockID() const = 0;
58
59 // If !mayThrow and there is an error, ignores it and returns zero.
60 // That may be appropriate when only attempting to display samples, not edit.
61 size_t GetSamples(samplePtr dest,
62 sampleFormat destformat,
63 size_t sampleoffset,
64 size_t numsamples, bool mayThrow = true);
65
66 virtual BlockSampleView GetFloatSampleView(bool mayThrow) = 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(const SampleBlock&) >
105BlockSpaceUsageAccumulator (unsigned long long &total)
106{
107 return [&total]( const SampleBlock &block ){
108 total += block.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 using SampleBlockIDs = std::unordered_set<SampleBlockID>;
147
148protected:
149 // The override should throw more informative exceptions on error than the
150 // default InconsistencyException thrown by Create
152 size_t numsamples,
153 sampleFormat srcformat) = 0;
154
155 // The override should throw more informative exceptions on error than the
156 // default InconsistencyException thrown by CreateSilent
158 size_t numsamples,
159 sampleFormat srcformat) = 0;
160
161 // The override should throw more informative exceptions on error than the
162 // default InconsistencyException thrown by CreateFromXML
164 sampleFormat srcformat,
165 const AttributesList &attrs) = 0;
166};
167
168#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
long long SampleBlockID
Definition: ProjectFileIO.h:43
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: SampleBlock.h:28
std::function< void(const SampleBlock &) > BlockSpaceUsageAccumulator(unsigned long long &total)
Definition: SampleBlock.h:105
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:30
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:37
float max
Definition: SampleBlock.h:38
float RMS
Definition: SampleBlock.h:39
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:65
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:117
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:144
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:46
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.
Type of function that is informed when a block is about to be deleted.
Definition: SampleBlock.h:51
Global factory of per-project factories of sample blocks.
Definition: SampleBlock.h:122