#include "audio.hpp"

#include <chrono>
#include <stdexcept>

#include <fmt123.h>
#include <portaudio.h>


namespace drp::util {


std::uint32_t encoding_mpg123_to_PulseAudio(const int encoding_mpg123) {
    switch (encoding_mpg123) {
        case MPG123_ENC_UNSIGNED_8:
            return paUInt8;
        case MPG123_ENC_SIGNED_8:
            return paInt8;
        case MPG123_ENC_SIGNED_16:
            return paInt16;
        case MPG123_ENC_SIGNED_24:
            return paInt24;
        case MPG123_ENC_SIGNED_32:
            return paInt32;
        case MPG123_ENC_FLOAT:
        case MPG123_ENC_FLOAT_32:
            return paFloat32;
        default:
            throw std::runtime_error("Invalid encoding value.");
    }
}


std::chrono::milliseconds get_audio_chunk_duration(
    const long sampleRate,
    const int channels,
    const int encodingSize,
    const std::size_t length
) {
    return std::chrono::milliseconds(static_cast<std::uint64_t>(
        (1 / static_cast<double>(sampleRate * channels * encodingSize)) *
        1000 * static_cast<double>(length)
    ));
}


}