From ef13ffbfd3ae78cb327b2313d5f59cde652ce3ce Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 08:08:32 +0000 Subject: [PATCH] USB modems: the modem's speaker on the phone's, as on the other lines (the line at 8000 Hz) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRWkXKKQdZ1QTqN43P --- app/src/main/cpp/engine.cpp | 1 + app/src/main/cpp/usb/usb_modem.cpp | 35 ++++++++++++++++++++++++++++++ app/src/main/cpp/usb/usb_modem.h | 10 +++++++++ external/d-modem-re | 2 +- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/engine.cpp b/app/src/main/cpp/engine.cpp index 3ce4680..b2e54a8 100644 --- a/app/src/main/cpp/engine.cpp +++ b/app/src/main/cpp/engine.cpp @@ -236,6 +236,7 @@ void Engine::run(Config cfg, int dte_fd, StatusFn status, ExitFn done, #ifdef DMODEM_ENGINE_USB mo.rate = cfg.usb_rate; UsbModem um(dte, mo, cfg.usb_fd); + um.on_speaker(spk); if (um.ok()) { if (status) um.on_status(report); diff --git a/app/src/main/cpp/usb/usb_modem.cpp b/app/src/main/cpp/usb/usb_modem.cpp index 9604d37..0f60c70 100644 --- a/app/src/main/cpp/usb/usb_modem.cpp +++ b/app/src/main/cpp/usb/usb_modem.cpp @@ -50,6 +50,41 @@ UsbModem::UsbModem(dmd::Dte &dte, const dcore::Options &opt, int fd) usb_use_fd(-1); } +/* see usb_modem.h: the line's frames (20 ms, so its rate is 50 times + their length) to 8000 Hz by linear interpolation, then on */ +void UsbModem::on_speaker(dmd::SipModem::Speaker s) +{ + spk_audio_ = std::move(s.audio); + line_->on_speaker(std::move(s.volume), + [this](const s16 *in, const s16 *out, s16 n) { + if (!spk_audio_ || n <= 0) + return; + if (n == 160) { + spk_audio_(in, out, n); + return; + } + /* (each 8000 Hz sample step / 160 of the frame) */ + const float step = n / 160.0f; + s16 rin[400], rout[400]; + s16 k = 0; + float pos = spk_pos_; + while (pos < n && k < 400) { + s16 i = static_cast(pos); + float f = pos - i; + float a0 = i == 0 ? spk_last_in_ : in[i - 1]; + float b0 = i == 0 ? spk_last_out_ : out[i - 1]; + rin[k] = static_cast(a0 + f * (in[i] - a0)); + rout[k] = static_cast(b0 + f * (out[i] - b0)); + k++; + pos += step; + } + spk_pos_ = pos - n; + spk_last_in_ = in[n - 1]; + spk_last_out_ = out[n - 1]; + spk_audio_(rin, rout, k); + }); +} + /* the core first: it may still hang up on the line */ UsbModem::~UsbModem() { diff --git a/app/src/main/cpp/usb/usb_modem.h b/app/src/main/cpp/usb/usb_modem.h index fd3c856..722f289 100644 --- a/app/src/main/cpp/usb/usb_modem.h +++ b/app/src/main/cpp/usb/usb_modem.h @@ -45,6 +45,11 @@ public: status_fn_ = std::move(fn); } + /* before run(): the modem's speaker (ATM, ATL) on the host's, as + SipModem's (its audio at 8000 Hz, mixed as the app mixes it, + whatever the line's rate) */ + void on_speaker(dmd::SipModem::Speaker s); + /* until stop() (0), the DTE gone (0) or an error, the device unplugged among them (-1) */ int run(); @@ -61,6 +66,11 @@ private: int ev_[2] = { -1, -1 }; std::atomic stopped_{ false }; std::function status_fn_; + /* on_speaker()'s audio, and its resampler's state: the position + between the last frame's last pair of samples and the next */ + std::function spk_audio_; + float spk_pos_ = 0; + s16 spk_last_in_ = 0, spk_last_out_ = 0; Status status_; }; diff --git a/external/d-modem-re b/external/d-modem-re index 4e09e9c..2b03019 160000 --- a/external/d-modem-re +++ b/external/d-modem-re @@ -1 +1 @@ -Subproject commit 4e09e9ce220347603f51a0ba2474247ae6a2f8bd +Subproject commit 2b030197ce462e68388828047d2d11fbc4dcf47f