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