Audacity 3.2.0
Public Member Functions | Public Attributes | List of all members
DtmfBase::Instance Struct Reference

Temporary state of the computation. More...

#include <DtmfBase.h>

Inheritance diagram for DtmfBase::Instance:
[legend]
Collaboration diagram for DtmfBase::Instance:
[legend]

Public Member Functions

 Instance (const PerTrackEffect &effect, double t0)
 
bool ProcessInitialize (EffectSettings &settings, double sampleRate, ChannelNames chanMap) override
 
size_t ProcessBlock (EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen) override
 Called for destructive effect computation. More...
 
unsigned GetAudioInCount () const override
 How many input buffers to allocate at once. More...
 
unsigned GetAudioOutCount () const override
 How many output buffers to allocate at once. More...
 
- Public Member Functions inherited from PerTrackEffect::Instance
 Instance (const PerTrackEffect &processor)
 
 ~Instance () override
 
bool Process (EffectSettings &settings) final
 Uses the other virtual functions of this class. More...
 
bool ProcessInitialize (EffectSettings &settings, double sampleRate, ChannelNames chanMap) override
 
bool ProcessFinalize () noexcept override
 
- Public Member Functions inherited from EffectInstanceEx
virtual bool Init ()
 Call once to set up state for whole list of tracks to be processed. More...
 
virtual bool Process (EffectSettings &settings)=0
 Actually do the effect here. More...
 
 ~EffectInstanceEx () override
 
- Public Member Functions inherited from EffectInstance
virtual ~EffectInstance ()
 
virtual size_t GetBlockSize () const =0
 
virtual size_t SetBlockSize (size_t maxBlockSize)=0
 
virtual unsigned GetAudioInCount () const =0
 How many input buffers to allocate at once. More...
 
virtual unsigned GetAudioOutCount () const =0
 How many output buffers to allocate at once. More...
 
virtual bool RealtimeInitialize (EffectSettings &settings, double sampleRate)
 
virtual bool RealtimeAddProcessor (EffectSettings &settings, EffectOutputs *pOutputs, unsigned numChannels, float sampleRate)
 
virtual bool RealtimeSuspend ()
 
virtual bool RealtimeResume ()
 
virtual std::unique_ptr< MessageMakeMessage () const
 Called on the main thread, in which the result may be cloned. More...
 
virtual bool UsesMessages () const noexcept
 
virtual bool RealtimeProcessStart (MessagePackage &package)
 settings are possibly changed, since last call, by an asynchronous dialog More...
 
virtual size_t RealtimeProcess (size_t group, EffectSettings &settings, const float *const *inBuf, float *const *outBuf, size_t numSamples)
 
virtual void RealtimePassThrough (size_t group, EffectSettings &settings, const float *const *inBuf, size_t numSamples)
 Called instead of RealtimeProcess when the effect is bypassed. Default implementation does nothing. More...
 
virtual bool RealtimeProcessEnd (EffectSettings &settings) noexcept
 settings can be updated to let a dialog change appearance at idle More...
 
virtual bool RealtimeFinalize (EffectSettings &settings) noexcept
 
virtual size_t GetTailSize () const
 
virtual SampleCount GetLatency (const EffectSettings &settings, double sampleRate) const
 
virtual bool NeedsDither () const
 
virtual bool ProcessInitialize (EffectSettings &settings, double sampleRate, ChannelNames chanMap)=0
 
virtual bool ProcessFinalize () noexcept=0
 
virtual size_t ProcessBlock (EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen)=0
 Called for destructive effect computation. More...
 
- Public Member Functions inherited from EffectInstanceWithBlockSize
 ~EffectInstanceWithBlockSize () override
 
size_t GetBlockSize () const override
 
size_t SetBlockSize (size_t maxBlockSize) override
 

Public Attributes

const double mT0
 
double mSampleRate {}
 
sampleCount numSamplesSequence
 
sampleCount numSamplesTone
 
sampleCount numSamplesSilence
 
sampleCount diff
 
sampleCount numRemaining
 
sampleCount curTonePos
 
bool isTone
 
int curSeqPos
 

Additional Inherited Members

- Public Types inherited from EffectInstance
using Message = EffectSettingsAccess::Message
 
using SampleCount = uint64_t
 
- Protected Attributes inherited from PerTrackEffect::Instance
const PerTrackEffectmProcessor
 
- Protected Attributes inherited from EffectInstanceWithBlockSize
size_t mBlockSize { 0 }
 

Detailed Description

Temporary state of the computation.

Definition at line 54 of file DtmfBase.h.

Constructor & Destructor Documentation

◆ Instance()

DtmfBase::Instance::Instance ( const PerTrackEffect effect,
double  t0 
)
inline

Definition at line 56 of file DtmfBase.h.

57 : PerTrackEffect::Instance { effect }
58 , mT0 { t0 }
59 {
60 }
const double mT0
Definition: DtmfBase.h:79

Member Function Documentation

