#include "../../internal-include/SPU/spu_internal.hpp"
#include "../../internal-include/SPU/spu_mmu.hpp"
#include <PSX/System/IOPorts/spu_io.hpp>
#include <PSX/SPU/spu.hpp>
#include <stddef.hpp>

#include <stdio.hpp>

namespace JabyEngine {
    namespace SPU {
        SRAMAdr Voice :: allocate(size_t size) {
            Voice::stop();
            const auto voice_id = Voice::get_id();
            const auto adr      = SRAMAdr{static_cast<SRAMAdr::UnderlyingType>(reinterpret_cast<uintptr_t>(SPU_MMU::allocate(voice_id, size)))};

            SPU_IO::Voice[voice_id].adr.write(adr);
            return adr;
        }

        SRAMAdr Voice :: allocate(SPU_IO_Values::SampleRate frequency, size_t size) {
            const auto result = Voice::allocate(size);
            Voice::set_sample_rate(frequency);
            return result;
        }

        void Voice :: deallocate() {
            Voice::stop();
            SPU_MMU::deallocate(Voice::get_id());
        }
    }
}