//
//  AudioData.hpp
//  SwitchboardSDK
//
//  Created by Nádor Iván on 2022. 07. 25..
//

#pragma once

namespace switchboard::buffer {

/**
 * Audio data class.
 * @brief Provides helper methods to handle raw audio data arrays.
 */
template <typename T>
class AudioData final {
public:
    /**
     * @brief Creates an AudioData instance that allocates and owns maxNumberOfChannels * maxNumberOfFrames * sizeof(T) bytes.
     * @details This constructor is not realtime-safe as it allocates memory.
     * @details The allocated buffer is filled with zeros.
     *
     * @param maxNumberOfChannels The maximum number of channels.
     * @param maxNumberOfFrames The maximum number of frames.
     */
    AudioData(unsigned int maxNumberOfChannels, unsigned int maxNumberOfFrames);

    /**
     * @brief AudioData destructor.
     */
    ~AudioData() noexcept;

    /**
     * @brief Returns a pointer to the internal allocated audio memory location.
     *
     * @returns Pointer to the internal allocated audio memory location.
     */
    T* getBuffer();

    /**
     * @brief Returns a pointer to the stacked audio memory location.
     * @details This returns a pointer to a list of per-channel memory locations that have the same memory location as getBuffer().
     * @details This makes it possible to iterate over the channels of the buffer.
     *
     * @returns Pointer to the stacked audio memory location.
     */
    T** getBuffers();

    unsigned int getMaxNumberOfChannels() const {
        return maxNumberOfChannels;
    }

    unsigned int getMaxNumberOfFrames() const {
        return maxNumberOfFrames;
    }

private:
    unsigned int maxNumberOfChannels;
    unsigned int maxNumberOfFrames;

    T* buffer;
    T** buffers;
};

}
