summaryrefslogtreecommitdiff
path: root/src/codecs/include
diff options
context:
space:
mode:
authorailurux <ailuruxx@gmail.com>2024-01-11 05:54:30 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2024-01-11 05:54:30 +0000
commit0e04eb918ec976017276306181282769d8896c83 (patch)
tree562da509df2ab03d3906cf3c0f56063f6acc9865 /src/codecs/include
parent55bde70b9651b411ac0135bd4704f5b6972ea799 (diff)
downloadtangara-fw-0e04eb918ec976017276306181282769d8896c83.tar.gz
wav-codec (#13)
here is a wav decoder, enjoy! Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/13 Reviewed-by: cooljqln <cooljqln@noreply.codeberg.org> Co-authored-by: ailurux <ailuruxx@gmail.com> Co-committed-by: ailurux <ailuruxx@gmail.com>
Diffstat (limited to 'src/codecs/include')
-rw-r--r--src/codecs/include/codec.hpp4
-rw-r--r--src/codecs/include/sample.hpp6
-rw-r--r--src/codecs/include/types.hpp2
-rw-r--r--src/codecs/include/wav.hpp57
4 files changed, 63 insertions, 6 deletions
diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp
index 87f6637c..36dda8ff 100644
--- a/src/codecs/include/codec.hpp
+++ b/src/codecs/include/codec.hpp
@@ -76,6 +76,8 @@ class ICodec {
kOutOfInput,
// Indicates that the data within the input buffer is fatally malformed.
kMalformedData,
+ // Indicated that the format is unsupported
+ kUnsupportedFormat,
kInternalError,
};
@@ -88,6 +90,8 @@ class ICodec {
return "malformed data";
case Error::kInternalError:
return "internal error";
+ case Error::kUnsupportedFormat:
+ return "unsupported format";
}
return "uhh";
}
diff --git a/src/codecs/include/sample.hpp b/src/codecs/include/sample.hpp
index 7e550680..7b3f96a3 100644
--- a/src/codecs/include/sample.hpp
+++ b/src/codecs/include/sample.hpp
@@ -24,10 +24,6 @@ namespace sample {
// 3. Monty from Xiph.org reckons it's all you need.
typedef int16_t Sample;
-constexpr auto Clip(int64_t v) -> Sample {
- return std::clamp<int64_t>(v, INT16_MIN, INT16_MAX);
-}
-
auto shiftWithDither(int64_t src, uint_fast8_t bits) -> Sample;
constexpr auto FromSigned(int32_t src, uint_fast8_t bits) -> Sample {
@@ -42,7 +38,7 @@ constexpr auto FromSigned(int32_t src, uint_fast8_t bits) -> Sample {
constexpr auto FromUnsigned(uint32_t src, uint_fast8_t bits) -> Sample {
// Left-align, then substract the max value / 2 to make the sample centred
// around zero.
- return (src << (sizeof(uint16_t) * 8 - bits)) - (~0UL >> 1);
+ return (src << (sizeof(uint16_t) * 8 - bits)) - (INT16_MAX+1);
}
constexpr auto FromFloat(float src) -> Sample {
diff --git a/src/codecs/include/types.hpp b/src/codecs/include/types.hpp
index c9eefe45..c6dcb486 100644
--- a/src/codecs/include/types.hpp
+++ b/src/codecs/include/types.hpp
@@ -12,10 +12,10 @@ namespace codecs {
enum class StreamType {
kMp3,
- kPcm,
kVorbis,
kFlac,
kOpus,
+ kWav,
};
auto StreamTypeToString(StreamType t) -> std::string;
diff --git a/src/codecs/include/wav.hpp b/src/codecs/include/wav.hpp
new file mode 100644
index 00000000..896976dd
--- /dev/null
+++ b/src/codecs/include/wav.hpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2023 Daniel <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+
+#include "sample.hpp"
+#include "source_buffer.hpp"
+
+#include "codec.hpp"
+
+namespace codecs {
+
+static const uint16_t kWaveFormatPCM = 0x0001;
+static const uint16_t kWaveFormatIEEEFloat = 0x0003;
+static const uint16_t kWaveFormatAlaw = 0x0006;
+static const uint16_t kWaveFormatMulaw = 0x0007;
+static const uint16_t kWaveFormatExtensible = 0xFFFE;
+
+class WavDecoder : public ICodec {
+ public:
+ WavDecoder();
+ ~WavDecoder();
+
+ auto OpenStream(std::shared_ptr<IStream> input)
+ -> cpp::result<OutputFormat, Error> override;
+
+ auto DecodeTo(cpp::span<sample::Sample> destination)
+ -> cpp::result<OutputInfo, Error> override;
+
+ auto SeekTo(std::size_t target_sample) -> cpp::result<void, Error> override;
+
+ WavDecoder(const WavDecoder&) = delete;
+ WavDecoder& operator=(const WavDecoder&) = delete;
+
+ private:
+ std::shared_ptr<IStream> input_;
+ SourceBuffer buffer_;
+ uint16_t wave_format_;
+ uint16_t subformat_;
+ OutputFormat output_format_;
+ uint16_t bytes_per_sample_;
+ uint16_t num_channels_;
+
+ auto GetFormat() const -> uint16_t;
+};
+
+} // namespace codecs