Audacity 3.2.0
SampleTrackSource.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file SampleTrackSource.cpp
6
7 Dominic Mazzoni
8 Vaughan Johnson
9 Martyn Shaw
10
11 Paul Licameli split from PerTrackEffect.cpp
12
13*******************************************************************//*******************************************************************/
19
20
21#include "SampleTrackSource.h"
22
23#include "AudioGraphBuffers.h"
24#include "SampleTrack.h"
25#include <cassert>
26
28 const SampleTrack &left, const SampleTrack *pRight,
29 sampleCount start, sampleCount len, Poller pollUser
30) : mLeft{ left }, mpRight{ pRight }, mPollUser{ move(pollUser) }
31 , mPos{ start }, mOutputRemaining{ len }
32{
33}
34
36
38{
39 return mOutputRemaining == 0 || buffers.Channels() > 0;
40}
41
43{
44 return true;
45}
46
48{
49 return std::max<sampleCount>(0, mOutputRemaining);
50}
51
52std::optional<size_t> SampleTrackSource::Acquire(Buffers &data, size_t bound)
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 =
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,
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}
85
87{
90 mLastProduced = 0;
91 assert(mOutputRemaining >= 0);
92 return !mPollUser || mPollUser(mPos);
93}
int min(int a, int b)
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
Accumulates (non-interleaved) data during effect processing.
size_t BlockSize() const
float & GetWritePosition(unsigned iChannel)
Get writable position for one channel.
size_t Remaining() const
unsigned Channels() const
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
SampleTrackSource(const SampleTrack &left, const SampleTrack *pRight, sampleCount start, sampleCount len, Poller pollUser)
const Poller mPollUser
std::optional< size_t > Acquire(Buffers &data, size_t bound) override
Occupy vacant space in Buffers with some data.
const SampleTrack & mLeft
bool Release() override
Can test for user cancellation.
sampleCount mOutputRemaining
std::function< bool(sampleCount blockSize)> Poller
Type of function returning false if user cancels progress.
const SampleTrack *const mpRight
bool AcceptsBuffers(const Buffers &buffers) const override
bool AcceptsBlockSize(size_t blockSize) const override
Always true.
~SampleTrackSource() override
sampleCount Remaining() const override
Result includes any amount Acquired and not yet Released.
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19