Audacity  3.0.3
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 "SampleFormat.h"
18 #include "xml/XMLTagHandler.h"
19 
20 #include <wx/longlong.h>
21 
22 #include <vector>
23 #include <functional>
24 
25 class BlockArray;
26 class Envelope;
27 class ProgressDialog;
28 class SampleBlock;
29 class SampleBlockFactory;
30 using SampleBlockFactoryPtr = std::shared_ptr<SampleBlockFactory>;
31 class Sequence;
33 class WaveCache;
34 class WaveTrackCache;
35 class wxFileNameWrapper;
36 
37 class AUDACITY_DLL_API SpecCache {
38 public:
39 
40  // Make invalid cache
42  : algorithm(-1)
43  , pps(-1.0)
44  , start(-1.0)
45  , windowType(-1)
46  , frequencyGain(-1)
47  , dirty(-1)
48  {
49  }
50 
52  {
53  }
54 
55  bool Matches(int dirty_, double pixelsPerSecond,
56  const SpectrogramSettings &settings, double rate) const;
57 
58  // Calculate one column of the spectrum
59  bool CalculateOneSpectrum
61  WaveTrackCache &waveTrackCache,
62  const int xx, sampleCount numSamples,
63  double offset, double rate, double pixelsPerSecond,
64  int lowerBoundX, int upperBoundX,
65  const std::vector<float> &gainFactors,
66  float* __restrict scratch,
67  float* __restrict out) const;
68 
69  // Grow the cache while preserving the (possibly now invalid!) contents
70  void Grow(size_t len_, const SpectrogramSettings& settings,
71  double pixelsPerSecond, double start_);
72 
73  // Calculate the dirty columns at the begin and end of the cache
74  void Populate
75  (const SpectrogramSettings &settings, WaveTrackCache &waveTrackCache,
76  int copyBegin, int copyEnd, size_t numPixels,
77  sampleCount numSamples,
78  double offset, double rate, double pixelsPerSecond);
79 
80  size_t len { 0 }; // counts pixels, not samples
81  int algorithm;
82  double pps;
83  double start;
85  size_t windowSize { 0 };
86  unsigned zeroPaddingFactor { 0 };
88  std::vector<float> freq;
89  std::vector<sampleCount> where;
90 
91  int dirty;
92 };
93 
94 class SpecPxCache {
95 public:
96  SpecPxCache(size_t cacheLen)
97  : len{ cacheLen }
98  , values{ len }
99  {
100  valid = false;
101  scaleType = 0;
102  range = gain = -1;
103  minFreq = maxFreq = -1;
104  }
105 
106  size_t len;
108  bool valid;
109 
111  int range;
112  int gain;
113  int minFreq;
114  int maxFreq;
115 };
116 
117 class WaveClip;
118 
119 // Array of pointers that assume ownership
120 using WaveClipHolder = std::shared_ptr< WaveClip >;
121 using WaveClipHolders = std::vector < WaveClipHolder >;
122 using WaveClipConstHolders = std::vector < std::shared_ptr< const WaveClip > >;
123 
124 // A bundle of arrays needed for drawing waveforms. The object may or may not
125 // own the storage for those arrays. If it does, it destroys them.
127 {
128 public:
129  int width;
131  float *min, *max, *rms;
132  int* bl;
133 
134  std::vector<sampleCount> ownWhere;
135  std::vector<float> ownMin, ownMax, ownRms;
136  std::vector<int> ownBl;
137 
138 public:
139  WaveDisplay(int w)
140  : width(w), where(0), min(0), max(0), rms(0), bl(0)
141  {
142  }
143 
144  // Create "own" arrays.
145  void Allocate()
146  {
147  ownWhere.resize(width + 1);
148  ownMin.resize(width);
149  ownMax.resize(width);
150  ownRms.resize(width);
151  ownBl.resize(width);
152 
153  where = &ownWhere[0];
154  if (width > 0) {
155  min = &ownMin[0];
156  max = &ownMax[0];
157  rms = &ownRms[0];
158  bl = &ownBl[0];
159  }
160  else {
161  min = max = rms = 0;
162  bl = 0;
163  }
164  }
165 
167  {
168  }
169 };
170 
171 class AUDACITY_DLL_API WaveClip final : public XMLTagHandler
172 {
173 private:
174  // It is an error to copy a WaveClip without specifying the
175  // sample block factory.
176 
177  WaveClip(const WaveClip&) PROHIBITED;
178  WaveClip& operator= (const WaveClip&) PROHIBITED;
179 
180 public:
181  // typical constructor
183  int rate, int colourIndex);
184 
185  // essentially a copy constructor - but you must pass in the
186  // current sample block factory, because we might be copying
187  // from one project to another
188  WaveClip(const WaveClip& orig,
190  bool copyCutlines);
191 
192  // Copy only a range from the given WaveClip
193  WaveClip(const WaveClip& orig,
195  bool copyCutlines,
196  double t0, double t1);
197 
198  virtual ~WaveClip();
199 
200  void ConvertToSampleFormat(sampleFormat format,
201  const std::function<void(size_t)> & progressReport = {});
202 
203  // Always gives non-negative answer, not more than sample sequence length
204  // even if t0 really falls outside that range
205  void TimeToSamplesClip(double t0, sampleCount *s0) const;
206 
207  int GetRate() const { return mRate; }
208 
209  // Set rate without resampling. This will change the length of the clip
210  void SetRate(int rate);
211 
212  // Resample clip. This also will set the rate, but without changing
213  // the length of the clip
214  void Resample(int rate, ProgressDialog *progress = NULL);
215 
216  void SetColourIndex( int index ){ mColourIndex = index;};
217  int GetColourIndex( ) const { return mColourIndex;};
218  void SetOffset(double offset);
219  double GetOffset() const { return mOffset; }
221  void Offset(double delta)
222  { SetOffset(GetOffset() + delta); }
223  double GetStartTime() const;
224  double GetEndTime() const;
225  sampleCount GetStartSample() const;
226  sampleCount GetEndSample() const;
227  sampleCount GetNumSamples() const;
228 
229  // One and only one of the following is true for a given t (unless the clip
230  // has zero length -- then BeforeClip() and AfterClip() can both be true).
231  // Within() is true if the time is substantially within the clip
232  bool WithinClip(double t) const;
233  bool BeforeClip(double t) const;
234  bool AfterClip(double t) const;
235  bool IsClipStartAfterClip(double t) const;
236 
237  bool GetSamples(samplePtr buffer, sampleFormat format,
238  sampleCount start, size_t len, bool mayThrow = true) const;
239  void SetSamples(constSamplePtr buffer, sampleFormat format,
240  sampleCount start, size_t len);
241 
242  Envelope* GetEnvelope() { return mEnvelope.get(); }
243  const Envelope* GetEnvelope() const { return mEnvelope.get(); }
244  BlockArray* GetSequenceBlockArray();
245  const BlockArray* GetSequenceBlockArray() const;
246 
247  // Get low-level access to the sequence. Whenever possible, don't use this,
248  // but use more high-level functions inside WaveClip (or add them if you
249  // think they are useful for general use)
250  Sequence* GetSequence() { return mSequence.get(); }
251  const Sequence* GetSequence() const { return mSequence.get(); }
252 
257  void MarkChanged()
258  { mDirty++; }
259 
262  bool GetWaveDisplay(WaveDisplay &display,
263  double t0, double pixelsPerSecond) const;
264  bool GetSpectrogram(WaveTrackCache &cache,
265  const float *& spectrogram,
266  const sampleCount *& where,
267  size_t numPixels,
268  double t0, double pixelsPerSecond) const;
269  std::pair<float, float> GetMinMax(
270  double t0, double t1, bool mayThrow = true) const;
271  float GetRMS(double t0, double t1, bool mayThrow = true) const;
272 
276  void UpdateEnvelopeTrackLen();
277 
279  std::shared_ptr<SampleBlock> AppendNewBlock(
280  samplePtr buffer, sampleFormat format, size_t len);
281 
283  void AppendSharedBlock(const std::shared_ptr<SampleBlock> &pBlock);
284 
288  size_t len, unsigned int stride);
290  void Flush();
291 
294  void Clear(double t0, double t1);
295 
297  void ClearAndAddCutLine(double t0, double t1);
298 
300  void Paste(double t0, const WaveClip* other);
301 
304  void InsertSilence( double t, double len, double *pEnvelopeValue = nullptr );
305 
308  void AppendSilence( double len, double envelopeValue );
309 
311  WaveClipHolders &GetCutLines() { return mCutLines; }
313  { return reinterpret_cast< const WaveClipConstHolders& >( mCutLines ); }
314  size_t NumCutLines() const { return mCutLines.size(); }
315 
319  bool FindCutLine(double cutLinePosition,
320  double* cutLineStart = NULL,
321  double *cutLineEnd = NULL) const;
322 
326  void ExpandCutLine(double cutLinePosition);
327 
329  bool RemoveCutLine(double cutLinePosition);
330 
332  void OffsetCutLines(double t0, double len);
333 
334  void CloseLock(); //should be called when the project closes.
335  // not balanced by unlocking calls.
336 
338  void ClearWaveCache();
339 
340  //
341  // XMLTagHandler callback methods for loading and saving
342  //
343 
344  bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
345  void HandleXMLEndTag(const wxChar *tag) override;
346  XMLTagHandler *HandleXMLChild(const wxChar *tag) override;
347  void WriteXML(XMLWriter &xmlFile) const /* not override */;
348 
349  // AWD, Oct 2009: for pasting whitespace at the end of selection
350  bool GetIsPlaceholder() const { return mIsPlaceholder; }
351  void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }
352 
353  // used by commands which interact with clips using the keyboard
354  bool SharesBoundaryWithNextClip(const WaveClip* next) const;
355 
356 public:
357  // Cache of values to colour pixels of Spectrogram - used by TrackArtist
358  mutable std::unique_ptr<SpecPxCache> mSpecPxCache;
359 
360 protected:
361  double mOffset { 0 };
362  int mRate;
363  int mDirty { 0 };
365 
366  std::unique_ptr<Sequence> mSequence;
367  std::unique_ptr<Envelope> mEnvelope;
368 
369  mutable std::unique_ptr<WaveCache> mWaveCache;
370  mutable std::unique_ptr<SpecCache> mSpecCache;
371  SampleBuffer mAppendBuffer {};
372  size_t mAppendBufferLen { 0 };
373 
374  // Cut Lines are nothing more than ordinary wave clips, with the
375  // offset relative to the start of the clip.
376  WaveClipHolders mCutLines {};
377 
378  // AWD, Oct. 2009: for whitespace-at-end-of-selection pasting
379  bool mIsPlaceholder { false };
380 };
381 
382 #endif
WaveTrackCache
A short-lived object, during whose lifetime, the contents of the WaveTrack are assumed not to change.
Definition: WaveTrack.h:624
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
SpectrogramSettings
Spectrogram settings, either for one track or as defaults.
Definition: SpectrogramSettings.h:27
WaveClip::mEnvelope
std::unique_ptr< Envelope > mEnvelope
Definition: WaveClip.h:367
WaveClip::WaveClip
WaveClip(const WaveClip &) PROHIBITED
wxFileNameWrapper
Definition: wxFileNameWrapper.h:21
SpecCache::windowType
int windowType
Definition: WaveClip.h:84
WaveClip::GetEnvelope
Envelope * GetEnvelope()
Definition: WaveClip.h:242
WaveDisplay::ownWhere
std::vector< sampleCount > ownWhere
Definition: WaveClip.h:134
constSamplePtr
const char * constSamplePtr
Definition: Types.h:215
WaveClip::GetOffset
double GetOffset() const
Definition: WaveClip.h:219
WaveClip::GetRate
int GetRate() const
Definition: WaveClip.h:207
Envelope
Piecewise linear or piecewise exponential function from double to double.
Definition: Envelope.h:71
SpecPxCache::scaleType
int scaleType
Definition: WaveClip.h:110
XMLTagHandler::HandleXMLEndTag
virtual void HandleXMLEndTag(const wxChar *WXUNUSED(tag))
Definition: XMLTagHandler.h:97
WaveDisplay::ownMax
std::vector< float > ownMax
Definition: WaveClip.h:135
WaveCache
Cache used with WaveClip to cache wave information (for drawing).
Definition: WaveClip.cpp:47
WaveClip::mWaveCache
std::unique_ptr< WaveCache > mWaveCache
Definition: WaveClip.h:369
Sequence
A WaveTrack contains WaveClip(s). A WaveClip contains a Sequence. A Sequence is primarily an interfac...
Definition: Sequence.h:61
WaveClipHolders
std::vector< WaveClipHolder > WaveClipHolders
Definition: WaveClip.h:121
SpecCache::SpecCache
SpecCache()
Definition: WaveClip.h:41
SpecCache::dirty
int dirty
Definition: WaveClip.h:91
WaveClip::GetCutLines
WaveClipHolders & GetCutLines()
Get access to cut lines list.
Definition: WaveClip.h:311
SpecPxCache::SpecPxCache
SpecPxCache(size_t cacheLen)
Definition: WaveClip.h:96
WaveClip::GetSequence
Sequence * GetSequence()
Definition: WaveClip.h:250
WaveClip::mColourIndex
int mColourIndex
Definition: WaveClip.h:364
WaveClip
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:172
SpecPxCache::values
Floats values
Definition: WaveClip.h:107
samplePtr
char * samplePtr
Definition: Types.h:214
SpecPxCache::len
size_t len
Definition: WaveClip.h:106
WaveDisplay::~WaveDisplay
~WaveDisplay()
Definition: WaveClip.h:166
sampleFormat
sampleFormat
Definition: Types.h:194
XMLTagHandler.h
ProgressDialog
ProgressDialog Class.
Definition: ProgressDialog.h:56
factory
static RegisteredToolbarFactory factory
Definition: ControlToolBar.cpp:807
SpecCache
Definition: WaveClip.h:37
WaveDisplay::max
float * max
Definition: WaveClip.h:131
WaveClip::GetColourIndex
int GetColourIndex() const
Definition: WaveClip.h:217
WaveClip::GetEnvelope
const Envelope * GetEnvelope() const
Definition: WaveClip.h:243
Append
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;})
WaveClip::Offset
void Offset(double delta)
Definition: WaveClip.h:221
WaveDisplay::width
int width
Definition: WaveClip.h:129
SpecCache::~SpecCache
~SpecCache()
Definition: WaveClip.h:51
format
int format
Definition: ExportPCM.cpp:54
WaveClip::mRate
int mRate
Definition: WaveClip.h:362
WaveClip::SetColourIndex
void SetColourIndex(int index)
Definition: WaveClip.h:216
Resample
Interface to libsoxr.
Definition: Resample.h:29
WaveClipHolder
std::shared_ptr< WaveClip > WaveClipHolder
Definition: WaveClip.h:120
SpecPxCache::valid
bool valid
Definition: WaveClip.h:108
XMLTagHandler
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:80
SampleBuffer
Definition: SampleFormat.h:57
WaveDisplay::ownRms
std::vector< float > ownRms
Definition: WaveClip.h:135
WaveClip::GetIsPlaceholder
bool GetIsPlaceholder() const
Definition: WaveClip.h:350
WaveDisplay::ownBl
std::vector< int > ownBl
Definition: WaveClip.h:136
BlockArray
Definition: Sequence.h:49
WaveClip::NumCutLines
size_t NumCutLines() const
Definition: WaveClip.h:314
sampleCount
Definition: Types.h:66
WaveDisplay::ownMin
std::vector< float > ownMin
Definition: WaveClip.h:135
WaveClip::GetCutLines
const WaveClipConstHolders & GetCutLines() const
Definition: WaveClip.h:312
WaveDisplay::rms
float * rms
Definition: WaveClip.h:131
SpecCache::algorithm
int algorithm
Definition: WaveClip.h:81
SampleBlock
Abstract class allows access to contents of a block of sound samples, serialization as XML,...
Definition: SampleBlock.h:45
WaveClipConstHolders
std::vector< std::shared_ptr< const WaveClip > > WaveClipConstHolders
Definition: WaveClip.h:122
SpecPxCache::gain
int gain
Definition: WaveClip.h:112
XMLTagHandler::HandleXMLTag
virtual bool HandleXMLTag(const wxChar *tag, const wxChar **attrs)=0
WaveClip::mSpecPxCache
std::unique_ptr< SpecPxCache > mSpecPxCache
Definition: WaveClip.h:358
XMLTagHandler::HandleXMLChild
virtual XMLTagHandler * HandleXMLChild(const wxChar *tag)=0
SampleBlockFactory
abstract base class with methods to produce SampleBlock objects
Definition: SampleBlock.h:106
SpecCache::freq
std::vector< float > freq
Definition: WaveClip.h:88
SpecCache::where
std::vector< sampleCount > where
Definition: WaveClip.h:89
WaveDisplay::min
float * min
Definition: WaveClip.h:131
WaveClip::SetIsPlaceholder
void SetIsPlaceholder(bool val)
Definition: WaveClip.h:351
WaveClip::mSpecCache
std::unique_ptr< SpecCache > mSpecCache
Definition: WaveClip.h:370
WaveClip::mSequence
std::unique_ptr< Sequence > mSequence
Definition: WaveClip.h:366
SampleBlockFactoryPtr
std::shared_ptr< SampleBlockFactory > SampleBlockFactoryPtr
Definition: SampleBlock.h:25
SpecPxCache::range
int range
Definition: WaveClip.h:111
SpecPxCache::minFreq
int minFreq
Definition: WaveClip.h:113
settings
static Settings & settings()
Definition: TrackInfo.cpp:87
WaveClip::MarkChanged
void MarkChanged()
Definition: WaveClip.h:257
WaveClip::GetSequence
const Sequence * GetSequence() const
Definition: WaveClip.h:251
WaveDisplay::Allocate
void Allocate()
Definition: WaveClip.h:145
WaveDisplay::WaveDisplay
WaveDisplay(int w)
Definition: WaveClip.h:139
SampleFormat.h
SpecCache::start
double start
Definition: WaveClip.h:83
ArrayOf< float >
SpecCache::pps
double pps
Definition: WaveClip.h:82
SpecPxCache
Definition: WaveClip.h:94
WaveDisplay
Definition: WaveClip.h:127
WaveDisplay::where
sampleCount * where
Definition: WaveClip.h:130
SpecPxCache::maxFreq
int maxFreq
Definition: WaveClip.h:114
WaveDisplay::bl
int * bl
Definition: WaveClip.h:132
SpecCache::frequencyGain
int frequencyGain
Definition: WaveClip.h:87