Audacity 3.2.0
Sequence.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Sequence.h
6
7 Dominic Mazzoni
8
9**********************************************************************/
10
11#ifndef __AUDACITY_SEQUENCE__
12#define __AUDACITY_SEQUENCE__
13
14
15#include <vector>
16#include <functional>
17
18#include "SampleFormat.h"
19#include "XMLTagHandler.h"
20
21#include "SampleCount.h"
22
23class SampleBlock;
25using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
26
27// This is an internal data structure! For advanced use only.
28class SeqBlock {
29 public:
30 using SampleBlockPtr = std::shared_ptr<SampleBlock>;
34
36 : sb{}, start(0)
37 {}
38
40 : sb(sb_), start(start_)
41 {}
42
43 // Construct a SeqBlock with changed start, same file
45 {
46 return SeqBlock(sb, start + delta);
47 }
48};
49class BlockArray : public std::vector<SeqBlock> {};
50using BlockPtrArray = std::vector<SeqBlock*>; // non-owning pointers
51
52// Put extra symbol information in the release build, for the purpose of gathering
53// profiling information (as from Windows Process Monitor), when there otherwise
54// isn't a need for AUDACITY_DLL_API.
55#ifdef IS_ALPHA
56 #define PROFILE_DLL_API AUDACITY_DLL_API
57#else
58 #define PROFILE_DLL_API
59#endif
60
62 public:
63
64 //
65 // Static methods
66 //
67
68 static void SetMaxDiskBlockSize(size_t bytes);
69 static size_t GetMaxDiskBlockSize();
70
72 static bool IsValidSampleFormat(const int nValue);
73
74 //
75 // Constructor / Destructor / Duplicator
76 //
77
79
80 Sequence(const Sequence &orig, const SampleBlockFactoryPtr &pFactory);
81
82 Sequence( const Sequence& ) = delete;
83 Sequence& operator= (const Sequence&) PROHIBITED;
84
85 ~Sequence();
86
87 //
88 // Editing
89 //
90
91 sampleCount GetNumSamples() const { return mNumSamples; }
92
93 bool Get(samplePtr buffer, sampleFormat format,
94 sampleCount start, size_t len, bool mayThrow) const;
95
96 // Note that len is not size_t, because nullptr may be passed for buffer, in
97 // which case, silence is inserted, possibly a large amount.
98 void SetSamples(constSamplePtr buffer, sampleFormat format,
99 sampleCount start, sampleCount len);
100
101 // Return non-null, or else throw!
102 // Must pass in the correct factory for the result. If it's not the same
103 // as in this, then block contents must be copied.
104 std::unique_ptr<Sequence> Copy( const SampleBlockFactoryPtr &pFactory,
105 sampleCount s0, sampleCount s1) const;
106 void Paste(sampleCount s0, const Sequence *src);
107
108 size_t GetIdealAppendLen() const;
109 void Append(constSamplePtr buffer, sampleFormat format, size_t len);
110
112 SeqBlock::SampleBlockPtr AppendNewBlock(
113 constSamplePtr buffer, sampleFormat format, size_t len);
115 void AppendSharedBlock(const SeqBlock::SampleBlockPtr &pBlock);
116 void Delete(sampleCount start, sampleCount len);
117
118 void SetSilence(sampleCount s0, sampleCount len);
119 void InsertSilence(sampleCount s0, sampleCount len);
120
121 const SampleBlockFactoryPtr &GetFactory() { return mpFactory; }
122
123 //
124 // XMLTagHandler callback methods for loading and saving
125 //
126
127 bool HandleXMLTag(const std::string_view& tag, const AttributesList& attrs) override;
128 void HandleXMLEndTag(const std::string_view& tag) override;
129 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
130 void WriteXML(XMLWriter &xmlFile) const /* not override */;
131
132 bool GetErrorOpening() { return mErrorOpening; }
133
134 //
135 // Lock all of this sequence's sample blocks, keeping them
136 // from being destroyed when closing.
137
138 bool CloseLock();//should be called upon project close.
139 // not balanced by unlocking calls.
140
141 //
142 // Manipulating Sample Format
143 //
144
145 sampleFormat GetSampleFormat() const;
146
147 // Return true iff there is a change
148 bool ConvertToSampleFormat(sampleFormat format,
149 const std::function<void(size_t)> & progressReport = {});
150
151 //
152 // Retrieving summary info
153 //
154
155 std::pair<float, float> GetMinMax(
156 sampleCount start, sampleCount len, bool mayThrow) const;
157 float GetRMS(sampleCount start, sampleCount len, bool mayThrow) const;
158
159 //
160 // Getting block size and alignment information
161 //
162
163 // This returns a possibly large or negative value
164 sampleCount GetBlockStart(sampleCount position) const;
165
166 // These return a nonnegative number of samples meant to size a memory buffer
167 size_t GetBestBlockSize(sampleCount start) const;
168 size_t GetMaxBlockSize() const;
169 size_t GetIdealBlockSize() const;
170
171 //
172 // This should only be used if you really, really know what
173 // you're doing!
174 //
175
176 BlockArray &GetBlockArray() { return mBlock; }
177 const BlockArray &GetBlockArray() const { return mBlock; }
178
179 private:
180
181 //
182 // Private static variables
183 //
184
185 static size_t sMaxDiskBlockSize;
186
187 //
188 // Private variables
189 //
190
192
195
196 // Not size_t! May need to be large:
197 sampleCount mNumSamples{ 0 };
198
199 size_t mMinSamples; // min samples per block
200 size_t mMaxSamples; // max samples per block
201
202 bool mErrorOpening{ false };
203
204 //
205 // Private methods
206 //
207
209 constSamplePtr buffer, sampleFormat format, size_t len, bool coalesce);
210
211 static void AppendBlock(SampleBlockFactory *pFactory, sampleFormat format,
212 BlockArray &blocks,
213 sampleCount &numSamples,
214 const SeqBlock &b);
215
216 // Accumulate NEW block files onto the end of a block array.
217 // Does not change this sequence. The intent is to use
218 // CommitChangesIfConsistent later.
219 static void Blockify(SampleBlockFactory &factory,
220 size_t maxSamples,
222 BlockArray &list,
223 sampleCount start,
224 constSamplePtr buffer,
225 size_t len);
226
227 bool Get(int b,
228 samplePtr buffer,
230 sampleCount start,
231 size_t len,
232 bool mayThrow) const;
233
234public:
235
236 //
237 // Public methods
238 //
239
240 int FindBlock(sampleCount pos) const;
241
242 static bool Read(samplePtr buffer, sampleFormat format,
243 const SeqBlock &b,
244 size_t blockRelativeStart, size_t len, bool mayThrow);
245
246 // This function throws if the track is messed up
247 // because of inconsistent block starts & lengths
248 void ConsistencyCheck (const wxChar *whereStr, bool mayThrow = true) const;
249
250 // This function prints information to stdout about the blocks in the
251 // tracks and indicates if there are inconsistencies.
252 static void DebugPrintf
253 (const BlockArray &block, sampleCount numSamples, wxString *dest);
254
255private:
257 (const BlockArray &block, size_t maxSamples, size_t from,
258 sampleCount numSamples, const wxChar *whereStr,
259 bool mayThrow = true);
260
261 // The next two are used in methods that give a strong guarantee.
262 // They either throw because final consistency check fails, or swap the
263 // changed contents into place.
264
265 void CommitChangesIfConsistent
266 (BlockArray &newBlock, sampleCount numSamples, const wxChar *whereStr);
267
268 void AppendBlocksIfConsistent
269 (BlockArray &additionalBlocks, bool replaceLast,
270 sampleCount numSamples, const wxChar *whereStr);
271
272};
273
274#endif // __AUDACITY_SEQUENCE__
275
static RegisteredToolbarFactory factory
int format
Definition: ExportPCM.cpp:56
gPrefs Read(wxT("/GUI/VerticalZooming"), &bVZoom, false)
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< SeqBlock * > BlockPtrArray
Definition: Sequence.h:50
#define PROFILE_DLL_API
Definition: Sequence.h:58
Append([](My &table) -> Registry::BaseItemPtr { if(WaveTrackSubViews::slots() > 1) return std::make_unique< Entry >("MultiView", Entry::CheckItem, OnMultiViewID, XXO("&Multi-view"), POPUP_MENU_FN(OnMultiView), table, [](PopupMenuHandler &handler, wxMenu &menu, int id){ auto &table=static_cast< WaveTrackMenuTable & >(handler);auto &track=table.FindWaveTrack();const auto &view=WaveTrackView::Get(track);menu.Check(id, view.GetMultiView());});else return nullptr;})
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:110
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:44
Data structure containing pointer to a sample block and a start time. Element of a BlockArray.
Definition: Sequence.h:28
sampleCount start
the sample in the global wavetrack that this block starts at.
Definition: Sequence.h:33
SampleBlockPtr sb
Definition: Sequence.h:31
SeqBlock()
Definition: Sequence.h:35
std::shared_ptr< SampleBlock > SampleBlockPtr
Definition: Sequence.h:30
SeqBlock(const SampleBlockPtr &sb_, sampleCount start_)
Definition: Sequence.h:39
SeqBlock Plus(sampleCount delta) const
Definition: Sequence.h:44
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:61
bool GetErrorOpening()
Definition: Sequence.h:132
const SampleBlockFactoryPtr & GetFactory()
Definition: Sequence.h:121
sampleFormat mSampleFormat
Definition: Sequence.h:194
BlockArray mBlock
Definition: Sequence.h:193
const BlockArray & GetBlockArray() const
Definition: Sequence.h:177
SampleBlockFactoryPtr mpFactory
Definition: Sequence.h:191
static void ConsistencyCheck(const BlockArray &block, size_t maxSamples, size_t from, sampleCount numSamples, const wxChar *whereStr, bool mayThrow=true)
static size_t sMaxDiskBlockSize
Definition: Sequence.h:185
sampleCount GetNumSamples() const
Definition: Sequence.h:91
size_t mMinSamples
Definition: Sequence.h:199
Sequence(const Sequence &)=delete
size_t mMaxSamples
Definition: Sequence.h:200
BlockArray & GetBlockArray()
Definition: Sequence.h:176
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
virtual XMLTagHandler * HandleXMLChild(const std::string_view &tag)=0
virtual void HandleXMLEndTag(const std::string_view &WXUNUSED(tag))
Definition: XMLTagHandler.h:59
virtual bool HandleXMLTag(const std::string_view &tag, const AttributesList &attrs)=0
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26