summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcooljqln <cooljqln@noreply.codeberg.org>2025-04-25 02:35:14 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2025-04-25 02:35:14 +0000
commitc49787657cc81b465a22fecedaec5e20fd963168 (patch)
treea0b2466eca0885c166f1643360f2e3cd850f7573 /src
parent79c9f0b6ccfec97f23563010f134d842c4240582 (diff)
parent2b4ed254c60c576fc6889cf6e3b2cc2068e29fbf (diff)
downloadtangara-fw-c49787657cc81b465a22fecedaec5e20fd963168.tar.gz
Merge pull request 'WavPack decoding and seeking speed improvements' (#342) from ayumi/tangara-fw:wavpack into main
Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/342
Diffstat (limited to 'src')
-rw-r--r--src/codecs/sample.cpp14
-rw-r--r--src/codecs/wavpack.cpp18
2 files changed, 23 insertions, 9 deletions
diff --git a/src/codecs/sample.cpp b/src/codecs/sample.cpp
index 63d14203..faf8b0f9 100644
--- a/src/codecs/sample.cpp
+++ b/src/codecs/sample.cpp
@@ -13,13 +13,17 @@
namespace sample {
-static uint64_t sSeed1{0};
-static uint64_t sSeed2{0};
-
auto shiftWithDither(int64_t src, uint_fast8_t bits) -> Sample {
// FIXME: Use a better dither.
- int16_t noise = static_cast<int16_t>(komirand(&sSeed1, &sSeed2) & 1);
- return (src >> bits) ^ noise;
+ static uint64_t sSeed1{0};
+ static uint64_t sSeed2{0};
+ static uint64_t noise;
+ static uint_fast8_t pos = 0;
+ if (pos++ % 64 == 0)
+ noise = komirand(&sSeed1, &sSeed2);
+ else
+ noise >>= 1;
+ return (src >> bits) ^ (noise & 1);
}
} // namespace sample
diff --git a/src/codecs/wavpack.cpp b/src/codecs/wavpack.cpp
index 21865785..709302e6 100644
--- a/src/codecs/wavpack.cpp
+++ b/src/codecs/wavpack.cpp
@@ -73,7 +73,7 @@ auto WavPackDecoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
);
const auto rate = WavpackGetSampleRate(&wavpack_);
if (offset && total && input_.get()->CanSeek()) {
- const uint32_t want = offset * rate - 1;
+ const uint32_t want = offset * rate;
if (total < want) {
ESP_LOGE(kTag, "seeking: offset points beyond the end of the file");
return cpp::fail(Error::kInternalError);
@@ -96,7 +96,11 @@ auto WavPackDecoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
}
const uint32_t blockIndex = loadLe32(header + 16);
const uint32_t blockSamples = loadLe32(header + 20);
- if (want >= blockIndex && want <= blockIndex + blockSamples) {
+ if (want >= blockIndex && want == blockIndex + blockSamples) {
+ input_->SeekTo(size - 24, IStream::SeekFrom::kCurrentPosition);
+ target = 0;
+ break;
+ } else if (want >= blockIndex && want < blockIndex + blockSamples) {
input_->SeekTo(-32, IStream::SeekFrom::kCurrentPosition);
target = want - blockIndex;
break;
@@ -150,8 +154,14 @@ auto WavPackDecoder::DecodeTo(std::span<sample::Sample> output)
ESP_LOGE(kTag, "CRC error");
return cpp::fail(Error::kMalformedData);
}
- for (size_t i = 0; i < samples; i++)
- output[i] = sample::FromSigned(buf_[i], bitdepth_);
+ if (bitdepth_ == 16)
+ for (size_t i = 0; i < samples; i++)
+ output[i] = buf_[i];
+ else if (bitdepth_ > 16)
+ for (size_t i = 0; i < samples; i++)
+ output[i] = sample::shiftWithDither(buf_[i], bitdepth_ - 16);
+ else for (size_t i = 0; i < samples; i++)
+ output[i] = buf_[i] << (16 - bitdepth_);
return OutputInfo{
.samples_written = samples,
.is_stream_finished = samples == 0,