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 sampleFormat effectiveFormat
210 );
211
212 Envelope* GetEnvelope() { return mEnvelope.get(); }
213 const Envelope* GetEnvelope() const { return mEnvelope.get(); }
214 BlockArray* GetSequenceBlockArray();
215 const BlockArray* GetSequenceBlockArray() const;
216
217 // Get low-level access to the sequence. Whenever possible, don't use this,
218 // but use more high-level functions inside WaveClip (or add them if you
219 // think they are useful for general use)
220 Sequence* GetSequence() { return mSequence.get(); }
221 const Sequence* GetSequence() const { return mSequence.get(); }
222
227 void MarkChanged();
228
231 std::pair<float, float> GetMinMax(
232 double t0, double t1, bool mayThrow = true) const;
233 float GetRMS(double t0, double t1, bool mayThrow = true) const;
234
238 void UpdateEnvelopeTrackLen();
239
241 std::shared_ptr<SampleBlock> AppendNewBlock(
242 samplePtr buffer, sampleFormat format, size_t len);
243
245 void AppendSharedBlock(const std::shared_ptr<SampleBlock> &pBlock);
246
250 size_t len, unsigned int stride,
251 sampleFormat effectiveFormat
257 );
259 void Flush();
260
263 void Clear(double t0, double t1);
264
269 void ClearLeft(double t);
273 void ClearRight(double t);
274
276 void ClearAndAddCutLine(double t0, double t1);
277
279 void Paste(double t0, const WaveClip* other);
280
283 void InsertSilence( double t, double len, double *pEnvelopeValue = nullptr );
284
287 void AppendSilence( double len, double envelopeValue );
288
290 WaveClipHolders &GetCutLines() { return mCutLines; }
292 { return reinterpret_cast< const WaveClipConstHolders& >( mCutLines ); }
293 size_t NumCutLines() const { return mCutLines.size(); }
294
298 bool FindCutLine(double cutLinePosition,
299 double* cutLineStart = NULL,
300 double *cutLineEnd = NULL) const;
301
305 void ExpandCutLine(double cutLinePosition);
306
308 bool RemoveCutLine(double cutLinePosition);
309
311 void OffsetCutLines(double t0, double len);
312
313 void CloseLock(); //should be called when the project closes.
314 // not balanced by unlocking calls.
315
316 //
317 // XMLTagHandler callback methods for loading and saving
318 //
319
320 bool HandleXMLTag(const std::string_view& tag, const AttributesList &attrs) override;
321 void HandleXMLEndTag(const std::string_view& tag) override;
322 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
323 void WriteXML(XMLWriter &xmlFile) const /* not override */;
324
325 // AWD, Oct 2009: for pasting whitespace at the end of selection
326 bool GetIsPlaceholder() const { return mIsPlaceholder; }
327 void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }
328
329 // used by commands which interact with clips using the keyboard
330 bool SharesBoundaryWithNextClip(const WaveClip* next) const;
331
332 void SetName(const wxString& name);
333 const wxString& GetName() const;
334
335 sampleCount TimeToSamples(double time) const noexcept;
336 double SamplesToTime(sampleCount s) const noexcept;
337
339 void SetSilence(sampleCount offset, sampleCount length);
340
341 constSamplePtr GetAppendBuffer() const;
342 size_t GetAppendBufferLen() const;
343
344protected:
347 void ClearSequence(double t0, double t1);
348
349
350
351 double mSequenceOffset { 0 };
352 double mTrimLeft{ 0 };
353 double mTrimRight{ 0 };
354
355 int mRate;
357
358 std::unique_ptr<Sequence> mSequence;
359 std::unique_ptr<Envelope> mEnvelope;
360
361 // Cut Lines are nothing more than ordinary wave clips, with the
362 // offset relative to the start of the clip.
363 WaveClipHolders mCutLines {};
364
365 // AWD, Oct. 2009: for whitespace-at-end-of-selection pasting
366 bool mIsPlaceholder { false };
367
368private:
369 wxString mName;
370};
371
372#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
const TranslatableString name
Definition: Distortion.cpp:74
int format
Definition: ExportPCM.cpp:53
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:28
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:55
const char * constSamplePtr
Definition: SampleFormat.h:56
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:327
std::unique_ptr< Sequence > mSequence
Definition: WaveClip.h:358
std::unique_ptr< Envelope > mEnvelope
Definition: WaveClip.h:359
wxString mName
Definition: WaveClip.h:369
int mRate
Definition: WaveClip.h:355
WaveClip(const WaveClip &) PROHIBITED
Sequence * GetSequence()
Definition: WaveClip.h:220
bool GetIsPlaceholder() const
Definition: WaveClip.h:326
int mColourIndex
Definition: WaveClip.h:356
const Sequence * GetSequence() const
Definition: WaveClip.h:221
WaveClipHolders & GetCutLines()
Get access to cut lines list.
Definition: WaveClip.h:290
void SetColourIndex(int index)
Definition: WaveClip.h:148
int GetRate() const
Definition: WaveClip.h:139
const WaveClipConstHolders & GetCutLines() const
Definition: WaveClip.h:291
size_t NumCutLines() const
Definition: WaveClip.h:293
int GetColourIndex() const
Definition: WaveClip.h:149
const Envelope * GetEnvelope() const
Definition: WaveClip.h:213
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