Audacity 3.2.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CompressorInstance Class Referencefinal

#include <CompressorInstance.h>

Inheritance diagram for CompressorInstance:
[legend]
Collaboration diagram for CompressorInstance:
[legend]

Public Member Functions

 CompressorInstance (const PerTrackEffect &effect)
 
 CompressorInstance (CompressorInstance &&other)
 
const std::optional< double > & GetSampleRate () const
 
float GetLatencyMs () const
 
void SetOutputQueue (std::weak_ptr< DynamicRangeProcessorOutputPacketQueue >)
 
void SetMeterValuesQueue (std::weak_ptr< DynamicRangeProcessorMeterValuesQueue > queue)
 
- 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 Member Functions inherited from Observer::Publisher< Message, NotifyAll >
template<typename Alloc = std::allocator<Record>>
 Publisher (ExceptionPolicy *pPolicy=nullptr, Alloc a={})
 Constructor supporting type-erased custom allocation/deletion. More...
 
 Publisher (Publisher &&)=default
 
Publisheroperator= (Publisher &&)=default
 
Subscription Subscribe (Callback callback)
 Connect a callback to the Publisher; later-connected are called earlier. More...
 
template<typename Object , typename Return , typename... Args>
Subscription Subscribe (Object &obj, Return(Object::*callback)(Args...))
 Overload of Subscribe takes an object and pointer-to-member-function. More...
 

Private Member Functions

bool ProcessInitialize (EffectSettings &settings, double sampleRate, ChannelNames chanMap) override
 
bool ProcessFinalize () noexcept override
 
size_t ProcessBlock (EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen) override
 Called for destructive effect computation. More...
 
bool RealtimeInitialize (EffectSettings &settings, double sampleRate) override
 
bool RealtimeResume () override
 
bool RealtimeAddProcessor (EffectSettings &settings, EffectOutputs *pOutputs, unsigned numChannels, float sampleRate) override
 
bool RealtimeFinalize (EffectSettings &settings) noexcept override
 
size_t RealtimeProcess (size_t group, EffectSettings &settings, const float *const *inbuf, float *const *outbuf, size_t numSamples) override
 
void RealtimePassThrough (size_t group, EffectSettings &settings, const float *const *inbuf, size_t numSamples) override
 Called instead of RealtimeProcess when the effect is bypassed. Default implementation does nothing. More...
 
void InstanceInit (EffectSettings &settings, CompressorInstance &instance, int numChannels, float sampleRate)
 
size_t InstanceProcess (EffectSettings &settings, CompressorProcessor &instance, const float *const *inBlock, float *const *outBlock, size_t blockLen)
 
EffectInstance::SampleCount GetLatency (const EffectSettings &settings, double sampleRate) const override
 
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...
 

Private Attributes

std::unique_ptr< CompressorProcessormCompressor
 
std::vector< CompressorInstancemSlaves
 
long long mSampleCounter = 0
 
std::optional< double > mSampleRate
 
std::weak_ptr< DynamicRangeProcessorOutputPacketQueuemOutputQueue
 
std::weak_ptr< DynamicRangeProcessorMeterValuesQueuemCompressionValueQueue
 

Additional Inherited Members

- Public Types inherited from EffectInstance
using Message = EffectSettingsAccess::Message
 
using SampleCount = uint64_t
 
- Public Types inherited from Observer::Publisher< Message, NotifyAll >
using message_type = Message
 
using CallbackReturn = std::conditional_t< NotifyAll, void, bool >
 
using Callback = std::function< CallbackReturn(const Message &) >
 Type of functions that can be connected to the Publisher. More...
 
- Static Public Attributes inherited from Observer::Publisher< Message, NotifyAll >
static constexpr bool notifies_all = NotifyAll
 
- Protected Member Functions inherited from Observer::Publisher< Message, NotifyAll >
CallbackReturn Publish (const Message &message)
 Send a message to connected callbacks. More...
 
- Protected Attributes inherited from PerTrackEffect::Instance
const PerTrackEffectmProcessor
 
- Protected Attributes inherited from EffectInstanceWithBlockSize
size_t mBlockSize { 0 }
 

Detailed Description

Definition at line 26 of file CompressorInstance.h.

Constructor & Destructor Documentation

◆ CompressorInstance() [1/2]

CompressorInstance::CompressorInstance ( const PerTrackEffect effect)
explicit

Audacity: A Digital Audio Editor

CompressorInstance.cpp

Matthieu Hodgkinson

Definition at line 16 of file CompressorInstance.cpp.

17 : PerTrackEffect::Instance { effect }
18 , mCompressor { std::make_unique<CompressorProcessor>() }
19{
20}
std::unique_ptr< CompressorProcessor > mCompressor

◆ CompressorInstance() [2/2]

CompressorInstance::CompressorInstance ( CompressorInstance &&  other)
explicit

