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

Combines multiple audio graph sources into a single source. More...

#include <DownmixStage.h>

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

Public Types

enum class  ApplyVolume { Discard , MapChannels , Mixdown }
 
- Public Types inherited from AudioGraph::Source
using Buffers = AudioGraph::Buffers
 

Public Member Functions

 DownmixStage (std::vector< std::unique_ptr< DownmixSource > > downmixSources, size_t numChannels, size_t bufferSize, ApplyVolume applyGain)
 
 ~DownmixStage () override
 
bool AcceptsBuffers (const Buffers &buffers) const override
 
bool AcceptsBlockSize (size_t blockSize) const override
 
std::optional< size_t > Acquire (Buffers &data, size_t maxToProcess) override
 Occupy vacant space in Buffers with some data. More...
 
sampleCount Remaining () const override
 Result includes any amount Acquired and not yet Released. More...
 
bool Release () override
 Caller is done examining last Acquire()d positions. More...
 
- Public Member Functions inherited from AudioGraph::Source
virtual ~Source ()
 
virtual bool AcceptsBuffers (const Buffers &buffers) const =0
 
virtual bool AcceptsBlockSize (size_t blockSize) const =0
 
virtual std::optional< size_t > Acquire (Buffers &data, size_t bound)=0
 Occupy vacant space in Buffers with some data. More...
 
virtual sampleCount Remaining () const =0
 Result includes any amount Acquired and not yet Released. More...
 
virtual bool Release ()=0
 Caller is done examining last Acquire()d positions. More...
 
virtual bool Terminates () const
 Needed only to make some postconditions assertable; defaults true. More...
 

Private Attributes

std::vector< std::unique_ptr< DownmixSource > > mDownmixSources
 
AudioGraph::Buffers mFloatBuffers
 
size_t mNumChannels
 
ApplyVolume mApplyVolume
 

Detailed Description

Combines multiple audio graph sources into a single source.

Definition at line 20 of file DownmixStage.h.

Member Enumeration Documentation

◆ ApplyVolume

enum class DownmixStage::ApplyVolume
strong
Enumerator
Discard 
MapChannels 
Mixdown 

Definition at line 23 of file DownmixStage.h.

24 {
25 Discard,//< No source volume is applied
26 MapChannels, //< Apply volume per source's channel
27 Mixdown, //< Average volume from all channels in the source, numOutChannels should be 1
28 };

Constructor & Destructor Documentation

◆ DownmixStage()

DownmixStage::DownmixStage ( std::vector< std::unique_ptr< DownmixSource > >  downmixSources,
size_t  numChannels,
size_t  bufferSize,
ApplyVolume  applyGain 
)

Definition at line 42 of file DownmixStage.cpp.

46 : mDownmixSources(std::move(downmixSources))
47 // PRL: Bug2536: see other comments below for the last, padding argument
48 // TODO: more-than-two-channels
49 // Issue 3565 workaround: allocate one extra buffer when applying a
50 // GVerb effect stage. It is simply discarded
51 // See also issue 3854, when the number of out channels expected by the
52 // plug-in is yet larger
53 , mFloatBuffers { 3, bufferSize, 1, 1 }
54 , mNumChannels(numChannels)
55 , mApplyVolume(applyGain)
56{
57
58}
AudioGraph::Buffers mFloatBuffers
Definition: DownmixStage.h:33
std::vector< std::unique_ptr< DownmixSource > > mDownmixSources
Definition: DownmixStage.h:31
size_t mNumChannels
Definition: DownmixStage.h:34
ApplyVolume mApplyVolume
Definition: DownmixStage.h:35

◆ ~DownmixStage()

DownmixStage::~DownmixStage ( )
overridedefault

Member Function Documentation

◆ AcceptsBlockSize()

bool DownmixStage::AcceptsBlockSize ( size_t  blockSize) const
overridevirtual

Implements AudioGraph::Source.

Definition at line 68 of file DownmixStage.cpp.

69{
70 return blockSize <= mFloatBuffers.BufferSize();
71}
size_t BufferSize() const

References AudioGraph::Buffers::BufferSize(), and mFloatBuffers.

Referenced by AcceptsBuffers().

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

◆ AcceptsBuffers()

bool DownmixStage::AcceptsBuffers ( const Buffers buffers) const
overridevirtual

Implements AudioGraph::Source.

Definition at line 62 of file DownmixStage.cpp.

63{
64 return buffers.Channels() == mNumChannels &&
65 AcceptsBlockSize(buffers.BlockSize());
66}
bool AcceptsBlockSize(size_t blockSize) const override

References AcceptsBlockSize(), AudioGraph::Buffers::BlockSize(), AudioGraph::Buffers::Channels(), and mNumChannels.

Here is the call graph for this function:

◆ Acquire()

std::optional< size_t > DownmixStage::Acquire ( Buffers data,
size_t  bound 
)
overridevirtual

Occupy vacant space in Buffers with some data.

May exceeed a single block of production Can assume same buffer is passed each time, while the caller advances it over the previous production, or discards it, or rotates the buffer. May rewind or rotate the buffer.

