// Copyright 2023 Kushview, LLC <info@kushview.net>
// SPDX-License-Identifier: GPL-3.0-or-later

#include <element/oversampler.hpp>

namespace element {

template <typename T>
Oversampler<T>::~Oversampler()
{
    reset();
    processors.clear (true);
}

template <typename T>
float Oversampler<T>::getLatencySamples (int index) const
{
    if (auto* const proc = getProcessor (index))
        return proc->getLatencyInSamples();
    return 0.f;
}

template <typename T>
int Oversampler<T>::getFactor (int index) const
{
    if (auto* const proc = getProcessor (index))
        return static_cast<int> (proc->getOversamplingFactor());
    return 1;
}

template <typename T>
void Oversampler<T>::prepare (int numChannels, int blockSize)
{
    numChannels = juce::jmax (1, numChannels);
    const bool procSpecChanged = channels != numChannels || buffer != blockSize;
    channels = numChannels;
    buffer = blockSize;

    if (processors.size() <= 0 || procSpecChanged)
    {
        processors.clear();
        for (int f = 0; f < maxProc; ++f)
            processors.add (new ProcessorType (channels, f + 1, ProcessorType::FilterType::filterHalfBandPolyphaseIIR));
    }

    for (auto* proc : processors)
        proc->initProcessing (buffer);
}

template <typename T>
void Oversampler<T>::reset()
{
    for (auto* const proc : processors)
        proc->reset();
}

template class Oversampler<float>;
template class Oversampler<double>;

} // namespace element
