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

Adapts WideSampleSequence to the interface AudioGraph::Source. More...

#include <WideSampleSource.h>

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

Public Types

using Poller = std::function< bool(sampleCount blockSize)>
 Type of function returning false if user cancels progress. More...
 
- Public Types inherited from AudioGraph::Source
using Buffers = AudioGraph::Buffers
 

Public Member Functions

 WideSampleSource (const WideSampleSequence &sequence, size_t nChannels, sampleCount start, sampleCount len, Poller pollUser)
 
 ~WideSampleSource () override
 
bool AcceptsBuffers (const Buffers &buffers) const override
 
bool AcceptsBlockSize (size_t blockSize) const override
 Always true. More...
 
std::optional< size_t > Acquire (Buffers &data, size_t bound) 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
 Can test for user cancellation. 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

const WideSampleSequencemSequence
 
const size_t mnChannels
 
const Poller mPollUser
 
sampleCount mPos {}
 
sampleCount mOutputRemaining {}
 
size_t mLastProduced {}
 
size_t mFetched {}
 
bool mInitialized { false }
 

Detailed Description

Adapts WideSampleSequence to the interface AudioGraph::Source.

Definition at line 24 of file WideSampleSource.h.

Member Typedef Documentation

◆ Poller

using WideSampleSource::Poller = std::function<bool(sampleCount blockSize)>

Type of function returning false if user cancels progress.

Definition at line 27 of file WideSampleSource.h.

Constructor & Destructor Documentation

◆ WideSampleSource()

WideSampleSource::WideSampleSource ( const WideSampleSequence sequence,
size_t  nChannels,
sampleCount  start,
sampleCount  len,
Poller  pollUser 
)
Precondition
nChannels <= sequence.NChannels()
Postcondition
Remaining() == len

Definition at line 25 of file WideSampleSource.cpp.

27 : mSequence{ sequence }, mnChannels{ nChannels }, mPollUser{ move(pollUser) }
28 , mPos{ start }, mOutputRemaining{ len }
29{
30 assert(nChannels <= sequence.NChannels());
31}
virtual size_t NChannels() const =0
A constant property.
const WideSampleSequence & mSequence
const Poller mPollUser
sampleCount mOutputRemaining
const size_t mnChannels

References WideSampleSequence::NChannels().

Here is the call graph for this function:

◆ ~WideSampleSource()

WideSampleSource::~WideSampleSource ( )
overridedefault

Member Function Documentation

◆ AcceptsBlockSize()

bool WideSampleSource::AcceptsBlockSize ( size_t  blockSize) const
overridevirtual

Always true.

Implements AudioGraph::Source.

Definition at line 40 of file WideSampleSource.cpp.

41{
42 return true;
43}

Referenced by Acquire().

Here is the caller graph for this function:

◆ AcceptsBuffers()

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

If constructed with positive length, then accepts buffers only when number of channels is positive

Implements AudioGraph::Source.

Definition at line 35 of file WideSampleSource.cpp.

36{
37 return mOutputRemaining == 0 || buffers.Channels() > 0;
38}

References AudioGraph::Buffers::Channels(), and mOutputRemaining.

Referenced by Acquire().

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

◆ Acquire()

std::optional< size_t > WideSampleSource::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 52 of file WideSampleSource.cpp.

53{
54 assert(bound <= data.BlockSize());
55 assert(data.BlockSize() <= data.Remaining());
56 assert(AcceptsBuffers(data));
57 assert(AcceptsBlockSize(data.BlockSize()));
58
59 if (!mInitialized || mFetched < bound) {
60 // Need to fill sufficent data in the buffers
61 // Calculate the number of samples to get
62 const auto fetch =
63 limitSampleBufferSize(data.Remaining() - mFetched, Remaining());
64 // guarantees write won't overflow
65 assert(mFetched + fetch <= data.Remaining());
66 // Fill the buffers
67 auto buffers = stackAllocate(float *, mnChannels);
68 if (mnChannels > 0)
69 buffers[0] = &data.GetWritePosition(0) + mFetched;
70 if (mnChannels > 1)
71 buffers[1] = &data.GetWritePosition(1) + mFetched;
72 mSequence.GetFloats(0, mnChannels, buffers, mPos, fetch);
73 mPos += fetch;
74 mFetched += fetch;
75 mInitialized = true;
76 }
77 assert(data.Remaining() > 0);
78 auto result = mLastProduced = std::min(bound,
79 limitSampleBufferSize(data.Remaining(), Remaining()));
80 // assert post
81 assert(result <= bound);
82 assert(result <= data.Remaining());
83 assert(result <= Remaining());
84 // true because the three terms of the min would be positive
85 assert(bound == 0 || Remaining() == 0 || result > 0);
86 return { result };
87}
int min(int a, int b)
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
#define stackAllocate(T, count)
bool GetFloats(size_t iChannel, size_t nBuffers, float *const buffers[], sampleCount start, size_t len, bool backwards=false, fillFormat fill=FillFormat::fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
bool AcceptsBlockSize(size_t blockSize) const override
Always true.
bool AcceptsBuffers(const Buffers &buffers) const override
sampleCount Remaining() const override
Result includes any amount Acquired and not yet Released.

References AcceptsBlockSize(), AcceptsBuffers(), AudioGraph::Buffers::BlockSize(), WideSampleSequence::GetFloats(), AudioGraph::Buffers::GetWritePosition(), limitSampleBufferSize(), mFetched, min(), mInitialized, mLastProduced, mnChannels, mPos, mSequence, AudioGraph::Buffers::Remaining(), Remaining(), and stackAllocate.

Here is the call graph for this function:

◆ Release()

bool WideSampleSource::Release ( )
overridevirtual

Can test for user cancellation.

Implements AudioGraph::Source.

Definition at line 89 of file WideSampleSource.cpp.

90{
93 mLastProduced = 0;
94 assert(mOutputRemaining >= 0);
95 return !mPollUser || mPollUser(mPos);
96}

References mFetched, mLastProduced, mOutputRemaining, mPollUser, and mPos.

◆ Remaining()

sampleCount WideSampleSource::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 45 of file WideSampleSource.cpp.

46{
47 return std::max<sampleCount>(0, mOutputRemaining);
48}

References mOutputRemaining.

Referenced by Acquire().

Here is the caller graph for this function:

Member Data Documentation

◆ mFetched

size_t WideSampleSource::mFetched {}
private

Definition at line 56 of file WideSampleSource.h.

Referenced by Acquire(), and Release().

◆ mInitialized

bool WideSampleSource::mInitialized { false }
private

Definition at line 57 of file WideSampleSource.h.

Referenced by Acquire().

◆ mLastProduced

size_t WideSampleSource::mLastProduced {}
private

Definition at line 55 of file WideSampleSource.h.

Referenced by Acquire(), and Release().

◆ mnChannels

const size_t WideSampleSource::mnChannels
private

Definition at line 50 of file WideSampleSource.h.

Referenced by Acquire().

◆ mOutputRemaining

sampleCount WideSampleSource::mOutputRemaining {}
private

Definition at line 54 of file WideSampleSource.h.

Referenced by AcceptsBuffers(), Release(), and Remaining().

◆ mPollUser

const Poller WideSampleSource::mPollUser
private

Definition at line 51 of file WideSampleSource.h.

Referenced by Release().

◆ mPos

sampleCount WideSampleSource::mPos {}
private

Definition at line 53 of file WideSampleSource.h.

Referenced by Acquire(), and Release().

◆ mSequence

const WideSampleSequence& WideSampleSource::mSequence
private

Definition at line 49 of file WideSampleSource.h.

Referenced by Acquire().


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