Audacity 3.2.0
WaveClip.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 WaveClip.h
6
7 ?? Dominic Mazzoni
8 ?? Markus Meyer
9
10*******************************************************************/
11
12#ifndef __AUDACITY_WAVECLIP__
13#define __AUDACITY_WAVECLIP__
14
15
16
17#include "ClientData.h"
18#include "SampleFormat.h"
19#include "XMLTagHandler.h"
20#include "SampleCount.h"
21
22#include <wx/longlong.h>
23
24#include <vector>
25#include <functional>
26
27class BlockArray;
28class Envelope;
29class ProgressDialog;
30class sampleCount;
31class SampleBlock;
33using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
34class Sequence;
36namespace BasicUI { class ProgressDialog; }
37
38class WaveClip;
39
40// Array of pointers that assume ownership
41using WaveClipHolder = std::shared_ptr< WaveClip >;
42using WaveClipHolders = std::vector < WaveClipHolder >;
43using WaveClipConstHolders = std::vector < std::shared_ptr< const WaveClip > >;
44
45// A bundle of arrays needed for drawing waveforms. The object may or may not
46// own the storage for those arrays. If it does, it destroys them.
48{
49public:
50 int width;
52 float *min, *max, *rms;
53 int* bl;
54
55 std::vector<sampleCount> ownWhere;
56 std::vector<float> ownMin, ownMax, ownRms;
57 std::vector<int> ownBl;
58
59public:
61 : width(w), where(0), min(0), max(0), rms(0), bl(0)
62 {
63 }
64
65 // Create "own" arrays.
66 void Allocate()
67 {
68 ownWhere.resize(width + 1);
69 ownMin.resize(width);
70 ownMax.resize(width);
71 ownRms.resize(width);
72 ownBl.resize(width);
73
74 where = &ownWhere[0];
75 if (width > 0) {
76 min = &ownMin[0];
77 max = &ownMax[0];
78 rms = &ownRms[0];
79 bl = &ownBl[0];
80 }
81 else {
82 min = max = rms = 0;
83 bl = 0;
84 }
85 }
86
88 {
89 }
90};
91
92struct AUDACITY_DLL_API WaveClipListener
93{
94 virtual ~WaveClipListener() = 0;
95 virtual void MarkChanged() = 0;
96 virtual void Invalidate() = 0;
97};
98
99class AUDACITY_DLL_API WaveClip final : public XMLTagHandler
100 , public ClientData::Site< WaveClip, WaveClipListener >
101{
102private:
103 // It is an error to copy a WaveClip without specifying the
104 // sample block factory.
105
106 WaveClip(const WaveClip&) PROHIBITED;
107 WaveClip& operator= (const WaveClip&) PROHIBITED;
108
109public:
111
112 // typical constructor
114 int rate, int colourIndex);
115
116 // essentially a copy constructor - but you must pass in the
117 // current sample block factory, because we might be copying
118 // from one project to another
119 WaveClip(const WaveClip& orig,
121 bool copyCutlines);
122
123 // Copy only a range from the given WaveClip
124 WaveClip(const WaveClip& orig,
126 bool copyCutlines,
127 double t0, double t1);
128
129 virtual ~WaveClip();
130
131 void ConvertToSampleFormat(sampleFormat format,
132 const std::function<void(size_t)> & progressReport = {});
133
134 // Always gives non-negative answer, not more than sample sequence length
135 // even if t0 really falls outside that range
136 sampleCount TimeToSequenceSamples(double t) const;
137 sampleCount ToSequenceSamples(sampleCount s) const;
138
139 int GetRate() const { return mRate; }
140
141 // Set rate without resampling. This will change the length of the clip
142 void SetRate(int rate);
143
144 // Resample clip. This also will set the rate, but without changing
145 // the length of the clip
146 void Resample(int rate, BasicUI::ProgressDialog *progress = NULL);
147
148 void SetColourIndex( int index ){ mColourIndex = index;};
149 int GetColourIndex( ) const { return mColourIndex;};
150
151 double GetSequenceStartTime() const noexcept;
152 void SetSequenceStartTime(double startTime);
153 double GetSequenceEndTime() const;
155 sampleCount GetSequenceStartSample() const;
157 sampleCount GetSequenceEndSample() const;
159 sampleCount GetSequenceSamplesCount() const;
160
161 double GetPlayStartTime() const noexcept;
162 void SetPlayStartTime(double time);
163
164 double GetPlayEndTime() const;
165
166 sampleCount GetPlayStartSample() const;
167 sampleCount GetPlayEndSample() const;
168 sampleCount GetPlaySamplesCount() const;
169
171 void SetTrimLeft(double trim);
173 double GetTrimLeft() const noexcept;
174
176 void SetTrimRight(double trim);
178 double GetTrimRight() const noexcept;
179
181 void TrimLeft(double deltaTime);
183 void TrimRight(double deltaTime);
184
186 void TrimLeftTo(double to);
188 void TrimRightTo(double to);
189
191 void Offset(double delta) noexcept;
192
193 // One and only one of the following is true for a given t (unless the clip
194 // has zero length -- then BeforePlayStartTime() and AfterPlayEndTime() can both be true).
195 // WithinPlayRegion() is true if the time is substantially within the clip
196 bool WithinPlayRegion(double t) const;
197 bool BeforePlayStartTime(double t) const;
198 bool AfterPlayEndTime(double t) const;
199
200 bool GetSamples(samplePtr buffer, sampleFormat format,
201 sampleCount start, size_t len, bool mayThrow = true) const;
202 void SetSamples(constSamplePtr buffer, sampleFormat format,
203 sampleCount start, size_t len);
204
205 Envelope* GetEnvelope() { return mEnvelope.get(); }
206 const Envelope* GetEnvelope() const { return mEnvelope.get(); }
207 BlockArray* GetSequenceBlockArray();
208 const BlockArray* GetSequenceBlockArray() const;
209
210 // Get low-level access to the sequence. Whenever possible, don't use this,
211 // but use more high-level functions inside WaveClip (or add them if you
212 // think they are useful for general use)
213 Sequence* GetSequence() { return mSequence.get(); }
214 const Sequence* GetSequence() const { return mSequence.get(); }
215
220 void MarkChanged();
221
224 std::pair<float, float> GetMinMax(
225 double t0, double t1, bool mayThrow = true) const;
226 float GetRMS(double t0, double t1, bool mayThrow = true) const;
227
231 void UpdateEnvelopeTrackLen();
232
234 std::shared_ptr<SampleBlock> AppendNewBlock(
235 samplePtr buffer, sampleFormat format, size_t len);
236
238 void AppendSharedBlock(const std::shared_ptr<SampleBlock> &pBlock);
239
243 size_t len, unsigned int stride);
245 void Flush();
246
249 void Clear(double t0, double t1);
250
255 void ClearLeft(double t);
259 void ClearRight(double t);
260
262 void ClearAndAddCutLine(double t0, double t1);
263
265 void Paste(double t0, const WaveClip* other);
266
269 void InsertSilence( double t, double len, double *pEnvelopeValue = nullptr );
270
273 void AppendSilence( double len, double envelopeValue );
274
276 WaveClipHolders &GetCutLines() { return mCutLines; }
278 { return reinterpret_cast< const WaveClipConstHolders& >( mCutLines ); }
279 size_t NumCutLines() const { return mCutLines.size(); }
280
284 bool FindCutLine(double cutLinePosition,
285 double* cutLineStart = NULL,
286 double *cutLineEnd = NULL) const;
287
291 void ExpandCutLine(double cutLinePosition);
292
294 bool RemoveCutLine(double cutLinePosition);
295
297 void OffsetCutLines(double t0, double len);
298
299 void CloseLock(); //should be called when the project closes.
300 // not balanced by unlocking calls.
301
302 //
303 // XMLTagHandler callback methods for loading and saving
304 //
305
306 bool HandleXMLTag(const std::string_view& tag, const AttributesList &attrs) override;
307 void HandleXMLEndTag(const std::string_view& tag) override;
308 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
309 void WriteXML(XMLWriter &xmlFile) const /* not override */;
310
311 // AWD, Oct 2009: for pasting whitespace at the end of selection
312 bool GetIsPlaceholder() const { return mIsPlaceholder; }
313 void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }
314
315 // used by commands which interact with clips using the keyboard
316 bool SharesBoundaryWithNextClip(const WaveClip* next) const;
317
318 void SetName(const wxString& name);
319 const wxString& GetName() const;
320
321 sampleCount TimeToSamples(double time) const noexcept;
322 double SamplesToTime(sampleCount s) const noexcept;
323
325 void SetSilence(sampleCount offset, sampleCount length);
326
327 const SampleBuffer &GetAppendBuffer() const { return mAppendBuffer; }
328 size_t GetAppendBufferLen() const { return mAppendBufferLen; }
329
330protected:
333 void ClearSequence(double t0, double t1);
334
335
336
337 double mSequenceOffset { 0 };
338 double mTrimLeft{ 0 };
339 double mTrimRight{ 0 };
340
341 int mRate;
343
344 std::unique_ptr<Sequence> mSequence;
345 std::unique_ptr<Envelope> mEnvelope;
346
347 SampleBuffer mAppendBuffer {};
348 size_t mAppendBufferLen { 0 };
349
350 // Cut Lines are nothing more than ordinary wave clips, with the
351 // offset relative to the start of the clip.
352 WaveClipHolders mCutLines {};
353
354 // AWD, Oct. 2009: for whitespace-at-end-of-selection pasting
355 bool mIsPlaceholder { false };
356
357private:
358 wxString mName;
359};
360
361#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
const TranslatableString name
Definition: Distortion.cpp:82
int format
Definition: ExportPCM.cpp:56
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::shared_ptr< WaveClip > WaveClipHolder
Definition: WaveClip.h:41
std::vector< WaveClipHolder > WaveClipHolders
Definition: WaveClip.h:42
std::vector< std::shared_ptr< const WaveClip > > WaveClipConstHolders
Definition: WaveClip.h:43
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
Abstraction of a progress dialog with well defined time-to-completion estimate.
Definition: BasicUI.h:154
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:220
Piecewise linear or piecewise exponential function from double to double.
Definition: Envelope.h:72
ProgressDialog Class.
Interface to libsoxr.
Definition: Resample.h:27
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
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:61
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:101
void SetIsPlaceholder(bool val)
Definition: WaveClip.h:313
std::unique_ptr< Sequence > mSequence
Definition: WaveClip.h:344
std::unique_ptr< Envelope > mEnvelope
Definition: WaveClip.h:345
wxString mName
Definition: WaveClip.h:358
int mRate
Definition: WaveClip.h:341
WaveClip(const WaveClip &) PROHIBITED
Sequence * GetSequence()
Definition: WaveClip.h:213
const SampleBuffer & GetAppendBuffer() const
Definition: WaveClip.h:327
bool GetIsPlaceholder() const
Definition: WaveClip.h:312
int mColourIndex
Definition: WaveClip.h:342
const Sequence * GetSequence() const
Definition: WaveClip.h:214
WaveClipHolders & GetCutLines()
Get access to cut lines list.
Definition: WaveClip.h:276
void SetColourIndex(int index)
Definition: WaveClip.h:148
int GetRate() const
Definition: WaveClip.h:139
const WaveClipConstHolders & GetCutLines() const
Definition: WaveClip.h:277
size_t NumCutLines() const
Definition: WaveClip.h:279
size_t GetAppendBufferLen() const
Definition: WaveClip.h:328
int GetColourIndex() const
Definition: WaveClip.h:149
const Envelope * GetEnvelope() const
Definition: WaveClip.h:206
Site< WaveClip, WaveClipListener > Caches
Definition: WaveClip.h:110
std::vector< float > ownMin
Definition: WaveClip.h:56
WaveDisplay(int w)
Definition: WaveClip.h:60
int * bl
Definition: WaveClip.h:53
float * rms
Definition: WaveClip.h:52
sampleCount * where
Definition: WaveClip.h:51
~WaveDisplay()
Definition: WaveClip.h:87
std::vector< sampleCount > ownWhere
Definition: WaveClip.h:55
float * min
Definition: WaveClip.h:52
std::vector< float > ownMax
Definition: WaveClip.h:56
int width
Definition: WaveClip.h:50
float * max
Definition: WaveClip.h:52
void Allocate()
Definition: WaveClip.h:66
std::vector< int > ownBl
Definition: WaveClip.h:57
std::vector< float > ownRms
Definition: WaveClip.h:56
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
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
static RegisteredToolbarFactory factory
virtual void MarkChanged()=0
virtual void Invalidate()=0