#pragma once #include #if !defined(ssize_t) && defined(WIN32) #define ssize_t int64_t #endif namespace tc::audio::codec { namespace type { enum value { undefined, /* supported */ opus, speex, /* unsupported */ flac, celt }; extern bool supported(value); } class Converter { public: Converter(size_t /* channels */, size_t /* sample rate */, size_t /* frame size */); virtual ~Converter(); /* initialize parameters depend on the codec */ virtual bool valid() = 0; virtual void finalize() = 0; virtual void reset_encoder() = 0; virtual void reset_decoder() = 0; /** * @return number of bytes written on success */ virtual ssize_t encode(std::string& /* error */, const void* /* source */, void* /* destination */, size_t /* destination byte length */, bool /* head package */) = 0; /** * @return number of samples on success */ virtual ssize_t decode(std::string& /* error */, const void* /* source */, size_t /* source byte length */, void* /* destination */, bool /* fec decoding */) = 0; virtual ssize_t decode_lost(std::string& /* error */, size_t /* packets */) = 0; virtual size_t expected_encoded_length(size_t /* sample count */) = 0; virtual size_t expected_decoded_length(const void* /* source */, size_t /* source byte length */) { return this->bytes_per_frame(); } inline size_t channels() { return this->_channels; } inline size_t sample_rate() { return this->_sample_rate; } inline size_t frame_size() { return this->_frame_size; } inline size_t bytes_per_frame() { return this->_channels * this->_frame_size * 4; } protected: size_t _frame_size; size_t _channels; size_t _sample_rate; }; struct EncoderBufferInfo { size_t sample_count{0}; bool head_sequence{false}; bool flush_encoder{false}; }; /** * Encoders should only be accessed by one thread at once */ class AudioEncoder { public: explicit AudioEncoder() = default; virtual ~AudioEncoder() = default; [[nodiscard]] virtual bool valid() const = 0; [[nodiscard]] virtual bool initialize(std::string& /* error */) = 0; virtual void reset_sequence() = 0; /** * Get the codecs sample rate. */ [[nodiscard]] virtual size_t sample_rate() const = 0; /** * Get the codecs audio channel count. */ [[nodiscard]] virtual size_t channel_count() const = 0; /** * @returns the expected output length. * If unknown a size near the MTU will be returned. */ [[nodiscard]] virtual size_t expected_encoded_length(const float* /* samples */, size_t /* sample count */) const = 0; /** * Encode a chunk of audio data. * @return `true` on success else `false` */ [[nodiscard]] virtual bool encode(std::string& /* error */, void* /* target buffer */, size_t& /* target length */, const EncoderBufferInfo& /* buffer info */, const float* /* samples */) = 0; }; class AudioDecoder {}; }