From a2c1dfbabddc2b4abaf8bf27c9ed9d1b99594859 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 15 Jun 2023 10:33:46 +1000 Subject: Add vorbis and flac decoders, flesh out codec interface vorbis doesn't quite work yet, not sure why. will pick it up again later. --- src/codecs/include/codec.hpp | 60 ++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'src/codecs/include/codec.hpp') diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp index 31c67e13..4b5ab47f 100644 --- a/src/codecs/include/codec.hpp +++ b/src/codecs/include/codec.hpp @@ -21,48 +21,58 @@ namespace codecs { +/* + * Common interface to be implemented by all audio decoders. + */ class ICodec { public: virtual ~ICodec() {} + /* Errors that may be returned by codecs. */ + enum class Error { + // Indicates that more data is required before this codec can finish its + // operation. E.g. the input buffer ends with a truncated frame. + kOutOfInput, + // Indicates that the data within the input buffer is fatally malformed. + kMalformedData, + + kInternalError, + }; + + /* + * Alias for more readable return types. All codec methods, success or + * failure, should also return the number of bytes they consumed. + */ + template + using Result = std::pair>; + struct OutputFormat { uint8_t num_channels; uint8_t bits_per_sample; uint32_t sample_rate_hz; }; - virtual auto GetOutputFormat() -> std::optional = 0; - - enum ProcessingError { MALFORMED_DATA }; - - virtual auto SetInput(cpp::span input) -> void = 0; - /* - * Returns the codec's next read position within the input buffer. If the - * codec is out of usable data, but there is still some data left in the - * stream, that data should be prepended to the next input buffer. + * Decodes metadata or headers from the given input stream, and returns the + * format for the samples that will be decoded from it. */ - virtual auto GetInputPosition() -> std::size_t = 0; + virtual auto BeginStream(cpp::span input) + -> Result = 0; - /* - * Read one frame (or equivalent discrete chunk) from the input, and - * synthesize output samples for it. - * - * Returns true if we are out of usable data from the input stream, or false - * otherwise. - */ - virtual auto ProcessNextFrame() -> cpp::result = 0; + struct OutputInfo { + std::size_t bytes_written; + bool is_finished_writing; + }; /* * Writes PCM samples to the given output buffer. - * - * Returns the number of bytes that were written, and true if all of the - * samples synthesized from the last call to `ProcessNextFrame` have been - * written. If this returns false, then this method should be called again - * after flushing the output buffer. */ - virtual auto WriteOutputSamples(cpp::span output) - -> std::pair = 0; + virtual auto ContinueStream(cpp::span input, + cpp::span output) + -> Result = 0; + + virtual auto SeekStream(cpp::span input, + std::size_t target_sample) -> Result = 0; }; auto CreateCodecForType(StreamType type) -> std::optional; -- cgit v1.2.3