Definition at line 22 of file CompressorInstance.cpp.

23 : PerTrackEffect::Instance { other }
24 , mCompressor { std::move(other.mCompressor) }
25 , mSlaves { std::move(other.mSlaves) }
26 , mSampleCounter { std::move(other.mSampleCounter) }
27 , mSampleRate { std::move(other.mSampleRate) }
28 , mOutputQueue { std::move(other.mOutputQueue) }
30{
31}
std::weak_ptr< DynamicRangeProcessorMeterValuesQueue > mCompressionValueQueue
std::optional< double > mSampleRate
std::weak_ptr< DynamicRangeProcessorOutputPacketQueue > mOutputQueue
std::vector< CompressorInstance > mSlaves

Member Function Documentation

◆ GetAudioInCount()

unsigned CompressorInstance::GetAudioInCount ( ) const
overrideprivatevirtual

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 238 of file CompressorInstance.cpp.

239{
240 return 2;
241}

Referenced by ProcessInitialize().

Here is the caller graph for this function:

◆ GetAudioOutCount()

unsigned CompressorInstance::GetAudioOutCount ( ) const
overrideprivatevirtual

How many output buffers to allocate at once.

The result is not necessarily well defined before RealtimeInitialize

Implements EffectInstance.

Definition at line 233 of file CompressorInstance.cpp.

234{
235 return 2;
236}

◆ GetLatency()

EffectInstance::SampleCount CompressorInstance::GetLatency ( const EffectSettings settings,
double  sampleRate 
) const
overrideprivatevirtual

Default implementation returns 0

Reimplemented from EffectInstance.

Definition at line 226 of file CompressorInstance.cpp.

228{
230 1000;
231}
static Settings & settings()
Definition: TrackInfo.cpp:51
DynamicRangeProcessorSettings GetDynamicRangeProcessorSettings(const EffectSettings &settings)

References anonymous_namespace{CompressorInstance.cpp}::GetDynamicRangeProcessorSettings(), DynamicRangeProcessorSettings::lookaheadMs, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and settings().

Here is the call graph for this function:

◆ GetLatencyMs()

float CompressorInstance::GetLatencyMs ( ) const

Definition at line 38 of file CompressorInstance.cpp.

39{
40 return mSlaves.empty() ?
41 mCompressor->GetSettings().lookaheadMs :
42 mSlaves.front().mCompressor->GetSettings().lookaheadMs;
43}

References mCompressor, and mSlaves.

Referenced by DynamicRangeProcessorHistoryPanel::OnTimer().

Here is the caller graph for this function:

◆ GetSampleRate()

const std::optional< double > & CompressorInstance::GetSampleRate ( ) const

Definition at line 33 of file CompressorInstance.cpp.

34{
35 return mSampleRate;
36}

References mSampleRate.

◆ InstanceInit()

void CompressorInstance::InstanceInit ( EffectSettings settings,
CompressorInstance instance,
int  numChannels,
float  sampleRate 
)
private

Definition at line 206 of file CompressorInstance.cpp.

209{
210 instance.mOutputQueue = mOutputQueue;
212 instance.mCompressor->ApplySettingsIfNeeded(
214 instance.mCompressor->Init(sampleRate, numChannels, GetBlockSize());
215}
size_t GetBlockSize() const override

References EffectInstanceWithBlockSize::GetBlockSize(), anonymous_namespace{CompressorInstance.cpp}::GetDynamicRangeProcessorSettings(), mCompressionValueQueue, mCompressor, mOutputQueue, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and settings().

Referenced by ProcessInitialize(), and RealtimeAddProcessor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InstanceProcess()

size_t CompressorInstance::InstanceProcess ( EffectSettings settings,
CompressorProcessor instance,
const float *const *  inBlock,
float *const *  outBlock,
size_t  blockLen 
)
private

Definition at line 217 of file CompressorInstance.cpp.

220{
222 instance.Process(inBlock, outBlock, blockLen);
223 return blockLen;
224}
void ApplySettingsIfNeeded(const DynamicRangeProcessorSettings &settings)
void Process(const float *const *inBlock, float *const *outBlock, int blockLen)

References CompressorProcessor::ApplySettingsIfNeeded(), anonymous_namespace{CompressorInstance.cpp}::GetDynamicRangeProcessorSettings(), CompressorProcessor::Process(), and settings().

Referenced by ProcessBlock(), and RealtimeProcess().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProcessBlock()

size_t CompressorInstance::ProcessBlock ( EffectSettings settings,
const float *const *  inBlock,
float *const *  outBlock,
size_t  blockLen 
)
overrideprivatevirtual

Called for destructive effect computation.

Implements EffectInstance.

Definition at line 100 of file CompressorInstance.cpp.

103{
104 return InstanceProcess(settings, *mCompressor, inBlock, outBlock, blockLen);
105}
size_t InstanceProcess(EffectSettings &settings, CompressorProcessor &instance, const float *const *inBlock, float *const *outBlock, size_t blockLen)

References InstanceProcess(), mCompressor, and settings().

Here is the call graph for this function:

◆ ProcessFinalize()

bool CompressorInstance::ProcessFinalize ( )
overrideprivatevirtualnoexcept

Called at end of destructive processing, for each (mono/stereo) track Default implementation does nothing, returns true This may be called during stack unwinding:

Implements EffectInstance.

Definition at line 69 of file CompressorInstance.cpp.

70{
71 mSampleRate.reset();
72 return true;
73}

References mSampleRate.

◆ ProcessInitialize()

bool CompressorInstance::ProcessInitialize ( EffectSettings settings,
double  sampleRate,
ChannelNames  chanMap 
)
overrideprivatevirtual

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 61 of file CompressorInstance.cpp.

63{
66 return true;
67}
void InstanceInit(EffectSettings &settings, CompressorInstance &instance, int numChannels, float sampleRate)
unsigned GetAudioInCount() const override
How many input buffers to allocate at once.

References GetAudioInCount(), InstanceInit(), mSampleRate, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and settings().

Here is the call graph for this function:

◆ RealtimeAddProcessor()

bool CompressorInstance::RealtimeAddProcessor ( EffectSettings settings,
EffectOutputs pOutputs,
unsigned  numChannels,
float  sampleRate 
)
overrideprivatevirtual
Returns
success Default implementation does nothing, returns true

Reimplemented from EffectInstance.

Definition at line 128 of file CompressorInstance.cpp.

131{
132 mSlaves.emplace_back(mProcessor);
133 InstanceInit(settings, mSlaves.back(), numChannels, sampleRate);
134 return true;
135}
const PerTrackEffect & mProcessor

References InstanceInit(), PerTrackEffect::Instance::mProcessor, mSlaves, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and settings().

Here is the call graph for this function:

◆ RealtimeFinalize()

bool CompressorInstance::RealtimeFinalize ( EffectSettings settings)
overrideprivatevirtualnoexcept
Returns
success Default implementation does nothing, returns true

Reimplemented from EffectInstance.

Definition at line 137 of file CompressorInstance.cpp.

138{
139 mSlaves.clear();
140 mSampleRate.reset();
142 return true;
143}
CallbackReturn Publish(const Message &message)
Send a message to connected callbacks.
Definition: Observer.h:207

References Observer::Publisher< Message, NotifyAll >::Publish().

Here is the call graph for this function:

◆ RealtimeInitialize()

bool CompressorInstance::RealtimeInitialize ( EffectSettings settings,
double  sampleRate 
)
overrideprivatevirtual
Returns
success
Postcondition
GetAudioInCount() and GetAudioOutCount() are well defined

Default implementation does nothing, returns false (so assume realtime is not supported). Other member functions related to realtime return true or zero, but will not be called, unless a derived class overrides RealtimeInitialize.

Reimplemented from EffectInstance.

Definition at line 107 of file CompressorInstance.cpp.

108{
109 SetBlockSize(512);
110 mSlaves.clear();
111 mSampleCounter = 0;
114 std::make_optional(InitializeProcessingSettings { sampleRate }));
115 return true;
116}
size_t SetBlockSize(size_t maxBlockSize) override

References mSampleCounter, mSampleRate, mSlaves, Observer::Publisher< Message, NotifyAll >::Publish(), anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and EffectInstanceWithBlockSize::SetBlockSize().

Here is the call graph for this function:

◆ RealtimePassThrough()

void CompressorInstance::RealtimePassThrough ( size_t  group,
EffectSettings settings,
const float *const *  inBuf,
size_t  numSamples 
)
overrideprivatevirtual

Called instead of RealtimeProcess when the effect is bypassed. Default implementation does nothing.

Reimplemented from EffectInstance.

Definition at line 195 of file CompressorInstance.cpp.

198{
199 // Keep track of the amount of samples that passed by, so that when
200 // processing resumes, the visualization reflects the elapsed time while
201 // bypassed.
202 if (group < mSlaves.size())
203 mSlaves[group].mSampleCounter += numSamples;
204}

References mSlaves.

◆ RealtimeProcess()

size_t CompressorInstance::RealtimeProcess ( size_t  group,
EffectSettings settings,
const float *const *  inBuf,
float *const *  outBuf,
size_t  numSamples 
)
overrideprivatevirtual
Returns
success Default implementation does nothing, returns 0

Reimplemented from EffectInstance.

Definition at line 156 of file CompressorInstance.cpp.

