#pragma once

#if CHOWDSP_USE_LIBSAMPLERATE || DOXYGEN

namespace chowdsp::ResamplingTypes
{
/** Resampling processor using libsamplerate (Secret Rabbit Code) internally.
 *  In order to enable `SRCResampler`, you must define `CHOWDSP_USE_LIBSAMPLERATE=1`
 *  and link against libsamplerate.
 * 
 *  Reference: https://github.com/libsndfile/libsamplerate
 */
template <size_t QUALITY = SRC_SINC_FASTEST>
class SRCResampler : public BaseResampler
{
public:
    /** Default constructor */
    SRCResampler() = default;

    /** Prepares the resampler for a given input sample rate */
    void prepare (double sampleRate, double startRatio = 1.0) override;

    /** Resets the state of the resampler */
    void reset() override;

    /** Sets the ratio of the output sample rate over input sample rate */
    void setResampleRatio (float newRatio) override { ratio = static_cast<double> (newRatio); }

    /** Returns the ratio of the output sample rate over input sample rate */
    float getResampleRatio() const noexcept override { return (float) ratio; }

    /** Processes a buffer of samples
     * 
     *  @return the number of output samples generated by the resampler
     */
    size_t process (const float* input, float* output, size_t numSamples) noexcept override;

private:
    double fs = 48000.0;
    double ratio = 1.0;

    std::unique_ptr<SRC_STATE, decltype (&src_delete)> src_state { nullptr, &src_delete };

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SRCResampler)
};

} // namespace chowdsp::ResamplingTypes

#endif // CHOWDSP_USE_LIBSAMPLERATE || DOXYGEN
