//
//  BufferSizeRegulator.hpp
//  SwitchboardSDK
//
//  Created by Balázs Kiss on 2022. 11. 02..
//

#pragma once

#include "RingBuffer.hpp"

#include <memory>

namespace switchboard::audio {

using BufferSizeRegulatorCallbackFunction = void (*)(float* buffer, void* userPointer);

/**
 * @brief Converts a variable sized buffer stream into a fixed sized buffer stream.
 */
class BufferSizeRegulator {
public:
    BufferSizeRegulator(
        unsigned int targetNumberOfFrames,
        BufferSizeRegulatorCallbackFunction callback,
        void* userPointer
    );
    ~BufferSizeRegulator();

    bool process(float* buffer, unsigned int numberOfSamples);

private:
    unsigned int targetNumberOfFrames;
    BufferSizeRegulatorCallbackFunction callback;
    void* userPointer;

    std::unique_ptr<RingBuffer<float>> inputBuffer;
    std::unique_ptr<RingBuffer<float>> outputBuffer;
    float* tempBuffer;
};

}