◆ GetAudioInCount()

unsigned DtmfBase::Instance::GetAudioInCount ( ) const
inlineoverridevirtual

How many input buffers to allocate at once.

If the instance processes channels independently, this can return 1 The result is not necessarily well defined before RealtimeInitialize

Implements EffectInstance.

Definition at line 69 of file DtmfBase.h.

70 {
71 return 0;
72 }

◆ GetAudioOutCount()

unsigned DtmfBase::Instance::GetAudioOutCount ( ) const
inlineoverridevirtual

How many output buffers to allocate at once.

The result is not necessarily well defined before RealtimeInitialize

Implements EffectInstance.

Definition at line 74 of file DtmfBase.h.

75 {
76 return 1;
77 }

◆ ProcessBlock()

size_t DtmfBase::Instance::ProcessBlock ( EffectSettings settings,
const float *const *  inBlock,
float *const *  outBlock,
size_t  blockLen 
)
overridevirtual

Called for destructive effect computation.

Implements EffectInstance.

Definition at line 146 of file DtmfBase.cpp.

149{
150 auto& dtmfSettings = GetSettings(settings);
151 float* buffer = outbuf[0];
152 decltype(size) processed = 0;
153
154 // for the whole dtmf sequence, we will be generating either tone or silence
155 // according to a bool value, and this might be done in small chunks of size
156 // 'block', as a single tone might sometimes be larger than the block
157 // tone and silence generally have different duration, thus two generation
158 // blocks
159 //
160 // Note: to overcome a 'clicking' noise introduced by the abrupt transition
161 // from/to silence, I added a fade in/out of 1/250th of a second (4ms). This
162 // can still be tweaked but gives excellent results at 44.1kHz: I haven't
163 // tried other freqs. A problem might be if the tone duration is very short
164 // (<10ms)... (?)
165 //
166 // One more problem is to deal with the approximations done when calculating
167 // the duration of both tone and silence: in some cases the final sum might
168 // not be same as the initial duration. So, to overcome this, we had a
169 // redistribution block up, and now we will spread the remaining samples in
170 // every bin in order to achieve the full duration: test case was to generate
171 // an 11 tone DTMF sequence, in 4 seconds, and with DutyCycle=75%: after
172 // generation you ended up with 3.999s or in other units: 3 seconds and 44097
173 // samples.
174 //
175 while (size)
176 {
177 if (numRemaining == 0)
178 {
179 isTone = !isTone;
180
181 if (isTone)
182 {
183 curSeqPos++;
185 curTonePos = 0;
186 }
187 else
188 {
190 }
191
192 // the statement takes care of extracting one sample from the diff bin
193 // and adding it into the current block until depletion
194 numRemaining += (diff-- > 0 ? 1 : 0);
195 }
196
197 const auto len = limitSampleBufferSize(size, numRemaining);
198
199 if (isTone)
200 {
201 // generate the tone and append
202 assert(curSeqPos < dtmfSettings.dtmfNTones);
204 buffer, len, mSampleRate, dtmfSettings.dtmfSequence[curSeqPos],
205 curTonePos, numSamplesTone, dtmfSettings.dtmfAmplitude);
206 curTonePos += len;
207 }
208 else
209 {
210 memset(buffer, 0, sizeof(float) * len);
211 }
212
213 numRemaining -= len;
214
215 buffer += len;
216 size -= len;
217 processed += len;
218 }
219
220 return processed;
221}
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
static Settings & settings()
Definition: TrackInfo.cpp:51
static bool MakeDtmfTone(float *buffer, size_t len, float fs, wxChar tone, sampleCount last, sampleCount total, float amplitude)
Definition: DtmfBase.cpp:282
static DtmfSettings & GetSettings(EffectSettings &settings)
Assume settings originated from MakeSettings() and copies thereof.
Definition: Effect.h:166
sampleCount diff
Definition: DtmfBase.h:85
sampleCount numSamplesSilence
Definition: DtmfBase.h:84
sampleCount curTonePos
Definition: DtmfBase.h:88
sampleCount numSamplesTone
Definition: DtmfBase.h:83
double mSampleRate
Definition: DtmfBase.h:80
sampleCount numRemaining
Definition: DtmfBase.h:87

References EffectWithSettings< DtmfSettings, PerTrackEffect >::GetSettings(), limitSampleBufferSize(), DtmfBase::MakeDtmfTone(), settings(), and size.

Here is the call graph for this function:

◆ ProcessInitialize()

bool DtmfBase::Instance::ProcessInitialize ( EffectSettings settings,
double  sampleRate,
ChannelNames  chanMap 
)
overridevirtual

Called at start of destructive processing, for each (mono/stereo) track Default implementation does nothing, returns true

Parameters
chanMapnull or array terminated with ChannelNameEOL. Do not retain the pointer
Postcondition
GetAudioInCount() and GetAudioOutCount() are well defined

Implements EffectInstance.

