From 5d437513d0eec0ceddd50f1a60c5abdba5da97b9 Mon Sep 17 00:00:00 2001 From: ayumi Date: Tue, 15 Apr 2025 03:15:16 +0200 Subject: Make WavPack seeking faster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out that “seeking to a first sample in a not–first block” is actually very common, because Tangara only seeks to exact seconds and the reference encoder tends to size blocks in a way that makes the first sample in a block likely be the sample that the firmware wants to seek to. --- src/codecs/wavpack.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/wavpack.cpp b/src/codecs/wavpack.cpp index 21865785..7990e4d6 100644 --- a/src/codecs/wavpack.cpp +++ b/src/codecs/wavpack.cpp @@ -73,7 +73,7 @@ auto WavPackDecoder::OpenStream(std::shared_ptr 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 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; -- cgit v1.2.3 From 48556dd603cac0107143f3cdc815c765baa640a9 Mon Sep 17 00:00:00 2001 From: ayumi Date: Wed, 23 Apr 2025 22:21:25 +0200 Subject: Avoid branching up to two times per sample in the WavPack decoder. In my limited tests this improves decoding speed by around 3%. --- src/codecs/wavpack.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/wavpack.cpp b/src/codecs/wavpack.cpp index 7990e4d6..709302e6 100644 --- a/src/codecs/wavpack.cpp +++ b/src/codecs/wavpack.cpp @@ -154,8 +154,14 @@ auto WavPackDecoder::DecodeTo(std::span 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, -- cgit v1.2.3 From f656c9f5cbfa8515dab287077b10f6769c6e66bc Mon Sep 17 00:00:00 2001 From: ayumi Date: Wed, 23 Apr 2025 23:11:45 +0200 Subject: Utilise more than one bit of the entropy returned by komihash before requesting more --- src/codecs/sample.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/codecs') 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(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 -- cgit v1.2.3