11namespace tracktion {
inline namespace engine
29 if (++maxSize != maxBufferSize)
32 buffer.
malloc ((
size_t) maxSize);
33 maxBufferSize = maxSize;
34 bufferSize =
std::min (bufferSize, maxBufferSize);
43 buffer.
clear ((
size_t) maxBufferSize);
49 lastBufferSize = bufferSize;
50 jassert (newSize < maxBufferSize);
52 if (newSize < bufferSize)
55 bufferSize =
std::min (maxBufferSize, newSize);
67 for (
int i = 0; i < numSamples; ++i)
70 bufferWritePos = bufferWritePos % maxBufferSize;
71 buffer[bufferWritePos] = samples[i];
73 int bufferReadPos = bufferWritePos - bufferSize;
75 if (bufferReadPos < 0)
76 bufferReadPos += maxBufferSize;
78 samples[i] = buffer[bufferReadPos];
85 if (bufferSize == lastBufferSize)
91 const float gainDelta = 1.0f / numSamples;
95 for (
int i = 0; i < numSamples; ++i)
98 bufferWritePos = bufferWritePos % maxBufferSize;
99 buffer[bufferWritePos] = samples[i];
101 int bufferReadPos = bufferWritePos - bufferSize;
102 int bufferReadOld = bufferWritePos - lastBufferSize;
104 if (bufferReadPos < 0) bufferReadPos += maxBufferSize;
105 if (bufferReadOld < 0) bufferReadOld += maxBufferSize;
107 const float newSample = buffer[(
int) bufferReadPos];
108 const float oldSample = buffer[(
int) bufferReadOld];
110 samples[i] = (newSample * newGain) + (oldSample * oldGain);
112 newGain += gainDelta;
113 oldGain -= gainDelta;
116 lastBufferSize = bufferSize;
122 int bufferIndex = 0, maxBufferSize = 0, bufferWritePos = 0;
123 int bufferSize = 0, lastBufferSize = 0;
130const char* LatencyPlugin::xmlTypeName (
"latencyTester");
138 latencyTimeSeconds.setConstrainer ([] (
float in) {
return juce::jlimit (0.0f, 5.0f, in); });
139 latencyTimeSeconds.referTo (state, IDs::time, getUndoManager(), 0.0f);
140 applyLatency.referTo (state, IDs::apply, getUndoManager(),
true);
142 playbackRestartTimer.setCallback ([
this]
144 edit.restartPlayback();
145 playbackRestartTimer.stopTimer();
149LatencyPlugin::~LatencyPlugin()
151 notifyListenersOfDeletion();
156 return createValueTree (IDs::PLUGIN,
157 IDs::type, xmlTypeName);
164 const int maxDelaySamples = (
int)
std::ceil (5.0 * sampleRate);
167 delayCompensator[i]->setMaxSize (maxDelaySamples);
173 delayCompensator[i]->clear();
178 if (! applyLatency.
get())
184 const int delayCompensationSamples =
juce::roundToInt (latencyTimeSeconds.
get() * (
float) sampleRate);
186 if (delayCompensationSamples != 0)
192 for (
int i = 0; i < numChannels; ++i)
194 delayCompensator[i]->setSize (delayCompensationSamples);
200void LatencyPlugin::restorePluginStateFromValueTree (
const juce::ValueTree& v)
202 if (v.hasProperty (IDs::time))
203 latencyTimeSeconds = v[IDs::time];
205 if (v.hasProperty (IDs::apply))
206 applyLatency = v[IDs::apply];
211 if (v == state && i == IDs::time)
212 playbackRestartTimer.startTimer (50);
214 Plugin::valueTreePropertyChanged (v, i);
int getNumChannels() const noexcept
Type *const * getArrayOfWritePointers() noexcept
Type get() const noexcept
void clear(SizeType numElements) noexcept
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Type get() const noexcept
Returns the current value of the property.
DelayRegister()=default
Creates an empty DelayRegister.
void setMaxSize(int maxSize)
Sets the max size of the register.
void processSamplesSmoothed(float *samples, int numSamples)
Processes a number of samples, crossfading the current and last read positions to avoid glitches.
int getSize() const
Gets the buffer size.
void processSamples(float *samples, int numSamples)
Processes a number of samples, replacing the contents with delayed samples.
void clear() noexcept
Clears the buffer.
void setSize(int newSize)
Sets the size of delay in samples.
void applyToBuffer(const PluginRenderContext &) override
Process the next block of data.
void deinitialise() override
Called after play stops to release resources.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
int roundToInt(const FloatType value) noexcept
constexpr int numElementsInArray(Type(&)[N]) noexcept
Passed into Plugins when they are being initialised, to give them useful contextual information that ...
The context passed to plugin render methods to provide it with buffers to fill.
int bufferNumSamples
The number of samples to write into the audio buffer.
juce::AudioBuffer< float > * destBuffer
The target audio buffer which needs to be filled.
int bufferStartSample
The index of the start point in the audio buffer from which data must be written.