summaryrefslogtreecommitdiff
path: root/src/codecs/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-02 13:39:00 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-02 13:39:00 +1100
commit222c810b07ffc635fc7908d121e97e4d65ccc5c8 (patch)
tree91c7b5c72a11770ebf3695bf0c234597b2bc419d /src/codecs/include
parent71a4f5166f5491dc0982a18d62c63e28b3a52faa (diff)
downloadtangara-fw-222c810b07ffc635fc7908d121e97e4d65ccc5c8.tar.gz
fix build errors
Diffstat (limited to 'src/codecs/include')
-rw-r--r--src/codecs/include/codec.hpp70
-rw-r--r--src/codecs/include/mad.hpp20
2 files changed, 54 insertions, 36 deletions
diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp
index 8e82bd71..764b63fc 100644
--- a/src/codecs/include/codec.hpp
+++ b/src/codecs/include/codec.hpp
@@ -1,18 +1,17 @@
#pragma once
#include <stdint.h>
+
#include <cstddef>
#include <cstdint>
+#include <memory>
+#include <string>
+#include <utility>
#include "result.hpp"
namespace codecs {
-enum CreateCodecError { UNKNOWN_EXTENSION };
-
-auto CreateCodecForFile(const std::string& extension)
- -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError>;
-
class ICodec {
public:
virtual ~ICodec() {}
@@ -22,39 +21,48 @@ class ICodec {
struct OutputFormat {
uint8_t num_channels;
uint8_t bits_per_sample;
- int sample_rate_hz;
+ uint32_t sample_rate_hz;
};
virtual auto GetOutputFormat() -> OutputFormat = 0;
- enum ProcessingError {};
-
- struct Result {
- bool need_more_input;
- /*
- * For need_more_input, this is how far we got in the input buffer
- * before we were unable to process more data. Any remaining data in the
- * buffer should be moved to the start before the next call.
- */
- std::size_t input_processed;
-
- bool flush_output;
- /*
- * For flush_output, this is how far we got in the output buffer before
- * we ran out of space for samples. The caller should flush this many
- * bytes downstream.
- */
- std::size_t output_written;
- };
+ enum ProcessingError { MALFORMED_DATA };
virtual auto ResetForNewStream() -> void = 0;
- virtual auto SetInput(uint8_t* buffer, std::size_t length) = 0;
- virtual auto Process(uint8_t* output, std::size_t output_length)
- -> cpp::result<size_t, Error> = 0;
-
- virtual auto GetOutputProcessed() -> std::size_t;
- = 0;
+ virtual auto SetInput(uint8_t* buffer, std::size_t length) -> 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.
+ */
+ virtual auto GetInputPosition() -> std::size_t = 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<bool, ProcessingError> = 0;
+
+ /*
+ * 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(uint8_t* output, std::size_t output_length)
+ -> std::pair<std::size_t, bool> = 0;
};
+enum CreateCodecError { UNKNOWN_EXTENSION };
+
+auto CreateCodecForFile(const std::string& extension)
+ -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError>;
+
} // namespace codecs
diff --git a/src/codecs/include/mad.hpp b/src/codecs/include/mad.hpp
index 241ea6c3..aa24f3c9 100644
--- a/src/codecs/include/mad.hpp
+++ b/src/codecs/include/mad.hpp
@@ -1,5 +1,11 @@
#pragma once
+#include <cstddef>
+#include <cstdint>
+#include <string>
+
+#include "mad.h"
+
#include "codec.hpp"
namespace codecs {
@@ -9,10 +15,14 @@ class MadMp3Decoder : public ICodec {
MadMp3Decoder();
~MadMp3Decoder();
- auto ProcessInput(Result* res, uint8_t* input, std::size_t input_len) -> void;
- auto WriteOutputSamples(Result* res,
- uint8_t* output,
- std::size_t output_length) -> void;
+ auto CanHandleFile(const std::string& path) -> bool override;
+ auto GetOutputFormat() -> OutputFormat override;
+ auto ResetForNewStream() -> void override;
+ auto SetInput(uint8_t* buffer, std::size_t length) -> void override;
+ auto GetInputPosition() -> std::size_t override;
+ auto ProcessNextFrame() -> cpp::result<bool, ProcessingError> override;
+ auto WriteOutputSamples(uint8_t* output, std::size_t output_length)
+ -> std::pair<std::size_t, bool> override;
private:
mad_stream stream_;
@@ -22,7 +32,7 @@ class MadMp3Decoder : public ICodec {
mad_header header_;
bool has_decoded_header_;
- int current_sample_ = -1;
+ int current_sample_;
};
} // namespace codecs