159{
160 if (group >= mSlaves.size())
161 return 0;
162 auto& slave = mSlaves[group];
163 auto& compressor = *slave.mCompressor;
164 const auto numProcessedSamples =
165 InstanceProcess(settings, compressor, inbuf, outbuf, numSamples);
166 if (const auto queue = slave.mOutputQueue.lock())
167 {
168 const auto& frameStats = compressor.GetLastFrameStats();
169 const auto& compressorSettings = compressor.GetSettings();
170 const float netGain = compressorSettings.outCompressionThreshDb -
171 compressorSettings.inCompressionThreshDb;
172 const auto targetCompressionDb =
173 compressor.EvaluateTransferFunction(frameStats.maxInputSampleDb) -
174 frameStats.maxInputSampleDb - netGain;
176 newPacket.indexOfFirstSample = slave.mSampleCounter;
177 newPacket.numSamples = numProcessedSamples;
178 newPacket.targetCompressionDb = targetCompressionDb;
179 newPacket.actualCompressionDb = frameStats.dbGainOfMaxInputSample;
180 newPacket.inputDb = frameStats.maxInputSampleDb;
181 newPacket.outputDb = GetOutputDb(frameStats, compressorSettings);
182 queue->Put(newPacket);
183 }
184
185 if (const auto queue = slave.mCompressionValueQueue.lock())
186 queue->Put(MeterValues {
187 compressor.GetLastFrameStats().dbGainOfMaxInputSample,
189 compressor.GetLastFrameStats(), compressor.GetSettings()) });
190
191 slave.mSampleCounter += numProcessedSamples;
192 return numProcessedSamples;
193}
float GetOutputDb(const CompressorProcessor::FrameStats &stats, const DynamicRangeProcessorSettings &settings)

References DynamicRangeProcessorOutputPacket::actualCompressionDb, anonymous_namespace{CompressorInstance.cpp}::GetOutputDb(), DynamicRangeProcessorOutputPacket::indexOfFirstSample, DynamicRangeProcessorOutputPacket::inputDb, InstanceProcess(), mSlaves, DynamicRangeProcessorOutputPacket::numSamples, DynamicRangeProcessorOutputPacket::outputDb, settings(), and DynamicRangeProcessorOutputPacket::targetCompressionDb.

Here is the call graph for this function:

◆ RealtimeResume()

bool CompressorInstance::RealtimeResume ( )
overrideprivatevirtual
Returns
success Default implementation does nothing, returns true

Reimplemented from EffectInstance.

Definition at line 118 of file CompressorInstance.cpp.

119{
120 for (auto& slave : mSlaves)
121 // Neither block size nore sample rate or any other parameter has changed,
122 // so `Reinit()` should not reallocate memory.
123 slave.mCompressor->Reinit();
125 return true;
126}

References mSlaves, and Observer::Publisher< Message, NotifyAll >::Publish().

Here is the call graph for this function:

◆ SetMeterValuesQueue()

void CompressorInstance::SetMeterValuesQueue ( std::weak_ptr< DynamicRangeProcessorMeterValuesQueue queue)

Definition at line 53 of file CompressorInstance.cpp.

55{
57 for (auto& slave : mSlaves)
58 slave.mCompressionValueQueue = queue;
59}

References mCompressionValueQueue, and mSlaves.

◆ SetOutputQueue()

void CompressorInstance::SetOutputQueue ( std::weak_ptr< DynamicRangeProcessorOutputPacketQueue outputQueue)

Definition at line 45 of file CompressorInstance.cpp.

47{
48 mOutputQueue = outputQueue;
49 for (auto& slave : mSlaves)
50 slave.mOutputQueue = outputQueue;
51}

References mOutputQueue, and mSlaves.

Referenced by DynamicRangeProcessorHistoryPanel::InitializeForPlayback().

Here is the caller graph for this function:

Member Data Documentation

◆ mCompressionValueQueue

std::weak_ptr<DynamicRangeProcessorMeterValuesQueue> CompressorInstance::mCompressionValueQueue
private

Definition at line 91 of file CompressorInstance.h.

Referenced by InstanceInit(), and SetMeterValuesQueue().

◆ mCompressor

std::unique_ptr<CompressorProcessor> CompressorInstance::mCompressor
private

Definition at line 86 of file CompressorInstance.h.

Referenced by GetLatencyMs(), InstanceInit(), and ProcessBlock().

◆ mOutputQueue

std::weak_ptr<DynamicRangeProcessorOutputPacketQueue> CompressorInstance::mOutputQueue
private

Definition at line 90 of file CompressorInstance.h.

Referenced by InstanceInit(), and SetOutputQueue().

◆ mSampleCounter

long long CompressorInstance::mSampleCounter = 0
private

Definition at line 88 of file CompressorInstance.h.

Referenced by RealtimeInitialize().

◆ mSampleRate

std::optional<double> CompressorInstance::mSampleRate
private

◆ mSlaves

std::vector<CompressorInstance> CompressorInstance::mSlaves
private

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