From f655f2c1d53025e4bb953502f4403868aab23d74 Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 13 Oct 2026 22:46:02 +0200 Subject: [PATCH] Keep playback audio compatible with the Android voice build --- src/Makefile.am | 3 ++- src/stream/stream_playback_audio.cpp | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 5b857817..65edb3f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,8 @@ bin_PROGRAMS = utun noinst_LIBRARIES = libutun.a -EXTRA_DIST = lwip_tcp/COPYING dnsmasq/COPYING dnsmasq/COPYING-v3 routing_layer/address_profile.h routing_layer/route_ping_doc.md +EXTRA_DIST = lwip_tcp/COPYING dnsmasq/COPYING dnsmasq/COPYING-v3 routing_layer/address_profile.h routing_layer/route_ping_doc.md \ + stream/stream_playback_audio.h stream/stream_playback_audio.cpp stream_core_files = \ stream/stream.h \ diff --git a/src/stream/stream_playback_audio.cpp b/src/stream/stream_playback_audio.cpp index d0ac9012..401f449d 100644 --- a/src/stream/stream_playback_audio.cpp +++ b/src/stream/stream_playback_audio.cpp @@ -13,10 +13,14 @@ extern "C" { namespace { constexpr unsigned rate = 48000, framesPerPacket = 960, maxFrames = 8192, mapCapacity = 1024; -struct Span { double first = 0, last = 0, ptsFirst = 0, ptsLast = 0; }; +struct Span { + double first, last, ptsFirst, ptsLast; + Span(double begin = 0, double end = 0, double ptsBegin = 0, double ptsEnd = 0) + : first(begin), last(end), ptsFirst(ptsBegin), ptsLast(ptsEnd) {} +}; double mapped(const Span& span, double position) { if (span.last <= span.first) return span.ptsLast; - double fraction = std::clamp((position - span.first) / (span.last - span.first), 0.0, 1.0); + double fraction = std::max(0.0, std::min(1.0, (position - span.first) / (span.last - span.first))); return span.ptsFirst + fraction * (span.ptsLast - span.ptsFirst); } } @@ -156,7 +160,8 @@ int stream_playback_audio_pull(struct stream_playback_audio* a, int16_t* pcm, un frames = std::min(frames, std::min(available, a->stretch.numSamples())); if (!frames) return 0; unsigned got = a->stretch.receiveSamples(a->samples, frames); - for (unsigned i = 0; i < got * 2; i++) pcm[i] = (int16_t)std::lrint(std::clamp(a->samples[i] * 32767.0f, -32768.0f, 32767.0f)); + for (unsigned i = 0; i < got * 2; i++) + pcm[i] = (int16_t)std::lrint(std::max(-32768.0f, std::min(32767.0f, a->samples[i] * 32767.0f))); double position = a->producedOutput, end = position + got; for (unsigned i = 0; i < a->inputCount && position < end; i++) { const auto& span = a->input[(a->inputHead + i) % mapCapacity];