Returns
number of positions available to read from data or nullopt to fail
Precondition
AcceptsBuffers(data)
AcceptsBlockSize(data.BlockSize())
bound <= data.BlockSize()
data.BlockSize() <= data.Remaining()
Postcondition
result: !result || *result <= bound
result: !result || *result <= data.Remaining()
result: !result || *result <= Remaining()
data.Remaining() > 0
result: !result || bound == 0 || Remaining() == 0 || *result > 0 (progress guarantee)
!Terminates() or Remaining() was not previously defined, or is unchanged

Implements AudioGraph::Source.

Definition at line 73 of file DownmixStage.cpp.

74{
75 // TODO: more-than-two-channels
76 auto maxChannels = std::max(2u, mFloatBuffers.Channels());
77 const auto channelFlags = stackAllocate(unsigned char, mNumChannels);
78 const auto volumes = stackAllocate(float, mNumChannels);
80 std::fill(volumes, volumes + mNumChannels, 1.0f);
81
82 size_t maxOut = 0;
83 for (auto c = 0;c < data.Channels(); ++c)
84 data.ClearBuffer(c, maxToProcess);
85
86 for (auto& downmixSource : mDownmixSources)
87 {
88 auto oResult = downmixSource->GetDownstream().Acquire(mFloatBuffers, maxToProcess);
89 // One of MixVariableRates or MixSameRate assigns into mTemp[*][*]
90 // which are the sources for the CopySamples calls, and they copy into
91 // mBuffer[*][*]
92 if (!oResult)
93 return 0;
94 const auto result = *oResult;
95 maxOut = std::max(maxOut, result);
96
97 // Insert effect stages here! Passing them all channels of the track
98
99 const auto limit = std::min<size_t>(downmixSource->NChannels(), maxChannels);
100 for (size_t j = 0; j < limit; ++j)
101 {
102 const auto pFloat = (const float*)mFloatBuffers.GetReadPosition(j);
104 {
105 for (size_t c = 0; c < mNumChannels; ++c)
106 {
107 if (mNumChannels > 1)
108 volumes[c] = downmixSource->GetChannelGain(c);
109 else
110 volumes[c] = downmixSource->GetChannelGain(j);
111 }
113 mNumChannels == 1 &&
114 downmixSource->CanMakeMono())
115 {
116 volumes[0] /= static_cast<float>(limit);
117 }
118 }
119
120 downmixSource->FindChannelFlags(channelFlags, mNumChannels, j);
121 MixBuffers(mNumChannels, channelFlags, volumes, *pFloat, data, result);
122 }
123
124 downmixSource->GetDownstream().Release();
125 mFloatBuffers.Advance(result);
127 }
128
129 // MB: this doesn't take warping into account, replaced with code based on mSamplePos
130 //mT += (maxOut / mRate);
131
132 assert(maxOut <= maxToProcess);
133 return maxOut;
134}
#define stackAllocate(T, count)
void Advance(size_t count)
Move the positions.
size_t Rotate()
Shift all data at and after the old position to position 0.
unsigned Channels() const
constSamplePtr GetReadPosition(unsigned iChannel) const
Get accumulated data for one channel.
void MixBuffers(unsigned numChannels, const unsigned char *channelFlags, const float *volumes, const float &src, AudioGraph::Buffers &dests, int len)

References AudioGraph::Buffers::Advance(), AudioGraph::Buffers::Channels(), AudioGraph::Buffers::ClearBuffer(), Discard, AudioGraph::Buffers::GetReadPosition(), mApplyVolume, mDownmixSources, mFloatBuffers, anonymous_namespace{DownmixStage.cpp}::MixBuffers(), Mixdown, mNumChannels, AudioGraph::Buffers::Rotate(), and stackAllocate.

Here is the call graph for this function:

◆ Release()

bool DownmixStage::Release ( )
overridevirtual

Caller is done examining last Acquire()d positions.

May be called only after at least one successful call to Acquire()

Returns
success
Postcondition
!Terminates() or Remaining() reduced by what was last returned by Acquire()

Implements AudioGraph::Source.

Definition at line 144 of file DownmixStage.cpp.

145{
146 return true;
147}

◆ Remaining()

sampleCount DownmixStage::Remaining ( ) const
overridevirtual

Result includes any amount Acquired and not yet Released.

May be undefined before the first successful call to Acquire()

Postcondition
result: result >= 0

Implements AudioGraph::Source.

Definition at line 135 of file DownmixStage.cpp.

136{
137 return std::accumulate(
138 mDownmixSources.begin(), mDownmixSources.end(), sampleCount { 0 },
139 [](sampleCount sum, const auto& source) {
140 return std::max(sum, source->GetDownstream().Remaining());
141 });
142}
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19

References mDownmixSources.

Member Data Documentation

◆ mApplyVolume

ApplyVolume DownmixStage::mApplyVolume
private

Definition at line 35 of file DownmixStage.h.

Referenced by Acquire().

◆ mDownmixSources

std::vector<std::unique_ptr<DownmixSource> > DownmixStage::mDownmixSources
private

Definition at line 31 of file DownmixStage.h.

Referenced by Acquire(), and Remaining().

◆ mFloatBuffers

AudioGraph::Buffers DownmixStage::mFloatBuffers
private

Definition at line 33 of file DownmixStage.h.

Referenced by AcceptsBlockSize(), and Acquire().

◆ mNumChannels

size_t DownmixStage::mNumChannels
private

Definition at line 34 of file DownmixStage.h.

Referenced by AcceptsBuffers(), and Acquire().


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