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 WAVE_TRACK_API WaveClipListener
93{
94 virtual ~WaveClipListener() = 0;
95 virtual void MarkChanged() = 0;
96 virtual void Invalidate() = 0;
97};
98
99class WAVE_TRACK_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
125 WaveClip(const WaveClip& orig,
127 bool copyCutlines,
128 double t0, double t1);
129
130 virtual ~WaveClip();
131
132 void ConvertToSampleFormat(sampleFormat format,
133 const std::function<void(size_t)> & progressReport = {});
134
135 // Always gives non-negative answer, not more than sample sequence length
136 // even if t0 really falls outside that range
137 sampleCount TimeToSequenceSamples(double t) const;
138 sampleCount ToSequenceSamples(sampleCount s) const;
139
140 int GetRate() const { return mRate; }
141
142 // Set rate without resampling. This will change the length of the clip
143 void SetRate(int rate);
144
145 // Resample clip. This also will set the rate, but without changing
146 // the length of the clip
147 void Resample(int rate, BasicUI::ProgressDialog *progress = NULL);
148
149 void SetColourIndex( int index ){ mColourIndex = index;};
150 int GetColourIndex( ) const { return mColourIndex;};
151
152 double GetSequenceStartTime() const noexcept;
153 void SetSequenceStartTime(double startTime);
154 double GetSequenceEndTime() const;
156 sampleCount GetSequenceStartSample() const;
158 sampleCount GetSequenceEndSample() const;
160 sampleCount GetSequenceSamplesCount() const;
161
162 double GetPlayStartTime() const noexcept;
163 void SetPlayStartTime(double time);
164
165 double GetPlayEndTime() const;
166
167 sampleCount GetPlayStartSample() const;
168 sampleCount GetPlayEndSample() const;
169 sampleCount GetPlaySamplesCount() const;
170
172 void SetTrimLeft(double trim);
174 double GetTrimLeft() const noexcept;
175
177 void SetTrimRight(double trim);
179 double GetTrimRight() const noexcept;
180
182 void TrimLeft(double deltaTime);
184 void TrimRight(double deltaTime);
185
187 void TrimLeftTo(double to);
189 void TrimRightTo(double to);
190
192 void Offset(double delta) noexcept;
193
194 // One and only one of the following is true for a given t (unless the clip
195 // has zero length -- then BeforePlayStartTime() and AfterPlayEndTime() can both be true).
196 // WithinPlayRegion() is true if the time is substantially within the clip
197 bool WithinPlayRegion(double t) const;
198 bool BeforePlayStartTime(double t) const;
199 bool AfterPlayEndTime(double t) const;
200
205 sampleCount CountSamples(double t0, double t1) const;
206
207 bool GetSamples(samplePtr buffer, sampleFormat format,
208 sampleCount start, size_t len, bool mayThrow = true) const;
209 void SetSamples(constSamplePtr buffer, sampleFormat format,
210 sampleCount start, size_t len,
211 sampleFormat effectiveFormat
217 );
218
219 Envelope* GetEnvelope() { return mEnvelope.get(); }
220 const Envelope* GetEnvelope() const { return mEnvelope.get(); }
221 BlockArray* GetSequenceBlockArray();
222 const BlockArray* GetSequenceBlockArray() const;
223
224 // Get low-level access to the sequence. Whenever possible, don't use this,
225 // but use more high-level functions inside WaveClip (or add them if you
226 // think they are useful for general use)
227 Sequence* GetSequence() { return mSequence.get(); }
228 const Sequence* GetSequence() const { return mSequence.get(); }
229
234 void MarkChanged();
235
238 std::pair<float, float> GetMinMax(
239 double t0, double t1, bool mayThrow = true) const;
240 float GetRMS(double t0, double t1, bool mayThrow = true) const;
241
245 void UpdateEnvelopeTrackLen();
246
248 std::shared_ptr<SampleBlock> AppendNewBlock(
249 samplePtr buffer, sampleFormat format, size_t len);
250
252 void AppendSharedBlock(const std::shared_ptr<SampleBlock> &pBlock);
253
257 size_t len, unsigned int stride,
258 sampleFormat effectiveFormat
264 );
266 void Flush();
267
270 void Clear(double t0, double t1);
271
276 void ClearLeft(double t);
280 void ClearRight(double t);
281
284 void ClearAndAddCutLine(double t0, double t1);
285
287 void Paste(double t0, const WaveClip* other);
288
291 void InsertSilence( double t, double len, double *pEnvelopeValue = nullptr );
292
295 void AppendSilence( double len, double envelopeValue );
296
298 WaveClipHolders &GetCutLines() { return mCutLines; }
300 { return reinterpret_cast< const WaveClipConstHolders& >( mCutLines ); }
301 size_t NumCutLines() const { return mCutLines.size(); }
302
306 bool FindCutLine(double cutLinePosition,
307 double* cutLineStart = NULL,
308 double *cutLineEnd = NULL) const;
309
313 void ExpandCutLine(double cutLinePosition);
314
316 bool RemoveCutLine(double cutLinePosition);
317
319 void OffsetCutLines(double t0, double len);
320
321 void CloseLock(); //should be called when the project closes.
322 // not balanced by unlocking calls.
323
324 //
325 // XMLTagHandler callback methods for loading and saving
326 //
327
328 bool HandleXMLTag(const std::string_view& tag, const AttributesList &attrs) override;
329 void HandleXMLEndTag(const std::string_view& tag) override;
330 XMLTagHandler *HandleXMLChild(const std::string_view& tag) override;
331 void WriteXML(XMLWriter &xmlFile) const /* not override */;
332
333 // AWD, Oct 2009: for pasting whitespace at the end of selection
334 bool GetIsPlaceholder() const { return mIsPlaceholder; }
335 void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }
336
337 // used by commands which interact with clips using the keyboard
338 bool SharesBoundaryWithNextClip(const WaveClip* next) const;
339
340 void SetName(const wxString& name);
341 const wxString& GetName() const;
342
343 sampleCount TimeToSamples(double time) const noexcept;
344 double SamplesToTime(sampleCount s) const noexcept;
345
347 void SetSilence(sampleCount offset, sampleCount length);
348
349 constSamplePtr GetAppendBuffer() const;
350 size_t GetAppendBufferLen() const;
351
352protected:
355 void ClearSequence(double t0, double t1);
356
357
358
359 double mSequenceOffset { 0 };
360 double mTrimLeft{ 0 };
361 double mTrimRight{ 0 };
362
363 int mRate;
365
366 std::unique_ptr<Sequence> mSequence;
367 std::unique_ptr<Envelope> mEnvelope;
368
369 // Cut Lines are nothing more than ordinary wave clips, with the
370 // offset relative to the start of the clip.
371 WaveClipHolders mCutLines {};
372
373 // AWD, Oct. 2009: for whitespace-at-end-of-selection pasting
374 bool mIsPlaceholder { false };
375
376private:
377 wxString mName;
378};
379
380#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
const TranslatableString name
Definition: Distortion.cpp:76
int format
Definition: ExportPCM.cpp:53
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:29
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:156
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:114
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:45
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:52
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:101
void SetIsPlaceholder(bool val)
Definition: WaveClip.h:335
std::unique_ptr< Sequence > mSequence
Definition: WaveClip.h:366
std::unique_ptr< Envelope > mEnvelope
Definition: WaveClip.h:367
wxString mName
Definition: WaveClip.h:377
int mRate
Definition: WaveClip.h:363
WaveClip(const WaveClip &) PROHIBITED
Sequence * GetSequence()
Definition: WaveClip.h:227
bool GetIsPlaceholder() const
Definition: WaveClip.h:334
int mColourIndex
Definition: WaveClip.h:364
const Sequence * GetSequence() const
Definition: WaveClip.h:228
WaveClipHolders & GetCutLines()
Get access to cut lines list.
Definition: WaveClip.h:298
void SetColourIndex(int index)
Definition: WaveClip.h:149
int GetRate() const
Definition: WaveClip.h:140
const WaveClipConstHolders & GetCutLines() const
Definition: WaveClip.h:299
size_t NumCutLines() const
Definition: WaveClip.h:301
int GetColourIndex() const
Definition: WaveClip.h:150
const Envelope * GetEnvelope() const
Definition: WaveClip.h:220
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:25
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