Definition at line 79 of file DtmfBase.cpp.

81{
83
84 auto& dtmfSettings = GetSettings(settings);
85 if (dtmfSettings.dtmfNTones == 0)
86 { // Bail if no DTFM sequence.
87 using namespace BasicUI;
88 // TODO: don't use mProcessor for this
90 XO("DTMF sequence empty.\nCheck ALL settings for this effect."),
91 MessageBoxOptions {}.IconStyle(Icon::Error));
92 return false;
93 }
94
95 double duration = settings.extra.GetDuration();
96
97 // all dtmf sequence durations in samples from seconds
98 // MJS: Note that mDuration is in seconds but will have been quantised to the
99 // units of the TTC. If this was 'samples' and the project rate was lower
100 // than the track rate, extra samples may get created as mDuration may now be
101 // > mT1 - mT0; However we are making our best efforts at creating what was
102 // asked for.
103
104 auto nT0 = (sampleCount)floor(mT0 * mSampleRate + 0.5);
105 auto nT1 = (sampleCount)floor((mT0 + duration) * mSampleRate + 0.5);
107 nT1 - nT0; // needs to be exact number of samples selected
108
109 // make under-estimates if anything, and then redistribute the few remaining
110 // samples
111 numSamplesTone = sampleCount(floor(dtmfSettings.dtmfTone * mSampleRate));
113 sampleCount(floor(dtmfSettings.dtmfSilence * mSampleRate));
114
115 // recalculate the sum, and spread the difference - due to approximations.
116 // Since diff should be in the order of "some" samples, a division (resulting
117 // in zero) is not sufficient, so we add the additional remaining samples in
118 // each tone/silence block, at least until available.
119 diff = numSamplesSequence - (dtmfSettings.dtmfNTones * numSamplesTone) -
120 (dtmfSettings.dtmfNTones - 1) * numSamplesSilence;
121 while (diff > 2 * dtmfSettings.dtmfNTones - 1)
122 { // more than one per thingToBeGenerated
123 // in this case, both numSamplesTone and numSamplesSilence would change,
124 // so it makes sense
125 // to recalculate diff here, otherwise just keep the value we already
126 // have
127
128 // should always be the case that dtmfNTones>1, as if 0, we don't even
129 // start processing, and with 1 there is no difference to spread (no
130 // silence slot)...
131 wxASSERT(dtmfSettings.dtmfNTones > 1);
132 numSamplesTone += (diff / (dtmfSettings.dtmfNTones));
133 numSamplesSilence += (diff / (dtmfSettings.dtmfNTones - 1));
134 diff = numSamplesSequence - (dtmfSettings.dtmfNTones * numSamplesTone) -
135 (dtmfSettings.dtmfNTones - 1) * numSamplesSilence;
136 }
137 wxASSERT(diff >= 0); // should never be negative
138
139 curSeqPos = -1; // pointer to string in dtmfSequence
140 isTone = false;
141 numRemaining = 0;
142
143 return true;
144}
XO("Cut/Copy/Paste")
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
MessageBoxResult ShowMessageBox(const TranslatableString &message, MessageBoxOptions options={})
Show a modal message box with either Ok or Yes and No, and optionally Cancel.
Definition: BasicUI.h:287
MessageBoxOptions && IconStyle(Icon style) &&
Definition: BasicUI.h:104
sampleCount numSamplesSequence
Definition: DtmfBase.h:82

References curSeqPos, diff, EffectWithSettings< DtmfSettings, PerTrackEffect >::GetSettings(), BasicUI::MessageBoxOptions::IconStyle(), isTone, mSampleRate, mT0, numRemaining, numSamplesSequence, numSamplesSilence, numSamplesTone, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, settings(), BasicUI::ShowMessageBox(), and XO().

Here is the call graph for this function:

Member Data Documentation

◆ curSeqPos

int DtmfBase::Instance::curSeqPos

Definition at line 90 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ curTonePos

sampleCount DtmfBase::Instance::curTonePos

Definition at line 88 of file DtmfBase.h.

◆ diff

sampleCount DtmfBase::Instance::diff

Definition at line 85 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ isTone

bool DtmfBase::Instance::isTone

Definition at line 89 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ mSampleRate

double DtmfBase::Instance::mSampleRate {}

Definition at line 80 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ mT0

const double DtmfBase::Instance::mT0

Definition at line 79 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ numRemaining

sampleCount DtmfBase::Instance::numRemaining

Definition at line 87 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ numSamplesSequence

sampleCount DtmfBase::Instance::numSamplesSequence

Definition at line 82 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ numSamplesSilence

sampleCount DtmfBase::Instance::numSamplesSilence

Definition at line 84 of file DtmfBase.h.

Referenced by ProcessInitialize().

◆ numSamplesTone

sampleCount DtmfBase::Instance::numSamplesTone

Definition at line 83 of file DtmfBase.h.

Referenced by ProcessInitialize().


The documentation for this struct was generated from the following files: