Audacity  3.0.3
SimpleMono.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  SimpleMono.cpp
6 
7  Dominic Mazzoni
8 
9 *******************************************************************//*******************************************************************/
20 
21 
22 
23 #include "SimpleMono.h"
24 
25 #include "../WaveTrack.h"
26 
27 #include <math.h>
28 
30 {
31  //Iterate over each track
32  this->CopyInputTracks(); // Set up mOutputTracks.
33  bool bGoodResult = true;
34 
35  mCurTrackNum = 0;
36  for( auto pOutWaveTrack : mOutputTracks->Selected< WaveTrack >() )
37  {
38  //Get start and end times from track
39  double trackStart = pOutWaveTrack->GetStartTime();
40  double trackEnd = pOutWaveTrack->GetEndTime();
41 
42  //Set the current bounds to whichever left marker is
43  //greater and whichever right marker is less:
44  mCurT0 = mT0 < trackStart? trackStart: mT0;
45  mCurT1 = mT1 > trackEnd? trackEnd: mT1;
46 
47  // Process only if the right marker is to the right of the left marker
48  if (mCurT1 > mCurT0) {
49 
50  //Transform the marker timepoints to samples
51  auto start = pOutWaveTrack->TimeToLongSamples(mCurT0);
52  auto end = pOutWaveTrack->TimeToLongSamples(mCurT1);
53 
54  //Get the track rate and samples
55  mCurRate = pOutWaveTrack->GetRate();
56  mCurChannel = pOutWaveTrack->GetChannel();
57 
58  //NewTrackSimpleMono() will returns true by default
59  //ProcessOne() processes a single track
60  if (!NewTrackSimpleMono() || !ProcessOne(pOutWaveTrack, start, end))
61  {
62  bGoodResult = false;
63  break;
64  }
65  }
66 
67  mCurTrackNum++;
68  }
69 
70  this->ReplaceProcessedTracks(bGoodResult);
71  return bGoodResult;
72 }
73 
74 
75 //ProcessOne() takes a track, transforms it to bunch of buffer-blocks,
76 //and executes ProcessSimpleMono on these blocks
78  sampleCount start, sampleCount end)
79 {
80  //Get the length of the buffer (as double). len is
81  //used simple to calculate a progress meter, so it is easier
82  //to make it a double now than it is to do it later
83  auto len = (end - start).as_double();
84 
85  //Initiate a processing buffer. This buffer will (most likely)
86  //be shorter than the length of the track being processed.
87  Floats buffer{ track->GetMaxBlockSize() };
88 
89  //Go through the track one buffer at a time. s counts which
90  //sample the current buffer starts at.
91  auto s = start;
92  while (s < end) {
93  //Get a block of samples (smaller than the size of the buffer)
94  //Adjust the block size if it is the final block in the track
95  const auto block =
96  limitSampleBufferSize( track->GetBestBlockSize(s), end - s );
97 
98  //Get the samples from the track and put them in the buffer
99  track->GetFloats(buffer.get(), s, block);
100 
101  //Process the buffer. If it fails, clean up and exit.
102  if (!ProcessSimpleMono(buffer.get(), block))
103  //Return false because the effect failed.
104  return false;
105 
106  //Processing succeeded. copy the newly-changed samples back
107  //onto the track.
108  track->Set((samplePtr) buffer.get(), floatSample, s, block);
109 
110  //Increment s one blockfull of samples
111  s += block;
112 
113  //Update the Progress meter
115  (s - start).as_double() /
116  len))
117  return false;
118  }
119 
120  //Return true because the effect processing succeeded.
121  return true;
122 }
123 
124 //null implementation of NewTrackSimpleMono
126 {
127  return true;
128 }
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:69
EffectSimpleMono::mCurChannel
int mCurChannel
Definition: SimpleMono.h:45
Effect::CopyInputTracks
void CopyInputTracks(bool allSyncLockSelected=false)
Definition: Effect.cpp:2071
Effect::mT1
double mT1
Definition: Effect.h:467
EffectSimpleMono::Process
bool Process() override
Definition: SimpleMono.cpp:29
SimpleMono.h
WaveTrack::Set
void Set(constSamplePtr buffer, sampleFormat format, sampleCount start, size_t len)
Definition: WaveTrack.cpp:2076
floatSample
@ floatSample
Definition: SampleFormat.h:34
Effect::mT0
double mT0
Definition: Effect.h:466
EffectSimpleMono::ProcessSimpleMono
virtual bool ProcessSimpleMono(float *buffer, size_t len)=0
Effect::ReplaceProcessedTracks
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2193
EffectSimpleMono::ProcessOne
bool ProcessOne(WaveTrack *t, sampleCount start, sampleCount end)
Definition: SimpleMono.cpp:77
Effect::mOutputTracks
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:465
samplePtr
char * samplePtr
Definition: SampleFormat.h:49
WaveTrack::GetMaxBlockSize
size_t GetMaxBlockSize() const
Definition: WaveTrack.cpp:1669
sampleCount
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
EffectSimpleMono::NewTrackSimpleMono
virtual bool NewTrackSimpleMono()=0
Definition: SimpleMono.cpp:125
WaveTrack::GetBestBlockSize
size_t GetBestBlockSize(sampleCount t) const
Definition: WaveTrack.cpp:1651
EffectSimpleMono::mCurT0
double mCurT0
Definition: SimpleMono.h:43
EffectSimpleMono::mCurRate
double mCurRate
Definition: SimpleMono.h:42
EffectSimpleMono::mCurTrackNum
int mCurTrackNum
Definition: SimpleMono.h:41
limitSampleBufferSize
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
WaveTrack::GetFloats
bool GetFloats(float *buffer, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
Retrieve samples from a track in floating-point format, regardless of the storage format.
Definition: WaveTrack.h:265
Effect::TrackProgress
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={})
Definition: Effect.cpp:2025
ArrayOf< float >
EffectSimpleMono::mCurT1
double mCurT1
Definition: SimpleMono.h:44