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

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

#include <SampleTrackSource.h>

Inheritance diagram for SampleTrackSource:
[legend]
Collaboration diagram for SampleTrackSource:
[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

 SampleTrackSource (const SampleTrack &left, const SampleTrack *pRight, sampleCount start, sampleCount len, Poller pollUser)
 
 ~SampleTrackSource () 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 SampleTrackmLeft
 
const SampleTrack *const mpRight
 
const Poller mPollUser
 
sampleCount mPos {}
 
sampleCount mOutputRemaining {}
 
size_t mLastProduced {}
 
size_t mFetched {}
 
bool mInitialized { false }
 

Detailed Description

Adapts SampleTrack to the interface AudioGraph::Source.

Definition at line 25 of file SampleTrackSource.h.

Member Typedef Documentation

◆ Poller

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

Type of function returning false if user cancels progress.

Definition at line 28 of file SampleTrackSource.h.

Constructor & Destructor Documentation

◆ SampleTrackSource()

SampleTrackSource::SampleTrackSource ( const SampleTrack left,
const SampleTrack pRight,
sampleCount  start,
sampleCount  len,
Poller  pollUser 
)
Postcondition
Remaining() == len

Definition at line 27 of file SampleTrackSource.cpp.

30 : mLeft{ left }, mpRight{ pRight }, mPollUser{ move(pollUser) }
31 , mPos{ start }, mOutputRemaining{ len }
32{
33}
const Poller mPollUser
const SampleTrack & mLeft
sampleCount mOutputRemaining
const SampleTrack *const mpRight

◆ ~SampleTrackSource()

SampleTrackSource::~SampleTrackSource ( )
overridedefault

Member Function Documentation

◆ AcceptsBlockSize()

bool SampleTrackSource::AcceptsBlockSize ( size_t  blockSize) const
overridevirtual

Always true.

Implements AudioGraph::Source.

Definition at line 42 of file SampleTrackSource.cpp.

43{
44 return true;
45}

Referenced by Acquire().

Here is the caller graph for this function:

◆ AcceptsBuffers()

bool SampleTrackSource::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 37 of file SampleTrackSource.cpp.

38{
39 return mOutputRemaining == 0 || buffers.Channels() > 0;
40}

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 > SampleTrackSource::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 SampleTrackSource.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 mLeft.GetFloats(&data.GetWritePosition(0) + mFetched, mPos, fetch);
68 if (mpRight && data.Channels() > 1)
69 mpRight->GetFloats(&data.GetWritePosition(1) + mFetched, mPos, fetch);
70 mPos += fetch;
71 mFetched += fetch;
72 mInitialized = true;
73 }
74 assert(data.Remaining() > 0);
75 auto result = mLastProduced = std::min(bound,
76 limitSampleBufferSize(data.Remaining(), Remaining()));
77 // assert post
78 assert(result <= bound);
79 assert(result <= data.Remaining());
80 assert(result <= Remaining());
81 // true because the three terms of the min would be positive
82 assert(bound == 0 || Remaining() == 0 || result > 0);
83 return { result };
84}
int min(int a, int b)
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
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: SampleTrack.h:83
bool AcceptsBuffers(const Buffers &buffers) const override
bool AcceptsBlockSize(size_t blockSize) const override
Always true.
sampleCount Remaining() const override
Result includes any amount Acquired and not yet Released.

References AcceptsBlockSize(), AcceptsBuffers(), AudioGraph::Buffers::BlockSize(), AudioGraph::Buffers::Channels(), SampleTrack::GetFloats(), AudioGraph::Buffers::GetWritePosition(), limitSampleBufferSize(), mFetched, min(), mInitialized, mLastProduced, mLeft, mPos, mpRight, AudioGraph::Buffers::Remaining(), and Remaining().

Here is the call graph for this function:

◆ Release()

bool SampleTrackSource::Release ( )
overridevirtual

Can test for user cancellation.

Implements AudioGraph::Source.

Definition at line 86 of file SampleTrackSource.cpp.

87{
90 mLastProduced = 0;
91 assert(mOutputRemaining >= 0);
92 return !mPollUser || mPollUser(mPos);
93}

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

◆ Remaining()

sampleCount SampleTrackSource::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 47 of file SampleTrackSource.cpp.

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

References mOutputRemaining.

Referenced by Acquire().

Here is the caller graph for this function:

Member Data Documentation

◆ mFetched

size_t SampleTrackSource::mFetched {}
private

Definition at line 56 of file SampleTrackSource.h.

Referenced by Acquire(), and Release().

◆ mInitialized

bool SampleTrackSource::mInitialized { false }
private

Definition at line 57 of file SampleTrackSource.h.

Referenced by Acquire().

◆ mLastProduced

size_t SampleTrackSource::mLastProduced {}
private

Definition at line 55 of file SampleTrackSource.h.

Referenced by Acquire(), and Release().

◆ mLeft

const SampleTrack& SampleTrackSource::mLeft
private

Definition at line 49 of file SampleTrackSource.h.

Referenced by Acquire().

◆ mOutputRemaining

sampleCount SampleTrackSource::mOutputRemaining {}
private

Definition at line 54 of file SampleTrackSource.h.

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

◆ mPollUser

const Poller SampleTrackSource::mPollUser
private

Definition at line 51 of file SampleTrackSource.h.

Referenced by Release().

◆ mPos

sampleCount SampleTrackSource::mPos {}
private

Definition at line 53 of file SampleTrackSource.h.

Referenced by Acquire(), and Release().

◆ mpRight

const SampleTrack* const SampleTrackSource::mpRight
private

Definition at line 50 of file SampleTrackSource.h.

Referenced by Acquire().


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