Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++

Fix Xbox shutdown and VRR capture timing; add resume and playback speed

Veyra Goal Agent74c8394937cc
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6ec2d9c8..4d6d9309 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -524,7 +524,8 @@ if(VEYRA_FFMPEG_ROOT AND EXISTS "${VEYRA_FFMPEG_ROOT}/include/libavformat/avform
target_compile_definitions(veyra_sinks PRIVATE VEYRA_SHADER_DIR="${CMAKE_BINARY_DIR}/shaders")
add_dependencies(veyra_sinks veyra_shader_rgb_nv12)
target_link_libraries(veyra_sinks PUBLIC veyra_base veyra_gfx ole32 windowscodecs mmdevapi avrt mfplat mfuuid mf)
- target_link_libraries(veyra_sinks PRIVATE ${FFMPEG_LIBRARIES})
+ add_subdirectory(third_party/soundtouch EXCLUDE_FROM_ALL)
+ target_link_libraries(veyra_sinks PRIVATE ${FFMPEG_LIBRARIES} veyra_soundtouch)
if(FFMPEG_INCLUDE_DIRS)
target_include_directories(veyra_sinks PRIVATE ${FFMPEG_INCLUDE_DIRS})
endif()
@@ -799,6 +800,9 @@ if(VEYRA_FFMPEG_ROOT AND EXISTS "${VEYRA_FFMPEG_ROOT}/include/libavformat/avform
target_compile_features(veyra_player_tracks_tests PRIVATE cxx_std_20)
target_link_libraries(veyra_player_tracks_tests PRIVATE veyra_engine)
add_executable(veyra_audio_track_output_tests tests/integration/AudioTrackOutputTests.cpp)
+ add_executable(veyra_audio_playback_rate_tests tests/integration/AudioPlaybackRateTests.cpp)
+ target_compile_features(veyra_audio_playback_rate_tests PRIVATE cxx_std_20)
+ target_link_libraries(veyra_audio_playback_rate_tests PRIVATE veyra_sinks)
target_compile_features(veyra_audio_track_output_tests PRIVATE cxx_std_20)
target_link_libraries(veyra_audio_track_output_tests PRIVATE veyra_engine)
target_compile_features(veyra_fg_settings_tests PRIVATE cxx_std_20)
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
index 59d9f21d..4c54ffdf 100644
--- a/THIRD_PARTY_NOTICES.md
+++ b/THIRD_PARTY_NOTICES.md
@@ -626,3 +626,25 @@ the published upstream releases plus the patch above. License texts: `licenses/x
Magewell's copyright notice and disclaimer retained; the full text is in
`licenses/magewell/MWCapture-SDK-NOTICE.txt`. The SDK as a whole (examples, tools) is under
Magewell's EULA and is not redistributed.
+
+
+## SoundTouch playback tempo (2026-10-03)
+
+Movie preview uses SoundTouch 2.4.1, LGPL-2.1, upstream
+https://codeberg.org/soundtouch/soundtouch commit
+`0047e0b1ecfceb041348579119bf79b73a322a3a` (tag 2.4.1).
+Unmodified headers and SoundTouch processing sources are under
+`third_party/soundtouch/`; Veyra supplies only its build definition and origin
+note. Copyright and COPYING.TXT are preserved. It is statically linked from
+these sources; the Veyra corresponding-source archive includes them and the
+build integration. Portable releases include the LGPL text and origin note.
+No separately downloaded SoundTouch binary is used.
+
+Xbox video feedback extends the existing Greenlight port at the same pinned
+commit `58e832a1b1e4bf6b5119a2c84df06ae5e5534993` (MIT), specifically
+`packages/player/src/client/lib/channel/input/packet.ts::_writeMetadataData`
+and `render/video.ts::processVideoMetadata`. `Protocol.h` encodes the original
+seven u32 fields and RTP key; `XboxSessionSource.cpp` records actual arrival,
+decode submission/completion and presentation-return times.
+`WebRtcSession.cpp` sends one bounded report per presented source frame, without
+an unbounded metadata queue. These CPU timestamps do not claim physical scanout.
diff --git a/apps/veyra-qml/main.cpp b/apps/veyra-qml/main.cpp
index 03dc8479..07b6ddb7 100644
--- a/apps/veyra-qml/main.cpp
+++ b/apps/veyra-qml/main.cpp
@@ -656,6 +656,8 @@ static int runApplication(int argc, char** argv, QString& restartProgram, QStrin
engine.rootContext()->setContextProperty(QStringLiteral("vyTest"), testOptions);
if (!openPath.isEmpty()) {
QTimer::singleShot(600, &bridge, [&bridge, openPath] { bridge.openPath(openPath); });
+ } else {
+ QTimer::singleShot(600, &bridge, [&bridge] { bridge.autoResumeLastSource(); });
}
engine.loadFromModule("Veyra", "Main");
diff --git a/docs/CURRENT_STATUS.md b/docs/CURRENT_STATUS.md
index 439bb624..c0686bce 100644
--- a/docs/CURRENT_STATUS.md
+++ b/docs/CURRENT_STATUS.md
@@ -1,5 +1,10 @@
# 当前项目状态 / Current Status
+## 2026-10-03 独立候选:Xbox/采集 VRR、启动恢复与倍速
+
+当前 `codex/player-startup-speed-20261003` 已实现:默认关闭的全屏显存保护开关,启动自动继续视频进度/采集配置,双页面 1/1.5/2/3× 保调倍速;修复 Xbox 关闭时发送异常、回调寿命、空字段并补帧反馈;修复 VRR 可变采集间隔被固定帧率条件反复重置以及补帧相位使用标称间隔。软件回归及实际播放/普通采集测试通过,详情见 `PLAYER_STARTUP_SPEED_PLAN_2026-10-03.md` 和 WORKLOG。Xbox 反馈者连接稳定性、VRR 主机实卡结果、5060 Ti 显存根因仍未确认。D3D12 UI 的 0x887A002B 在新旧候选都可复现,软件 UI 对照通过;不与 Claude 的 RTSS 分支重叠改动。未合并 main、未推送、未发布 2.0.1;下方历史保护“自动退出”现在仅显式启用时执行。
+
+
## 2026-10-02 2.0.1 接管候选(尚未发布)
Claude 的 `8f42fd5` 已在 nrvideo/main,2.0.1 tag/Release 未创建。当前修复在 `codex/field-2.0.1-20261002` 的 E 盘隔离 worktree,README 不动。Xbox 空值/等待/错误码/ICE 解析已修,80 项协议/本地 WebRTC 验证通过;显存做持续全屏增长退出全屏的应急保护,补帧开/关故障注入均通过,**5060 Ti 4K 全屏泄漏根因与真实现场结果未确认,不能称为彻底修好**。本地 5070 加载相同插件时仍稳定,不能只凭插件加载归因。用户授权修好后发布;先保持未发布,候选待反馈者验证。完整接管记录 `FIELD_2.0.1_REPAIR_PLAN_2026-10-02.md`,历史状态不覆盖本节。
diff --git a/docs/PLAYER_STARTUP_SPEED_PLAN_2026-10-03.md b/docs/PLAYER_STARTUP_SPEED_PLAN_2026-10-03.md
new file mode 100644
index 00000000..ac4754fe
--- /dev/null
+++ b/docs/PLAYER_STARTUP_SPEED_PLAN_2026-10-03.md
@@ -0,0 +1,27 @@
+# Player startup, playback rate and optional fullscreen protection
+
+Current user authorises: put automatic fullscreen VRAM fallback behind a saved Settings switch, off by default; optionally resume the last source at application startup (movie position or remembered capture configuration); choose minimal/professional startup page; add real 1x/1.5x/2x/3x movie playback controls to both pages. This explicitly authorises the necessary audio/engine/QML/build integration beyond the historic UI-only freeze. Existing Xbox repair stays included; field VRAM root cause remains unconfirmed and public 2.0.1 publication remains pending. README stays unchanged.
+
+Branch codex/player-startup-speed-20261003 starts at 15a0e39 in E:/项目/Veyra/worktrees/player-startup-speed-20261003. Baseline hashes are immutable in archives/player-startup-speed-20261003; task outputs, tests and process TEMP/TMP use E:/项目/Veyra/<purpose>/player-startup-speed-20261003. Checkpoint checkpoint/pre-player-startup-speed-20261003. No desktop/main changes.
+
+- Protection only acts when explicitly enabled. Disabling also releases the current UI restriction; the switch does not claim to fix retained allocations.
+- Startup reuse is opt-in, one attempt, and explicit command-line media takes priority. File/capture restoration uses the existing source and position code; unavailable inputs report failure. Existing startup-page preference is retained and grouped with automatic resume.
+- Rate is a file-preview transport setting, default 1x. Capture/streams, image processing and export are not sped up. Media PTS stay in original coordinates; the device audio clock maps output samples to those coordinates. Speed changes use the existing seek/reset boundary. SoundTouch 2.4.1, fixed upstream commit 0047e0b1ecfceb041348579119bf79b73a322a3a, supplies pitch-preserving tempo; no proprietary runtime changes. The copied LGPL-2.1 sources remain unmodified, with attribution and licence, statically built from the corresponding Veyra source archive.
+- Verify generated-tone pitch/duration, real WASAPI media-clock rates, switching/seek/pause, 1x regressions, startup file position and capture configuration, QML controls and default-off/opt-in VRAM behavior. Tests bounded to 300 seconds. Build and package candidate with identity audit; no fake real-device acceptance.
+
+Status: implementation and local regressions complete; final build/package audit in progress. Evidence and exact commands are in WORKLOG and task artifacts. Field Xbox/VRR and 5060 Ti results remain unverified; no publication or integration into Claude/main worktrees.
+
+Additional current user authorisation: investigate and fix repeated Xbox disconnects
+from veyra-qml(13).log, and frame generation with console VRR passed through a
+VRR-capable capture card. The user clarified this is the PS5/Switch capture input,
+not the PC monitor's VRR setting. Claude works in other branches; all work here
+stays in this isolated branch, with no changes to his worktrees or shared dependencies.
+The scope guard is extended for the Xbox session/protocol and capture timing only;
+the external baseline remains unchanged. Log evidence: SCTP errno=108 immediately
+precedes the unhandled C++ exception at 14:56:05.080Z. libdatachannel 0.24.5 itself
+closes after Disconnected, so an application grace timer would not recover it.
+Check missing client frame feedback against the already pinned Greenlight source,
+contain sends during shutdown, and test real local peers. Capture tests must vary
+both PTS and arrival intervals; keep actual backward clocks, stalls, mailbox
+boundaries, and compressed decode discontinuities effective. User-device VRR and
+Xbox acceptance cannot be inferred from synthetic or local-peer tests.
diff --git a/docs/RELEASE_NOTES_2.0.1.md b/docs/RELEASE_NOTES_2.0.1.md
index 524521de..37dce761 100644
--- a/docs/RELEASE_NOTES_2.0.1.md
+++ b/docs/RELEASE_NOTES_2.0.1.md
@@ -7,8 +7,12 @@
### 2.0.1 修复
- **导出时显卡被系统重置,不再整段作废**:8K、长视频开 NR 导出时,有 40 系用户遇到驱动卡死并被 Windows 重置。现在已导出的部分会自动保存,新开一个导出进程从断点接着导,最后无损拼接成一个文件,不重新编码;最多续 3 次。这是重置后的恢复,不代表驱动挂起根因已修复;已用模拟设备移除验证续导,8K 反馈者实机结果待确认。
-- **全屏显存异常增长保护**:设置稳定后检测持续增长,自动退出全屏并在本次播放中限制再次进入,保持片源、NR 和补帧设置,用窗口继续播放,避免原先反复重建却继续增长。已分配的异常显存不保证释放,重新开程序可重测。**这是应急保护,5060 Ti 的 4K 全屏泄漏根因尚未确认、现场结果待验证**;本机加载同一 NVIDIA 插件也能稳定播放,不能仅凭模块出现就认定它是原因。设置中的 OBS 游戏采集兼容可用于切换界面显示路径后重测。
-- **Xbox 空值应答导致协商中断**:成功应答里的 `null` 错误字段不会再触发字符串异常;HTTP 200/202 的未完成应答继续有限轮询,数字错误码保留,空 ICE 元数据不会再次中断连接。协商失败显示实际阶段,不再一律提示 UDP 被拦截。本机 80 项协议/本地 WebRTC 测试通过,反馈者真实 Xbox 连接结果待确认。
+- **全屏显存异常增长保护(默认关闭)**:仅在设置中开启后检测持续增长,自动退出全屏并在本次播放中限制再次进入,保持片源、NR 和补帧设置,用窗口继续播放,避免原先反复重建却继续增长。已分配的异常显存不保证释放,重新开程序可重测。**这是应急保护,5060 Ti 的 4K 全屏泄漏根因尚未确认、现场结果待验证**;本机加载同一 NVIDIA 插件也能稳定播放,不能仅凭模块出现就认定它是原因。设置中的 OBS 游戏采集兼容可用于切换界面显示路径后重测。
+- **Xbox 空值应答导致协商中断**:成功应答里的 `null` 错误字段不会再触发字符串异常;HTTP 200/202 的未完成应答继续有限轮询,数字错误码保留,空 ICE 元数据不会再次中断连接。协商失败显示实际阶段,不再一律提示 UDP 被拦截。本机 83 项协议/本地 WebRTC 测试通过,反馈者真实 Xbox 连接结果待确认。
+- **Xbox 断开时崩溃与视频反馈**:关闭期间的输入/控制发送异常被捕获,异步回调不会访问已关闭的会话;补齐参考客户端的视频帧反馈。已验证本地双端握手、报文和并发关闭,真实主机长时间稳定性待复测。
+- **采集卡 VRR 补帧**:可变帧间隔不再仅因偏离标称帧率就反复重置补帧历史,呈现相位跟随实际 A/B 时间差;保留真实断流与时间倒退保护。可变间隔软件回归通过,主机/VRR 采集卡组合仍待实测。
+- **启动继续上次内容**:设置中可开启自动恢复视频与播放进度,或按上次采集配置直接开始;启动页可选极简/专业。
+- **视频倍速**:极简与专业播放栏均可选 1×、1.5×、2×、3×,音调保持;导出与实时采集不变速。
- 以下几项已在 2.0.0 的更新包里修过,2.0.1 一并包含:
- 输出稳定器·抗闪烁打开再关闭后画面定住;
- 首页"继续上次"的采集卡没有声音;
@@ -17,18 +21,22 @@
- 点击任务栏图标不能最小化;
- 采集开始时显卡设备丢失或崩溃,且检测到 NVIDIA App 画面插件或 RTSS 注入时,提示去哪里关闭。
-对应源码:`Veyra-2.0.1-source.zip`;第三方依赖源码与 2.0.0 相同,`Veyra-2.0.0-dependency-source.zip` 随本 Release 一起提供。运行库没有变化。
+对应源码:`Veyra-2.0.1-source.zip`;新增 SoundTouch 2.4.1 源码已包含于 Veyra 源码中,其余第三方依赖源码与 2.0.0 相同,`Veyra-2.0.0-dependency-source.zip` 随本 Release 一起提供。运行库没有变化。
## English
### 2.0.1 fixes
- **An export survives a GPU reset**: some RTX 40 users encountered driver hangs/resets during 8K or long NR exports. Completed parts are kept, a fresh export process resumes, and parts are joined without re-encoding (up to three resets). Recovery was tested with injected device removal; the original driver's hang and the reporting user's 8K result remain unverified.
-- **Protection against sustained fullscreen VRAM growth**: after settings settle, sustained growth makes Veyra return to windowed playback and restrict fullscreen re-entry for that source session, keeping the source, NR and frame generation settings. It avoids repeated rebuilds that do not reclaim memory. Retained memory may require restarting Veyra. **This is containment; the RTX 5060 Ti 4K fullscreen leak and its field outcome remain unconfirmed.** The same NVIDIA plug-in is present in stable local playback, so its presence alone does not establish the cause. OBS game capture compatibility in Settings provides another UI display path for testing.
-- **Xbox nullable replies interrupted negotiation**: null error fields in successful responses are accepted; pending HTTP 200/202 wrappers are polled within a bounded budget; numeric service errors are preserved; nullable ICE metadata no longer aborts connection setup. Failures report their actual stage rather than always blaming blocked UDP. All 80 local protocol/WebRTC checks passed; the reporting user's real-console connection still needs confirmation.
+- **Optional fullscreen VRAM growth protection (off by default)**: when enabled in Settings, sustained growth makes Veyra return to windowed playback and restrict fullscreen re-entry for that source session, keeping the source, NR and frame generation settings. It avoids repeated rebuilds that do not reclaim memory. Retained memory may require restarting Veyra. **This is containment; the RTX 5060 Ti 4K fullscreen leak and its field outcome remain unconfirmed.** The same NVIDIA plug-in is present in stable local playback, so its presence alone does not establish the cause. OBS game capture compatibility in Settings provides another UI display path for testing.
+- **Xbox nullable replies interrupted negotiation**: null error fields in successful responses are accepted; pending HTTP 200/202 wrappers are polled within a bounded budget; numeric service errors are preserved; nullable ICE metadata no longer aborts connection setup. Failures report their actual stage rather than always blaming blocked UDP. All 83 local protocol/WebRTC checks passed; the reporting user's real-console connection still needs confirmation.
+- **Xbox disconnect safety and frame feedback**: transport send exceptions during shutdown are contained, callbacks cannot touch a closed session, and the reference client's video-frame feedback is now sent. Local-peer negotiation, packets and concurrent shutdown passed; real-console long-session stability needs retesting.
+- **VRR capture frame generation**: variable frame intervals no longer repeatedly reset history merely for departing from the negotiated frame rate; presentation phase follows the real A/B interval. Actual stalls and backward clocks remain protected. Variable-timing regression passed; console/VRR capture hardware remains unverified.
+- **Resume at startup**: optionally reopen the previous video and position, or the saved capture device/configuration; choose Minimal or Professional as the startup page.
+- **Movie playback speed**: 1×, 1.5×, 2× and 3× controls on both pages, preserving audio pitch. Export and live capture rates are unchanged.
- Already in the refreshed 2.0.0 package and included here: the picture froze after turning the output stabiliser (anti-flicker) on and off; "Continue" on a capture card had no sound; the load gauge counted one of several NR layers; colour Texture / Clarity / Dehaze had no effect; clicking the taskbar button did not minimise; when the GPU device is lost or Veyra crashes as a capture starts with NVIDIA App's plug-in or RTSS injected, Veyra says where to turn them off.
-Corresponding source: `Veyra-2.0.1-source.zip`; third-party dependency sources are unchanged from 2.0.0 and `Veyra-2.0.0-dependency-source.zip` is attached to this Release too. Runtimes are unchanged.
+Corresponding source: `Veyra-2.0.1-source.zip`; SoundTouch 2.4.1 source is included in the Veyra archive; other third-party dependency sources are unchanged from 2.0.0 and `Veyra-2.0.0-dependency-source.zip` is attached to this Release too. Runtimes are unchanged.
---
diff --git a/docs/WORKLOG.md b/docs/WORKLOG.md
index 57058da6..dcb2b063 100644
--- a/docs/WORKLOG.md
+++ b/docs/WORKLOG.md
@@ -1,5 +1,22 @@
# Veyra 工作记录
+## 2026-10-03 启动恢复、倍速、Xbox 断开与采集 VRR(独立候选)
+
+- 最终 `build-final.log` 编译退出 0,`player-startup-speed-tests.py regression-final` 七组全部退出 0;包含最终“生成帧不发送源帧反馈”修正。`git diff --check`、scope guard、SoundTouch 31 文件身份、中英 README 无修改以及 2.0.0 Release 完整区块/二维码字节比对通过。
+- 用户新增授权:全屏自动退窗改成设置开关、默认关闭;可选启动继续上次视频进度或采集卡配置;极简/专业启动页;双页面 1/1.5/2/3× 视频倍速。随后追加 Xbox 反复断开及主机 VRR 经采集卡输入不能补帧的修复,要求与 Claude 分支隔离。
+- 分支 `codex/player-startup-speed-20261003`,基线 `15a0e39`,目录 `E:/项目/Veyra/worktrees/player-startup-speed-20261003`;checkpoint `checkpoint/pre-player-startup-speed-20261003`。全部新产物位于 `E:/项目/Veyra/build|tests|logs|tmp|archives/player-startup-speed-20261003`。历史 UI-only 禁令由当前明确功能授权扩展;独立 scope guard 检查允许文件,外部 baseline 未变。未改桌面、main 或 Claude 工作树。
+- Xbox 日志 13:五次连接后 Disconnected→Closed,14:56:05.080Z 的 SCTP send errno=108 后紧跟未处理 C++ 异常。最初按通用 WebRTC 语义怀疑可恢复 Disconnected,进一步读取实际 libdatachannel 0.24.5 源码确认库本身立即 remoteClose,因此没有实现无效的等待宽限。现在所有 transport sends 捕获关闭竞态异常,回调共享生命周期门控,停止解码线程后再关闭 RTC,异常 JSON 字段不会逸出回调;细化 ICE/transport 日志。
+- 对照已固定 Greenlight `58e832a1` 补齐原本缺失的输入通道视频反馈:原 RTP key + 到达/解码提交/解码完成/Present 返回时间,单帧有界报文;生成帧和缓存重画不伪装成真实源帧反馈。缺少此类反馈是协议差异,**尚无证据证明它解释了现场全部断开**。没有凭空宣称 Xbox 网络问题根治。
+- VRR:用户明确指 PS5/Switch→支持 VRR 的采集卡,不是 PC 显示器开关。旧的持续断点过滤要求 PTS 在标称间隔的 0.5–1.5 倍内,另以 2.5 倍判断断流;120 Hz 格式下正常的 48–120 Hz 变化会反复清空历史。改用有界的 PTS/到达时钟一致性,保留首帧、反向/非有限时钟、真正长停顿、单次驱动断点和压缩参考链保护;补帧呈现相位使用实际 A/B 间隔。2400 个可变间隔及异常边界自动回归通过。**本机 60 Hz 卡通过不能替代反馈者 VRR 主机实测。**
+- 倍速复用共享播放/seek/reset,不改变导出和实时输入速度。音频 SoundTouch 2.4.1(LGPL-2.1,固定提交 `0047e0b1ecfceb041348579119bf79b73a322a3a`)保调变速,设备音频时钟映射原媒体 PTS;视频与补帧 deadline 同时按速度换算。31 个上游源码/头/许可文件字节一致,身份记录 `archives/.../soundtouch-identity.json`;源码、许可证和构建集成随对应源码提供,proprietary runtime 不变。
+- `python -B scripts/acceptance/player-startup-speed-build.py build-v1.log ...` 到 build-v4.log 均成功;`...-tests.py regression-v2`:音频 8 项(全部速度、0/1.2 秒起播、音高约 440 Hz、精确时长/媒体时钟/无溢出)、Xbox 83 项真实本地双端、repair 243 项、Qt Quick 32 项含真实菜单点击、QML 数据/翻译及 live timing 全过。第一轮音频夹具误设 paused,只有预填充,超时失败;改为正常消费后通过,原证据保留。
+- `python -B E:/项目/Veyra/tmp/player-startup-speed-20261003/product-tests.py product-v3-software-ui`:实际 WASAPI 播放测得 1.5004×/2.0010×/3.0008×,暂停、暂停跳转、速度切回 1×、保存 26.9667 秒位置后重启恢复通过;rememberPosition 关闭而 autoResume 开启仍恢复。专业/极简启动页面及关闭自动恢复通过。界面通过 `QT_QUICK_BACKEND=software` 绘制,视频仍为 D3D12;解码设软件以隔离对照。
+- 默认 D3D12 UI 对照:product-v1 硬解起播、product-v2 软解暂停跳转遇到 `0x887A002B`,Qt 和视频设备同时丢失。用修改前 `field-2.0.1` EXE 及 QML 重跑 (`baseline-d3d12-ui`) 同样复现;未归因于新倍速,也没有根据插件加载就下因果结论。挂起的测试进程经绝对路径核对后只终止本轮实例,日志保留。观察到 Claude 的 `claude/rtss-compat-20261003` 工作树,因此不在此分支重叠修改 RTSS/UI renderer。该环境问题仍需后续合并对应修复,不据软件 UI 测试宣称默认渲染路径已通过。
+- `capture-tests.py capture-v1`:真实 1440p60 采集+NR,在注入持续 GPU 分配时,默认关闭保护保持全屏,启用后退窗且片源/效果保留。启用例的后置验证脚本误匹配日志文案,原 summary 记录失败;`capture-resume.py` 针对同一日志复核正确事件,写 `reviewed-summary.json`,未伪造重测。接着独立启动已保存采集会话,通过 2560×1440/60 NV12 配置自动打开,无需点击。
+- 自审:检查 Xbox 回调寿命与异步发送关闭竞态;只对真实源帧发帧反馈;VRR 仍有时钟跳变/压缩丢参考保护;所有 rate 下媒体 PTS 不重写、音调保持、导出不改;保护默认关闭、禁用立即解除 UI 全屏限制;CLI 显式片源优先于自动恢复。未声称有独立 Reviewer。
+- README 未改、完整 2.0.0 Release 内容和双 220px 二维码保持。公开 2.0.1 仍待现场验证与并行分支收尾,不合并或推送其他工作;本轮交付独立源码提交和候选包。5060 Ti 显存持续增长根因仍未确认,设置保护只是一种可选应急措施。
+
+
## 2026-10-02 Codex 接管 2.0.1:Xbox 修复、显存保护候选
- 当前用户要求先处理日志 8/12 爆显存与日志 11 Xbox 连接失败,再按 Claude 对话发 2.0.1;发布授权已存在,不另问批准。已读取 Claude 本机原对话:README 不动,Release 保留 2.0.0 全部内容并追加修复,中英及双 220px 二维码保留。
diff --git a/i18n/catalog.json b/i18n/catalog.json
index bc4a7f98..6bbc3af0 100644
--- a/i18n/catalog.json
+++ b/i18n/catalog.json
@@ -1505,6 +1505,13 @@
"en": "Xbox sign-in has expired; sign in again.",
"ja": "Xbox のサインインの有効期限が切れました。再度サインインしてください。"
},
+ {
+ "zh": "Xbox 连接失败:{}",
+ "src": "src/source/XboxSessionSource.cpp",
+ "zh-TW": "Xbox 連線失敗:{}",
+ "en": "Xbox connection failed: {}",
+ "ja": "Xbox 接続失敗:{}"
+ },
{
"zh": "XeSS 初始化失败;当前为普通呈现",
"src": "src/engine/EngineController.cpp",
@@ -1659,6 +1666,13 @@
"en": "The last capture target \"%1\" is gone; choose again",
"ja": "前回のキャプチャー対象「%1」が見つかりません。選び直してください"
},
+ {
+ "zh": "上次的视频文件不存在:%1",
+ "src": "src/ui/QmlPlayerBridge.cpp",
+ "zh-TW": "上次的影片檔案不存在:%1",
+ "en": "The previous video file does not exist: %1",
+ "ja": "前回の動画ファイルが見つかりません:%1"
+ },
{
"zh": "上次的采集格式不可用,请在采集卡面板重新选择",
"src": "src/ui/QmlPlayerBridge.cpp",
@@ -2611,6 +2625,20 @@
"en": "Fullscreen",
"ja": "全画面"
},
+ {
+ "zh": "全屏显存增长保护",
+ "src": "qml/Veyra/SettingsPage.qml",
+ "zh-TW": "全螢幕顯存增長保護",
+ "en": "Fullscreen VRAM growth protection",
+ "ja": "全画面の VRAM 増加保護"
+ },
+ {
+ "zh": "全屏期间显存持续增长了 %1 GB,已退出全屏保护播放;当前会话使用窗口播放。增强设置保留。原因尚未确认,可开启设置中的 OBS 游戏采集兼容、重启后测试全屏。",
+ "src": "src/ui/QmlPlayerBridge.cpp",
+ "zh-TW": "全螢幕期間顯示記憶體持續增加了 %1 GB,已退出全螢幕保護播放;目前工作階段使用視窗播放。增強設定保留。原因尚未確認,可開啟設定中的 OBS 遊戲擷取相容、重新啟動後測試全螢幕。",
+ "en": "VRAM kept growing by %1 GB in fullscreen. Playback has returned to a window for this session, keeping enhancement settings. The cause is unconfirmed. You can enable OBS game capture compatibility in Settings, restart, and test fullscreen again.",
+ "ja": "全画面中に VRAM が %1 GB 増え続けたため、この再生はウィンドウ表示に戻りました。映像処理の設定は維持します。原因は未確認です。設定で OBS ゲームキャプチャ互換を有効にし、再起動して全画面を再テストできます。"
+ },
{
"zh": "全屏画面(双击画面 / F11)",
"src": "qml/NodePage.qml",
@@ -3669,6 +3697,13 @@
"en": "No",
"ja": "いいえ"
},
+ {
+ "zh": "启动后自动继续上次内容",
+ "src": "qml/Veyra/SettingsPage.qml",
+ "zh-TW": "啟動後自動繼續上次內容",
+ "en": "Automatically resume on startup",
+ "ja": "起動時に前回の再生を再開"
+ },
{
"zh": "启动窗口大小",
"src": "qml/SettingsPage.qml",
@@ -6540,6 +6575,13 @@
"en": "Playback bar hidden · move the pointer to the top and click the eye in the dock to bring it back",
"ja": "再生バーを非表示にしました · マウスを上端に移動し、ドックの目のアイコンで再表示できます"
},
+ {
+ "zh": "播放速度",
+ "src": "qml/Veyra/PlaybackRateButton.qml",
+ "zh-TW": "播放速度",
+ "en": "Playback speed",
+ "ja": "再生速度"
+ },
{
"zh": "操作失败;请检查网络、输入和可用磁盘空间。",
"src": "src/ui/QmlPlayerBridge.cpp",
@@ -7079,6 +7121,13 @@
"en": "Could not drain while reclaiming VRAM",
"ja": "VRAM 回収時に排出できませんでした"
},
+ {
+ "zh": "显存在设置未变时增加了 %1 GB,正在尝试回收;回收结果见诊断日志。原因尚未确认。",
+ "src": "src/ui/QmlPlayerBridge.cpp",
+ "zh-TW": "顯示記憶體在設定未變時增加了 %1 GB,正在嘗試回收;回收結果請見診斷日誌。原因尚未確認。",
+ "en": "VRAM grew by %1 GB without a settings change. Recovery is being attempted; see the diagnostic log for the result. The cause is unconfirmed.",
+ "ja": "設定を変えていないのに VRAM が %1 GB 増加したため、回収を試みています。結果は診断ログをご覧ください。原因は未確認です。"
+ },
{
"zh": "显存在设置没变的情况下涨了 %1 GB。检测到 NVIDIA App 的画面插件(RTX HDR / 智能平滑运动 / RTX 动态鲜艳度)注入了 Veyra:它只在全屏时介入,这部分显存很可能由它占用,Veyra 重建也释放不了。请在 NVIDIA App → 图形 → 程序设置里为 Veyra 关闭这几项,或退出全屏;已占用的显存要重开 Veyra 才会释放",
"src": "src/ui/QmlPlayerBridge.cpp",
@@ -7611,6 +7660,13 @@
"en": "This export uses AMD FSR super resolution",
"ja": "今回の書き出しは AMD FSR 超解像を使用します"
},
+ {
+ "zh": "本次播放检测到全屏显存持续增长,请使用窗口播放;重新打开片源后可再次测试全屏。",
+ "src": "qml/Veyra/Main.qml",
+ "zh-TW": "本次播放偵測到全螢幕顯示記憶體持續增加,請使用視窗播放;重新開啟來源後可再次測試全螢幕。",
+ "en": "VRAM kept growing in fullscreen during this session. Use windowed playback; reopen the source to test fullscreen again.",
+ "ja": "この再生では全画面中に VRAM が増え続けました。ウィンドウ表示を使用してください。ソースを開き直すと全画面を再テストできます。"
+ },
{
"zh": "松开恢复",
"src": "qml/ColourPanel.qml",
@@ -7730,6 +7786,13 @@
"en": "RivaTuner (RTSS on-screen display) is injected into Veyra; set Application detection level to None for veyra_qml_ui.exe in RTSS and try again",
"ja": "RivaTuner(RTSS の OSD)が Veyra に挿入されています。RTSS で veyra_qml_ui.exe の Application detection level を None にしてから再試行してください"
},
+ {
+ "zh": "检测异常增长时自动退出全屏;默认关闭",
+ "src": "qml/Veyra/SettingsPage.qml",
+ "zh-TW": "偵測異常增長時自動退出全螢幕;預設關閉",
+ "en": "Exit fullscreen if abnormal growth is detected; off by default",
+ "ja": "異常な増加を検出すると全画面を解除(初期設定はオフ)"
+ },
{
"zh": "模型参数",
"src": "qml/NrLayerEditor.qml",
@@ -10131,6 +10194,13 @@
"en": "The video was encoded, but saving the file name failed (Windows error {}); the partial file can be kept",
"ja": "動画はエンコードしましたが、ファイル名を付けて保存できませんでした(Windows エラー {})。partial ファイルは残せます"
},
+ {
+ "zh": "视频恢复上次进度;采集卡沿用上次设备与设置",
+ "src": "qml/Veyra/SettingsPage.qml",
+ "zh-TW": "影片恢復上次進度;擷取卡沿用上次裝置與設定",
+ "en": "Resume the video position or reopen the last capture device and settings",
+ "ja": "動画の前回の位置、または前回のキャプチャーデバイスと設定を復元"
+ },
{
"zh": "视频文件 (*.mp4 *.mkv *.mov *.avi *.webm *.ts *.m2ts);;所有文件 (*)",
"src": "src/ui/QmlPlayerBridge.cpp",
@@ -13189,34 +13259,6 @@
"zh-TW": ";本次匯出使用 AMD FSR 超解析",
"en": "; this export uses AMD FSR super resolution",
"ja": ";今回の書き出しは AMD FSR 超解像を使用します"
- },
- {
- "zh": "全屏期间显存持续增长了 %1 GB,已退出全屏保护播放;当前会话使用窗口播放。增强设置保留。原因尚未确认,可开启设置中的 OBS 游戏采集兼容、重启后测试全屏。",
- "src": "src/ui/QmlPlayerBridge.cpp",
- "zh-TW": "全螢幕期間顯示記憶體持續增加了 %1 GB,已退出全螢幕保護播放;目前工作階段使用視窗播放。增強設定保留。原因尚未確認,可開啟設定中的 OBS 遊戲擷取相容、重新啟動後測試全螢幕。",
- "en": "VRAM kept growing by %1 GB in fullscreen. Playback has returned to a window for this session, keeping enhancement settings. The cause is unconfirmed. You can enable OBS game capture compatibility in Settings, restart, and test fullscreen again.",
- "ja": "全画面中に VRAM が %1 GB 増え続けたため、この再生はウィンドウ表示に戻りました。映像処理の設定は維持します。原因は未確認です。設定で OBS ゲームキャプチャ互換を有効にし、再起動して全画面を再テストできます。"
- },
- {
- "zh": "显存在设置未变时增加了 %1 GB,正在尝试回收;回收结果见诊断日志。原因尚未确认。",
- "src": "src/ui/QmlPlayerBridge.cpp",
- "zh-TW": "顯示記憶體在設定未變時增加了 %1 GB,正在嘗試回收;回收結果請見診斷日誌。原因尚未確認。",
- "en": "VRAM grew by %1 GB without a settings change. Recovery is being attempted; see the diagnostic log for the result. The cause is unconfirmed.",
- "ja": "設定を変えていないのに VRAM が %1 GB 増加したため、回収を試みています。結果は診断ログをご覧ください。原因は未確認です。"
- },
- {
- "zh": "本次播放检测到全屏显存持续增长,请使用窗口播放;重新打开片源后可再次测试全屏。",
- "src": "qml/Veyra/Main.qml",
- "zh-TW": "本次播放偵測到全螢幕顯示記憶體持續增加,請使用視窗播放;重新開啟來源後可再次測試全螢幕。",
- "en": "VRAM kept growing in fullscreen during this session. Use windowed playback; reopen the source to test fullscreen again.",
- "ja": "この再生では全画面中に VRAM が増え続けました。ウィンドウ表示を使用してください。ソースを開き直すと全画面を再テストできます。"
- },
- {
- "zh": "Xbox 连接失败:{}",
- "src": "src/source/XboxSessionSource.cpp",
- "zh-TW": "Xbox 連線失敗:{}",
- "en": "Xbox connection failed: {}",
- "ja": "Xbox 接続失敗:{}"
}
]
}
diff --git a/include/veyra/engine/EngineController.h b/include/veyra/engine/EngineController.h
index 0193110a..459d2daf 100644
--- a/include/veyra/engine/EngineController.h
+++ b/include/veyra/engine/EngineController.h
@@ -107,6 +107,7 @@ struct PlayerSnapshot {
// proof that "adjust while paused" works.
uint64_t pausedFrameRefreshes=0;
double playbackSpeed=0;
+ double playbackRate=1; // requested file-preview transport rate; separate from measurement
bool fgBudgetLimited=false,xessGenerationSuppressed=false;
unsigned previewFgMultiplier=0;
// Runtime-reported DLSSG MultiFrameCountMax: 1 = 2X only, 5 = 6X.
@@ -168,6 +169,8 @@ public:
void stop();
void comparison(int mode,bool base,float split=.5f){comparisonMode_=mode;comparisonBase_=base;comparisonSplit_=std::clamp(split,0.0f,1.0f);}
void pause(bool p);
+ bool setPlaybackRate(double rate);
+ void setFullscreenMemoryProtection(bool enabled);
void previewView(PreviewView view){if(!std::isfinite(view.zoom)||!std::isfinite(view.centerX)||!std::isfinite(view.centerY)||!std::isfinite(view.displayAspect)||view.displayAspect<0||view.mode<0||view.mode>6)return;std::lock_guard lock(mutex_);view.zoom=std::clamp(view.zoom,.05f,64.0f);if(view.displayAspect==0)view.displayAspect=previewView_.displayAspect;previewView_=view;}
PreviewView previewView()const{std::lock_guard lock(mutex_);return previewView_;}
void setVolume(float gain,bool mute);
@@ -199,6 +202,8 @@ private:
std::atomic<bool> stop_{false},paused_{false},muted_{false};
std::atomic<float> volume_{1};uint64_t sessionId_=0;
std::atomic<double> seekSeconds_{-1};
+ std::atomic<double> requestedPlaybackRate_{1};
+ std::atomic<bool> fullscreenMemoryProtection_{false};
int pendingAudioTrack_=-1;uint64_t pendingAudioSession_=0; // mutex_
std::atomic<int> comparisonMode_{0};std::atomic<bool> comparisonBase_{false};std::atomic<float> comparisonSplit_{.5f};
// Guarded by mutex_: DLSSG MultiFrameCountMax of the active session.
diff --git a/include/veyra/engine/LivePresentationTiming.h b/include/veyra/engine/LivePresentationTiming.h
index ff378269..0783bbb6 100644
--- a/include/veyra/engine/LivePresentationTiming.h
+++ b/include/veyra/engine/LivePresentationTiming.h
@@ -15,6 +15,15 @@ inline int64_t liveSourceInterval100ns(pipeline::Rational duration,double nomina
inline int64_t livePhaseInterval100ns(pipeline::Rational duration,double nominalFps,bool compositor){
return liveSourceInterval100ns(compositor?pipeline::Rational{}:duration,nominalFps);
}
+// Variable-refresh capture often retains the negotiated maximum-rate sample
+// duration. The actual A/B source span controls interpolation, not that cap.
+// Never use a stale/reset predecessor to establish the new phase.
+inline int64_t capturePairInterval100ns(pipeline::Rational duration,double nominalFps,
+ double previousPtsMs,double ptsMs,bool reset){
+ const double span=ptsMs-previousPtsMs;
+ if(!reset&&std::isfinite(span)&&span>=1&&span<=100)return int64_t(std::llround(span*10000));
+ return liveSourceInterval100ns(duration,nominalFps);
+}
// Live capture never waits on an absolute source PTS. Once B is available,
// display generated(A,B), then B up to half an input interval later. Anchor
// each pair to current host time, not to the first (possibly stale) sample.
diff --git a/include/veyra/sink/WasapiAudioSink.h b/include/veyra/sink/WasapiAudioSink.h
index be59e0e7..15c99227 100644
--- a/include/veyra/sink/WasapiAudioSink.h
+++ b/include/veyra/sink/WasapiAudioSink.h
@@ -16,6 +16,7 @@
#include <deque>
#include <format>
#include <mutex>
+#include <memory>
#include <limits>
#include <string>
#include <thread>
@@ -33,6 +34,7 @@ struct AVStream;
struct AVPacket;
struct SwrContext;
typedef struct AVFrame AVFrame;
+namespace soundtouch { class SoundTouch; }
namespace veyra::sink {
@@ -94,13 +96,16 @@ public:
// explicit silence; a live source may return 0 so the owner can preserve
// its media timeline and apply its bounded recovery policy.
size_t pull(float* dst, size_t maxFrames, double* firstPtsMs) override;
+ std::optional<double> lastPullEndPtsMs()const override;
+ // Applied by the audio owner on initial open or the next atomic seek.
+ bool setPlaybackRate(double rate);
void stopThread();
void setPaused(bool value) { paused_.store(value); }
// File playback: audio is the master clock. Prefill while explicitly held
// (open/seek/settings rebuild/pause); once released the device clock runs
- // at one-times speed regardless of video lag — slow enhancement drops
+ // at the requested tempo regardless of video lag — slow enhancement drops
// preview frames instead of pausing sound. Coverage is published for
// diagnostics only.
void holdForVideo();
@@ -127,10 +132,14 @@ private:
struct Segment {
double startPtsMs;
size_t frames;
+ double stepMs=1000.0/kAudioRate;
};
void pushDecoded(const AVFrame* frame);
void pushConverted(int frames);
+ void pushPcm(const float*,size_t,double,double);
+ void drainTempo(bool finish=false);
+ void resetTempo();
void decodeBlock();
void closeAll();
@@ -151,6 +160,11 @@ private:
std::atomic<bool> decodedEof_{false};
double nextPtsMs_ = 0.0; // PTS of the NEXT decoded sample
double discardUntilPtsMs_ = -1.0; // seek pruning
+ std::unique_ptr<soundtouch::SoundTouch> tempo_;
+ std::atomic<double> requestedRate_{1},activeRate_{1},lastPullEnd_{-1};
+ double tempoPtsMs_=0,tempoInputEndMs_=0;
+ bool tempoAnchored_=false,tempoFlushed_=false;
+ std::vector<float> tempoBuffer_;
static constexpr size_t kMaxRingFrames = kAudioRate * 2; // 2s hard bound
mutable std::mutex mutex_;
diff --git a/include/veyra/source/CaptureTiming.h b/include/veyra/source/CaptureTiming.h
index 59e3a54a..da4f9d67 100644
--- a/include/veyra/source/CaptureTiming.h
+++ b/include/veyra/source/CaptureTiming.h
@@ -2,7 +2,18 @@
#include "veyra/pipeline/FramePacket.h"
#include <limits>
#include <cmath>
+#include <algorithm>
namespace veyra::source {
+// The negotiated format is the maximum/cap for VRR capture, not a promise
+// that every source frame arrives exactly at that interval. A 120 Hz format
+// may continuously deliver 48-120 Hz content. Keep long stalls discontinuous.
+inline double captureMaximumContinuousGap(double nominalFps){
+ return std::max(.1,std::isfinite(nominalFps)&&nominalFps>0?2.5/nominalFps:0.1);
+}
+inline bool captureClockBreak(double previous,double current,double nominalFps){
+ return !std::isfinite(previous)||!std::isfinite(current)||current<=previous||
+ current-previous>captureMaximumContinuousGap(nominalFps);
+}
class CaptureDriverDiscontinuity {
unsigned continuousFlags_=0;
public:
@@ -10,12 +21,13 @@ public:
bool observe(bool flagged,bool native,bool completeTime,bool havePrevious,
double ptsDelta,double arrivalDelta,double nominalFps){
// Some raw-video drivers leave the flag set on every sample. Only
- // discount a sustained flag when both clocks demonstrate normal cadence.
+ // discount a sustained flag when both clocks demonstrate continuity.
const double interval=nominalFps>0?1.0/nominalFps:0;
const bool continuous=native&&completeTime&&havePrevious&&interval>0&&
std::isfinite(ptsDelta)&&std::isfinite(arrivalDelta)&&
- ptsDelta>=interval*.5&&ptsDelta<=interval*1.5&&
- arrivalDelta>0&&arrivalDelta<=interval*2.5;
+ ptsDelta>=interval*.5&&ptsDelta<captureMaximumContinuousGap(nominalFps)&&
+ arrivalDelta>0&&arrivalDelta<captureMaximumContinuousGap(nominalFps)&&
+ std::abs(ptsDelta-arrivalDelta)<=std::max(.005,ptsDelta*.5);
if(!flagged||!continuous){reset();return flagged;}
if(continuousFlags_<3)++continuousFlags_;
return continuousFlags_<3;
@@ -23,9 +35,8 @@ public:
};
inline bool captureDiscontinuity(bool pending,bool retained,bool driver,bool havePrevious,
double previous,double current,double nominalFps){
- const double maxGap=nominalFps>0?2.5/nominalFps:0.1;
return (pending&&retained)||driver||
- (havePrevious&&(current<=previous||current-previous>maxGap));
+ (havePrevious&&captureClockBreak(previous,current,nominalFps));
}
inline pipeline::Rational captureDuration(int64_t start,int64_t end,bool completeSampleTime,int64_t nominal100ns){
if(completeSampleTime&&end>start){
diff --git a/include/veyra/source/XboxSessionSource.h b/include/veyra/source/XboxSessionSource.h
index 9ade0334..9041437f 100644
--- a/include/veyra/source/XboxSessionSource.h
+++ b/include/veyra/source/XboxSessionSource.h
@@ -71,7 +71,7 @@ public:
sink::CaptureAudioState audioState() const { return audio_.snapshot(); }
void setAudioGain(float value) { audio_.setGain(value); }
void setAudioSync(unsigned mode, int offset) { audio_.setSync(mode, offset); }
- void videoPresented(double ptsMs, int64_t host100ns);
+ void videoPresented(double ptsMs, int64_t host100ns, bool sourceFrame = true);
void videoReset(bool resetAudio = true) { audio_.videoReset(resetAudio); }
// Controller 0; sent when it changes and at least every 33 ms (the console expects a heartbeat).
diff --git a/include/veyra/ui/QmlPlayerBridge.h b/include/veyra/ui/QmlPlayerBridge.h
index 3a53d141..959e7480 100644
--- a/include/veyra/ui/QmlPlayerBridge.h
+++ b/include/veyra/ui/QmlPlayerBridge.h
@@ -19,10 +19,7 @@
// QML animation.
//
// Scope note: this exposes what the engine can actually do today. Playback
-// speed is reported but has no setter (the engine has none), and export
-// resolution is absent (the engine derives it from the NR size policy). A
-// control that cannot change anything is worse than no control, so they are
-// not offered until the engine side exists.
+// speed measurement and requested file-preview rate are separate values.
#include <QObject>
#include <QString>
#include <QRectF>
@@ -353,6 +350,7 @@ class QmlPlayerBridge : public QObject {
Q_PROPERTY(double volume READ volume WRITE setVolume NOTIFY snapshotChanged)
Q_PROPERTY(bool muted READ muted WRITE setMuted NOTIFY snapshotChanged)
Q_PROPERTY(double playbackSpeed READ playbackSpeed NOTIFY snapshotChanged)
+ Q_PROPERTY(double playbackRate READ playbackRate WRITE setPlaybackRate NOTIFY snapshotChanged)
// True while the engine has not yet applied what the UI asked for. The UI
// shows the pending value and marks it, rather than lying about the state.
Q_PROPERTY(bool applying READ applying NOTIFY snapshotChanged)
@@ -678,6 +676,8 @@ public:
bool muted() const;
void setMuted(bool value);
double playbackSpeed() const;
+ double playbackRate() const;
+ void setPlaybackRate(double rate);
bool nrEnabled() const;
int selectedNrLayer() const;
@@ -808,6 +808,7 @@ public:
// Split position from a pointer on the picture host (fractions of the host).
Q_INVOKABLE void setCompareSplitAt(double hostFractionX);
Q_INVOKABLE void resumeLastSource();
+ void autoResumeLastSource();
// List mode: an NR layer's model and residual back to their defaults.
Q_INVOKABLE bool resetNrLayer(int index);
QVariantList protectionRegions() const;
diff --git a/include/veyra/xbox/Protocol.h b/include/veyra/xbox/Protocol.h
index 57380363..9349c469 100644
--- a/include/veyra/xbox/Protocol.h
+++ b/include/veyra/xbox/Protocol.h
@@ -58,6 +58,20 @@ inline std::vector<uint8_t> clientMetadataReport(uint32_t sequence, double timeM
return b;
}
+// Greenlight 58e832a1: input/packet.ts _writeMetadataData. Preserve the actual
+// RTP key and seven u32 fields; times share the session's monotonic origin.
+struct VideoFeedback {
+ uint32_t rtpTimestamp=0, arrivalMs=0, submittedMs=0, decodedMs=0, renderedMs=0;
+};
+inline std::vector<uint8_t> videoFeedbackReport(uint32_t sequence,double timeMs,const VideoFeedback& f){
+ std::vector<uint8_t> b(43,0);
+ detail::put16(b,0,ReportMetadata);detail::put32(b,2,sequence);detail::putDouble(b,6,timeMs);b[14]=1;
+ const auto sent=uint32_t(uint64_t(timeMs));
+ const uint32_t values[]={f.rtpTimestamp,f.arrivalMs,f.submittedMs,f.decodedMs,f.renderedMs,sent,sent};
+ for(size_t i=0;i<7;++i)detail::put32(b,15+4*i,values[i]);
+ return b;
+}
+
// One report with up to a few gamepads: count (u8), then per pad 23 bytes: index, buttons, 4 axes,
// 2 triggers, physicality (u32 LE 1) and virtual physicality (u32 written big-endian 1, as the
// reference client does).
diff --git a/include/veyra/xbox/WebRtcSession.h b/include/veyra/xbox/WebRtcSession.h
index 652886ee..21a2e946 100644
--- a/include/veyra/xbox/WebRtcSession.h
+++ b/include/veyra/xbox/WebRtcSession.h
@@ -70,6 +70,7 @@ public:
void sendGamepad(const GamepadFrame& frame); // sent when it changes, and at least every 33 ms by the caller
void requestKeyframe();
+ void videoPresented(uint32_t rtpTimestamp,int64_t arrival,int64_t submitted,int64_t decoded,int64_t presented);
void close();
WebRtcStats stats() const;
@@ -80,11 +81,19 @@ public:
private:
void onMessageChannel(const std::string& text);
void onInputChannel(const uint8_t* data, size_t size);
- void sendMessageJson(const std::string& text);
- void sendClientConfig();
+ bool sendMessageJson(const std::string& text);
+ bool sendClientConfig();
+ bool sendBinary(const std::shared_ptr<rtc::DataChannel>& channel,const uint8_t* data,size_t size);
+ void sendFailure(const std::exception& error);
double nowMs() const;
WebRtcCallbacks callbacks_;
+ // Shared with asynchronous callbacks so a queued callback can check the
+ // gate after destruction without ever dereferencing its captured this.
+ struct CallbackGate { std::recursive_mutex mutex; bool open=true; };
+ std::shared_ptr<CallbackGate> gate_=std::make_shared<CallbackGate>();
+ bool sendErrorLogged_=false,feedbackSent_=false;
+ uint32_t lastFeedbackRtp_=0;
std::shared_ptr<rtc::PeerConnection> pc_;
std::shared_ptr<rtc::Track> audio_, video_;
std::shared_ptr<rtc::DataChannel> chat_, control_, input_, message_;
diff --git a/qml/Veyra/CineBar.qml b/qml/Veyra/CineBar.qml
index 69a2d9e7..11ef6642 100644
--- a/qml/Veyra/CineBar.qml
+++ b/qml/Veyra/CineBar.qml
@@ -183,6 +183,7 @@ Rectangle {
CBtn { glyph: "fwd10"; onTapped: veyra.seekBy(10) }
CBtn { glyph: "music"; onTapped: audioMenu.openAt(this, "up") }
+ PlaybackRateButton { objectName: "cine-playback-rate" }
}
// .seekrow: mono times either side of the rail.
diff --git a/qml/Veyra/PlaybackRateButton.qml b/qml/Veyra/PlaybackRateButton.qml
new file mode 100644
index 00000000..67766dc0
--- /dev/null
+++ b/qml/Veyra/PlaybackRateButton.qml
@@ -0,0 +1,31 @@
+import QtQuick
+import QtQuick.Controls
+
+Rectangle {
+ id: control
+ implicitWidth: 44
+ implicitHeight: 28
+ radius: 8
+ visible: veyra.hasSource && !veyra.isCapture && veyra.duration > 0
+ enabled: veyra.running
+ color: hover.hovered ? Qt.rgba(1, 1, 1, 0.10) : "transparent"
+ readonly property string rateLabel: Number(veyra.playbackRate || 1).toString() + "×"
+ Text {
+ anchors.centerIn: parent
+ text: control.rateLabel
+ color: Theme.t1
+ font.family: Theme.fontMono
+ font.pixelSize: 12
+ }
+ HoverHandler { id: hover; cursorShape: Qt.PointingHandCursor }
+ TapHandler { onTapped: rateMenu.openAt(control, "up") }
+ ToolTip.visible: hover.hovered
+ ToolTip.text: qsTr("播放速度")
+ VMenu {
+ id: rateMenu
+ objectName: control.objectName + "-menu"
+ title: qsTr("播放速度")
+ items: [1, 1.5, 2, 3].map(r => ({ label: r + "×", rate: r, checked: Number(veyra.playbackRate || 1) === r }))
+ onPicked: (i, o) => veyra.playbackRate = o.rate
+ }
+}
diff --git a/qml/Veyra/ProPage.qml b/qml/Veyra/ProPage.qml
index e9eafdc0..4e55536f 100644
--- a/qml/Veyra/ProPage.qml
+++ b/qml/Veyra/ProPage.qml
@@ -583,6 +583,7 @@ VPage {
font.pixelSize: Theme.fsSmall
}
VTag { visible: veyra.sourceRateText.length > 0; text: qsTr("源 ") + veyra.sourceRateText }
+ PlaybackRateButton { objectName: "pro-playback-rate" }
VTag { visible: veyra.outputSummary.length > 0; text: qsTr("输出 ") + veyra.outputSummary.replace("x", "×") }
VTag {
objectName: "pro-fg-tag"
diff --git a/qml/Veyra/SettingsPage.qml b/qml/Veyra/SettingsPage.qml
index 5bcd9b98..7df7f0a1 100644
--- a/qml/Veyra/SettingsPage.qml
+++ b/qml/Veyra/SettingsPage.qml
@@ -271,6 +271,24 @@ VPage {
}
}
VGroup {
+ VRow {
+ label: qsTr("启动后自动继续上次内容")
+ hint: qsTr("视频恢复上次进度;采集卡沿用上次设备与设置")
+ VSwitch {
+ objectName: "set-auto-resume"
+ checked: !!veyra.preferences.autoResume
+ onToggled: v => veyra.setPreference("autoResume", v)
+ }
+ }
+ VRow {
+ label: qsTr("全屏显存增长保护")
+ hint: qsTr("检测异常增长时自动退出全屏;默认关闭")
+ VSwitch {
+ objectName: "set-fullscreen-memory-protection"
+ checked: !!veyra.preferences.fullscreenMemoryProtection
+ onToggled: v => veyra.setPreference("fullscreenMemoryProtection", v)
+ }
+ }
VRow {
label: qsTr("打开时的默认页面")
hint: qsTr("首页 = 选择片源的页面(点顶部 Logo 也能回到这里)")
diff --git a/qml/Veyra/qmldir b/qml/Veyra/qmldir
index db7ae2ff..feec1a16 100644
--- a/qml/Veyra/qmldir
+++ b/qml/Veyra/qmldir
@@ -1,4 +1,5 @@
module Veyra
+PlaybackRateButton 1.0 PlaybackRateButton.qml
singleton Theme 1.0 Theme.qml
VAccordion 1.0 VAccordion.qml
VSubGroup 1.0 VSubGroup.qml
diff --git a/scripts/acceptance/player-startup-speed-build.py b/scripts/acceptance/player-startup-speed-build.py
new file mode 100644
index 00000000..f6f809f3
--- /dev/null
+++ b/scripts/acceptance/player-startup-speed-build.py
@@ -0,0 +1,45 @@
+"""Bounded isolated build with the current product's validated dependency paths."""
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[2]
+BASE = Path('E:/项目/Veyra')
+TASK = 'player-startup-speed-20261003'
+BUILD, TMP, LOGS = (BASE / part / TASK for part in ('build', 'tmp', 'logs'))
+CONTROL = ROOT / 'scripts/acceptance/player-startup-speed-control.py'
+subprocess.run([sys.executable, '-B', str(CONTROL)], check=True)
+for p in (BUILD, TMP, LOGS):
+ p.mkdir(parents=True, exist_ok=True)
+settings = []
+for line in (BASE / 'build/main-merge-20261002/CMakeCache.txt').read_text(encoding='utf8').splitlines():
+ if ':' not in line or '=' not in line or line.startswith(('#', '//')):
+ continue
+ keytype, value = line.split('=', 1)
+ key, kind = keytype.split(':', 1)
+ if kind in ('INTERNAL', 'STATIC'):
+ continue
+ if key.startswith('VEYRA_') or key in ('CMAKE_PREFIX_PATH', 'CMAKE_MAKE_PROGRAM', 'PROTOC', 'PKG_CONFIG_EXECUTABLE'):
+ settings.append(f'set({key} [[{value}]] CACHE {kind} "Existing product dependency" FORCE)')
+(TMP / 'dependencies.cmake').write_text('\n'.join(settings), encoding='utf8')
+targets = sys.argv[2:] or ['veyra_qml_ui', 'veyra_xbox_tests']
+if any(not t.replace('_', '').isalnum() for t in targets):
+ raise SystemExit('Invalid target')
+cmd = TMP / 'build.cmd'
+cmd.write_text('@echo off\nchcp 65001 >nul\ncall "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Auxiliary\\Build\\vcvars64.bat" >nul 2>&1\nif errorlevel 1 exit /b 4\n"%TASK_CMAKE%" --preset x64-release -S "%TASK_ROOT%" -B "%TASK_BUILD%" -C "%TASK_TMP%\\dependencies.cmake"\nif errorlevel 1 exit /b 5\n"%TASK_CMAKE%" --build "%TASK_BUILD%" --parallel 6 --target ' + ' '.join(targets) + '\nexit /b %errorlevel%\n', encoding='ascii')
+env = os.environ.copy()
+env.update(VSLANG='1033', TASK_ROOT=str(ROOT), TASK_BUILD=str(BUILD), TASK_TMP=str(TMP),
+ TEMP=str(TMP), TMP=str(TMP),
+ TASK_CMAKE='C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe')
+log = LOGS / sys.argv[1]
+with log.open('x', encoding='utf8') as output:
+ try:
+ result = subprocess.run(['cmd.exe', '/d', '/c', str(cmd)], cwd=ROOT, env=env,
+ stdout=output, stderr=subprocess.STDOUT, timeout=890).returncode
+ except subprocess.TimeoutExpired:
+ result = 124
+print('BUILD', result, log)
+print('\n'.join(log.read_text(encoding='utf8', errors='replace').splitlines()[-25:]))
+subprocess.run([sys.executable, '-B', str(CONTROL)], check=True)
+raise SystemExit(result)
diff --git a/scripts/acceptance/player-startup-speed-control.py b/scripts/acceptance/player-startup-speed-control.py
new file mode 100644
index 00000000..67edd461
--- /dev/null
+++ b/scripts/acceptance/player-startup-speed-control.py
@@ -0,0 +1,30 @@
+"""Guard this field repair against an immutable, external source baseline."""
+import hashlib
+import json
+import subprocess
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[2]
+BASELINE = Path('E:/项目/Veyra/archives/player-startup-speed-20261003/scope-baseline.json')
+ALLOWED = {'qml/Veyra/CineBar.qml', 'scripts/acceptance/player-startup-speed-control.py', 'include/veyra/sink/WasapiAudioSink.h', 'scripts/acceptance/player-startup-speed-build.py', 'include/veyra/ui/QmlPlayerBridge.h', 'src/ui/QmlPlayerBridge.cpp', 'qml/Veyra/PlaybackRateButton.qml', 'include/veyra/engine/EngineController.h', 'src/sink/WasapiAudioSink.cpp', 'tests/qml/quick/tst_components.qml', 'THIRD_PARTY_NOTICES.md', 'CMakeLists.txt', 'scripts/package-qml-release.py', 'docs/RELEASE_NOTES_2.0.1.md', 'src/engine/EngineController.cpp', 'src/ui/PlayerUiFacade.cpp', 'tests/integration/AudioPlaybackRateTests.cpp', 'qml/Veyra/qmldir', 'apps/veyra-qml/main.cpp', 'qml/Veyra/SettingsPage.qml', 'i18n/catalog.json', 'scripts/acceptance/player-startup-speed-tests.py', 'docs/WORKLOG.md', 'docs/PLAYER_STARTUP_SPEED_PLAN_2026-10-03.md', 'qml/Veyra/ProPage.qml', 'docs/CURRENT_STATUS.md'}
+
+baseline = json.loads(BASELINE.read_text(encoding='utf8'))
+ALLOWED.update({
+ 'src/xbox/WebRtcSession.cpp', 'include/veyra/xbox/WebRtcSession.h',
+ 'include/veyra/xbox/Protocol.h', 'src/source/XboxSessionSource.cpp',
+ 'include/veyra/source/XboxSessionSource.h',
+ 'src/source/CaptureCardSource.cpp', 'include/veyra/source/CaptureTiming.h',
+ 'include/veyra/engine/LivePresentationTiming.h', 'tests/unit/LivePresentationTimingTests.cpp',
+ 'tests/xbox/XboxTests.cpp', 'tests/qml/quick/tst_playback_rate.qml',
+})
+tracked = subprocess.check_output(['git', 'ls-files', '-z'], cwd=ROOT).decode().split('\0')
+new = subprocess.check_output(['git', 'ls-files', '--others', '--exclude-standard', '-z'], cwd=ROOT).decode().split('\0')
+current = {p: hashlib.sha256((ROOT / p).read_bytes()).hexdigest()
+ for p in tracked + new if p and (ROOT / p).is_file()}
+changed = {p for p in current.keys() | baseline['files'].keys()
+ if current.get(p) != baseline['files'].get(p)}
+unexpected = {p for p in changed - ALLOWED if not p.startswith('third_party/soundtouch/')}
+print('FIELD SCOPE', 'FAIL' if unexpected else 'PASS', len(changed), 'changed files')
+if unexpected:
+ print('\n'.join(sorted(unexpected)))
+raise SystemExit(bool(unexpected))
diff --git a/scripts/acceptance/player-startup-speed-tests.py b/scripts/acceptance/player-startup-speed-tests.py
new file mode 100644
index 00000000..5ceac618
--- /dev/null
+++ b/scripts/acceptance/player-startup-speed-tests.py
@@ -0,0 +1,37 @@
+"""Isolated, bounded product regression. All artifacts live under the task root."""
+import json, math, os, shutil, struct, subprocess, sys, wave
+from pathlib import Path
+ROOT=Path(__file__).resolve().parents[2]
+BASE=Path('E:/项目/Veyra');TASK='player-startup-speed-20261003'
+BUILD=BASE/'build'/TASK;TESTS=BASE/'tests'/TASK;APP=TESTS/'app'
+QT=Path('C:/veyra-deps/qt-veyra/6.8.3/msvc2022_64')
+def main():
+ label=sys.argv[1]
+ if not label.replace('-','').isalnum():raise ValueError('label')
+ out=TESTS/label;out.mkdir(exist_ok=False)
+ tmp=BASE/'tmp'/TASK/label;tmp.mkdir(exist_ok=False)
+ env=os.environ.copy();env.update(TEMP=str(tmp),TMP=str(tmp),
+ PATH=str(QT/'bin')+os.pathsep+'C:/veyra-deps/ffmpeg-ps5-dav1d-installed/bin'+os.pathsep+env['PATH'])
+ if not APP.exists():
+ shutil.copytree(BASE/'tests/field-2.0.1-20261002/app',APP)
+ for f in BUILD.glob('*.exe'):shutil.copy2(f,APP/f.name)
+ shutil.copytree(ROOT/'qml',APP/'qml',dirs_exist_ok=True)
+ shutil.copytree(ROOT/'tests/qml/quick',APP/'qml-tests',dirs_exist_ok=True)
+ for name in ('Qt6Test.dll','Qt6QuickTest.dll'):shutil.copy2(QT/'bin'/name,APP/name)
+ tone=out/'tone.wav'
+ with wave.open(str(tone),'wb') as f:
+ f.setnchannels(2);f.setsampwidth(2);f.setframerate(48000)
+ f.writeframes(b''.join(struct.pack('<hh',*(2*[int(12000*math.sin(2*math.pi*440*i/48000))])) for i in range(240000)))
+ env.update(QT_QPA_PLATFORM='offscreen',QT_QUICK_BACKEND='software',QT_QUICK_CONTROLS_STYLE='Basic',
+ QML_DISABLE_DISK_CACHE='1',QT_FORCE_STDERR_LOGGING='1',VEYRA_EXPORT_TEST_ARTIFACTS=str(out/'qml-images'))
+ (out/'qml-images').mkdir()
+ results=[]
+ for name in ('audio_playback_rate','live_timing','xbox','repair_contract','qml_data','ui_i18n','qml_quick'):
+ args=[str(tone)] if name=='audio_playback_rate' else [str(out/'data')] if name=='qml_data' else []
+ with (out/(name+'.log')).open('xb') as log:
+ try:code=subprocess.run([str(APP/('veyra_'+name+'_tests.exe')),*args],cwd=APP,env=env,stdout=log,stderr=subprocess.STDOUT,timeout=290).returncode
+ except subprocess.TimeoutExpired:code=124
+ results.append({'name':name,'exit':code});print(results[-1],flush=True)
+ (out/'summary.json').write_text(json.dumps(results,indent=2),encoding='utf8')
+ raise SystemExit(any(r['exit'] for r in results))
+if __name__=='__main__':main()
diff --git a/scripts/package-qml-release.py b/scripts/package-qml-release.py
index 68705d31..903eb70f 100644
--- a/scripts/package-qml-release.py
+++ b/scripts/package-qml-release.py
@@ -26,7 +26,8 @@ REQUIRED = {'veyra_qml_ui.exe', 'qml/Veyra/Main.qml', 'platforms/qwindows.dll',
'licenses/NVIDIA_RTX_VIDEO_SDK_LICENSE.pdf', 'licenses/INTEL_XESS_LICENSE.txt',
'licenses/AMD-FIDELITYFX-LICENSE.txt', 'licenses/moonlight/LICENSE.txt',
'licenses/xbox/libdatachannel.txt', 'licenses/magewell/MWCapture-SDK-NOTICE.txt',
- 'licenses/DLSSG-Transfusion-MIT.txt', 'release-runtime-manifest.json',
+ 'licenses/DLSSG-Transfusion-MIT.txt', 'licenses/SoundTouch-LGPL-2.1.txt',
+ 'licenses/SoundTouch-origin.md', 'qml/Veyra/PlaybackRateButton.qml', 'release-runtime-manifest.json',
'runtime/experimental/release-runtime-manifest.json'}
@@ -246,6 +247,8 @@ foreach($f in $lock.files) {
tree(ROOT / 'licenses', 'licenses')
copy(ARTIFACTS / 'deps/moonlight/moonlight-common-c/LICENSE.txt', 'licenses/moonlight/LICENSE.txt')
copy(ROOT / 'src/ngx/transfusion/LICENSE.txt', 'licenses/DLSSG-Transfusion-MIT.txt')
+ copy(ROOT / 'third_party/soundtouch/COPYING.TXT', 'licenses/SoundTouch-LGPL-2.1.txt')
+ copy(ROOT / 'third_party/soundtouch/VEYRA_ORIGIN.md', 'licenses/SoundTouch-origin.md')
tree(args.qt_licenses, 'licenses/qt')
tree(args.qt / 'sbom', 'licenses/qt/sbom')
copy(ffmpeg / 'share/ffmpeg/veyra-local-build.json', 'licenses/FFMPEG-VEYRA-BUILD.json')
diff --git a/src/engine/EngineController.cpp b/src/engine/EngineController.cpp
index 9ab8fdcb..9deab8c1 100644
--- a/src/engine/EngineController.cpp
+++ b/src/engine/EngineController.cpp
@@ -74,13 +74,27 @@ void EngineController::dispatch(){
}
void EngineController::post(std::function<void()> task){ {std::lock_guard lock(mutex_);stop_=true;pending_=std::move(task);}wake_.notify_one(); }
bool EngineController::idle()const{std::lock_guard lock(mutex_);return !busy_&&!pending_;}
+bool EngineController::setPlaybackRate(double rate){
+ if(rate!=1.0&&rate!=1.5&&rate!=2.0&&rate!=3.0)return false;
+ std::lock_guard lock(mutex_);
+ if(!snapshot_.running||snapshot_.capture||snapshot_.image)return false;
+ if(requestedPlaybackRate_==rate)return true;
+ requestedPlaybackRate_=rate;snapshot_.playbackRate=rate;
+ if(seekSeconds_<0){snapshot_.seekTarget=snapshot_.position;++snapshot_.seekRequested;seekSeconds_=snapshot_.position;}
+ veyra::log::info("playback-rate",std::format("requested {:.2f}x at {:.3f}s",rate,snapshot_.position));
+ return true;
+}
+void EngineController::setFullscreenMemoryProtection(bool enabled){
+ fullscreenMemoryProtection_=enabled;
+ if(!enabled){std::lock_guard lock(mutex_);snapshot_.vramFullscreenUnsafe=false;}
+}
void EngineController::open(HWND video,const std::wstring& path,PlayerOptions opts){
const bool captureReplay=opts.captureReplayForTest;
// Diagnostics-only PlayerOptions fields never enter EnhancementSettings;
// keep them across the snapshot round-trip below.
const bool captureCpuUnpack=opts.captureCpuUnpack;
const bool softwareDecode=opts.softwareDecode;const bool hardwareDecodeOnly=opts.hardwareDecodeOnly;
- {std::lock_guard lock(mutex_);snapshot_={};activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
+ {std::lock_guard lock(mutex_);snapshot_={};requestedPlaybackRate_=1;activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
opts.captureReplayForTest=captureReplay;
opts.captureCpuUnpack=captureCpuUnpack;
opts.softwareDecode=softwareDecode;opts.hardwareDecodeOnly=hardwareDecodeOnly;
@@ -89,7 +103,7 @@ void EngineController::open(HWND video,const std::wstring& path,PlayerOptions op
#ifdef VEYRA_ENABLE_REMOTEPLAY
void EngineController::openRemotePlay(HWND window,source::RemotePlayConnectDesc desc,PlayerOptions opts){
auto request=std::make_shared<source::RemotePlayConnectDesc>(std::move(desc));
- {std::lock_guard lock(mutex_);snapshot_={};activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.remotePlay=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
+ {std::lock_guard lock(mutex_);snapshot_={};requestedPlaybackRate_=1;activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.remotePlay=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
post([this,window,request,opts]{paused_=false;seekSeconds_=-1;run(window,L"remoteplay:",opts,request);});
}
remoteplay::ControllerFeedback EngineController::remotePlayFeedback(){std::lock_guard lock(mutex_);return activeRemote_?activeRemote_->takeFeedback():remoteplay::ControllerFeedback{};}
@@ -115,14 +129,14 @@ remoteplay::ControllerFeedback EngineController::xboxFeedback(){std::shared_ptr<
#ifdef VEYRA_ENABLE_MOONLIGHT
void EngineController::openMoonlight(HWND window,source::MoonlightConnectDesc desc,PlayerOptions opts){
auto request=std::make_shared<source::MoonlightConnectDesc>(std::move(desc));
- {std::lock_guard lock(mutex_);snapshot_={};activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.moonlightActive=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
+ {std::lock_guard lock(mutex_);snapshot_={};requestedPlaybackRate_=1;activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.moonlightActive=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
post([this,window,request,opts]{paused_=false;seekSeconds_=-1;run(window,L"moonlight:",opts,{},request);});
}
#endif
#ifdef VEYRA_ENABLE_XBOX
void EngineController::openXbox(HWND window,source::XboxConnectDesc desc,PlayerOptions opts){
auto request=std::make_shared<source::XboxConnectDesc>(std::move(desc));
- {std::lock_guard lock(mutex_);snapshot_={};activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.xboxActive=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
+ {std::lock_guard lock(mutex_);snapshot_={};requestedPlaybackRate_=1;activeFlow_.reset();previewView_={};fgMultiFrameMaxCap_=0;xessMaxInterpolatedFramesCap_=0;fsrMaxGeneratedFramesCap_=0;snapshot_.sessionId=++sessionId_;snapshot_.transport=TransportState::Opening;snapshot_.xboxActive=true;snapshot_.capture=true;savePath_.clear();desired_=opts.snapshot();desiredNodeOrder_=opts.nodeOrder;desired_.revision=++nextRevision_;snapshot_.desired=desired_;opts=PlayerOptions::from(desired_,desiredNodeOrder_);}
post([this,window,request,opts]{paused_=false;seekSeconds_=-1;run(window,L"xbox:",opts,{},{},request);});
}
#endif
@@ -651,6 +665,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
uint64_t vramFloor=0,vramRevision=~0ull,vramRecoverBefore=0,vramRecoverGrowth=0;auto vramSettleUntil=Clock::now(),vramRecoverCheckAt=Clock::now();
bool vramWarned=false;int vramRecoverStep=0,vramRecoverDone=0,vramRecoveries=0;
bool vramFullscreenBlocked=false;unsigned vramGrowthSamples=0;
+ bool vramProtectionEnabled=fullscreenMemoryProtection_.load();
// VEYRA_TEST_VRAM_LEAK_MIB=n: hold on to n MiB more of video memory every second;
// VEYRA_TEST_VRAM_LEAK_RELEASE=1 lets the swapchain rebuild free it (as a leak tied
// to the swapchain would), otherwise nothing frees it.
@@ -667,6 +682,11 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
Microsoft::WRL::ComPtr<ID3D12Resource> r;
if(SUCCEEDED(ctx.device()->CreateCommittedResource(&heap,D3D12_HEAP_FLAG_NONE,&desc,D3D12_RESOURCE_STATE_COMMON,nullptr,IID_PPV_ARGS(&r))))testLeak.push_back(r);
}
+ const bool protection=fullscreenMemoryProtection_.load();
+ if(protection!=vramProtectionEnabled){
+ vramProtectionEnabled=protection;vramFullscreenBlocked=false;vramGrowthSamples=0;vramFloor=0;
+ vramSettleUntil=Clock::now()+std::chrono::seconds(10);
+ }
if(!usage)return;
// Leaving fullscreen stopped further growth in both field logs. Preserve
// the source/effects and avoid repeated failed graph rebuilds. This only
@@ -693,7 +713,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
if(!vramFloor||usage<vramFloor)vramFloor=usage;
const uint64_t growth=usage-vramFloor;
const uint64_t fullscreenLimit=std::max<uint64_t>(512ull<<20,std::min<uint64_t>(2048ull<<20,budget/8));
- if(fullscreen&&growth>fullscreenLimit){
+ if(vramProtectionEnabled&&fullscreen&&growth>fullscreenLimit){
if(++vramGrowthSamples<3)return;
vramFullscreenBlocked=true;vramWarned=true;
const std::wstring injected=gfx::riskyInjections();
@@ -721,6 +741,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
if(vramRecoveries<3){++vramRecoveries;vramRecoverStep=1;vramRecoverGrowth=growth;}
else{vramFloor=usage;} // give up rebuilding; report again only on further growth
};
+ double playbackRate=1.0;
auto anchor=Clock::now(),statsStart=anchor;double anchorMs=0;uint64_t frames=0,sourceFrames=0;bool wasPaused=false;double discardBefore=0;
double lastAudioClockMs=0;bool audioClockExhausted=false;auto audioTailAnchor=anchor;
bool publishedAudioRecovery=false;HRESULT publishedAudioError=S_OK;uint64_t publishedAudioRecoveries=0;
@@ -945,14 +966,14 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
// while an endpoint recovers. Steady-state video scheduling reads
// this clock; it never waits on audio coverage.
auto nowMs=[&](){
- if(!audioStarted)return anchorMs+std::chrono::duration<double,std::milli>(Clock::now()-anchor).count();
+ if(!audioStarted)return anchorMs+std::chrono::duration<double,std::milli>(Clock::now()-anchor).count()*playbackRate;
publishAudioStatus();const double a=audio.mediaTimeMs();
if(std::isfinite(a)){lastAudioClockMs=a;audioClockExhausted=false;return a;}
// A disconnected endpoint freezes the shared media clock. Only a
// fully exhausted audio stream may hand its tail to the wall clock.
if(audioPipe.clockExhausted()){
if(!audioClockExhausted){audioClockExhausted=true;audioTailAnchor=Clock::now();}
- return lastAudioClockMs+std::chrono::duration<double,std::milli>(Clock::now()-audioTailAnchor).count();
+ return lastAudioClockMs+std::chrono::duration<double,std::milli>(Clock::now()-audioTailAnchor).count()*playbackRate;
}
return lastAudioClockMs;
};
@@ -1204,6 +1225,10 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
if(seekSeconds_.load()<0){snapshot_.seekTarget=trackPosition;++snapshot_.seekRequested;seekSeconds_=trackPosition;}}
log::info("audio-track",std::format("requested={} selected={} changed={} positionSeconds={:.3f}",track,audioPipe.selectedTrack(),changed,trackPosition));
}
+ if(!isImage&&!isCapture&&playbackRate!=requestedPlaybackRate_.load()){
+ playbackRate=requestedPlaybackRate_.load();audioPipe.setPlaybackRate(playbackRate);
+ playbackSpeedLastWall={};cadence.reset();
+ }
double seek;{std::lock_guard lock(mutex_);seek=seekSeconds_.exchange(-1);if(seek>=0)activeSeekId=snapshot_.seekRequested;}
if(seek>=0&&!isImage&&!isCapture){
seekStarted=Clock::now();seekDecoded=0;
@@ -1502,7 +1527,9 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
}
}
{std::lock_guard lock(mutex_);snapshot_.previewFgMultiplier=previewFgMultiplier;}
- const auto liveInterval=livePhaseInterval100ns(pkt.duration,activeSource->info().averageFps/(isScreen&&halfRate?2:1),isScreen);
+ const auto liveInterval=physicalCapture?
+ capturePairInterval100ns(pkt.duration,activeSource->info().averageFps,lastProcessedPtsMs,pts,historyReset):
+ livePhaseInterval100ns(pkt.duration,activeSource->info().averageFps/(isScreen&&halfRate?2:1),isScreen);
const auto processingAllowance=isCapture&&options.fg&&!presentSinkFrameGeneration(options.settings.frameGenerationBackend)?
fgBudget.processingAllowance(host100ns(),liveInterval):0;
// Present-sink providers own subframe pacing after receiving B.
@@ -1522,7 +1549,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
// Never extend it based on processing/ready completion.
if(pairAnchoredLive)liveTimeline.resetPair(batch.identity.epoch,batch.b100ns,liveInputReady,pairDelay);
else if(!liveTimeline.anchored(batch.identity.epoch))liveTimeline.reset(batch.identity.epoch,batch.b100ns,liveInputReady,liveSourceInterval100ns(pkt.duration,activeSource->info().averageFps));
- const auto interval=liveSourceInterval100ns(pkt.duration,activeSource->info().averageFps);
+ const auto interval=liveInterval;
const auto a=(historyReset||batch.b100ns<=batch.a100ns||batch.b100ns-batch.a100ns>10000000)?batch.b100ns-interval:batch.a100ns;
const auto lastGenerated=pipeline::FrameBatch::interpolate(a,batch.b100ns,previewFgMultiplier-1,previewFgMultiplier);
const auto now=host100ns(),deadline=liveTimeline.deadline(lastGenerated);
@@ -1556,10 +1583,10 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
const auto now=host100ns();
const auto media=int64_t(nowMs()*10000.0);
const auto firstPts=pipeline::FrameBatch::interpolate(a,batch.b100ns,1,previewFgMultiplier);
- const auto firstDeadline=now+firstPts-media,deadline=now+lastGenerated-media;
+ const auto firstDeadline=now+int64_t((firstPts-media)/playbackRate),deadline=now+int64_t((lastGenerated-media)/playbackRate);
const auto first=frameFlow->snapshot(now).gpuTiming[size_t(diagnostics::GpuStage::Fg1)].p95;
const auto elapsed=elapsedMs(processStart),blit=livePresentGpu.p95();
- const auto admitted=fgBudget.admitFile(now,firstDeadline,deadline,interval/previewFgMultiplier,elapsed,blit,first,warmingHistory,queuedGpuMs,options.settings.fgStrictAdmission);
+ const auto admitted=fgBudget.admitFile(now,firstDeadline,deadline,int64_t(interval/playbackRate)/previewFgMultiplier,elapsed,blit,first,warmingHistory,queuedGpuMs,options.settings.fgStrictAdmission);
logAdmission(batch,now,deadline,elapsed,blit,admitted,warmingHistory,"file-audio",firstDeadline,queuedGpuMs);
if(admitted)return pipeline::EnhanceGraph::FgDecision::Evaluate;
// Over budget for the full group: emit a presentable 2X
@@ -1582,8 +1609,8 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
// VEYRA_TEST_FG_NO_REDUCED=1 disables it for A/B runs.
static const bool reducedGroups=GetEnvironmentVariableW(L"VEYRA_TEST_FG_NO_REDUCED",nullptr,0)==0;
if(reducedGroups&&!warmingHistory&&previewFgMultiplier>2){
- const auto midpoint=now+pipeline::FrameBatch::interpolate(a,batch.b100ns,1,2)-media;
- if(fgBudget.canAdmitReduced(now,midpoint,interval/2,elapsed,blit,first,queuedGpuMs)){
+ const auto midpoint=now+int64_t((pipeline::FrameBatch::interpolate(a,batch.b100ns,1,2)-media)/playbackRate);
+ if(fgBudget.canAdmitReduced(now,midpoint,int64_t(interval/playbackRate)/2,elapsed,blit,first,queuedGpuMs)){
++fgReducedPairs;
if(now>=nextFgReducedLog){nextFgReducedLog=now+10000000;
veyra::log::info("live-fg-admission",std::format("timeline=file-audio revision={} source={} reduced=2X requestedMultiplier={} reducedPairs={} remainingMidpointMs={:.3f} predictedFullMs={:.3f} (history kept; presentable midpoint instead of whole-group hole)",batch.identity.settingsRevision,batch.identity.sourceFrameId,options.fgMultiplier,fgReducedPairs,double(midpoint-now)/10000,fgBudget.predicted(now,false).value_or(-1)));}
@@ -1737,7 +1764,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
};
auto step=std::make_shared<LiveStepState>();
for(unsigned i=0;i<out.batch.count;++i)if(out.batch.frames[i].kind!=pipeline::FrameKind::Generated||out.batch.frames[i].validity==pipeline::GenerationValidity::Valid)++step->remaining;
- const double sourceIntervalMs=double(liveSourceInterval100ns(pkt.duration,activeSource->info().averageFps))/10000;
+ const double sourceIntervalMs=double(isCapture?liveInterval:liveSourceInterval100ns(pkt.duration,activeSource->info().averageFps))/10000;
const auto baselineReady=pkt.decodedHost100ns>0?pkt.decodedHost100ns:captureArrival;
const bool delayEnhanced=options.nr||options.sr||options.fg;
// Captures are explicit on purpose (sweep 2026-09-22 B1): this step runs
@@ -1757,7 +1784,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
#ifdef VEYRA_ENABLE_XBOX
&xb,
#endif
- &seekDecoded,&seekStarted,&audio,&audioPipe,&audioStarted,&cadence,&ctx,&fileAudioAlignPending,&fileAwaitingVideo,&fileInputEnded,&frameFlow,&graph,&host100ns,&isCapture,&isImage,&livePresent,&liveScheduler,&nextFgDeadlineLog,&nowMs,&options,&pendingCompletions,&physicalCapture,&presentEntryDeviation,&presentReturnDeviation,&presentationEffective,&presentationGeneration,&presentationSkippedGenerated,&presenter,&ring,&runSessionId,&seekPreviewPending,&traceFrame,&traceSubframes,watch,step,timeline,captureArrival,baselineReady,delayEnhanced,activeSeekId,lineage,jobGeneration,rereadCached,sourceIntervalMs,flow=frameFlow](int64_t now)->LiveGpuScheduler::Step{
+ &seekDecoded,&seekStarted,&audio,&audioPipe,&audioStarted,&cadence,&ctx,&fileAudioAlignPending,&fileAwaitingVideo,&fileInputEnded,&playbackRate,&frameFlow,&graph,&host100ns,&isCapture,&isImage,&livePresent,&liveScheduler,&nextFgDeadlineLog,&nowMs,&options,&pendingCompletions,&physicalCapture,&presentEntryDeviation,&presentReturnDeviation,&presentationEffective,&presentationGeneration,&presentationSkippedGenerated,&presenter,&ring,&runSessionId,&seekPreviewPending,&traceFrame,&traceSubframes,watch,step,timeline,captureArrival,baselineReady,delayEnhanced,activeSeekId,lineage,jobGeneration,rereadCached,sourceIntervalMs,flow=frameFlow](int64_t now)->LiveGpuScheduler::Step{
using State=LiveGpuScheduler::State;auto& batch=watch->output;auto& s=*step;
auto updatePending=[&](LiveStepState*){
unsigned left=0;for(unsigned i=s.next;i<batch.batch.count;++i)if(batch.batch.frames[i].kind!=pipeline::FrameKind::Generated||batch.batch.frames[i].validity==pipeline::GenerationValidity::Valid)++left;
@@ -1801,18 +1828,18 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
if(isCapture){if(host100ns()<timeline.deadline(item.pts100ns))return {State::Pending,timeline.deadline(item.pts100ns)};}
else if(!fileAwaitingVideo){
if(audioStarted)audioPipe.videoReady(itemPtsMs);
- if(nowMs()+.25<itemPtsMs)return {State::Pending,now+std::min<int64_t>(10000,int64_t((itemPtsMs-nowMs())*10000))};
+ if(nowMs()+.25<itemPtsMs)return {State::Pending,now+std::min<int64_t>(10000,int64_t((itemPtsMs-nowMs())*10000/playbackRate))};
}
const auto optionalNow=host100ns();
if(!isCapture&&!fileAwaitingVideo&&options.fg&&!presentSinkFrameGeneration(options.settings.frameGenerationBackend)){
- const auto interval=std::max<int64_t>(1,int64_t(sourceIntervalMs*10000)/std::max(1u,batch.batch.count));
- const auto due=cadence.due(optionalNow+int64_t((itemPtsMs-nowMs())*10000),interval,20);
+ const auto interval=std::max<int64_t>(1,int64_t(sourceIntervalMs*10000/playbackRate)/std::max(1u,batch.batch.count));
+ const auto due=cadence.due(optionalNow+int64_t((itemPtsMs-nowMs())*10000/playbackRate),interval,20);
if(optionalNow<due)return {State::Pending,due};
}
int64_t capInterval=0;
if(presentationEffective.enabled||presentationEffective.outputRate==OutputRateMode::Custom){
- const auto interval=std::max<int64_t>(1,int64_t(sourceIntervalMs*10000)/std::max(1u,batch.batch.count));
- const auto mediaDeadline=isCapture?timeline.cadenceDeadline(item.pts100ns,optionalNow):fileAwaitingVideo?optionalNow:optionalNow+int64_t((itemPtsMs-nowMs())*10000);
+ const auto interval=std::max<int64_t>(1,int64_t(sourceIntervalMs*10000/playbackRate)/std::max(1u,batch.batch.count));
+ const auto mediaDeadline=isCapture?timeline.cadenceDeadline(item.pts100ns,optionalNow):fileAwaitingVideo?optionalNow:optionalNow+int64_t((itemPtsMs-nowMs())*10000/playbackRate);
// Media time is the deadline. The old "even" mode
// added a spacing floor on top; it measured as a
// no-op because production already lands within
@@ -1898,7 +1925,7 @@ void EngineController::run(HWND window,std::wstring path,PlayerOptions options,s
if(didPresent&&moon)moon->videoPresented(double(item.pts100ns)/10000,host100ns());
#endif
#ifdef VEYRA_ENABLE_XBOX
- if(didPresent&&xb)xb->videoPresented(double(item.pts100ns)/10000,host100ns());
+ if(didPresent&&xb)xb->videoPresented(double(item.pts100ns)/10000,host100ns(),!generated&&!rereadCached);
#endif
if(didPresent&&!generated&&!rereadCached){
const auto returned=host100ns();
diff --git a/src/sink/WasapiAudioSink.cpp b/src/sink/WasapiAudioSink.cpp
index 46aa4e3f..f92e06f8 100644
--- a/src/sink/WasapiAudioSink.cpp
+++ b/src/sink/WasapiAudioSink.cpp
@@ -10,6 +10,7 @@
#include <cstring>
#include <format>
#include <limits>
+#include <SoundTouch.h>
extern "C" {
#include <libavformat/avformat.h>
@@ -76,6 +77,7 @@ bool AudioPipeline::selectTrack(int si)
}
avcodec_free_context(&codecCtx_);codecCtx_=candidate;streamIndex_=si;stream_=fmt_->streams[si];pcmFormat_=format;
if(swr_)swr_free(&swr_);
+ resetTempo();
av_packet_unref(packet_);havePacket_=demuxEof_=drainSent_=false;decodedEof_=false;
{std::lock_guard lock(mutex_);ring_.clear();segments_.clear();ringFrames_=0;}
log::info("audio-format",std::format("file input channels={} mask=0x{:X}; preserve to renderer",pcmFormat_.channels,pcmFormat_.mask));
@@ -102,7 +104,7 @@ double AudioPipeline::tailPtsMs() const
std::lock_guard<std::mutex> lock(mutex_);
if (segments_.empty()) return -1.0;
const Segment& t = segments_.back();
- return t.startPtsMs + 1000.0 * static_cast<double>(t.frames) / kAudioRate;
+ return t.startPtsMs + static_cast<double>(t.frames) * t.stepMs;
}
uint64_t AudioPipeline::underruns() const { return underruns_.load(); }
@@ -115,6 +117,7 @@ size_t AudioPipeline::pull(float* dst, size_t maxFrames, double* firstPtsMs)
{
std::lock_guard<std::mutex> lock(mutex_);
if (firstPtsMs) *firstPtsMs = segments_.empty() ? -1.0 : segments_.front().startPtsMs;
+ lastPullEnd_=-1;
const size_t take = std::min(maxFrames, ringFrames_);
size_t copied = 0;
while (copied < take && !segments_.empty()) {
@@ -127,7 +130,8 @@ size_t AudioPipeline::pull(float* dst, size_t maxFrames, double* firstPtsMs)
}
copied += n;
seg.frames -= n;
- seg.startPtsMs += 1000.0 * static_cast<double>(n) / kAudioRate;
+ seg.startPtsMs += static_cast<double>(n) * seg.stepMs;
+ lastPullEnd_=seg.startPtsMs;
if (seg.frames == 0) segments_.pop_front();
}
ringFrames_ -= take;
@@ -235,40 +239,73 @@ void AudioPipeline::videoPresented(double nextPtsMs)
wake_.notify_all();
}
-void AudioPipeline::pushConverted(int frames)
+bool AudioPipeline::setPlaybackRate(double rate)
+{
+ if(rate!=1.0&&rate!=1.5&&rate!=2.0&&rate!=3.0)return false;
+ requestedRate_=rate;return true;
+}
+
+std::optional<double> AudioPipeline::lastPullEndPtsMs()const
{
- if (frames <= 0) return;
- size_t skip = 0;
- if (discardUntilPtsMs_ >= 0 && nextPtsMs_ < discardUntilPtsMs_) {
- skip = std::min(static_cast<size_t>(frames), static_cast<size_t>(
- std::ceil((discardUntilPtsMs_ - nextPtsMs_) * kAudioRate / 1000.0 - 1e-7)));
+ const double end=lastPullEnd_.load();
+ return activeRate_.load()!=1.0&&end>=0?std::optional<double>(end):std::nullopt;
+}
+
+void AudioPipeline::resetTempo()
+{
+ activeRate_=requestedRate_.load();tempo_.reset();tempoAnchored_=tempoFlushed_=false;lastPullEnd_=-1;
+ if(activeRate_!=1.0){
+ tempo_=std::make_unique<soundtouch::SoundTouch>();
+ tempo_->setSampleRate(kAudioRate);tempo_->setChannels(pcmFormat_.channels);
+ tempo_->setTempo(activeRate_.load());
+ tempoBuffer_.resize(4096*pcmFormat_.channels);
}
- nextPtsMs_ += 1000.0 * skip / kAudioRate;
- const size_t addFrames = static_cast<size_t>(frames) - skip;
- if (!addFrames) return;
+}
- std::unique_lock<std::mutex> lock(mutex_);
- if (ringFrames_ + addFrames > kMaxRingFrames) {
- // Player mode: never drop. This is a hard error (bounded ring
- // should never overflow because production is watermarked).
- overruns_.fetch_add(1);
- veyra::log::error("audio", "ring overflow despite watermarks (bug)");
- return;
+void AudioPipeline::pushPcm(const float* samples,size_t frames,double pts,double step)
+{
+ if(!frames)return;
+ std::lock_guard lock(mutex_);
+ if(ringFrames_+frames>kMaxRingFrames){
+ ++overruns_;log::error("audio","ring overflow despite watermarks (bug)");return;
}
- ring_.insert(ring_.end(), converted_.data() + skip * pcmFormat_.channels, converted_.data() + static_cast<size_t>(frames) * pcmFormat_.channels);
- ringFrames_ += addFrames;
- if (!segments_.empty()) {
- Segment& last = segments_.back();
- const double lastEnd = last.startPtsMs + 1000.0 * static_cast<double>(last.frames) / kAudioRate;
- if (std::fabs(lastEnd - nextPtsMs_) < 1.0) {
- last.frames += addFrames; // contiguous: extend
- } else {
- segments_.push_back({ nextPtsMs_, addFrames });
- }
- } else {
- segments_.push_back({ nextPtsMs_, addFrames });
+ ring_.insert(ring_.end(),samples,samples+frames*pcmFormat_.channels);ringFrames_+=frames;
+ if(!segments_.empty()){
+ auto& last=segments_.back();
+ if(std::abs(last.stepMs-step)<1e-9&&std::abs(last.startPtsMs+last.frames*last.stepMs-pts)<1.0){last.frames+=frames;return;}
}
- nextPtsMs_ += 1000.0 * static_cast<double>(addFrames) / kAudioRate;
+ segments_.push_back({pts,frames,step});
+}
+
+void AudioPipeline::drainTempo(bool finish)
+{
+ if(!tempo_||!tempoAnchored_)return;
+ if(finish&&!tempoFlushed_){tempo_->flush();tempoFlushed_=true;}
+ for(unsigned count;(count=tempo_->receiveSamples(tempoBuffer_.data(),4096))!=0;){
+ const double end=std::min(tempoInputEndMs_,tempoPtsMs_+count*1000.0*activeRate_.load()/kAudioRate);
+ if(end>tempoPtsMs_)pushPcm(tempoBuffer_.data(),count,tempoPtsMs_,(end-tempoPtsMs_)/count);
+ tempoPtsMs_=end;
+ }
+}
+
+void AudioPipeline::pushConverted(int frames)
+{
+ if(frames<=0)return;
+ size_t skip=0;
+ if(discardUntilPtsMs_>=0&&nextPtsMs_<discardUntilPtsMs_)
+ skip=std::min(size_t(frames),size_t(std::ceil((discardUntilPtsMs_-nextPtsMs_)*kAudioRate/1000.0-1e-7)));
+ nextPtsMs_+=1000.0*skip/kAudioRate;
+ const size_t count=size_t(frames)-skip;
+ if(!count)return;
+ const double start=nextPtsMs_;nextPtsMs_+=1000.0*count/kAudioRate;
+ const float* samples=converted_.data()+skip*pcmFormat_.channels;
+ if(!tempo_){pushPcm(samples,count,start,1000.0/kAudioRate);return;}
+ if(tempoAnchored_&&std::abs(start-tempoInputEndMs_)>1.0){
+ drainTempo(true);tempo_->clear();tempoAnchored_=tempoFlushed_=false;
+ }
+ if(!tempoAnchored_){tempoPtsMs_=start;tempoAnchored_=true;}
+ tempoInputEndMs_=nextPtsMs_;
+ tempo_->putSamples(samples,static_cast<unsigned>(count));drainTempo();
}
void AudioPipeline::decodeBlock()
@@ -291,6 +328,7 @@ void AudioPipeline::decodeBlock()
if (count > 0) { pushConverted(count); continue; }
if (count < 0) log::error("audio", std::format("resampler drain failed code={}", count));
}
+ drainTempo(true);
decodedEof_ = true;
break;
}
@@ -798,6 +836,7 @@ void AudioPipeline::runOnAudioThread(AudioRenderer* renderer, bool ownEndpoint,
if(havePacket_){av_packet_unref(packet_);havePacket_=false;}
avcodec_flush_buffers(codecCtx_);
if(swr_)swr_free(&swr_);
+ resetTempo();
// Matroska commonly indexes only video keyframes. Seeking an audio
// stream can use its sparse, previously observed packet index and
// decode minutes of audio. Seek the container's video index, then
@@ -818,6 +857,7 @@ void AudioPipeline::runOnAudioThread(AudioRenderer* renderer, bool ownEndpoint,
return true;
};
if(renderer&&ownEndpoint&&!endpointReady)recovering(renderer->lastError());
+ resetTempo();
// Initial prefill (open case).
if(initialMs>=0){if(!rewind(initialMs))recovering(E_FAIL);}
else decodeBlock();
diff --git a/src/source/CaptureCardSource.cpp b/src/source/CaptureCardSource.cpp
index 3acc233c..144044bf 100644
--- a/src/source/CaptureCardSource.cpp
+++ b/src/source/CaptureCardSource.cpp
@@ -589,7 +589,7 @@ struct CaptureCardSource::Impl:ISampleGrabberCB {
// Interframe packet PTS may move backwards in decode order.
// Apply cadence checks to decoded output, not B-frame packets.
const bool clockBreak=received&&(!compressedPath||codec==CaptureCodec::Mjpeg)&&
- (time<=previous||time-previous>(info.averageFps>0?2.5/info.averageFps:.1));
+ captureClockBreak(previous,time,info.averageFps);
if(driverBreak||clockBreak){
++discontinuitySamples;
if(discontinuitySamples<=4||discontinuitySamples%120==0)
diff --git a/src/source/XboxSessionSource.cpp b/src/source/XboxSessionSource.cpp
index c950805d..211b6b75 100644
--- a/src/source/XboxSessionSource.cpp
+++ b/src/source/XboxSessionSource.cpp
@@ -139,7 +139,7 @@ struct XboxSessionSource::Impl {
unsigned width = 1920, height = 1080;
Clock::time_point lastKeyframeRequest{};
- struct Meta { int64_t pts; int64_t arrival; };
+ struct Meta { int64_t pts,arrival; uint32_t rtp=0; int64_t submitted=0,decoded=0; };
std::deque<Meta> presented;
std::mutex presentedMutex;
@@ -446,7 +446,7 @@ void XboxSessionSource::decodeLoop() {
int64_t pts = (rtpHigh + int64_t(unit.rtp)) * 1000 / 9;
if (pts <= lastPts) pts = lastPts + 166667;
lastPts = pts;
- metas.push_back({pts, unit.arrival});
+ metas.push_back({pts,unit.arrival,unit.rtp,host100ns(),0});
if (metas.size() > 64) metas.pop_front();
const auto begin = Clock::now();
@@ -481,9 +481,11 @@ void XboxSessionSource::decodeLoop() {
p.height = unsigned(frame->height);
}
+ Impl::Meta frameMeta{pts,unit.arrival,unit.rtp,host100ns(),host100ns()};
int64_t arrival = unit.arrival;
const int64_t framePts = frame->pts != AV_NOPTS_VALUE ? frame->pts : pts;
- for (const auto& meta : metas) if (meta.pts == framePts) { arrival = meta.arrival; break; }
+ for (const auto& meta : metas) if (meta.pts == framePts) { frameMeta=meta;arrival=meta.arrival;break; }
+ frameMeta.pts=framePts;frameMeta.decoded=host100ns();
pipeline::FramePacket packet{};
packet.sourceKind = pipeline::SourceKind::Xbox;
@@ -515,7 +517,7 @@ void XboxSessionSource::decodeLoop() {
}
{
std::lock_guard lock(p.presentedMutex);
- p.presented.push_back({framePts, arrival});
+ p.presented.push_back(frameMeta);
if (p.presented.size() > 64) p.presented.pop_front();
}
{
@@ -557,12 +559,18 @@ SourceReadStatus XboxSessionSource::read(pipeline::FramePacket& packet, const AV
return SourceReadStatus::Frame;
}
-void XboxSessionSource::videoPresented(double ptsMs, int64_t host) {
+void XboxSessionSource::videoPresented(double ptsMs, int64_t host, bool sourceFrame) {
std::optional<int64_t> arrival;
if (p_) {
std::lock_guard lock(p_->presentedMutex);
const int64_t pts100 = int64_t(ptsMs * 10000.0);
- for (const auto& meta : p_->presented) if (std::llabs(meta.pts - pts100) < 5) { arrival = meta.arrival; break; }
+ for (const auto& meta : p_->presented) if (std::llabs(meta.pts - pts100) < 5) {
+ arrival=meta.arrival;
+ // A generated midpoint can coincide with a decoded-but-dropped
+ // source PTS. Never acknowledge it as that original console frame.
+ if(sourceFrame)p_->rtc.videoPresented(meta.rtp,meta.arrival,meta.submitted,meta.decoded,host);
+ break;
+ }
}
audio_.videoPresented(ptsMs, host, arrival);
}
@@ -649,7 +657,6 @@ void XboxSessionSource::close() noexcept {
std::lock_guard lock(p.inputMutex);
if (p.streaming.exchange(false) && p.padSent) p.rtc.sendGamepad(xbox::GamepadFrame{}); // release everything on the console
}
- p.rtc.close();
{
std::lock_guard lock(p.queueMutex);
p.stopDecode = true;
@@ -657,6 +664,7 @@ void XboxSessionSource::close() noexcept {
p.queueCv.notify_all();
if (p.decodeThread.joinable()) p.decodeThread.join();
if (p.keepaliveThread.joinable()) p.keepaliveThread.join();
+ p.rtc.close();
if (p.decoderOpen) { p.decoder.close(); p.decoderOpen = false; }
// End the console session so the next start does not find it busy (best effort, short timeout).
// The session's own transport is cancelled by now, and teardown must not wait on the network:
diff --git a/src/ui/PlayerUiFacade.cpp b/src/ui/PlayerUiFacade.cpp
index c1473e02..64a9383c 100644
--- a/src/ui/PlayerUiFacade.cpp
+++ b/src/ui/PlayerUiFacade.cpp
@@ -43,7 +43,7 @@ bool sameSnapshot(const veyra::engine::PlayerSnapshot& a, const veyra::engine::P
a.sourceWidth == b.sourceWidth && a.sourceHeight == b.sourceHeight &&
a.sourceDisplayAspect == b.sourceDisplayAspect && a.sourceRotationDegrees == b.sourceRotationDegrees &&
a.capture == b.capture && a.image == b.image &&
- a.vramRunawayMiB == b.vramRunawayMiB && a.vramFullscreenUnsafe == b.vramFullscreenUnsafe &&
+ a.playbackRate == b.playbackRate && a.vramRunawayMiB == b.vramRunawayMiB && a.vramFullscreenUnsafe == b.vramFullscreenUnsafe &&
a.metrics.resolution.base.width == b.metrics.resolution.base.width &&
a.metrics.resolution.base.height == b.metrics.resolution.base.height &&
a.metrics.resolution.nr.width == b.metrics.resolution.nr.width &&
diff --git a/src/ui/QmlPlayerBridge.cpp b/src/ui/QmlPlayerBridge.cpp
index 85019a2d..4113517c 100644
--- a/src/ui/QmlPlayerBridge.cpp
+++ b/src/ui/QmlPlayerBridge.cpp
@@ -764,6 +764,7 @@ QmlPlayerBridge::QmlPlayerBridge(engine::EngineController& engine, std::filesyst
connect(this,&QmlPlayerBridge::snapshotChanged,this,&QmlPlayerBridge::fgChoicesChanged);
connect(this,&QmlPlayerBridge::settingsChanged,this,&QmlPlayerBridge::fgChoicesChanged);
impl_->loadPrefs();
+ impl_->engine.setFullscreenMemoryProtection(impl_->prefBool("fullscreenMemoryProtection",false));
impl_->enumerateGpuAdapters();
impl_->resolveGpuMonitor();
connect(&impl_->exportQueue,&QmlExportQueueModel::changed,this,&QmlPlayerBridge::exportChanged);
@@ -924,7 +925,7 @@ QmlPlayerBridge::QmlPlayerBridge(engine::EngineController& engine, std::filesyst
if (impl_->resumeSession && s.sessionId == impl_->resumeSession && s.running && s.duration > 0) {
const double at = impl_->resumeAt;
impl_->resumeSession = 0;
- if (at > 5 && at < s.duration - 10) {
+ if (at >= 0.5 && at < s.duration - 0.5) {
seekTo(at);
veyra::log::info("ui-resume", std::format("resumed at {:.1f}s", at));
emit notice(tr("已从上次位置 %1 继续").arg(formatTime(at)), false);
@@ -3711,6 +3712,11 @@ void QmlPlayerBridge::setMuted(bool value) {
impl_->engine.setVolume(impl_->snapshot.volume, value);
}
double QmlPlayerBridge::playbackSpeed() const { return impl_->snapshot.playbackSpeed; }
+double QmlPlayerBridge::playbackRate() const { return impl_->snapshot.playbackRate; }
+void QmlPlayerBridge::setPlaybackRate(double rate) {
+ if(!impl_->engine.setPlaybackRate(rate))return;
+ impl_->snapshot.playbackRate=rate;emit snapshotChanged();
+}
// --- chain ------------------------------------------------------------------
// The settings the UI is showing: the facade's pending copy, which the engine
@@ -4183,6 +4189,7 @@ int QmlPlayerBridge::thumbnailGeneration() const { return impl_->thumbnailGenera
void QmlPlayerBridge::openPath(const QString& path) {
if (path.isEmpty()) return;
+ rememberPosition(true);
impl_->exportQueue.setSelectedId(0);
++impl_->liveOpenGen; impl_->pendingLiveText.clear(); // a waiting live open is dropped
impl_->exportTrimStartSeconds = 0.0;
@@ -4208,7 +4215,7 @@ void QmlPlayerBridge::openPath(const QString& path) {
// design's source menu implies.
if (impl_->currentPage.isEmpty() || impl_->currentPage == QLatin1String("home"))
emit navigate(QStringLiteral("min"));
- rememberPosition(true); // the file being left
+ rememberSource(QStringLiteral("file"), path);
if (impl_->preOpen) impl_->preOpen();
auto options = impl_->options;
options.softwareDecode = impl_->prefString("decode") == QLatin1String("software");
@@ -4216,7 +4223,7 @@ void QmlPlayerBridge::openPath(const QString& path) {
impl_->engine.open(impl_->videoWindow, wide, options);
impl_->openingSessionId = impl_->engine.snapshot().sessionId;
impl_->resumeSession = 0;
- if (impl_->prefBool("rememberPosition", true)) {
+ if ((impl_->prefBool("rememberPosition", true)||impl_->prefBool("autoResume",false))) {
const auto saved = impl_->prefs.value("positions").toMap().value(path).toMap();
if (!saved.isEmpty()) { impl_->resumeSession = impl_->openingSessionId; impl_->resumeAt = saved.value("at").toDouble(); }
}
@@ -4331,7 +4338,7 @@ bool QmlPlayerBridge::setPreference(const QString& key, const QVariant& value) {
{"subtitleMargin", {0, 240}}, {"subtitleLines", {0, 8}}};
static const QStringList flags{"subtitleEnabled", "subtitleBackground", "subtitleFit",
"subtitleSecondLanguage", "audioForceStereo", "holdCompare",
- "magewellLowLatency", "cinePillHidden", "exportStopsPlayback", "obsGameCapture"};
+ "magewellLowLatency", "cinePillHidden", "exportStopsPlayback", "obsGameCapture", "autoResume", "fullscreenMemoryProtection"};
QVariant stored;
if (ranges.contains(key)) {
bool ok = false; const int n = value.toInt(&ok);
@@ -4400,6 +4407,7 @@ bool QmlPlayerBridge::setPreference(const QString& key, const QVariant& value) {
return true;
}
void QmlPlayerBridge::applyPreference(const QString& key) {
+ if(key==QLatin1String("fullscreenMemoryProtection")){impl_->engine.setFullscreenMemoryProtection(impl_->prefBool("fullscreenMemoryProtection",false));emit snapshotChanged();}
if (key == QLatin1String("audioDevice")) sink::setPreferredRenderEndpoint(impl_->prefString("audioDevice").toStdWString());
else if (key == QLatin1String("audioForceStereo")) sink::setForceStereoDownmix(impl_->prefBool("audioForceStereo", false));
else if (key == QLatin1String("magewellLowLatency")) source::magewell::setLowLatencyPreference(impl_->prefBool("magewellLowLatency", false));
@@ -4550,10 +4558,10 @@ bool QmlPlayerBridge::eventFilter(QObject* watched, QEvent* event) {
void QmlPlayerBridge::rememberPosition(bool flush) {
const auto& s = impl_->snapshot;
const QString file = QString::fromStdWString(impl_->sourceLabel);
- if (s.running && !s.image && !s.capture && s.duration > 30 && !file.isEmpty() && QFileInfo(file).isFile()) {
+ if (s.running && !s.image && !s.capture && s.duration > 0 && !file.isEmpty() && QFileInfo(file).isFile()) {
auto positions = impl_->prefs.value("positions").toMap();
const double at = s.position;
- if (at > 5 && at < s.duration - 10) positions[file] = QVariantMap{{"at", at}, {"t", QDateTime::currentSecsSinceEpoch()}};
+ if (at >= 0.5 && at < s.duration - 0.5) positions[file] = QVariantMap{{"at", at}, {"t", QDateTime::currentSecsSinceEpoch()}};
else positions.remove(file); // finished or barely started: next time from the top
while (positions.size() > 40) {
auto oldest = positions.begin();
@@ -5623,7 +5631,7 @@ void QmlPlayerBridge::setContentRate(int value) {
}
int QmlPlayerBridge::displaySync() const { return int(impl_->presentation.display); }
-bool QmlPlayerBridge::fullscreenMemorySafe() const { return !impl_->snapshot.vramFullscreenUnsafe; }
+bool QmlPlayerBridge::fullscreenMemorySafe() const { return !impl_->prefBool("fullscreenMemoryProtection",false)||!impl_->snapshot.vramFullscreenUnsafe; }
void QmlPlayerBridge::setPresentationFullscreen(bool value) {
if(impl_->presentation.fullscreen==value)return;
impl_->presentation.fullscreen=value;
@@ -5769,7 +5777,9 @@ QString QmlPlayerBridge::exportSizeEstimate() const {
QVariantMap QmlPlayerBridge::lastSource() const {
auto saved = impl_->prefs.value(QStringLiteral("lastSource")).toMap();
const QString kind = saved.value(QStringLiteral("kind")).toString();
- if (kind == QLatin1String("capture")) {
+ if (kind == QLatin1String("file")) {
+ saved[QStringLiteral("summary")]=QFileInfo(saved.value(QStringLiteral("label")).toString()).fileName();
+ } else if (kind == QLatin1String("capture")) {
if (!hasCaptureSession()) return {};
saved[QStringLiteral("summary")] = captureSessionSummary();
} else if (kind == QLatin1String("ps5")) {
@@ -5787,8 +5797,7 @@ QVariantMap QmlPlayerBridge::lastSource() const {
return saved;
}
void QmlPlayerBridge::rememberSource(const QString& kind, const QString& label) {
- impl_->liveKind = kind;
- impl_->liveLabel = label;
+ if(kind!=QLatin1String("file")){impl_->liveKind = kind;impl_->liveLabel = label;}
QVariantMap value{{"kind", kind}, {"label", label}};
if (kind == QLatin1String("screen")) value[QStringLiteral("screenKind")] = screenOptions().value(QStringLiteral("kind")).toInt();
if (impl_->prefs.value(QStringLiteral("lastSource")).toMap() == value) return;
@@ -5796,10 +5805,21 @@ void QmlPlayerBridge::rememberSource(const QString& kind, const QString& label)
if (!impl_->savePrefs()) veyra::log::warn("ui-prefs", "last source not saved");
emit preferencesChanged();
}
+void QmlPlayerBridge::autoResumeLastSource() {
+ if(!impl_->prefBool("autoResume",false)||impl_->openingSource||impl_->snapshot.running)return;
+ if(lastSource().isEmpty())return;
+ veyra::log::info("ui-startup","automatically resuming the last source");
+ resumeLastSource();
+}
void QmlPlayerBridge::resumeLastSource() {
const auto last = lastSource();
const QString kind = last.value(QStringLiteral("kind")).toString();
veyra::log::info("ui-resume-source", "kind=" + kind.toStdString());
+ if(kind==QLatin1String("file")){
+ const QString path=last.value(QStringLiteral("label")).toString();
+ if(!QFileInfo(path).isFile()){emit notice(tr("上次的视频文件不存在:%1").arg(path),true);return;}
+ openPath(path);return;
+ }
if (kind == QLatin1String("capture")) { resumeCaptureSession(); return; }
if (kind == QLatin1String("ps5")) {
#ifdef VEYRA_ENABLE_REMOTEPLAY
diff --git a/src/xbox/WebRtcSession.cpp b/src/xbox/WebRtcSession.cpp
index 574884fa..5afa0740 100644
--- a/src/xbox/WebRtcSession.cpp
+++ b/src/xbox/WebRtcSession.cpp
@@ -51,7 +51,7 @@ void logOnce() {
std::call_once(once, [] {
// VEYRA_XBOX_RTC_DEBUG=1 logs libdatachannel's ICE/DTLS/SCTP details (for field diagnosis).
const bool debug = GetEnvironmentVariableA("VEYRA_XBOX_RTC_DEBUG", nullptr, 0) > 0;
- rtc::InitLogger(debug ? rtc::LogLevel::Verbose : rtc::LogLevel::Warning, [](rtc::LogLevel level, std::string text) {
+ rtc::InitLogger(debug ? rtc::LogLevel::Verbose : rtc::LogLevel::Info, [](rtc::LogLevel level, std::string text) {
if (level <= rtc::LogLevel::Error) log::warn("xbox-rtc", text);
else log::info("xbox-rtc", text);
});
@@ -105,7 +105,8 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
if (!bindAddress_.empty()) config.bindAddress = bindAddress_;
pc_ = std::make_shared<rtc::PeerConnection>(config);
- pc_->onStateChange([this](rtc::PeerConnection::State state) {
+ pc_->onStateChange([this, gate=gate_](rtc::PeerConnection::State state) {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
log::info("xbox", std::string("peer connection state ") + std::to_string(int(state)));
if (state == rtc::PeerConnection::State::Connected) connected_ = true;
if (state == rtc::PeerConnection::State::Failed || state == rtc::PeerConnection::State::Closed ||
@@ -116,7 +117,8 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
}
changed_.notify_all();
});
- pc_->onGatheringStateChange([this](rtc::PeerConnection::GatheringState) { changed_.notify_all(); });
+ pc_->onGatheringStateChange([this, gate=gate_](rtc::PeerConnection::GatheringState) { std::lock_guard lock(gate->mutex);if(gate->open)changed_.notify_all(); });
+ pc_->onIceStateChange([](rtc::PeerConnection::IceState state){log::info("xbox","ICE state "+std::to_string(int(state)));});
// Audio (send-receive, as the reference client; nothing is sent) and video (receive only, H.264
// in the reference client's order: Main, Constrained Baseline, Baseline).
@@ -132,7 +134,8 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
auto depacketizer = std::make_shared<rtc::H264RtpDepacketizer>(rtc::NalUnit::Separator::LongStartSequence);
video_->setMediaHandler(depacketizer);
video_->chainMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());
- video_->onFrame([this](rtc::binary data, rtc::FrameInfo info) {
+ video_->onFrame([this, gate=gate_](rtc::binary data, rtc::FrameInfo info) {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
++videoUnits_;
videoBytes_ += data.size();
if (!callbacks_.video) return;
@@ -142,7 +145,8 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
});
audio_->setMediaHandler(std::make_shared<rtc::OpusRtpDepacketizer>());
audio_->chainMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());
- audio_->onFrame([this](rtc::binary data, rtc::FrameInfo info) {
+ audio_->onFrame([this, gate=gate_](rtc::binary data, rtc::FrameInfo info) {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
++audioPackets_;
if (callbacks_.audio) callbacks_.audio(reinterpret_cast<const uint8_t*>(data.data()), data.size(), info.timestamp);
});
@@ -160,7 +164,8 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
// The handshake starts once every channel is open: its acknowledgement triggers sends on control and
// input, which would be dropped by a channel still opening.
- const auto maybeHandshake = [this] {
+ const auto maybeHandshake = [this, gate=gate_] {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
if (!message_ || !control_ || !input_ || !chat_) return;
if (!message_->isOpen() || !control_->isOpen() || !input_->isOpen() || !chat_->isOpen()) return;
if (handshakeSent_.exchange(true)) return;
@@ -168,12 +173,14 @@ bool WebRtcSession::start(Signaling& signaling, std::string* error, std::chrono:
sendMessageJson(json{{"type", "Handshake"}, {"version", "messageV1"}, {"id", "be0bfc6d-1e83-4c8a-90ed-fa8601c5a179"}, {"cv", "0"}}.dump());
};
for (const auto& dc : {chat_, control_, input_, message_}) dc->onOpen(maybeHandshake);
- message_->onMessage([this](rtc::message_variant data) {
+ message_->onMessage([this, gate=gate_](rtc::message_variant data) {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
if (const auto* text = std::get_if<std::string>(&data)) onMessageChannel(*text);
else if (const auto* bin = std::get_if<rtc::binary>(&data))
onMessageChannel(std::string(reinterpret_cast<const char*>(bin->data()), bin->size()));
});
- input_->onMessage([this](rtc::message_variant data) {
+ input_->onMessage([this, gate=gate_](rtc::message_variant data) {
+ std::lock_guard callbackLock(gate->mutex);if(!gate->open)return;
if (const auto* bin = std::get_if<rtc::binary>(&data)) onInputChannel(reinterpret_cast<const uint8_t*>(bin->data()), bin->size());
});
control_->onMessage([](rtc::message_variant data) {
@@ -247,41 +254,53 @@ bool WebRtcSession::waitReady(std::chrono::milliseconds timeout) {
return changed_.wait_for(lock, timeout, [&] { return handshakeDone_.load() || failed_.load(); }) && handshakeDone_;
}
-void WebRtcSession::sendMessageJson(const std::string& text) {
- if (message_ && message_->isOpen()) message_->send(rtc::binary(reinterpret_cast<const std::byte*>(text.data()), reinterpret_cast<const std::byte*>(text.data()) + text.size()));
+void WebRtcSession::sendFailure(const std::exception& error) {
+ if(!sendErrorLogged_){sendErrorLogged_=true;log::warn("xbox",std::string("transport send stopped: ")+error.what());}
+}
+bool WebRtcSession::sendBinary(const std::shared_ptr<rtc::DataChannel>& channel,const uint8_t* data,size_t size) {
+ if(closed_||!channel||!channel->isOpen())return false;
+ // isOpen() is only a snapshot. The transport may close before send().
+ try {channel->send(reinterpret_cast<const std::byte*>(data),size);return true;}
+ catch(const std::exception& error){sendFailure(error);return false;}
+}
+bool WebRtcSession::sendMessageJson(const std::string& text) {
+ return sendBinary(message_,reinterpret_cast<const uint8_t*>(text.data()),text.size());
}
-void WebRtcSession::sendClientConfig() {
- sendMessageJson(message("/streaming/systemUi/configuration", {{"version", {0, 2, 0}}, {"systemUis", json::array()}}));
- sendMessageJson(message("/streaming/properties/clientappinstallidchanged", {{"clientAppInstallId", "c97d7ee0-73b2-4239-bf1d-9d805a338429"}}));
- sendMessageJson(message("/streaming/characteristics/orientationchanged", {{"orientation", 0}}));
- sendMessageJson(message("/streaming/characteristics/touchinputenabledchanged", {{"touchInputEnabled", false}}));
- sendMessageJson(message("/streaming/characteristics/clientdevicecapabilities", json::object()));
- sendMessageJson(message("/streaming/characteristics/dimensionschanged", {
+bool WebRtcSession::sendClientConfig() {
+ bool sent=true;
+ sent &= sendMessageJson(message("/streaming/systemUi/configuration", {{"version", {0, 2, 0}}, {"systemUis", json::array()}}));
+ sent &= sendMessageJson(message("/streaming/properties/clientappinstallidchanged", {{"clientAppInstallId", "c97d7ee0-73b2-4239-bf1d-9d805a338429"}}));
+ sent &= sendMessageJson(message("/streaming/characteristics/orientationchanged", {{"orientation", 0}}));
+ sent &= sendMessageJson(message("/streaming/characteristics/touchinputenabledchanged", {{"touchInputEnabled", false}}));
+ sent &= sendMessageJson(message("/streaming/characteristics/clientdevicecapabilities", json::object()));
+ sent &= sendMessageJson(message("/streaming/characteristics/dimensionschanged", {
{"horizontal", 1920}, {"vertical", 1080}, {"preferredWidth", 1920}, {"preferredHeight", 1080},
{"safeAreaLeft", 0}, {"safeAreaTop", 0}, {"safeAreaRight", 1920}, {"safeAreaBottom", 1080}, {"supportsCustomResolution", true}}));
+ return sent;
}
void WebRtcSession::onMessageChannel(const std::string& text) {
json j;
try { j = json::parse(text); } catch (const json::exception&) { log::warn("xbox", "message channel: not JSON"); return; }
+ try {
const std::string type = j.value("type", "");
if (type == "HandshakeAck") {
log::info("xbox", "handshake acknowledged");
// Control: authorise, then announce pad 0 (the reference client adds, removes and re-adds it).
const auto sendControl = [this](const std::string& s) {
- if (control_ && control_->isOpen()) control_->send(rtc::binary(reinterpret_cast<const std::byte*>(s.data()), reinterpret_cast<const std::byte*>(s.data()) + s.size()));
+ return sendBinary(control_,reinterpret_cast<const uint8_t*>(s.data()),s.size());
};
- sendControl(json{{"message", "authorizationRequest"}, {"accessKey", "4BDB3609-C1F1-4195-9B37-FEFF45DA8B8E"}}.dump());
- sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", true}}.dump());
- sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", false}}.dump());
- sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", true}}.dump());
+ bool initialized=sendControl(json{{"message", "authorizationRequest"}, {"accessKey", "4BDB3609-C1F1-4195-9B37-FEFF45DA8B8E"}}.dump());
+ initialized &= sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", true}}.dump());
+ initialized &= sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", false}}.dump());
+ initialized &= sendControl(json{{"message", "gamepadChanged"}, {"gamepadIndex", 0}, {"wasAdded", true}}.dump());
if (input_ && input_->isOpen()) {
const auto report = clientMetadataReport(0, nowMs(), 1);
- input_->send(reinterpret_cast<const std::byte*>(report.data()), report.size());
+ initialized &= sendBinary(input_,report.data(),report.size());
}
- sendClientConfig();
- handshakeDone_ = true;
+ initialized &= sendClientConfig();
+ handshakeDone_ = initialized;
changed_.notify_all();
return;
}
@@ -301,6 +320,7 @@ void WebRtcSession::onMessageChannel(const std::string& text) {
return;
}
log::info("xbox-message", "unhandled " + type);
+ } catch(const json::exception&){log::warn("xbox","message channel: invalid field type");}
}
void WebRtcSession::onInputChannel(const uint8_t* data, size_t size) {
@@ -317,22 +337,43 @@ void WebRtcSession::onInputChannel(const uint8_t* data, size_t size) {
}
void WebRtcSession::sendGamepad(const GamepadFrame& frame) {
- if (!handshakeDone_ || !input_ || !input_->isOpen()) return;
- const auto report = gamepadReport(++inputSequence_, nowMs(), {frame});
- input_->send(reinterpret_cast<const std::byte*>(report.data()), report.size());
+ std::lock_guard lock(gate_->mutex);
+ if(!gate_->open||!handshakeDone_)return;
+ const auto report=gamepadReport(++inputSequence_,nowMs(),{frame});
+ sendBinary(input_,report.data(),report.size());
+}
+
+void WebRtcSession::videoPresented(uint32_t rtp,int64_t arrival,int64_t submitted,int64_t decoded,int64_t presented) {
+ std::lock_guard lock(gate_->mutex);
+ if(!gate_->open||!handshakeDone_||(feedbackSent_&&rtp==lastFeedbackRtp_))return;
+ const auto origin=std::chrono::duration_cast<std::chrono::nanoseconds>(origin_.time_since_epoch()).count()/100;
+ const auto ms=[origin](int64_t stamp){return uint32_t(uint64_t(std::max<int64_t>(0,stamp-origin)/10000));};
+ const auto report=videoFeedbackReport(++inputSequence_,nowMs(),{rtp,ms(arrival),ms(submitted),ms(decoded),ms(presented)});
+ if(sendBinary(input_,report.data(),report.size())){
+ if(!feedbackSent_)log::info("xbox","video presentation feedback started");
+ feedbackSent_=true;lastFeedbackRtp_=rtp;
+ }
}
void WebRtcSession::requestKeyframe() {
+ std::lock_guard lock(gate_->mutex);
+ if(!gate_->open||closed_)return;
++keyframeRequests_;
- if (video_) video_->requestKeyframe();
- if (control_ && control_->isOpen() && handshakeDone_) {
- const std::string s = json{{"message", "videoKeyframeRequested"}, {"ifrRequested", true}}.dump();
- control_->send(rtc::binary(reinterpret_cast<const std::byte*>(s.data()), reinterpret_cast<const std::byte*>(s.data()) + s.size()));
+ try{if(video_)video_->requestKeyframe();}catch(const std::exception& error){sendFailure(error);}
+ if(handshakeDone_){
+ const std::string s=json{{"message","videoKeyframeRequested"},{"ifrRequested",true}}.dump();
+ sendBinary(control_,reinterpret_cast<const uint8_t*>(s.data()),s.size());
}
}
void WebRtcSession::close() {
if (closed_.exchange(true)) return;
+ {std::lock_guard lock(gate_->mutex);gate_->open=false;handshakeDone_=false;connected_=false;}
+ // Wait for current callbacks via the gate, then detach callbacks before
+ // releasing channels. Do not hold the gate while library callbacks reset.
+ for(const auto& dc:{chat_,control_,input_,message_})if(dc)dc->resetCallbacks();
+ if(audio_)audio_->resetCallbacks();if(video_)video_->resetCallbacks();
+ if(pc_)pc_->resetCallbacks();
if (pc_) {
try { pc_->close(); } catch (...) {}
}
@@ -343,13 +384,14 @@ void WebRtcSession::close() {
}
WebRtcStats WebRtcSession::stats() const {
+ std::lock_guard lock(gate_->mutex);
WebRtcStats s;
s.videoUnits = videoUnits_.load();
s.audioPackets = audioPackets_.load();
s.keyframeRequests = keyframeRequests_.load();
s.videoBytes = videoBytes_.load();
s.handshakeDone = handshakeDone_.load();
- if (const auto pc = pc_) {
+ if (const auto pc = gate_->open?pc_:nullptr) {
s.bytesReceived = pc->bytesReceived();
if (const auto rtt = pc->rtt()) s.rttMs = double(rtt->count());
}
diff --git a/tests/integration/AudioPlaybackRateTests.cpp b/tests/integration/AudioPlaybackRateTests.cpp
new file mode 100644
index 00000000..53bc2ee6
--- /dev/null
+++ b/tests/integration/AudioPlaybackRateTests.cpp
@@ -0,0 +1,46 @@
+#include "veyra/sink/WasapiAudioSink.h"
+#include <chrono>
+#include <cmath>
+#include <iostream>
+#include <thread>
+#include <vector>
+
+// A five-second 440 Hz PCM fixture. Exercise the product decoder, bounded
+// buffer, tempo processor and media-PTS mapping, including seek/reuse.
+int wmain(int argc,wchar_t** argv){
+ if(argc!=2)return 2;
+ CoInitializeEx(nullptr,COINIT_MULTITHREADED);
+ using Clock=std::chrono::steady_clock;
+ bool all=true;
+ for(double rate:{1.,1.5,2.,3.})for(double start:{0.,1200.}){
+ veyra::sink::AudioPipeline audio;
+ if(!audio.open(argv[1])||!audio.setPlaybackRate(rate))return 1;
+ audio.setPaused(false);audio.startThread(nullptr,false,start);
+ const unsigned channels=audio.pcmFormat().channels;
+ std::vector<float> block(997*channels),pcm;
+ double first=-1,last=-1;bool continuous=true;
+ const auto deadline=Clock::now()+std::chrono::seconds(15);
+ while(Clock::now()<deadline){
+ double pts=-1;const auto n=audio.pull(block.data(),997,&pts);
+ if(n){
+ if(first<0)first=pts;
+ if(last>=0&&std::abs(pts-last)>1.)continuous=false;
+ last=audio.lastPullEndPtsMs().value_or(pts+1000.*n/48000.);
+ for(size_t i=0;i<n;++i)pcm.push_back(block[i*channels]);
+ }else if(audio.decodingComplete())break;
+ else std::this_thread::sleep_for(std::chrono::milliseconds(1));
+ }
+ audio.stopThread();
+ unsigned crossings=0;double energy=0;
+ for(size_t i=1;i<pcm.size();++i){if(pcm[i]>=0&&pcm[i-1]<0)++crossings;energy+=pcm[i]*pcm[i];}
+ const double hz=pcm.empty()?0:48000.*crossings/pcm.size();
+ const double duration=1000.*pcm.size()/48000.;
+ const bool ok=audio.decodingComplete()&&audio.overruns()==0&&continuous&&
+ std::abs(first-start)<1&&std::abs(last-5000)<2&&
+ std::abs(duration-(5000-start)/rate)<3&&std::abs(hz-440)<3&&energy>10;
+ all&=ok;
+ std::cout<<(ok?"PASS ":"FAIL ")<<"rate="<<rate<<" start="<<start<<" outputMs="<<duration
+ <<" media="<<first<<".."<<last<<" Hz="<<hz<<" continuous="<<continuous<<" overruns="<<audio.overruns()<<std::endl;
+ }
+ CoUninitialize();return all?0:1;
+}
diff --git a/tests/qml/quick/tst_playback_rate.qml b/tests/qml/quick/tst_playback_rate.qml
new file mode 100644
index 00000000..642ee458
--- /dev/null
+++ b/tests/qml/quick/tst_playback_rate.qml
@@ -0,0 +1,49 @@
+import QtQuick
+import QtTest
+import Veyra
+
+Item {
+ id: root
+ width: 600; height: 400
+ QtObject {
+ id: veyra
+ property bool hasSource: true
+ property bool isCapture: false
+ property bool running: true
+ property real duration: 100
+ property real playbackRate: 1
+ function logUi(category, text) { }
+ }
+ PlaybackRateButton { id: rate; objectName: "test-rate"; x: 200; y: 220 }
+ function byText(item, text) {
+ if (item.text === text) return item
+ for (const child of item.children || []) {
+ const found = byText(child, text)
+ if (found) return found
+ }
+ return null
+ }
+ TestCase {
+ name: "PlaybackRate"
+ when: windowShown
+ function test_rates() {
+ for (const value of [1.5, 2, 3, 1]) {
+ mouseClick(rate, rate.width / 2, rate.height / 2)
+ const menu = findChild(rate, "test-rate-menu")
+ verify(menu !== null)
+ tryCompare(menu, "visible", true)
+ wait(500)
+ const label = root.byText(menu.contentItem, value + "×")
+ verify(label !== null)
+ mouseClick(label, label.width / 2, label.height / 2)
+ tryCompare(veyra, "playbackRate", value)
+ compare(rate.rateLabel, value + "×")
+ }
+ veyra.isCapture = true
+ compare(rate.visible, false)
+ veyra.isCapture = false
+ veyra.duration = 0
+ compare(rate.visible, false)
+ }
+ }
+}
diff --git a/tests/unit/LivePresentationTimingTests.cpp b/tests/unit/LivePresentationTimingTests.cpp
index ce4977ca..dd2736f6 100644
--- a/tests/unit/LivePresentationTimingTests.cpp
+++ b/tests/unit/LivePresentationTimingTests.cpp
@@ -84,6 +84,23 @@ int main(){
check(captureDiscontinuity(false,false,true,true,1,1+1.0/60,60),"driver discontinuity retained at normal cadence");
check(captureDiscontinuity(false,false,false,true,1,0.5,60),"backwards callback clock remains hard discontinuity");
check(captureDiscontinuity(false,false,false,true,1,1.2,60),"callback gap remains hard discontinuity");
+ driverFlag.reset();
+ unsigned resets=0;double vrrPts=0;bool vrrContinuous=true;
+ for(int i=0;i<2400;++i){
+ const double delta=1./(i%4==0?48.:i%4==1?120.:i%4==2?60.:90.);
+ const bool boundary=driverFlag.observe(true,true,true,i>0,delta,delta+.0002,120);
+ resets+=boundary;
+ vrrContinuous&=!veyra::source::captureClockBreak(vrrPts,vrrPts+delta,120);
+ const auto interval=veyra::engine::capturePairInterval100ns({83333,10000000},120,vrrPts*1000,(vrrPts+delta)*1000,false);
+ if(std::abs(interval-delta*1e7)>1)++failed;
+ vrrPts+=delta;
+ }
+ check(vrrContinuous,"2400 variable 48-120 Hz intervals preserve source history");
+ check(resets==3,"120 Hz format with 48-120 Hz VRR exits persistent driver-flag warmup");
+ check(driverFlag.observe(true,true,true,true,.2,.2,120),"VRR still resets on a real 200 ms stall");
+ check(driverFlag.observe(true,true,true,true,.02,.09,120),"VRR does not hide source/arrival disagreement");
+ check(veyra::source::captureClockBreak(1,.9,120),"VRR preserves backward-clock reset");
+ check(veyra::engine::capturePairInterval100ns({83333,10000000},120,0,20,true)==83333,"reset does not reuse stale A/B interval");
constexpr auto drop=static_cast<veyra::pipeline::FrameFlags>(veyra::pipeline::FrameFlagBits::Drop);
constexpr auto resize=static_cast<veyra::pipeline::FrameFlags>(veyra::pipeline::FrameFlagBits::Resize);
check(veyra::pipeline::breaksHistory(drop),"mailbox overwrite resets temporal history");
diff --git a/tests/xbox/XboxTests.cpp b/tests/xbox/XboxTests.cpp
index e1d00a47..73d745bf 100644
--- a/tests/xbox/XboxTests.cpp
+++ b/tests/xbox/XboxTests.cpp
@@ -499,8 +499,33 @@ void webrtcTests() {
if (!(stats.videoUnits >= 2 && stats.audioPackets >= 2 && stats.handshakeDone && stats.videoBytes > 5000))
std::printf("stats: units=%llu audio=%llu handshake=%d bytes=%llu\n", (unsigned long long)stats.videoUnits, (unsigned long long)stats.audioPackets, int(stats.handshakeDone), (unsigned long long)stats.bytesReceived);
check(stats.videoUnits >= 2 && stats.audioPackets >= 2 && stats.handshakeDone && stats.videoBytes > 5000, "statistics");
- session.close();
- check(!session.ready() || true, "close() returns");
+ // Malformed fields used to throw through the asynchronous callback.
+ console.message->send(std::string("{\"type\":null}"));
+ console.message->send(std::string("{\"type\":\"Message\",\"target\":null}"));
+ session.videoPresented(0xFABCDE12,1000000,1100000,1200000,1300000);
+ check(console.waitFor([&]{for(const auto& report:console.reports)if(report.size()==43&&report[0]==ReportMetadata)return true;return false;}),"rendered video feedback is sent even without a gamepad change");
+ {
+ std::lock_guard lock(console.mutex);
+ bool valid=false;
+ for(const auto& report:console.reports)if(report.size()==43&&report[0]==ReportMetadata)
+ valid=report[14]==1&&detail::get32(report.data()+15)==0xFABCDE12;
+ check(valid,"feedback preserves the original RTP key and count");
+ }
+ const auto encoded=videoFeedbackReport(7,1234,{0xFABCDE12,101,102,103,104});
+ check(encoded.size()==43&&detail::get32(encoded.data()+2)==7&&detail::get32(encoded.data()+19)==101&&
+ detail::get32(encoded.data()+23)==102&&detail::get32(encoded.data()+27)==103&&
+ detail::get32(encoded.data()+31)==104&&detail::get32(encoded.data()+35)==1234&&
+ detail::get32(encoded.data()+39)==1234,"frame feedback matches Greenlight's seven-field wire format");
+ // Exercise real transport shutdown while the caller sends input/keyframe
+ // requests. This covers both the isOpen/send race and local close racing
+ // callbacks; exceptions escaping any thread terminate this test process.
+ std::atomic<bool> stopped=false;
+ std::thread sender([&]{while(!stopped){session.sendGamepad(pad);session.requestKeyframe();std::this_thread::sleep_for(std::chrono::milliseconds(1));}});
+ console.pc->close();
+ std::this_thread::sleep_for(std::chrono::milliseconds(50));
+ session.close();stopped=true;sender.join();
+ session.sendGamepad(pad);session.requestKeyframe();session.close();
+ check(!session.ready(), "remote/local close with active sends is contained and clears readiness");
}
} // namespace
diff --git a/third_party/soundtouch/CMakeLists.txt b/third_party/soundtouch/CMakeLists.txt
new file mode 100644
index 00000000..d0ea0734
--- /dev/null
+++ b/third_party/soundtouch/CMakeLists.txt
@@ -0,0 +1,19 @@
+# SoundTouch 2.4.1, LGPL-2.1. Fixed source identity: VEYRA_ORIGIN.md.
+add_library(veyra_soundtouch STATIC
+ source/SoundTouch/AAFilter.cpp
+ source/SoundTouch/BPMDetect.cpp
+ source/SoundTouch/cpu_detect_x86.cpp
+ source/SoundTouch/FIFOSampleBuffer.cpp
+ source/SoundTouch/FIRFilter.cpp
+ source/SoundTouch/InterpolateCubic.cpp
+ source/SoundTouch/InterpolateLinear.cpp
+ source/SoundTouch/InterpolateShannon.cpp
+ source/SoundTouch/mmx_optimized.cpp
+ source/SoundTouch/PeakFinder.cpp
+ source/SoundTouch/RateTransposer.cpp
+ source/SoundTouch/SoundTouch.cpp
+ source/SoundTouch/sse_optimized.cpp
+ source/SoundTouch/TDStretch.cpp)
+target_include_directories(veyra_soundtouch PUBLIC include)
+target_compile_definitions(veyra_soundtouch PUBLIC SOUNDTOUCH_FLOAT_SAMPLES)
+target_compile_features(veyra_soundtouch PRIVATE cxx_std_17)
diff --git a/third_party/soundtouch/COPYING.TXT b/third_party/soundtouch/COPYING.TXT
new file mode 100644
index 00000000..c04d4d80
--- /dev/null
+++ b/third_party/soundtouch/COPYING.TXT
@@ -0,0 +1,458 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
\ No newline at end of file
diff --git a/third_party/soundtouch/README.html b/third_party/soundtouch/README.html
new file mode 100644
index 00000000..2f33d045
--- /dev/null
+++ b/third_party/soundtouch/README.html
@@ -0,0 +1,1037 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+
+<head>
+ <title>SoundTouch library README</title>
+ <meta http-equiv="Content-Language" content="en-us">
+ <meta name="author" content="Olli Parviainen">
+ <meta name="description" content="Readme file for SoundTouch audio processing library">
+ <style>
+ body {
+ font-family: Arial, Helvetica;
+ }
+ </style>
+</head>
+
+<body class="normal">
+ <hr>
+ <h1>SoundTouch audio processing library v2.4.0</h1>
+ <p class="normal">SoundTouch library Copyright &copy; Olli Parviainen 2001-2025</p>
+ <hr>
+ <h2>1. Introduction </h2>
+ <p>SoundTouch is an open-source audio processing library that allows
+ changing the sound tempo, pitch and playback rate parameters
+ independently from each other, i.e.:</p>
+ <ul>
+ <li> Sound tempo can be increased or decreased while maintaining the
+ original pitch</li>
+ <li> Sound pitch can be increased or decreased while maintaining the
+ original tempo</li>
+ <li> Change playback rate that affects both tempo and pitch at the
+ same time</li>
+ <li> Choose any combination of tempo/pitch/rate</li>
+ </ul>
+ <h3>1.1 Contact information </h3>
+ <p>Author email: oparviai 'at' iki.fi </p>
+ <p>SoundTouch WWW page: <a href="http://soundtouch.surina.net">http://soundtouch.surina.net</a></p>
+ <p>SoundTouch git repository: <a
+ href="https://codeberg.org/soundtouch/soundtouch.git">https://codeberg.org/soundtouch/soundtouch.git</a></p>
+ <hr>
+ <h2>2. Compiling SoundTouch</h2>
+ <p>Before compiling, notice that you can choose the sample data format if it's
+ desirable to use 16bit integer sample data instead of floating point samples. See
+ section &quot;sample data format&quot; for more information.</p>
+ <p>Also notice that SoundTouch can use OpenMP instructions for parallel
+ computation to accelerate the runtime processing speed in multi-core systems,
+ however, these improvements need to be separately enabled before compiling. See
+ OpenMP notes in Chapter 3 below.</p>
+ <h3>2.1. Building in Microsoft Windows</h3>
+ <p>Project files for Microsoft Visual C++ are supplied with the source
+ code package. Go to Microsoft WWW page to download
+ <a href="http://www.visualstudio.com/en-US/products/visual-studio-express-vs">
+ Microsoft Visual Studio Express version for free</a>.
+ </p>
+ <p>To build the binaries with Visual C++ compiler, either run
+ "make-win.bat" script, or open the appropriate project files in source
+ code directories with Visual Studio. The final executable will appear
+ under the "SoundTouch\bin" directory. If using the Visual Studio IDE
+ instead of the make-win.bat script, directories bin and lib may need to
+ be created manually to the SoundTouch package root for the final
+ executables. The make-win.bat script creates these directories
+ automatically. </p>
+ <p><strong>C# example</strong>: The source code package includes also a C# example
+ application for Windows that shows how to invoke SoundTouch.dll
+ dynamic-load library for processing mp3 audio.
+ <p><strong>OpenMP NOTE</strong>: If activating the OpenMP parallel computing in
+ the compilation, the target program will require additional vcomp dll library to
+ properly run. In Visual C++ 9.0 these libraries can be found in the following
+ folders.</p>
+ <ul>
+ <li>x86 32bit: C:\Program Files (x86)\Microsoft Visual Studio
+ 9.0\VC\redist\x86\Microsoft.VC90.OPENMP\vcomp90.dll</li>
+ <li>x64 64bit: C:\Program Files (x86)\Microsoft Visual Studio
+ 9.0\VC\redist\amd64\Microsoft.VC90.OPENMP\vcomp90.dll</li>
+ </ul>
+ <p>In other VC++ versions the required library will be expectedly found in similar
+ &quot;redist&quot; location.</p>
+ <p>Notice that as minor demonstration of a &quot;dll hell&quot; phenomenon both the 32-bit
+ and 64-bit version of vcomp90.dll have the same filename but different contents,
+ thus choose the proper version to allow the program to start.</p>
+ <h3>2.2. Building in Gnu platforms</h3>
+ <p>The SoundTouch library compiles in practically any platform
+ supporting GNU compiler (GCC) tools.
+ <h4>2.2.1 Compiling with autotools</h4>
+ <p>To install build prerequisites for 'autotools' tool chain (for Ubuntu/Debian. Use dnf/yum/etc in other distros):</p>
+ <pre> sudo apt install -y automake autoconf libtool build-essential</pre>
+ <p>To build and install the binaries, run the following commands in
+ /soundtouch directory:</p>
+ <table border="0" cellpadding="0" cellspacing="4">
+ <tbody>
+ <tr>
+ <td style="vertical-align: top;">
+ <pre>./bootstrap -</pre>
+ </td>
+ <td style="vertical-align: top;">Creates "configure" file with
+ local autoconf/automake toolset.<br>
+ </td>
+ </tr>
+ <tr valign="top">
+ <td>
+ <pre>./configure -</pre>
+ </td>
+ <td>
+ <p>Configures the SoundTouch package for the local environment.
+ Notice that "configure" file is not available before running the
+ "./bootstrap" command as above.<br>
+ </p>
+ </td>
+ </tr>
+ <tr valign="top">
+ <td>
+ <pre>make -</pre>
+ </td>
+ <td>
+ <p>Builds the SoundTouch library &amp; SoundStretch utility. You can
+ optionally add &quot;-j&quot; switch after &quot;make&quot; to speed up the compilation in
+ multi-core systems.</p>
+ </td>
+ </tr>
+ <tr valign="top">
+ <td>
+ <pre>make install -</pre>
+ </td>
+ <td>
+ <p>Installs the SoundTouch &amp; BPM libraries to <b>/usr/local/lib</b>
+ and SoundStretch utility to <b>/usr/local/bin</b>. Please notice that
+ 'root' privileges may be required to install the binaries to the
+ destination locations.</p>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+
+ <b>Compiling portable Shared Library / DLL version</b>
+ <p> The GNU autotools compilation automatically builds an additional dynamic-link version
+ of SoundTouch library that features position-independent code and "C"-style API that is
+ more suitable for calling the SoundTouch routines from other programming languages.</p>
+ <p>This dynamic-link library is built under source/SoundTouchDLL directory, whose
+ subdirectories also comtain simple example apps that use the dynamic-link library.
+ </p>
+
+ <h4><b>2.2.2 Compiling with cmake</b></h4>
+ <p>'cmake' build scripts are provided as an alternative to the autotools toolchain.</p>
+ <p>To install cmake build prerequisites (for Ubuntu/Debian. Use dnf/yum/etc in other distros):</p>
+ <pre> sudo apt install -y libtool build-essential cmake</pre>
+ <p>To build:</p>
+ <pre>
+ cmake .
+ make -j
+ make install</pre>
+ <p>To list available build options:</p>
+ <pre>
+ cmake -LH</pre>
+ <p>To compile the additional portable Shared Library / DLL version with the native C-language API:</p>
+ <pre>
+ cmake . -DSOUNDTOUCH_DLL=ON
+ make -j
+ make install</pre>
+
+ <h3>2.3. Building in Android</h3>
+ <p>Android compilation instructions are within the
+ source code package, see file &quot;<b>source/Android-lib/README-SoundTouch-Android.html</b>&quot;
+ in the source code package. </p>
+ <p>The Android compilation automatically builds separate .so library binaries
+ for ARM, X86 and MIPS processor architectures. For optimal device support,
+ include all these .so library binaries into the Android .apk application
+ package, so the target Android device can automatically choose the proper
+ library binary version to use.</p>
+ <p>The <strong>source/Android-lib</strong> folder includes also an Android
+ example application that processes WAV audio files using SoundTouch library in
+ Android devices.</p>
+
+ <h3>2.4. Building in Mac</h3>
+ <p>Install autoconf tool as instructed in <a
+ href="http://macappstore.org/autoconf/">http://macappstore.org/autoconf/</a>, or alternatively the 'cmake' toolchain.</p>
+ <p>Then, build as described above in section "Building in Gnu platforms".</p>
+
+ <hr>
+ <h2>3. About implementation &amp; Usage tips <h3>3.1. Supported sample data formats</h3>
+ <p>The sample data format can be chosen between 16bit signed integer
+ and 32bit floating point values.</p>
+ </p> The default sample type is 32bit floating point format,
+ which also provides better sound quality than integer format because
+ integer algorithms need to scale already intermediate calculation results to
+ avoid integer overflows. These early integer scalings can slightly degrade
+ output quality.</p>
+ <p> In Windows environment, the sample data format is chosen in file
+ "STTypes.h" by choosing one of the following defines:</p>
+ <ul>
+ <li> <span style="font-weight: bold;">#define
+ SOUNDTOUCH_INTEGER_SAMPLES</span> for 16bit signed integer</li>
+ <li> <span style="font-weight: bold;">#define </span><span style="font-weight: bold;">SOUNDTOUCH_</span><span
+ style="font-weight: bold;">FLOAT_SAMPLES</span> for 32bit floating
+ point</li>
+ </ul>
+ <p> In GNU environment, the floating sample format is used by default,
+ but integer sample format can be chosen by giving the following switch
+ to the configure script: </p>
+ <blockquote>
+ <pre>./configure --enable-integer-samples</pre>
+ </blockquote>
+ <p>The sample data can have either single (mono) or double (stereo)
+ audio channel. Stereo data is interleaved so that every other data
+ value is for left channel and every second for right channel. Notice
+ that while it'd be possible in theory to process stereo sound as two
+ separate mono channels, this isn't recommended because processing the
+ channels separately would result in losing the phase coherency between
+ the channels, which consequently would ruin the stereo effect.</p>
+ <p>Sample rates between 8000-48000Hz are supported.</p>
+ <h3>3.2. Processing latency</h3>
+ <p>The processing and latency constraints of the SoundTouch library are:</p>
+ <ul>
+ <li> Input/output processing latency for the SoundTouch processor is
+ around 100 ms. This is when time-stretching is used. If the rate
+ transposing effect alone is used, the latency requirement is much
+ shorter, see section 'About algorithms'.</li>
+ <li> Processing CD-quality sound (16bit stereo sound with 44100H
+ sample rate) in real-time or faster is possible starting from
+ processors equivalent to Intel Pentium 133Mh or better, if using the
+ "quick" processing algorithm. If not using the "quick" mode or if
+ floating point sample data are being used, several times more CPU power
+ is typically required.</li>
+ </ul>
+ <h3>3.3. About algorithms</h3>
+ <p>SoundTouch provides three seemingly independent effects: tempo,
+ pitch and playback rate control. These three controls are implemented
+ as combination of two primary effects, <em>sample rate transposing</em>
+ and <em>time-stretching</em>.</p>
+ <p><em>Sample rate transposing</em> affects both the audio stream
+ duration and pitch. It's implemented simply by converting the original
+ audio sample stream to the desired duration by interpolating from
+ the original audio samples. In SoundTouch, linear interpolation with
+ anti-alias filtering is used. Theoretically a higher-order
+ interpolation provide better result than 1st order linear
+ interpolation, but in audio application linear interpolation together
+ with anti-alias filtering performs subjectively about as well as
+ higher-order filtering would.</p>
+ <p><em>Time-stretching </em>means changing the audio stream duration
+ without affecting it's pitch. SoundTouch uses WSOLA-like
+ time-stretching routines that operate in the time domain. Compared to
+ sample rate transposing, time-stretching is a much heavier operation
+ and also requires a longer processing "window" of sound samples used by
+ the processing algorithm, thus increasing the algorithm input/output
+ latency. Typical i/o latency for the SoundTouch time-stretch algorithm
+ is around 100 ms.</p>
+ <p>Sample rate transposing and time-stretching are then used together
+ to produce the tempo, pitch and rate controls:</p>
+ <ul>
+ <li> <strong>'Tempo'</strong> control is implemented purely by
+ time-stretching.</li>
+ <li> <strong>'Rate</strong>' control is implemented purely by sample
+ rate transposing.</li>
+ <li> <strong>'Pitch</strong>' control is implemented as a
+ combination of time-stretching and sample rate transposing. For
+ example, to increase pitch the audio stream is first time-stretched to
+ longer duration (without affecting pitch) and then transposed back to
+ original duration by sample rate transposing, which simultaneously
+ reduces duration and increases pitch. The result is original duration
+ but increased pitch.</li>
+ </ul>
+ <h3>3.4 Tuning the algorithm parameters</h3>
+ <p>The time-stretch algorithm has few parameters that can be tuned to
+ optimize sound quality for certain application. The current default
+ parameters have been chosen by iterative if-then analysis (read: "trial
+ and error") to obtain best subjective sound quality in pop/rock music
+ processing, but in applications processing different kind of sound the
+ default parameter set may result into a sub-optimal result.</p>
+ <p>The time-stretch algorithm default parameter values are set by the
+ following #defines in file "TDStretch.h":</p>
+ <blockquote>
+ <pre>#define DEFAULT_SEQUENCE_MS AUTOMATIC<br>#define DEFAULT_SEEKWINDOW_MS AUTOMATIC<br>#define DEFAULT_OVERLAP_MS 8</pre>
+ </blockquote>
+ <p>These parameters affect to the time-stretch algorithm as follows:</p>
+ <ul>
+ <li> <strong>DEFAULT_SEQUENCE_MS</strong>: This is the default
+ length of a single processing sequence in milliseconds which determines
+ the how the original sound is chopped in the time-stretch algorithm.
+ Larger values mean fewer sequences are used in processing. In principle
+ a larger value sounds better when slowing down the tempo, but worse
+ when increasing the tempo and vice versa.<br>
+ <br>
+ By default, this setting value is calculated automatically according to
+ tempo value.<br>
+ </li>
+ <li> <strong>DEFAULT_SEEKWINDOW_MS</strong>: The seeking window
+ default length in milliseconds is for the algorithm that seeks the best
+ possible overlapping location. This determines from how wide a sample
+ "window" the algorithm can use to find an optimal mixing location when
+ the sound sequences are to be linked back together.<br>
+ <br>
+ The bigger this window setting is, the higher the possibility to find a
+ better mixing position becomes, but at the same time large values may
+ cause a "drifting" sound artifact because neighboring sequences can be
+ chosen at more uneven intervals. If there's a disturbing artifact that
+ sounds as if a constant frequency was drifting around, try reducing
+ this setting.<br>
+ <br>
+ By default, this setting value is calculated automatically according to
+ tempo value.<br>
+ </li>
+ <li> <strong>DEFAULT_OVERLAP_MS</strong>: Overlap length in
+ milliseconds. When the sound sequences are mixed back together to form
+ again a continuous sound stream, this parameter defines how much the
+ ends of the consecutive sequences will overlap with each other.<br>
+ <br>
+ This shouldn't be that critical parameter. If you reduce the
+ DEFAULT_SEQUENCE_MS setting by a large amount, you might wish to try a
+ smaller value on this.
+ </li>
+ </ul>
+ <p>Notice that these parameters can also be set during execution time
+ with functions "<strong>TDStretch::setParameters()</strong>" and "<strong>SoundTouch::setSetting()</strong>".</p>
+ <p>The table below summaries how the parameters can be adjusted for
+ different applications:</p>
+ <table border="1">
+ <tbody>
+ <tr>
+ <td valign="top"><strong>Parameter name</strong></td>
+ <td valign="top"><strong>Default value magnitude</strong></td>
+ <td valign="top"><strong>Larger value affects...</strong></td>
+ <td valign="top"><strong>Smaller value affects...</strong></td>
+ <td valign="top"><strong>Effect to CPU burden</strong></td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>SEQUENCE_MS</pre>
+ </td>
+ <td valign="top">Default value is relatively large, chosen for
+ slowing down music tempo</td>
+ <td valign="top">Larger value is usually better for slowing down
+ tempo. Growing the value decelerates the "echoing" artifact when
+ slowing down the tempo.</td>
+ <td valign="top">Smaller value might be better for speeding up
+ tempo. Reducing the value accelerates the "echoing" artifact when
+ slowing down the tempo </td>
+ <td valign="top">Increasing the parameter value reduces
+ computation burden</td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>SEEKWINDOW_MS</pre>
+ </td>
+ <td valign="top">Default value is relatively large, chosen for
+ slowing down music tempo</td>
+ <td valign="top">Larger value eases finding a good mixing
+ position, but may cause a "drifting" artifact</td>
+ <td valign="top">Smaller reduce possibility to find a good mixing
+ position, but reduce the "drifting" artifact.</td>
+ <td valign="top">Increasing the parameter value increases
+ computation burden</td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>OVERLAP_MS</pre>
+ </td>
+ <td valign="top">Default value is relatively large, chosen to
+ suit with above parameters.</td>
+ <td valign="top"></td>
+ <td valign="top">If you reduce the "sequence ms" setting, you
+ might wish to try a smaller value.</td>
+ <td valign="top">Increasing the parameter value increases
+ computation burden</td>
+ </tr>
+ </tbody>
+ </table>
+ <h3>3.5 Performance Optimizations </h3>
+ <p><strong>Integer vs floating point:</strong></p>
+ <p>Floating point sample type is generally recommended because it provides
+ better sound quality.</p>
+
+ <p>However, execution speed difference between integer and floating point processing
+ depends on the CPU architecture. As rule of thumb,
+ <ul>
+ <li>in 32-bit x86 floating point and integer are roughly equally fast</li>
+ <li>in 64-bit x86/x64 floating point can be significantly faster than integer
+ version, because MMX integer optimizations are not available in the x64 architecture.
+ That depends on the compiler however, so that gcc can autovectorize integer routines
+ to work equally fast as floating point, where as Visual C++ (2017) does not
+ perform equally well and produces integer code that runs some 3x slower than
+ SSE-optimized floating poing code.
+ </li>
+ <li>in ARMv7 integer routines are twice as fast as floating point. Their
+ relative difference is roughly the same both with and without NEON; NEON
+ vfpu can however bring 2.4x speed improvement.
+ </li>
+ <li>in other platforms: try out if the execution time performance makes a
+ big difference</li>
+ </ul>
+ </p>
+ <p><strong>General optimizations:</strong></p>
+ <p>The time-stretch routine has a 'quick' mode that substantially
+ speeds up the algorithm but may slightly compromise the sound quality.
+ This mode is activated by calling SoundTouch::setSetting()
+ function with parameter id of SETTING_USE_QUICKSEEK and value
+ "1", i.e. </p>
+ <blockquote>
+ <p>setSetting(SETTING_USE_QUICKSEEK, 1);</p>
+ </blockquote>
+ <p><strong>CPU-specific optimizations:</strong></p>
+ <p>Intel x86 specific SIMD optimizations are implemented using compiler
+ intrinsics, providing about a 3x processing speedup for x86 compatible
+ processors vs. non-SIMD implementation:</p>
+ <ul>
+ <li> MMX optimized routines are used in 32-bit x86 build when 16bit integer
+ sample type is used</li>
+ <li> SSE optimized routines are used in 32- and 64-bit x86 CPUs when 32bit
+ floating point sample type is used</li>
+ </ul>
+ <p>The algorithms are tuned to utilize autovectorization efficiently
+ also in other CPU architectures, for example ARM cpus see approx 2.4x processing
+ speedup when NEON SIMD support is present.
+ </p>
+ <h3>3.5 OpenMP parallel computation</h3>
+ <p>SoundTouch 1.9 onwards support running the algorithms parallel in several CPU
+ cores. Based on benchmark the experienced multi-core processing speed-up gain
+ ranges between +30% (on a high-spec dual-core x86 Windows PC) to 215% (on a moderately low-spec
+ quad-core ARM of Raspberry Pi2). </p>
+ <p>See an external blog article with more detailed discussion about the
+ <a href="http://www.softwarecoven.com/parallel-computing-in-embedded-mobile-devices/">
+ SoundTouch OpenMP optimization</a>.
+ </p>
+ <p>The parallel computing support is implemented using OpenMP spec 3.0
+ instructions. These instructions are supported by Visual C++ 2008 and later, and
+ GCC v4.2 and later. Compilers that do not supporting OpenMP will ignore these
+ optimizations and routines will still work properly. Possible warnings about
+ unknown #pragmas are related to OpenMP support and can be safely ignored.</p>
+ <p>The OpenMP improvements are disabled by default, and need to be enabled by
+ developer during compile-time. Reason for this is that parallel processing adds
+ moderate runtime overhead in managing the multi-threading, so it may not be
+ necessary nor desirable in all applications. For example real-time processing
+ that is not constrained by CPU power will not benefit of speed-up provided by
+ the parallel processing, in the contrary it may increase power consumption due
+ to the increased overhead.</p>
+ <p>However, applications that run on low-spec multi-core CPUs and may otherwise
+ have possibly constrained performance will benefit of the OpenMP improvements.
+ This include for example multi-core embedded devices.</p>
+ <p>OpenMP parallel computation can be enabled before compiling SoundTouch
+ library as follows:</p>
+ <ul>
+ <li><strong>Visual Studio</strong>: Open properties for the <strong>SoundTouch
+ </strong>sub-project, browse to <strong>C/C++</strong> and <strong>Language
+ </strong>settings. Set
+ there &quot;<strong>OpenMP support</strong>&quot; to &quot;<strong>Yes</strong>&quot;. Alternatively add
+ <strong>/openmp</strong> switch to command-line
+ parameters
+ </li>
+ <li><strong>GNU</strong>: Run the configure script with &quot;<strong>./configure
+ --enable-openmp</strong>&quot; switch, then run make as usually</li>
+ <li><strong>Android</strong>: Add &quot;<strong>-fopenmp</strong>&quot; switches to compiler &amp; linker
+ options, see README-SoundTouch-Android.html in the source code package for
+ more detailed instructions.</li>
+ </ul>
+ <hr>
+ <h2><a name="SoundStretch"></a>4. SoundStretch audio processing utility
+ </h2>
+ <p>SoundStretch audio processing utility<br>
+ Copyright (c) Olli Parviainen 2002-2024</p>
+ <p>SoundStretch is a simple command-line application that can change
+ tempo, pitch and playback rates of WAV sound files. This program is
+ intended primarily to demonstrate how the "SoundTouch" library can be
+ used to process sound in your own program, but it can as well be used
+ for processing sound files.</p>
+ <h3>4.1. SoundStretch Usage Instructions</h3>
+ <p>SoundStretch Usage syntax:</p>
+ <blockquote>
+ <pre>soundstretch infilename outfilename [switches]</pre>
+ </blockquote>
+ <p>Where: </p>
+ <table width="100%" border="0" cellpadding="2">
+ <tbody>
+ <tr>
+ <td valign="top">
+ <pre>"infilename"</pre>
+ </td>
+ <td valign="top">Name of the input sound data file (in .WAV audio
+ file format). Give "stdin" as filename to use standard input pipe. </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>"outfilename"</pre>
+ </td>
+ <td valign="top">Name of the output sound file where the
+ resulting sound is saved (in .WAV audio file format). This parameter
+ may be omitted if you don't want to save the output (e.g. when
+ only calculating BPM rate with '-bpm' switch). Give "stdout" as
+ filename to use standard output pipe.</td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>[switches]</pre>
+ </td>
+ <td valign="top">Are one or more control switches.</td>
+ </tr>
+ </tbody>
+ </table>
+ <p>Available control switches are:</p>
+ <table width="100%" border="0" cellpadding="2">
+ <tbody>
+ <tr>
+ <td valign="top">
+ <pre>-tempo=n </pre>
+ </td>
+ <td valign="top">Change the sound tempo by n percents (n = -95.0
+ .. +5000.0 %) </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-pitch=n</pre>
+ </td>
+ <td valign="top">Change the sound pitch by n semitones (n = -60.0
+ .. + 60.0 semitones) </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-rate=n</pre>
+ </td>
+ <td valign="top">Change the sound playback rate by n percents (n
+ = -95.0 .. +5000.0 %) </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-bpm=n</pre>
+ </td>
+ <td valign="top">Detect the Beats-Per-Minute (BPM) rate of the
+ sound and adjust the tempo to meet 'n' BPMs. When this switch is
+ applied, the "-tempo" switch is ignored. If "=n" is omitted, i.e.
+ switch "-bpm" is used alone, then the BPM rate is estimated and
+ displayed, but tempo not adjusted according to the BPM value. </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-quick</pre>
+ </td>
+ <td valign="top">Use quicker tempo change algorithm. Gains speed
+ but loses sound quality. </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-naa</pre>
+ </td>
+ <td valign="top">Don't use anti-alias filtering in sample rate
+ transposing. Gains speed but loses sound quality. </td>
+ </tr>
+ <tr>
+ <td valign="top">
+ <pre>-license</pre>
+ </td>
+ <td valign="top">Displays the program license text (LGPL)</td>
+ </tr>
+ </tbody>
+ </table>
+ <p>Notes:</p>
+ <ul>
+ <li> To use standard input/output pipes for processing, give "stdin"
+ and "stdout" as input/output filenames correspondingly. The standard
+ input/output pipes will still carry the audio data in .wav audio file
+ format.</li>
+ <li> The numerical switches allow both integer (e.g. "-tempo=123")
+ and decimal (e.g. "-tempo=123.45") numbers.</li>
+ <li> The "-naa" and/or "-quick" switches can be used to reduce CPU
+ usage while compromising some sound quality</li>
+ <li> The BPM detection algorithm works by detecting repeating bass or
+ drum patterns at low frequencies of &lt;250Hz. A lower-than-expected
+ BPM figure may be reported for music with uneven or complex bass
+ patterns.</li>
+ </ul>
+ <h3>4.2. SoundStretch usage examples </h3>
+ <p><strong>Example 1</strong></p>
+ <p>The following command increases tempo of the sound file
+ "originalfile.wav" by 12.5% and stores result to file
+ "destinationfile.wav":</p>
+ <blockquote>
+ <pre>soundstretch originalfile.wav destinationfile.wav -tempo=12.5</pre>
+ </blockquote>
+ <p><strong>Example 2</strong></p>
+ <p>The following command decreases the sound pitch (key) of the sound
+ file "orig.wav" by two semitones and stores the result to file
+ "dest.wav":</p>
+ <blockquote>
+ <pre>soundstretch orig.wav dest.wav -pitch=-2</pre>
+ </blockquote>
+ <p><strong>Example 3</strong></p>
+ <p>The following command processes the file "orig.wav" by decreasing
+ the sound tempo by 25.3% and increasing the sound pitch (key) by 1.5
+ semitones. Resulting .wav audio data is directed to standard output
+ pipe:</p>
+ <blockquote>
+ <pre>soundstretch orig.wav stdout -tempo=-25.3 -pitch=1.5</pre>
+ </blockquote>
+ <p><strong>Example 4</strong></p>
+ <p>The following command detects the BPM rate of the file "orig.wav"
+ and adjusts the tempo to match 100 beats per minute. Result is stored
+ to file "dest.wav":</p>
+ <blockquote>
+ <pre>soundstretch orig.wav dest.wav -bpm=100</pre>
+ </blockquote>
+ <p><strong>Example 5</strong></p>
+ <p>The following command reads .wav sound data from standard input pipe
+ and estimates the BPM rate:</p>
+ <blockquote>
+ <pre>soundstretch stdin -bpm</pre>
+ </blockquote>
+ <p><strong>Example 6</strong></p>
+ <p>The following command tunes song from original 440Hz tuning to 432Hz tuning:
+ this corresponds to lowering the pitch by -0.318 semitones:</p>
+ <blockquote>
+ <pre>soundstretch original.wav output.wav -pitch=-0.318</pre>
+ </blockquote>
+ <hr>
+ <a name="changehistory"><h2>5. Change History</h2></a>
+ <h3>5.1. SoundTouch library Change History </h3>
+ <p><b>2.4.1:</b></p>
+ <ul class="current">
+ <li>Resolve buffer overflow in FIR filter when processing more than 16 channels</li>
+ </ul>
+ <p><b>2.4.0:</b></p>
+ <ul>
+ <li>Set CMake minimum version to 3.5 to avoid deprecation warning</li>
+ <li>Increase max nr. of channels from 16 to 32</li>
+ <li>Don't use `pow()` when using integer precision samples</li>
+ <li>Replace `-Ofast` that's being deprecated in some compilers, by `-O3 -ffast-math`</li>
+ </ul>
+ <p><b>2.3.3:</b></p>
+ <ul>
+ <li>Fixing compiler warnings, maintenance fixes to make/build files for various systems
+ </li>
+ </ul>
+ <p><b>2.3.2:</b></p>
+ <ul>
+ <li>Improve autotools makefiles to build the `SoundTouchDLL` dynamic-link link library with
+ C-style API. This library variation is easier to import and use from other programming
+ languages than the default C++ library.
+ </li>
+ </ul>
+ <p><b>2.3.1:</b></p>
+ <ul>
+ <li>Adjusted cmake build settings and header files that cmake installs</li>
+ </ul>
+ <p><b>2.3.0:</b></p>
+ <ul>
+ <li>Disable setting "SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION" by default. The original
+ purpose of this setting was to avoid performance penalty due to unaligned SIMD memory
+ accesses in old CPUs, but that is not any more issue in concurrent CPU SIMD implementations
+ and having this setting enabled can cause slight compromise in result quality.
+ </li>
+ <li>Bugfix: soundtouch.clear() to really clear whole processing pipeline state. Earlier
+ individual variables were left uncleared, which caused slightly different result if
+ the same audio stream were processed again after calling clear().
+ </li>
+ <li>Bugfix: TDstretch to align initial offset position to be in middle of correlation search
+ window. This ensures that with zero tempo change the output will be same as input.
+ </li>
+ <li>Bugfix: Fix a bug in TDstrectch with too small initial skipFract value that occurred
+ with certain processing parameter settings: Replace assert with assignment that
+ corrects the situation.
+ </li>
+ <li>Remove OpenMP "_init_threading" workaround from Android build as it's not needed with concurrent
+ Android SDKs any more.</li>
+ </ul>
+ <p><b>2.2:</b></p>
+ <ul>
+ <li>Improved source codes so that compiler can autovectorize them more effectively.
+ This brings remarkable improvement e.g. ARM cpus equipped with NEON vfpu: Bencmarked
+ 2.4x improvement in execution speed in ARMv7l vs the previous SoundTouch version
+ for both integer and floating point sample types.
+ </li>
+ <li>Bugfix: Resolved bad sound quality when using integer sample types in non-x86 CPU</li>
+ <li>Bugfix: Fixed possible reading past end of array in BPM peak detection algorithm</li>
+ </ul>
+ <p><b>2.1.2:</b></p>
+ <ul>
+ <li>Bump version to 2.1.2 also in configure.ac. The earlier release had old version info for GNU autotools.</li>
+ </ul>
+ <p><b>2.1.1:</b></p>
+ <ul>
+ <li>Bugfixes: Fixed potential buffer overwrite bugs in WavFile routines. Replaced asserts with runtime exceptions.
+ </li>
+ <li>Android: Migrated the SoundTouch Android example to new Android Studio</li>
+ <li>Automake: unset ACLOCAL in bootstrap script in case earlier build script has set it</li>
+
+ </ul>
+ <p><b>2.1:</b></p>
+ <ul>
+ <li>Refactored C# interface example</li>
+ <li>Disable anti-alias filter when switch
+ SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER defined because anti-alias
+ filter cause slight click if the rate change crosses zero during
+ processing</li>
+ <li>Added script for building SoundTouchDll dynamic-link-library for GNU platforms</li>
+ <li>Rewrote Beats-per-Minute analysis algorithm for more reliable BPM detection</li>
+ <li>Added BPM functions to SoundTouchDll API</li>
+ <li>Migrated Visual Studio project files to MSVC 201x format</li>
+ <li>Replaced function parameter value asserts with runtime exceptions</li>
+ <li>Code maintenance & style cleanup</li>
+ </ul>
+ <p><b>2.0:</b></p>
+ <ul>
+ <li>Added functions to get initial processing latency, duration ratio between the original input and processed
+ output tracks, and clarified reporting of input/output batch sizes</li>
+ <li>Fixed issue that added brief sequence of silence to beginning of output audio</li>
+ <li>Adjusted algorithm parameters to reduce reverberating effect at tempo slowdown</li>
+ <li>Bugfix: Fixed a glitch that could cause negative array indexing in quick seek algorithm</li>
+ <li>Bugfix: flush() didn't properly flush final samples from the pipeline on 2nd time in case that soundtouch
+ object instance was recycled and used for processing a second audio stream.</li>
+ <li>Bugfix: Pi value had incorrect 9th/10th decimals</li>
+ <li>Added C# example application that uses SoundTouch dll library for processing MP3 files</li>
+ </ul>
+ <p><b>1.9.2:</b></p>
+ <ul>
+ <li>Fix in GNU package configuration</li>
+ </ul>
+ <p><b>1.9.1:</b></p>
+ <ul>
+ <li>Improved SoundTouch::flush() function so that it returns precisely the desired amount of samples for exact
+ output duration control</li>
+ <li>Redesigned quickseek algorithm for improved sound quality when using the quickseek mode. The new quickseek
+ algorithm can find 99% as good results as the
+ default full-scan mode, while the quickseek algorithm is remarkable less
+ CPU intensive.</li>
+ <li>Added adaptive integer divider scaling for improved sound quality when using integer processing algorithm
+ </li>
+ </ul>
+ <p><b>1.9:</b></p>
+ <ul>
+ <li>Added support for parallel computation support via OpenMP primitives for better performance in multicore
+ systems.
+ Benchmarks show that achieved parallel processing speedup improvement
+ typically range from +30% (x86 dual-core) to +180% (ARM quad-core). The
+ OpenMP optimizations are disabled by default, see OpenMP notes above in this
+ readme file how to enabled these optimizations.</li>
+ <li>Android: Added support for Android devices featuring X86 and MIPS CPUs,
+ in addition to ARM CPUs.</li>
+ <li>Android: More versatile Android example application that processes WAV
+ audio files with SoundTouch library</li>
+ <li>Replaced Windows-like 'BOOL' types with native 'bool'</li>
+ <li>Changed documentation token to "dist_doc_DATA" in Makefile.am file</li>
+ <li>Miscellaneous small fixes and improvements</li>
+ </ul>
+ <p><b>1.8.0:</b></p>
+ <ul>
+ <li>Added support for multi-channel audio processing</li>
+ <li>Added support for <b>cubic</b> and <b>shannon</b> interpolation for rate and pitch shift effects besides
+ the original <b>linear</b> interpolation, to reduce aliasing at high frequencies due to interpolation.
+ Cubic interpolation is used as default for floating point processing, and linear interpolation for integer
+ processing.</li>
+ <li>Fixed bug in anti-alias filtering that limited stop-band attenuation to -10 dB instead of <-50dB, and
+ increased filter length from 32 to 64 taps to further reduce aliasing due to frequency folding.</li>
+ <li>Performance improvements in cross-correlation algorithm</li>
+ <li>Other bug and compatibility fixes</li>
+ </ul>
+ <p><b>1.7.1:</b></p>
+ <ul>
+ <li>Added files for Android compilation
+ </ul>
+ <p><b>1.7.0:</b></p>
+ <ul>
+ <li>Sound quality improvements/li>
+ <li>Improved flush() to adjust output sound stream duration to match better with
+ ideal duration</li>
+ <li>Rewrote x86 cpu feature check to resolve compatibility problems</li>
+ <li>Configure script automatically checks if CPU supports mmx & sse compatibility for GNU platform, and
+ the script support now "--enable-x86-optimizations" switch to allow disabling x86-specific optimizations.</li>
+ <li>Revised #define conditions for 32bit/64bit compatibility</li>
+ <li>gnu autoconf/automake script compatibility fixes</li>
+ <li>Tuned beat-per-minute detection algorithm</li>
+ </ul>
+ <p><b>1.6.0:</b></p>
+ <ul>
+ <li> Added automatic cutoff threshold adaptation to beat detection
+ routine to better adapt BPM calculation to different types of music</li>
+ <li> Retired 3DNow! optimization support as 3DNow! is nowadays
+ obsoleted and assembler code is nuisance to maintain</li>
+ <li>Retired "configure" file from source code package due to
+ autoconf/automake versio conflicts, so that it is from now on to be
+ generated by invoking "boostrap" script that uses locally available
+ toolchain version for generating the "configure" file</li>
+ <li>Resolved namespace/label naming conflicts with other libraries by
+ replacing global labels such as INTEGER_SAMPLES with more specific
+ SOUNDTOUCH_INTEGER_SAMPLES etc.<br>
+ </li>
+ <li>Updated windows build scripts &amp; project files for Visual
+ Studio 2008 support</li>
+ <li> Updated SoundTouch.dll API for .NET compatibility</li>
+ <li> Added API for querying nominal processing input &amp; output
+ sample batch sizes</li>
+ </ul>
+ <p><strong>1.5.0:</strong></p>
+ <ul>
+ <li> Added normalization to correlation calculation and improvement
+ automatic seek/sequence parameter calculation to improve sound quality</li>
+ <li> Bugfixes:
+ <ul>
+ <li> Fixed negative array indexing in quick seek algorithm</li>
+ <li> FIR autoalias filter running too far in processing buffer</li>
+ <li> Check against zero sample count in rate transposing</li>
+ <li> Fix for x86-64 support: Removed pop/push instructions from
+ the cpu detection algorithm.</li>
+ <li> Check against empty buffers in FIFOSampleBuffer</li>
+ <li> Other minor fixes &amp; code cleanup</li>
+ </ul>
+ </li>
+ <li> Fixes in compilation scripts for non-Intel platforms</li>
+ <li> Added Dynamic-Link-Library (DLL) version of SoundTouch library
+ build, provided with Delphi/Pascal wrapper for calling the dll routines
+ </li>
+ <li> Added #define PREVENT_CLICK_AT_RATE_CROSSOVER that prevents a
+ click artifact when crossing the nominal pitch from either positive to
+ negative side or vice versa</li>
+ </ul>
+ <p><strong>1.4.1:</strong></p>
+ <ul>
+ <li> Fixed a buffer overflow bug in BPM detect algorithm routines if
+ processing more than 2048 samples at one call</li>
+ </ul>
+ <p><strong>1.4.0:</strong></p>
+ <ul>
+ <li> Improved sound quality by automatic calculation of time stretch
+ algorithm processing parameters according to tempo setting</li>
+ <li> Moved BPM detection routines from SoundStretch application into
+ SoundTouch library</li>
+ <li> Bugfixes: Usage of uninitialied variables, GNU build scripts,
+ compiler errors due to 'const' keyword mismatch.</li>
+ <li> Source code cleanup</li>
+ </ul>
+ <p><strong>1.3.1: </strong> </p>
+ <ul>
+ <li> Changed static class declaration to GCC 4.x compiler compatible
+ syntax.</li>
+ <li> Enabled MMX/SSE-optimized routines also for GCC compilers.
+ Earlier the MMX/SSE-optimized routines were written in
+ compiler-specific inline assembler, now these routines are migrated to
+ use compiler intrinsic syntax which allows compiling the same
+ MMX/SSE-optimized source code with both Visual C++ and GCC compilers.</li>
+ <li> Set floating point as the default sample format and added switch
+ to the GNU configure script for selecting the other sample format.</li>
+ </ul>
+ <p><strong>1.3.0: </strong> </p>
+ <ul>
+ <li> Fixed tempo routine output duration inaccuracy due to rounding
+ error</li>
+ <li> Implemented separate processing routines for integer and
+ floating arithmetic to allow improvements to floating point routines
+ (earlier used algorithms mostly optimized for integer arithmetic also
+ for floating point samples)</li>
+ <li> Fixed a bug that distorts sound if sample rate changes during
+ the sound stream</li>
+ <li> Fixed a memory leak that appeared in MMX/SSE/3DNow! optimized
+ routines</li>
+ <li> Reduced redundant code pieces in MMX/SSE/3DNow! optimized
+ routines vs. the standard C routines.</li>
+ <li> MMX routine incompatibility with new gcc compiler versions</li>
+ <li> Other miscellaneous bug fixes</li>
+ </ul>
+ <p><strong>1.2.1: </strong> </p>
+ <ul>
+ <li> Added automake/autoconf scripts for GNU platforms (in courtesy
+ of David Durham)</li>
+ <li> Fixed SCALE overflow bug in rate transposer routine.</li>
+ <li> Fixed 64bit address space bugs.</li>
+ <li> Created a 'soundtouch' namespace for SAMPLETYPE definitions.</li>
+ </ul>
+ <p><strong>1.2.0: </strong> </p>
+ <ul>
+ <li> Added support for 32bit floating point sample data type with
+ SSE/3DNow! optimizations for Win32 platform (SSE/3DNow! optimizations
+ currently not supported in GCC environment)</li>
+ <li> Replaced 'make-gcc' script for GNU environment by master
+ Makefile</li>
+ <li> Added time-stretch routine configurability to SoundTouch main
+ class</li>
+ <li> Bugfixes</li>
+ </ul>
+ <p><strong>1.1.1: </strong> </p>
+ <ul>
+ <li> Moved SoundTouch under lesser GPL license (LGPL). This allows
+ using SoundTouch library in programs that aren't released under GPL
+ license.</li>
+ <li> Changed MMX routine organiation so that MMX optimized routines
+ are now implemented in classes that are derived from the basic classes
+ having the standard non-mmx routines.</li>
+ <li> MMX routines to support gcc version 3.</li>
+ <li> Replaced windows makefiles by script using the .dsw files</li>
+ </ul>
+ <p><strong>1.0.1: </strong> </p>
+ <ul>
+ <li> "mmx_gcc.cpp": Added "using namespace std" and removed "return
+ 0" from a function with void return value to fix compiler errors when
+ compiling the library in Solaris environment.</li>
+ <li> Moved file "FIFOSampleBuffer.h" to "include" directory to allow
+ accessing the FIFOSampleBuffer class from external files.</li>
+ </ul>
+ <p><strong>1.0: </strong> </p>
+ <ul>
+ <li> Initial release</li>
+ </ul>
+ <br><br>
+ <h3>5.2. SoundStretch application Change History </h3>
+ <p><b>2.4.0:</b></p>
+ <ul class="current_soundstretch">
+ <li>parse command-line argument values with double float precision.</li>
+ </ul>
+ <p><b>2.3.3:</b></p>
+ <ul>
+ <li>Added support for Asian / non-latin filenames in Windows. Gnu platform has supported them already earlier.</li>
+ </ul>
+ <p><b>1.9:</b></p>
+ <ul>
+ <li>Added support for WAV file 'fact' information chunk.</li>
+ </ul>
+ <p><b>1.7.0:</b></p>
+ <ul>
+ <li>Bugfixes in Wavfile: exception string formatting, avoid getLengthMs() integer
+ precision overflow, support WAV files using 24/32bit sample format.</li>
+ </ul>
+ <p><b>1.5.0:</b></p>
+ <ul>
+ <li> Added "-speech" switch to activate algorithm parameters more
+ suitable for speech processing than the default parameters tuned for
+ music processing.</li>
+ </ul>
+ <p><strong>1.4.0:</strong></p>
+ <ul>
+ <li> Moved BPM detection routines from SoundStretch application into
+ SoundTouch library</li>
+ <li> Allow using standard input/output pipes as audio processing
+ input/output streams</li>
+ </ul>
+ <p><strong>1.3.0:</strong></p>
+ <ul>
+ <li> Simplified accessing WAV files with floating point sample
+ format.</li>
+ </ul>
+ <p><strong>1.2.1: </strong> </p>
+ <ul>
+ <li> Fixed 64bit address space bugs.</li>
+ </ul>
+ <p><strong>1.2.0: </strong> </p>
+ <ul>
+ <li> Added support for 32bit floating point sample data type</li>
+ <li> Restructured the BPM routines into separate library</li>
+ <li> Fixed big-endian conversion bugs in WAV file routines (hopefully
+ :)</li>
+ </ul>
+ <p><strong>1.1.1: </strong> </p>
+ <ul>
+ <li> Fixed bugs in WAV file reading &amp; added byte-order conversion
+ for big-endian processors.</li>
+ <li> Moved SoundStretch source code under 'example' directory to
+ highlight difference from SoundTouch stuff.</li>
+ <li> Replaced windows makefiles by script using the .dsw files</li>
+ <li> Output file name isn't required if output isn't desired (e.g. if
+ using the switch '-bpm' in plain format only)</li>
+ </ul>
+ <p><strong>1.1:</strong></p>
+ <ul>
+ <li> Fixed "Release" settings in Microsoft Visual C++ project file
+ (.dsp)</li>
+ <li> Added beats-per-minute (BPM) detection routine and command-line
+ switch "-bpm"</li>
+ </ul>
+ <p><strong>1.01: </strong> </p>
+ <ul>
+ <li> Initial release</li>
+ </ul>
+ <hr>
+ <h2>6. Acknowledgements </h2>
+ <p>Kudos for these people who have contributed to development or
+ submitted bugfixes:</p>
+ <ul>
+ <li> Arthur A</li>
+ <li> Paul Adenot</li>
+ <li> Richard Ash</li>
+ <li> Stanislav Brabec</li>
+ <li> Christian Budde</li>
+ <li> Jamie Bullock</li>
+ <li> Chris Bryan</li>
+ <li> Jacek Caban</li>
+ <li> Marketa Calabkova</li>
+ <li> Brian Cameron</li>
+ <li> Jason Champion</li>
+ <li> Giuseppe Cigala</li>
+ <li> David Clark</li>
+ <li> Patrick Colis</li>
+ <li> Miquel Colon</li>
+ <li> Jim Credland</li>
+ <li> Sandro Cumerlato</li>
+ <li> Gerry Fan</li>
+ <li> Justin Frankel</li>
+ <li> Masa H.</li>
+ <li> Jason Garland</li>
+ <li> Takashi Iwai</li>
+ <li> Thomas Klausner</li>
+ <li> Lu Zhihe</li>
+ <li> Luzpaz</li>
+ <li> Tony Mechelynck </li>
+ <li> Mathias M&ouml;hl</li>
+ <li> Yuval Naveh</li>
+ <li> Mats Palmgren </li>
+ <li> Chandni Patel</li>
+ <li> Paulo Pizarro</li>
+ <li> Andrey Ponomarenko</li>
+ <li> Blaise Potard</li>
+ <li> Michael Pruett</li>
+ <li> Rajeev Puran</li>
+ <li> RJ Ryan</li>
+ <li> Serge Sans Paille</li>
+ <li> John Sheehy</li>
+ <li> Tim Shuttleworth</li>
+ <li> Albert Sirvent</li>
+ <li> Tyson Smith</li>
+ <li> John Stumpo</li>
+ <li> Mario di Vece</li>
+ <li> Rémi Verschelde</li>
+ <li> Katja Vetter</li>
+ <li> Wu Q.</li>
+ <li> Olivier Laflamme</li>
+ </ul>
+ <p>Moral greetings to all other contributors and users also!</p>
+ <hr>
+ <h2>7. LICENSE </h2>
+ <p>SoundTouch audio processing library<br>
+ Copyright (c) Olli Parviainen</p>
+ <p>This library is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License version 2.1
+ as published by the Free Software Foundation.</p>
+ <p>This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+ General Public License for more details.</p>
+ <p>You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
+ <p>---</p>
+ <p>commercial license alternative also available, contact author for details.</p>
+ <hr>
+</body>
+
+</html>
\ No newline at end of file
diff --git a/third_party/soundtouch/VEYRA_ORIGIN.md b/third_party/soundtouch/VEYRA_ORIGIN.md
new file mode 100644
index 00000000..86d07584
--- /dev/null
+++ b/third_party/soundtouch/VEYRA_ORIGIN.md
@@ -0,0 +1,10 @@
+# SoundTouch 2.4.1
+
+Upstream: https://codeberg.org/soundtouch/soundtouch.git
+Tag: 2.4.1
+Commit: 0047e0b1ecfceb041348579119bf79b73a322a3a
+Licence: LGPL-2.1; see COPYING.TXT and each source header.
+Author: Olli Parviainen and upstream contributors.
+
+Copied unchanged: include/*.h, source/SoundTouch/*.{h,cpp}, COPYING.TXT, README.html.
+Veyra supplies CMakeLists.txt for a private static float-sample build, without the command-line utility or DLL wrapper. Veyra's file-preview audio owner uses the public SoundTouch API; original audio channel layout and media timestamps remain Veyra's responsibility.
diff --git a/third_party/soundtouch/include/BPMDetect.h b/third_party/soundtouch/include/BPMDetect.h
new file mode 100644
index 00000000..bf596a1b
--- /dev/null
+++ b/third_party/soundtouch/include/BPMDetect.h
@@ -0,0 +1,205 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Beats-per-minute (BPM) detection routine.
+///
+/// The beat detection algorithm works as follows:
+/// - Use function 'inputSamples' to input a chunks of samples to the class for
+/// analysis. It's a good idea to enter a large sound file or stream in smallish
+/// chunks of around few kilosamples in order not to extinguish too much RAM memory.
+/// - Input sound data is decimated to approx 500 Hz to reduce calculation burden,
+/// which is basically ok as low (bass) frequencies mostly determine the beat rate.
+/// Simple averaging is used for anti-alias filtering because the resulting signal
+/// quality isn't of that high importance.
+/// - Decimated sound data is enveloped, i.e. the amplitude shape is detected by
+/// taking absolute value that's smoothed by sliding average. Signal levels that
+/// are below a couple of times the general RMS amplitude level are cut away to
+/// leave only notable peaks there.
+/// - Repeating sound patterns (e.g. beats) are detected by calculating short-term
+/// autocorrelation function of the enveloped signal.
+/// - After whole sound data file has been analyzed as above, the bpm level is
+/// detected by function 'getBpm' that finds the highest peak of the autocorrelation
+/// function, calculates it's precise location and converts this reading to bpm's.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _BPMDetect_H_
+#define _BPMDetect_H_
+
+#include <vector>
+#include "STTypes.h"
+#include "FIFOSampleBuffer.h"
+
+namespace soundtouch
+{
+
+ /// Minimum allowed BPM rate. Used to restrict accepted result above a reasonable limit.
+ #define MIN_BPM 45
+
+ /// Maximum allowed BPM rate range. Used for calculating algorithm parametrs
+ #define MAX_BPM_RANGE 200
+
+ /// Maximum allowed BPM rate range. Used to restrict accepted result below a reasonable limit.
+ #define MAX_BPM_VALID 190
+
+////////////////////////////////////////////////////////////////////////////////
+
+ typedef struct
+ {
+ float pos;
+ float strength;
+ } BEAT;
+
+
+ class IIR2_filter
+ {
+ double coeffs[5];
+ double prev[5];
+
+ public:
+ IIR2_filter(const double *lpf_coeffs);
+ float update(float x);
+ };
+
+
+ /// Class for calculating BPM rate for audio data.
+ class BPMDetect
+ {
+ protected:
+ /// Auto-correlation accumulator bins.
+ float *xcorr;
+
+ /// Sample average counter.
+ int decimateCount;
+
+ /// Sample average accumulator for FIFO-like decimation.
+ soundtouch::LONG_SAMPLETYPE decimateSum;
+
+ /// Decimate sound by this coefficient to reach approx. 500 Hz.
+ int decimateBy;
+
+ /// Auto-correlation window length
+ int windowLen;
+
+ /// Number of channels (1 = mono, 2 = stereo)
+ int channels;
+
+ /// sample rate
+ int sampleRate;
+
+ /// Beginning of auto-correlation window: Autocorrelation isn't being updated for
+ /// the first these many correlation bins.
+ int windowStart;
+
+ /// window functions for data preconditioning
+ float *hamw;
+ float *hamw2;
+
+ // beat detection variables
+ int pos;
+ int peakPos;
+ int beatcorr_ringbuffpos;
+ int init_scaler;
+ float peakVal;
+ float *beatcorr_ringbuff;
+
+ /// FIFO-buffer for decimated processing samples.
+ soundtouch::FIFOSampleBuffer *buffer;
+
+ /// Collection of detected beat positions
+ //BeatCollection beats;
+ std::vector<BEAT> beats;
+
+ // 2nd order low-pass-filter
+ IIR2_filter beat_lpf;
+
+ /// Updates auto-correlation function for given number of decimated samples that
+ /// are read from the internal 'buffer' pipe (samples aren't removed from the pipe
+ /// though).
+ void updateXCorr(int process_samples /// How many samples are processed.
+ );
+
+ /// Decimates samples to approx. 500 Hz.
+ ///
+ /// \return Number of output samples.
+ int decimate(soundtouch::SAMPLETYPE *dest, ///< Destination buffer
+ const soundtouch::SAMPLETYPE *src, ///< Source sample buffer
+ int numsamples ///< Number of source samples.
+ );
+
+ /// Calculates amplitude envelope for the buffer of samples.
+ /// Result is output to 'samples'.
+ void calcEnvelope(soundtouch::SAMPLETYPE *samples, ///< Pointer to input/output data buffer
+ int numsamples ///< Number of samples in buffer
+ );
+
+ /// remove constant bias from xcorr data
+ void removeBias();
+
+ // Detect individual beat positions
+ void updateBeatPos(int process_samples);
+
+
+ public:
+ /// Constructor.
+ BPMDetect(int numChannels, ///< Number of channels in sample data.
+ int sampleRate ///< Sample rate in Hz.
+ );
+
+ /// Destructor.
+ virtual ~BPMDetect();
+
+ /// Inputs a block of samples for analyzing: Envelopes the samples and then
+ /// updates the autocorrelation estimation. When whole song data has been input
+ /// in smaller blocks using this function, read the resulting bpm with 'getBpm'
+ /// function.
+ ///
+ /// Notice that data in 'samples' array can be disrupted in processing.
+ void inputSamples(const soundtouch::SAMPLETYPE *samples, ///< Pointer to input/working data buffer
+ int numSamples ///< Number of samples in buffer
+ );
+
+ /// Analyzes the results and returns the BPM rate. Use this function to read result
+ /// after whole song data has been input to the class by consecutive calls of
+ /// 'inputSamples' function.
+ ///
+ /// \return Beats-per-minute rate, or zero if detection failed.
+ float getBpm();
+
+ /// Get beat position arrays. Note: The array includes also really low beat detection values
+ /// in absence of clear strong beats. Consumer may wish to filter low values away.
+ /// - "pos" receive array of beat positions
+ /// - "values" receive array of beat detection strengths
+ /// - max_num indicates max.size of "pos" and "values" array.
+ ///
+ /// You can query a suitable array sized by calling this with nullptr in "pos" & "values".
+ ///
+ /// \return number of beats in the arrays.
+ int getBeats(float *pos, float *strength, int max_num);
+ };
+}
+#endif // _BPMDetect_H_
diff --git a/third_party/soundtouch/include/FIFOSampleBuffer.h b/third_party/soundtouch/include/FIFOSampleBuffer.h
new file mode 100644
index 00000000..e7cdbed9
--- /dev/null
+++ b/third_party/soundtouch/include/FIFOSampleBuffer.h
@@ -0,0 +1,180 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// A buffer class for temporarily storaging sound samples, operates as a
+/// first-in-first-out pipe.
+///
+/// Samples are added to the end of the sample buffer with the 'putSamples'
+/// function, and are received from the beginning of the buffer by calling
+/// the 'receiveSamples' function. The class automatically removes the
+/// output samples from the buffer as well as grows the storage size
+/// whenever necessary.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef FIFOSampleBuffer_H
+#define FIFOSampleBuffer_H
+
+#include "FIFOSamplePipe.h"
+
+namespace soundtouch
+{
+
+/// Sample buffer working in FIFO (first-in-first-out) principle. The class takes
+/// care of storage size adjustment and data moving during input/output operations.
+///
+/// Notice that in case of stereo audio, one sample is considered to consist of
+/// both channel data.
+class FIFOSampleBuffer : public FIFOSamplePipe
+{
+private:
+ /// Sample buffer.
+ SAMPLETYPE *buffer;
+
+ // Raw unaligned buffer memory. 'buffer' is made aligned by pointing it to first
+ // 16-byte aligned location of this buffer
+ SAMPLETYPE *bufferUnaligned;
+
+ /// Sample buffer size in bytes
+ uint sizeInBytes;
+
+ /// How many samples are currently in buffer.
+ uint samplesInBuffer;
+
+ /// Channels, 1=mono, 2=stereo.
+ uint channels;
+
+ /// Current position pointer to the buffer. This pointer is increased when samples are
+ /// removed from the pipe so that it's necessary to actually rewind buffer (move data)
+ /// only new data when is put to the pipe.
+ uint bufferPos;
+
+ /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real
+ /// beginning of the buffer.
+ void rewind();
+
+ /// Ensures that the buffer has capacity for at least this many samples.
+ void ensureCapacity(uint capacityRequirement);
+
+ /// Returns current capacity.
+ uint getCapacity() const;
+
+public:
+
+ /// Constructor
+ FIFOSampleBuffer(int numChannels = 2 ///< Number of channels, 1=mono, 2=stereo.
+ ///< Default is stereo.
+ );
+
+ /// destructor
+ ~FIFOSampleBuffer() override;
+
+ /// Returns a pointer to the beginning of the output samples.
+ /// This function is provided for accessing the output samples directly.
+ /// Please be careful for not to corrupt the book-keeping!
+ ///
+ /// When using this function to output samples, also remember to 'remove' the
+ /// output samples from the buffer by calling the
+ /// 'receiveSamples(numSamples)' function
+ virtual SAMPLETYPE *ptrBegin() override;
+
+ /// Returns a pointer to the end of the used part of the sample buffer (i.e.
+ /// where the new samples are to be inserted). This function may be used for
+ /// inserting new samples into the sample buffer directly. Please be careful
+ /// not corrupt the book-keeping!
+ ///
+ /// When using this function as means for inserting new samples, also remember
+ /// to increase the sample count afterwards, by calling the
+ /// 'putSamples(numSamples)' function.
+ SAMPLETYPE *ptrEnd(
+ uint slackCapacity ///< How much free capacity (in samples) there _at least_
+ ///< should be so that the caller can successfully insert the
+ ///< desired samples to the buffer. If necessary, the function
+ ///< grows the buffer size to comply with this requirement.
+ );
+
+ /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
+ /// the sample buffer.
+ virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples.
+ uint numSamples ///< Number of samples to insert.
+ ) override;
+
+ /// Adjusts the book-keeping to increase number of samples in the buffer without
+ /// copying any actual samples.
+ ///
+ /// This function is used to update the number of samples in the sample buffer
+ /// when accessing the buffer directly with 'ptrEnd' function. Please be
+ /// careful though!
+ virtual void putSamples(uint numSamples ///< Number of samples been inserted.
+ );
+
+ /// Output samples from beginning of the sample buffer. Copies requested samples to
+ /// output buffer and removes them from the sample buffer. If there are less than
+ /// 'numsample' samples in the buffer, returns all that available.
+ ///
+ /// \return Number of samples returned.
+ virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
+ uint maxSamples ///< How many samples to receive at max.
+ ) override;
+
+ /// Adjusts book-keeping so that given number of samples are removed from beginning of the
+ /// sample buffer without copying them anywhere.
+ ///
+ /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
+ /// with 'ptrBegin' function.
+ virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
+ ) override;
+
+ /// Returns number of samples currently available.
+ virtual uint numSamples() const override;
+
+ /// Sets number of channels, 1 = mono, 2 = stereo.
+ void setChannels(int numChannels);
+
+ /// Get number of channels
+ int getChannels()
+ {
+ return channels;
+ }
+
+ /// Returns nonzero if there aren't any samples available for outputting.
+ virtual int isEmpty() const override;
+
+ /// Clears all the samples.
+ virtual void clear() override;
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ uint adjustAmountOfSamples(uint numSamples) override;
+
+ /// Add silence to end of buffer
+ void addSilent(uint nSamples);
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/include/FIFOSamplePipe.h b/third_party/soundtouch/include/FIFOSamplePipe.h
new file mode 100644
index 00000000..93b6bf47
--- /dev/null
+++ b/third_party/soundtouch/include/FIFOSamplePipe.h
@@ -0,0 +1,230 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// 'FIFOSamplePipe' : An abstract base class for classes that manipulate sound
+/// samples by operating like a first-in-first-out pipe: New samples are fed
+/// into one end of the pipe with the 'putSamples' function, and the processed
+/// samples are received from the other end with the 'receiveSamples' function.
+///
+/// 'FIFOProcessor' : A base class for classes the do signal processing with
+/// the samples while operating like a first-in-first-out pipe. When samples
+/// are input with the 'putSamples' function, the class processes them
+/// and moves the processed samples to the given 'output' pipe object, which
+/// may be either another processing stage, or a fifo sample buffer object.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef FIFOSamplePipe_H
+#define FIFOSamplePipe_H
+
+#include <assert.h>
+#include <stdlib.h>
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+/// Abstract base class for FIFO (first-in-first-out) sample processing classes.
+class FIFOSamplePipe
+{
+protected:
+
+ bool verifyNumberOfChannels(int nChannels) const
+ {
+ if ((nChannels > 0) && (nChannels <= SOUNDTOUCH_MAX_CHANNELS))
+ {
+ return true;
+ }
+ ST_THROW_RT_ERROR("Error: Illegal number of channels");
+ return false;
+ }
+
+public:
+ // virtual default destructor
+ virtual ~FIFOSamplePipe() {}
+
+
+ /// Returns a pointer to the beginning of the output samples.
+ /// This function is provided for accessing the output samples directly.
+ /// Please be careful for not to corrupt the book-keeping!
+ ///
+ /// When using this function to output samples, also remember to 'remove' the
+ /// output samples from the buffer by calling the
+ /// 'receiveSamples(numSamples)' function
+ virtual SAMPLETYPE *ptrBegin() = 0;
+
+ /// Adds 'numSamples' pcs of samples from the 'samples' memory position to
+ /// the sample buffer.
+ virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples.
+ uint numSamples ///< Number of samples to insert.
+ ) = 0;
+
+
+ // Moves samples from the 'other' pipe instance to this instance.
+ void moveSamples(FIFOSamplePipe &other ///< Other pipe instance where from the receive the data.
+ )
+ {
+ const uint oNumSamples = other.numSamples();
+
+ putSamples(other.ptrBegin(), oNumSamples);
+ other.receiveSamples(oNumSamples);
+ }
+
+ /// Output samples from beginning of the sample buffer. Copies requested samples to
+ /// output buffer and removes them from the sample buffer. If there are less than
+ /// 'numsample' samples in the buffer, returns all that available.
+ ///
+ /// \return Number of samples returned.
+ virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
+ uint maxSamples ///< How many samples to receive at max.
+ ) = 0;
+
+ /// Adjusts book-keeping so that given number of samples are removed from beginning of the
+ /// sample buffer without copying them anywhere.
+ ///
+ /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
+ /// with 'ptrBegin' function.
+ virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
+ ) = 0;
+
+ /// Returns number of samples currently available.
+ virtual uint numSamples() const = 0;
+
+ // Returns nonzero if there aren't any samples available for outputting.
+ virtual int isEmpty() const = 0;
+
+ /// Clears all the samples.
+ virtual void clear() = 0;
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ virtual uint adjustAmountOfSamples(uint numSamples) = 0;
+
+};
+
+
+/// Base-class for sound processing routines working in FIFO principle. With this base
+/// class it's easy to implement sound processing stages that can be chained together,
+/// so that samples that are fed into beginning of the pipe automatically go through
+/// all the processing stages.
+///
+/// When samples are input to this class, they're first processed and then put to
+/// the FIFO pipe that's defined as output of this class. This output pipe can be
+/// either other processing stage or a FIFO sample buffer.
+class FIFOProcessor :public FIFOSamplePipe
+{
+protected:
+ /// Internal pipe where processed samples are put.
+ FIFOSamplePipe *output;
+
+ /// Sets output pipe.
+ void setOutPipe(FIFOSamplePipe *pOutput)
+ {
+ assert(output == nullptr);
+ assert(pOutput != nullptr);
+ output = pOutput;
+ }
+
+ /// Constructor. Doesn't define output pipe; it has to be set be
+ /// 'setOutPipe' function.
+ FIFOProcessor()
+ {
+ output = nullptr;
+ }
+
+ /// Constructor. Configures output pipe.
+ FIFOProcessor(FIFOSamplePipe *pOutput ///< Output pipe.
+ )
+ {
+ output = pOutput;
+ }
+
+ /// Destructor.
+ virtual ~FIFOProcessor() override
+ {
+ }
+
+ /// Returns a pointer to the beginning of the output samples.
+ /// This function is provided for accessing the output samples directly.
+ /// Please be careful for not to corrupt the book-keeping!
+ ///
+ /// When using this function to output samples, also remember to 'remove' the
+ /// output samples from the buffer by calling the
+ /// 'receiveSamples(numSamples)' function
+ virtual SAMPLETYPE *ptrBegin() override
+ {
+ return output->ptrBegin();
+ }
+
+public:
+
+ /// Output samples from beginning of the sample buffer. Copies requested samples to
+ /// output buffer and removes them from the sample buffer. If there are less than
+ /// 'numsample' samples in the buffer, returns all that available.
+ ///
+ /// \return Number of samples returned.
+ virtual uint receiveSamples(SAMPLETYPE *outBuffer, ///< Buffer where to copy output samples.
+ uint maxSamples ///< How many samples to receive at max.
+ ) override
+ {
+ return output->receiveSamples(outBuffer, maxSamples);
+ }
+
+ /// Adjusts book-keeping so that given number of samples are removed from beginning of the
+ /// sample buffer without copying them anywhere.
+ ///
+ /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
+ /// with 'ptrBegin' function.
+ virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
+ ) override
+ {
+ return output->receiveSamples(maxSamples);
+ }
+
+ /// Returns number of samples currently available.
+ virtual uint numSamples() const override
+ {
+ return output->numSamples();
+ }
+
+ /// Returns nonzero if there aren't any samples available for outputting.
+ virtual int isEmpty() const override
+ {
+ return output->isEmpty();
+ }
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ virtual uint adjustAmountOfSamples(uint numSamples) override
+ {
+ return output->adjustAmountOfSamples(numSamples);
+ }
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/include/STTypes.h b/third_party/soundtouch/include/STTypes.h
new file mode 100644
index 00000000..26e9af71
--- /dev/null
+++ b/third_party/soundtouch/include/STTypes.h
@@ -0,0 +1,191 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Common type definitions for SoundTouch audio processing library.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef STTypes_H
+#define STTypes_H
+
+typedef unsigned int uint;
+typedef unsigned long ulong;
+
+// Patch for MinGW: on Win64 long is 32-bit
+#ifdef _WIN64
+ typedef unsigned long long ulongptr;
+#else
+ typedef ulong ulongptr;
+#endif
+
+
+// Helper macro for aligning pointer up to next 16-byte boundary
+#define SOUNDTOUCH_ALIGN_POINTER_16(x) ( ( (ulongptr)(x) + 15 ) & ~(ulongptr)15 )
+
+
+#if (defined(__GNUC__) && !defined(ANDROID))
+ // In GCC, include soundtouch_config.h made by config scritps.
+ // Skip this in Android compilation that uses GCC but without configure scripts.
+ #include "soundtouch_config.h"
+#endif
+
+
+namespace soundtouch
+{
+ /// Max allowed number of channels. This is not a hard limit but to have some
+ /// maximum value for argument sanity checks -- can be increased if necessary
+ #define SOUNDTOUCH_MAX_CHANNELS 32
+
+ /// Activate these undef's to overrule the possible sampletype
+ /// setting inherited from some other header file:
+ //#undef SOUNDTOUCH_INTEGER_SAMPLES
+ //#undef SOUNDTOUCH_FLOAT_SAMPLES
+
+ /// If following flag is defined, always uses multichannel processing
+ /// routines also for mono and stero sound. This is for routine testing
+ /// purposes; output should be same with either routines, yet disabling
+ /// the dedicated mono/stereo processing routines will result in slower
+ /// runtime performance so recommendation is to keep this off.
+ // #define USE_MULTICH_ALWAYS
+
+ #if (defined(__SOFTFP__) && defined(ANDROID))
+ // For Android compilation: Force use of Integer samples in case that
+ // compilation uses soft-floating point emulation - soft-fp is way too slow
+ #undef SOUNDTOUCH_FLOAT_SAMPLES
+ #define SOUNDTOUCH_INTEGER_SAMPLES 1
+ #endif
+
+ #if !(SOUNDTOUCH_INTEGER_SAMPLES || SOUNDTOUCH_FLOAT_SAMPLES)
+
+ /// Choose either 32bit floating point or 16bit integer sampletype
+ /// by choosing one of the following defines, unless this selection
+ /// has already been done in some other file.
+ ////
+ /// Notes:
+ /// - In Windows environment, choose the sample format with the
+ /// following defines.
+ /// - In GNU environment, the floating point samples are used by
+ /// default, but integer samples can be chosen by giving the
+ /// following switch to the configure script:
+ /// ./configure --enable-integer-samples
+ /// However, if you still prefer to select the sample format here
+ /// also in GNU environment, then please #undef the INTEGER_SAMPLE
+ /// and FLOAT_SAMPLE defines first as in comments above.
+ //#define SOUNDTOUCH_INTEGER_SAMPLES 1 //< 16bit integer samples
+ #define SOUNDTOUCH_FLOAT_SAMPLES 1 //< 32bit float samples
+
+ #endif
+
+ #if (_M_IX86 || __i386__ || __x86_64__ || _M_X64)
+ /// Define this to allow X86-specific assembler/intrinsic optimizations.
+ /// Notice that library contains also usual C++ versions of each of these
+ /// these routines, so if you're having difficulties getting the optimized
+ /// routines compiled for whatever reason, you may disable these optimizations
+ /// to make the library compile.
+
+ #define SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS 1
+
+ /// In GNU environment, allow the user to override this setting by
+ /// giving the following switch to the configure script:
+ /// ./configure --disable-x86-optimizations
+ /// ./configure --enable-x86-optimizations=no
+ #ifdef SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS
+ #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS
+ #endif
+ #else
+ /// Always disable optimizations when not using a x86 systems.
+ #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS
+
+ #endif
+
+ // If defined, allows the SIMD-optimized routines to skip unevenly aligned
+ // memory offsets that can cause performance penalty in some SIMD implementations.
+ // Causes slight compromise in sound quality.
+ // #define SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION 1
+
+
+ #ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ // 16bit integer sample type
+ typedef short SAMPLETYPE;
+ // data type for sample accumulation: Use 32bit integer to prevent overflows
+ typedef long LONG_SAMPLETYPE;
+
+ #ifdef SOUNDTOUCH_FLOAT_SAMPLES
+ // check that only one sample type is defined
+ #error "conflicting sample types defined"
+ #endif // SOUNDTOUCH_FLOAT_SAMPLES
+
+ #ifdef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS
+ // Allow MMX optimizations (not available in X64 mode)
+ #if (!_M_X64)
+ #define SOUNDTOUCH_ALLOW_MMX 1
+ #endif
+ #endif
+
+ #else
+
+ // floating point samples
+ typedef float SAMPLETYPE;
+ // data type for sample accumulation: Use float also here to enable
+ // efficient autovectorization
+ typedef float LONG_SAMPLETYPE;
+
+ #ifdef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS
+ // Allow SSE optimizations
+ #define SOUNDTOUCH_ALLOW_SSE 1
+ #endif
+
+ #endif // SOUNDTOUCH_INTEGER_SAMPLES
+
+ #if ((SOUNDTOUCH_ALLOW_SSE) || (__SSE__) || (SOUNDTOUCH_USE_NEON))
+ #if SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION
+ #define ST_SIMD_AVOID_UNALIGNED
+ #endif
+ #endif
+
+}
+
+// define ST_NO_EXCEPTION_HANDLING switch to disable throwing std exceptions:
+// #define ST_NO_EXCEPTION_HANDLING 1
+#ifdef ST_NO_EXCEPTION_HANDLING
+ // Exceptions disabled. Throw asserts instead if enabled.
+ #include <assert.h>
+ #define ST_THROW_RT_ERROR(x) {assert((const char *)x);}
+#else
+ // use c++ standard exceptions
+ #include <stdexcept>
+ #include <string>
+ #define ST_THROW_RT_ERROR(x) {throw std::runtime_error(x);}
+#endif
+
+// When this #define is active, eliminates a clicking sound when the "rate" or "pitch"
+// parameter setting crosses from value <1 to >=1 or vice versa during processing.
+// Default is off as such crossover is untypical case and involves a slight sound
+// quality compromise.
+//#define SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER 1
+
+#endif
diff --git a/third_party/soundtouch/include/SoundTouch.h b/third_party/soundtouch/include/SoundTouch.h
new file mode 100644
index 00000000..3b197a1c
--- /dev/null
+++ b/third_party/soundtouch/include/SoundTouch.h
@@ -0,0 +1,348 @@
+//////////////////////////////////////////////////////////////////////////////
+///
+/// SoundTouch - main class for tempo/pitch/rate adjusting routines.
+///
+/// Notes:
+/// - Initialize the SoundTouch object instance by setting up the sound stream
+/// parameters with functions 'setSampleRate' and 'setChannels', then set
+/// desired tempo/pitch/rate settings with the corresponding functions.
+///
+/// - The SoundTouch class behaves like a first-in-first-out pipeline: The
+/// samples that are to be processed are fed into one of the pipe by calling
+/// function 'putSamples', while the ready processed samples can be read
+/// from the other end of the pipeline with function 'receiveSamples'.
+///
+/// - The SoundTouch processing classes require certain sized 'batches' of
+/// samples in order to process the sound. For this reason the classes buffer
+/// incoming samples until there are enough of samples available for
+/// processing, then they carry out the processing step and consequently
+/// make the processed samples available for outputting.
+///
+/// - For the above reason, the processing routines introduce a certain
+/// 'latency' between the input and output, so that the samples input to
+/// SoundTouch may not be immediately available in the output, and neither
+/// the amount of outputtable samples may not immediately be in direct
+/// relationship with the amount of previously input samples.
+///
+/// - The tempo/pitch/rate control parameters can be altered during processing.
+/// Please notice though that they aren't currently protected by semaphores,
+/// so in multi-thread application external semaphore protection may be
+/// required.
+///
+/// - This class utilizes classes 'TDStretch' for tempo change (without modifying
+/// pitch) and 'RateTransposer' for changing the playback rate (that is, both
+/// tempo and pitch in the same ratio) of the sound. The third available control
+/// 'pitch' (change pitch but maintain tempo) is produced by a combination of
+/// combining the two other controls.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef SoundTouch_H
+#define SoundTouch_H
+
+#include "FIFOSamplePipe.h"
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+/// Soundtouch library version string
+#define SOUNDTOUCH_VERSION "2.4.1"
+
+/// SoundTouch library version id
+#define SOUNDTOUCH_VERSION_ID (20401)
+
+//
+// Available setting IDs for the 'setSetting' & 'get_setting' functions:
+
+/// Enable/disable anti-alias filter in pitch transposer (0 = disable)
+#define SETTING_USE_AA_FILTER 0
+
+/// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32)
+#define SETTING_AA_FILTER_LENGTH 1
+
+/// Enable/disable quick seeking algorithm in tempo changer routine
+/// (enabling quick seeking lowers CPU utilization but causes a minor sound
+/// quality compromising)
+#define SETTING_USE_QUICKSEEK 2
+
+/// Time-stretch algorithm single processing sequence length in milliseconds. This determines
+/// to how long sequences the original sound is chopped in the time-stretch algorithm.
+/// See "STTypes.h" or README for more information.
+#define SETTING_SEQUENCE_MS 3
+
+/// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the
+/// best possible overlapping location. This determines from how wide window the algorithm
+/// may look for an optimal joining location when mixing the sound sequences back together.
+/// See "STTypes.h" or README for more information.
+#define SETTING_SEEKWINDOW_MS 4
+
+/// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences
+/// are mixed back together, to form a continuous sound stream, this parameter defines over
+/// how long period the two consecutive sequences are let to overlap each other.
+/// See "STTypes.h" or README for more information.
+#define SETTING_OVERLAP_MS 5
+
+
+/// Call "getSetting" with this ID to query processing sequence size in samples.
+/// This value gives approximate value of how many input samples you'll need to
+/// feed into SoundTouch after initial buffering to get out a new batch of
+/// output samples.
+///
+/// This value does not include initial buffering at beginning of a new processing
+/// stream, use SETTING_INITIAL_LATENCY to get the initial buffering size.
+///
+/// Notices:
+/// - This is read-only parameter, i.e. setSetting ignores this parameter
+/// - This parameter value is not constant but change depending on
+/// tempo/pitch/rate/samplerate settings.
+#define SETTING_NOMINAL_INPUT_SEQUENCE 6
+
+
+/// Call "getSetting" with this ID to query nominal average processing output
+/// size in samples. This value tells approcimate value how many output samples
+/// SoundTouch outputs once it does DSP processing run for a batch of input samples.
+///
+/// Notices:
+/// - This is read-only parameter, i.e. setSetting ignores this parameter
+/// - This parameter value is not constant but change depending on
+/// tempo/pitch/rate/samplerate settings.
+#define SETTING_NOMINAL_OUTPUT_SEQUENCE 7
+
+
+/// Call "getSetting" with this ID to query initial processing latency, i.e.
+/// approx. how many samples you'll need to enter to SoundTouch pipeline before
+/// you can expect to get first batch of ready output samples out.
+///
+/// After the first output batch, you can then expect to get approx.
+/// SETTING_NOMINAL_OUTPUT_SEQUENCE ready samples out for every
+/// SETTING_NOMINAL_INPUT_SEQUENCE samples that you enter into SoundTouch.
+///
+/// Example:
+/// processing with parameter -tempo=5
+/// => initial latency = 5509 samples
+/// input sequence = 4167 samples
+/// output sequence = 3969 samples
+///
+/// Accordingly, you can expect to feed in approx. 5509 samples at beginning of
+/// the stream, and then you'll get out the first 3969 samples. After that, for
+/// every approx. 4167 samples that you'll put in, you'll receive again approx.
+/// 3969 samples out.
+///
+/// This also means that average latency during stream processing is
+/// INITIAL_LATENCY-OUTPUT_SEQUENCE/2, in the above example case 5509-3969/2
+/// = 3524 samples
+///
+/// Notices:
+/// - This is read-only parameter, i.e. setSetting ignores this parameter
+/// - This parameter value is not constant but change depending on
+/// tempo/pitch/rate/samplerate settings.
+#define SETTING_INITIAL_LATENCY 8
+
+
+class SoundTouch : public FIFOProcessor
+{
+private:
+ /// Rate transposer class instance
+ class RateTransposer *pRateTransposer;
+
+ /// Time-stretch class instance
+ class TDStretch *pTDStretch;
+
+ /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
+ double virtualRate;
+
+ /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
+ double virtualTempo;
+
+ /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
+ double virtualPitch;
+
+ /// Flag: Has sample rate been set?
+ bool bSrateSet;
+
+ /// Accumulator for how many samples in total will be expected as output vs. samples put in,
+ /// considering current processing settings.
+ double samplesExpectedOut;
+
+ /// Accumulator for how many samples in total have been read out from the processing so far
+ long samplesOutput;
+
+ /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and
+ /// 'virtualPitch' parameters.
+ void calcEffectiveRateAndTempo();
+
+protected :
+ /// Number of channels
+ uint channels;
+
+ /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
+ double rate;
+
+ /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
+ double tempo;
+
+public:
+ SoundTouch();
+ virtual ~SoundTouch() override;
+
+ /// Get SoundTouch library version string
+ static const char *getVersionString();
+
+ /// Get SoundTouch library version Id
+ static uint getVersionId();
+
+ /// Sets new rate control value. Normal rate = 1.0, smaller values
+ /// represent slower rate, larger faster rates.
+ void setRate(double newRate);
+
+ /// Sets new tempo control value. Normal tempo = 1.0, smaller values
+ /// represent slower tempo, larger faster tempo.
+ void setTempo(double newTempo);
+
+ /// Sets new rate control value as a difference in percents compared
+ /// to the original rate (-50 .. +100 %)
+ void setRateChange(double newRate);
+
+ /// Sets new tempo control value as a difference in percents compared
+ /// to the original tempo (-50 .. +100 %)
+ void setTempoChange(double newTempo);
+
+ /// Sets new pitch control value. Original pitch = 1.0, smaller values
+ /// represent lower pitches, larger values higher pitch.
+ void setPitch(double newPitch);
+
+ /// Sets pitch change in octaves compared to the original pitch
+ /// (-1.00 .. +1.00)
+ void setPitchOctaves(double newPitch);
+
+ /// Sets pitch change in semi-tones compared to the original pitch
+ /// (-12 .. +12)
+ void setPitchSemiTones(int newPitch);
+ void setPitchSemiTones(double newPitch);
+
+ /// Sets the number of channels, 1 = mono, 2 = stereo
+ void setChannels(uint numChannels);
+
+ /// Sets sample rate.
+ void setSampleRate(uint srate);
+
+ /// Get ratio between input and output audio durations, useful for calculating
+ /// processed output duration: if you'll process a stream of N samples, then
+ /// you can expect to get out N * getInputOutputSampleRatio() samples.
+ ///
+ /// This ratio will give accurate target duration ratio for a full audio track,
+ /// given that the the whole track is processed with same processing parameters.
+ ///
+ /// If this ratio is applied to calculate intermediate offsets inside a processing
+ /// stream, then this ratio is approximate and can deviate +- some tens of milliseconds
+ /// from ideal offset, yet by end of the audio stream the duration ratio will become
+ /// exact.
+ ///
+ /// Example: if processing with parameters "-tempo=15 -pitch=-3", the function
+ /// will return value 0.8695652... Now, if processing an audio stream whose duration
+ /// is exactly one million audio samples, then you can expect the processed
+ /// output duration be 0.869565 * 1000000 = 869565 samples.
+ double getInputOutputSampleRatio();
+
+ /// Flushes the last samples from the processing pipeline to the output.
+ /// Clears also the internal processing buffers.
+ //
+ /// Note: This function is meant for extracting the last samples of a sound
+ /// stream. This function may introduce additional blank samples in the end
+ /// of the sound stream, and thus it's not recommended to call this function
+ /// in the middle of a sound stream.
+ void flush();
+
+ /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
+ /// the input of the object. Notice that sample rate _has_to_ be set before
+ /// calling this function, otherwise throws a runtime_error exception.
+ virtual void putSamples(
+ const SAMPLETYPE *samples, ///< Pointer to sample buffer.
+ uint numSamples ///< Number of samples in buffer. Notice
+ ///< that in case of stereo-sound a single sample
+ ///< contains data for both channels.
+ ) override;
+
+ /// Output samples from beginning of the sample buffer. Copies requested samples to
+ /// output buffer and removes them from the sample buffer. If there are less than
+ /// 'numsample' samples in the buffer, returns all that available.
+ ///
+ /// \return Number of samples returned.
+ virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
+ uint maxSamples ///< How many samples to receive at max.
+ ) override;
+
+ /// Adjusts book-keeping so that given number of samples are removed from beginning of the
+ /// sample buffer without copying them anywhere.
+ ///
+ /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
+ /// with 'ptrBegin' function.
+ virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
+ ) override;
+
+ /// Clears all the samples in the object's output and internal processing
+ /// buffers.
+ virtual void clear() override;
+
+ /// Changes a setting controlling the processing system behaviour. See the
+ /// 'SETTING_...' defines for available setting ID's.
+ ///
+ /// \return 'true' if the setting was successfully changed
+ bool setSetting(int settingId, ///< Setting ID number. see SETTING_... defines.
+ int value ///< New setting value.
+ );
+
+ /// Reads a setting controlling the processing system behaviour. See the
+ /// 'SETTING_...' defines for available setting ID's.
+ ///
+ /// \return the setting value.
+ int getSetting(int settingId ///< Setting ID number, see SETTING_... defines.
+ ) const;
+
+ /// Returns number of samples currently unprocessed.
+ virtual uint numUnprocessedSamples() const;
+
+ /// Return number of channels
+ uint numChannels() const
+ {
+ return channels;
+ }
+
+ /// Other handy functions that are implemented in the ancestor classes (see
+ /// classes 'FIFOProcessor' and 'FIFOSamplePipe')
+ ///
+ /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch.
+ /// - numSamples() : Get number of 'ready' samples that can be received with
+ /// function 'receiveSamples()'
+ /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples.
+ /// - clear() : Clears all samples from ready/processing buffers.
+};
+
+}
+#endif
diff --git a/third_party/soundtouch/include/soundtouch_config.h b/third_party/soundtouch/include/soundtouch_config.h
new file mode 100644
index 00000000..3942afe4
--- /dev/null
+++ b/third_party/soundtouch/include/soundtouch_config.h
@@ -0,0 +1,3 @@
+// autotools configuration step replaces this file with a configured version.
+// this empty file stub is provided to avoid error about missing include file
+// when not using autotools build
diff --git a/third_party/soundtouch/source/SoundTouch/AAFilter.cpp b/third_party/soundtouch/source/SoundTouch/AAFilter.cpp
new file mode 100644
index 00000000..f0972003
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/AAFilter.cpp
@@ -0,0 +1,222 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// FIR low-pass (anti-alias) filter with filter coefficient design routine and
+/// MMX optimization.
+///
+/// Anti-alias filter is used to prevent folding of high frequencies when
+/// transposing the sample rate with interpolation.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <memory.h>
+#include <assert.h>
+#include <math.h>
+#include <stdlib.h>
+#include "AAFilter.h"
+#include "FIRFilter.h"
+
+using namespace soundtouch;
+
+#define PI 3.14159265358979323846
+#define TWOPI (2 * PI)
+
+// define this to save AA filter coefficients to a file
+// #define _DEBUG_SAVE_AAFILTER_COEFFICIENTS 1
+
+#ifdef _DEBUG_SAVE_AAFILTER_COEFFICIENTS
+ #include <stdio.h>
+
+ static void _DEBUG_SAVE_AAFIR_COEFFS(SAMPLETYPE *coeffs, int len)
+ {
+ FILE *fptr = fopen("aa_filter_coeffs.txt", "wt");
+ if (fptr == nullptr) return;
+
+ for (int i = 0; i < len; i ++)
+ {
+ double temp = coeffs[i];
+ fprintf(fptr, "%lf\n", temp);
+ }
+ fclose(fptr);
+ }
+
+#else
+ #define _DEBUG_SAVE_AAFIR_COEFFS(x, y)
+#endif
+
+/*****************************************************************************
+ *
+ * Implementation of the class 'AAFilter'
+ *
+ *****************************************************************************/
+
+AAFilter::AAFilter(uint len)
+{
+ pFIR = FIRFilter::newInstance();
+ cutoffFreq = 0.5;
+ setLength(len);
+}
+
+
+AAFilter::~AAFilter()
+{
+ delete pFIR;
+}
+
+
+// Sets new anti-alias filter cut-off edge frequency, scaled to
+// sampling frequency (nyquist frequency = 0.5).
+// The filter will cut frequencies higher than the given frequency.
+void AAFilter::setCutoffFreq(double newCutoffFreq)
+{
+ cutoffFreq = newCutoffFreq;
+ calculateCoeffs();
+}
+
+
+// Sets number of FIR filter taps
+void AAFilter::setLength(uint newLength)
+{
+ length = newLength;
+ calculateCoeffs();
+}
+
+
+// Calculates coefficients for a low-pass FIR filter using Hamming window
+void AAFilter::calculateCoeffs()
+{
+ uint i;
+ double cntTemp, temp, tempCoeff,h, w;
+ double wc;
+ double scaleCoeff, sum;
+ double *work;
+ SAMPLETYPE *coeffs;
+
+ assert(length >= 2);
+ assert(length % 4 == 0);
+ assert(cutoffFreq >= 0);
+ assert(cutoffFreq <= 0.5);
+
+ work = new double[length];
+ coeffs = new SAMPLETYPE[length];
+
+ wc = 2.0 * PI * cutoffFreq;
+ tempCoeff = TWOPI / (double)length;
+
+ sum = 0;
+ for (i = 0; i < length; i ++)
+ {
+ cntTemp = (double)i - (double)(length / 2);
+
+ temp = cntTemp * wc;
+ if (temp != 0)
+ {
+ h = sin(temp) / temp; // sinc function
+ }
+ else
+ {
+ h = 1.0;
+ }
+ w = 0.54 + 0.46 * cos(tempCoeff * cntTemp); // hamming window
+
+ temp = w * h;
+ work[i] = temp;
+
+ // calc net sum of coefficients
+ sum += temp;
+ }
+
+ // ensure the sum of coefficients is larger than zero
+ assert(sum > 0);
+
+ // ensure we've really designed a lowpass filter...
+ assert(work[length/2] > 0);
+ assert(work[length/2 + 1] > -1e-6);
+ assert(work[length/2 - 1] > -1e-6);
+
+ // Calculate a scaling coefficient in such a way that the result can be
+ // divided by 16384
+ scaleCoeff = 16384.0f / sum;
+
+ for (i = 0; i < length; i ++)
+ {
+ temp = work[i] * scaleCoeff;
+ // scale & round to nearest integer
+ temp += (temp >= 0) ? 0.5 : -0.5;
+ // ensure no overfloods
+ assert(temp >= -32768 && temp <= 32767);
+ coeffs[i] = (SAMPLETYPE)temp;
+ }
+
+ // Set coefficients. Use divide factor 14 => divide result by 2^14 = 16384
+ pFIR->setCoefficients(coeffs, length, 14);
+
+ _DEBUG_SAVE_AAFIR_COEFFS(coeffs, length);
+
+ delete[] work;
+ delete[] coeffs;
+}
+
+
+// Applies the filter to the given sequence of samples.
+// Note : The amount of outputted samples is by value of 'filter length'
+// smaller than the amount of input samples.
+uint AAFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const
+{
+ return pFIR->evaluate(dest, src, numSamples, numChannels);
+}
+
+
+/// Applies the filter to the given src & dest pipes, so that processed amount of
+/// samples get removed from src, and produced amount added to dest
+/// Note : The amount of outputted samples is by value of 'filter length'
+/// smaller than the amount of input samples.
+uint AAFilter::evaluate(FIFOSampleBuffer &dest, FIFOSampleBuffer &src) const
+{
+ SAMPLETYPE *pdest;
+ const SAMPLETYPE *psrc;
+ uint numSrcSamples;
+ uint result;
+ int numChannels = src.getChannels();
+
+ assert(numChannels == dest.getChannels());
+
+ numSrcSamples = src.numSamples();
+ psrc = src.ptrBegin();
+ pdest = dest.ptrEnd(numSrcSamples);
+ result = pFIR->evaluate(pdest, psrc, numSrcSamples, numChannels);
+ src.receiveSamples(result);
+ dest.putSamples(result);
+
+ return result;
+}
+
+
+uint AAFilter::getLength() const
+{
+ return pFIR->getLength();
+}
diff --git a/third_party/soundtouch/source/SoundTouch/AAFilter.h b/third_party/soundtouch/source/SoundTouch/AAFilter.h
new file mode 100644
index 00000000..5b63ed6e
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/AAFilter.h
@@ -0,0 +1,93 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo
+/// while maintaining the original pitch by using a time domain WSOLA-like method
+/// with several performance-increasing tweaks.
+///
+/// Anti-alias filter is used to prevent folding of high frequencies when
+/// transposing the sample rate with interpolation.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef AAFilter_H
+#define AAFilter_H
+
+#include "STTypes.h"
+#include "FIFOSampleBuffer.h"
+
+namespace soundtouch
+{
+
+class AAFilter
+{
+protected:
+ class FIRFilter *pFIR;
+
+ /// Low-pass filter cut-off frequency, negative = invalid
+ double cutoffFreq;
+
+ /// num of filter taps
+ uint length;
+
+ /// Calculate the FIR coefficients realizing the given cutoff-frequency
+ void calculateCoeffs();
+public:
+ AAFilter(uint length);
+
+ ~AAFilter();
+
+ /// Sets new anti-alias filter cut-off edge frequency, scaled to sampling
+ /// frequency (nyquist frequency = 0.5). The filter will cut off the
+ /// frequencies than that.
+ void setCutoffFreq(double newCutoffFreq);
+
+ /// Sets number of FIR filter taps, i.e. ~filter complexity
+ void setLength(uint newLength);
+
+ uint getLength() const;
+
+ /// Applies the filter to the given sequence of samples.
+ /// Note : The amount of outputted samples is by value of 'filter length'
+ /// smaller than the amount of input samples.
+ uint evaluate(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ uint numSamples,
+ uint numChannels) const;
+
+ /// Applies the filter to the given src & dest pipes, so that processed amount of
+ /// samples get removed from src, and produced amount added to dest
+ /// Note : The amount of outputted samples is by value of 'filter length'
+ /// smaller than the amount of input samples.
+ uint evaluate(FIFOSampleBuffer &dest,
+ FIFOSampleBuffer &src) const;
+
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/source/SoundTouch/BPMDetect.cpp b/third_party/soundtouch/source/SoundTouch/BPMDetect.cpp
new file mode 100644
index 00000000..43763f73
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/BPMDetect.cpp
@@ -0,0 +1,571 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Beats-per-minute (BPM) detection routine.
+///
+/// The beat detection algorithm works as follows:
+/// - Use function 'inputSamples' to input a chunks of samples to the class for
+/// analysis. It's a good idea to enter a large sound file or stream in smallish
+/// chunks of around few kilosamples in order not to extinguish too much RAM memory.
+/// - Inputted sound data is decimated to approx 500 Hz to reduce calculation burden,
+/// which is basically ok as low (bass) frequencies mostly determine the beat rate.
+/// Simple averaging is used for anti-alias filtering because the resulting signal
+/// quality isn't of that high importance.
+/// - Decimated sound data is enveloped, i.e. the amplitude shape is detected by
+/// taking absolute value that's smoothed by sliding average. Signal levels that
+/// are below a couple of times the general RMS amplitude level are cut away to
+/// leave only notable peaks there.
+/// - Repeating sound patterns (e.g. beats) are detected by calculating short-term
+/// autocorrelation function of the enveloped signal.
+/// - After whole sound data file has been analyzed as above, the bpm level is
+/// detected by function 'getBpm' that finds the highest peak of the autocorrelation
+/// function, calculates it's precise location and converts this reading to bpm's.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#define _USE_MATH_DEFINES
+
+#include <math.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <cfloat>
+#include "FIFOSampleBuffer.h"
+#include "PeakFinder.h"
+#include "BPMDetect.h"
+
+using namespace soundtouch;
+
+// algorithm input sample block size
+static const int INPUT_BLOCK_SIZE = 2048;
+
+// decimated sample block size
+static const int DECIMATED_BLOCK_SIZE = 256;
+
+/// Target sample rate after decimation
+static const int TARGET_SRATE = 1000;
+
+/// XCorr update sequence size, update in about 200msec chunks
+static const int XCORR_UPDATE_SEQUENCE = (int)(TARGET_SRATE / 5);
+
+/// Moving average N size
+static const int MOVING_AVERAGE_N = 15;
+
+/// XCorr decay time constant, decay to half in 30 seconds
+/// If it's desired to have the system adapt quicker to beat rate
+/// changes within a continuing music stream, then the
+/// 'xcorr_decay_time_constant' value can be reduced, yet that
+/// can increase possibility of glitches in bpm detection.
+static const double XCORR_DECAY_TIME_CONSTANT = 30.0;
+
+/// Data overlap factor for beat detection algorithm
+static const int OVERLAP_FACTOR = 4;
+
+static const double TWOPI = (2 * M_PI);
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Enable following define to create bpm analysis file:
+
+//#define _CREATE_BPM_DEBUG_FILE
+
+#ifdef _CREATE_BPM_DEBUG_FILE
+
+ static void _SaveDebugData(const char *name, const float *data, int minpos, int maxpos, double coeff)
+ {
+ FILE *fptr = fopen(name, "wt");
+ int i;
+
+ if (fptr)
+ {
+ printf("\nWriting BPM debug data into file %s\n", name);
+ for (i = minpos; i < maxpos; i ++)
+ {
+ fprintf(fptr, "%d\t%.1lf\t%f\n", i, coeff / (double)i, data[i]);
+ }
+ fclose(fptr);
+ }
+ }
+
+ void _SaveDebugBeatPos(const char *name, const std::vector<BEAT> &beats)
+ {
+ printf("\nWriting beat detections data into file %s\n", name);
+
+ FILE *fptr = fopen(name, "wt");
+ if (fptr)
+ {
+ for (uint i = 0; i < beats.size(); i++)
+ {
+ BEAT b = beats[i];
+ fprintf(fptr, "%lf\t%lf\n", b.pos, b.strength);
+ }
+ fclose(fptr);
+ }
+ }
+#else
+ #define _SaveDebugData(name, a,b,c,d)
+ #define _SaveDebugBeatPos(name, b)
+#endif
+
+// Hamming window
+void hamming(float *w, int N)
+{
+ for (int i = 0; i < N; i++)
+ {
+ w[i] = (float)(0.54 - 0.46 * cos(TWOPI * i / (N - 1)));
+ }
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// IIR2_filter - 2nd order IIR filter
+
+IIR2_filter::IIR2_filter(const double *lpf_coeffs)
+{
+ memcpy(coeffs, lpf_coeffs, 5 * sizeof(double));
+ memset(prev, 0, sizeof(prev));
+}
+
+
+float IIR2_filter::update(float x)
+{
+ prev[0] = x;
+ double y = x * coeffs[0];
+
+ for (int i = 4; i >= 1; i--)
+ {
+ y += coeffs[i] * prev[i];
+ prev[i] = prev[i - 1];
+ }
+
+ prev[3] = y;
+ return (float)y;
+}
+
+
+// IIR low-pass filter coefficients, calculated with matlab/octave cheby2(2,40,0.05)
+const double _LPF_coeffs[5] = { 0.00996655391939, -0.01944529148401, 0.00996655391939, 1.96867605796247, -0.96916387431724 };
+
+////////////////////////////////////////////////////////////////////////////////
+
+BPMDetect::BPMDetect(int numChannels, int aSampleRate) :
+ beat_lpf(_LPF_coeffs)
+{
+ beats.reserve(250); // initial reservation to prevent frequent reallocation
+
+ this->sampleRate = aSampleRate;
+ this->channels = numChannels;
+
+ decimateSum = 0;
+ decimateCount = 0;
+
+ // choose decimation factor so that result is approx. 1000 Hz
+ decimateBy = sampleRate / TARGET_SRATE;
+ if ((decimateBy <= 0) || (decimateBy * DECIMATED_BLOCK_SIZE < INPUT_BLOCK_SIZE))
+ {
+ ST_THROW_RT_ERROR("Too small samplerate");
+ }
+
+ // Calculate window length & starting item according to desired min & max bpms
+ windowLen = (60 * sampleRate) / (decimateBy * MIN_BPM);
+ windowStart = (60 * sampleRate) / (decimateBy * MAX_BPM_RANGE);
+
+ assert(windowLen > windowStart);
+
+ // allocate new working objects
+ xcorr = new float[windowLen];
+ memset(xcorr, 0, windowLen * sizeof(float));
+
+ pos = 0;
+ peakPos = 0;
+ peakVal = 0;
+ init_scaler = 1;
+ beatcorr_ringbuffpos = 0;
+ beatcorr_ringbuff = new float[windowLen];
+ memset(beatcorr_ringbuff, 0, windowLen * sizeof(float));
+
+ // allocate processing buffer
+ buffer = new FIFOSampleBuffer();
+ // we do processing in mono mode
+ buffer->setChannels(1);
+ buffer->clear();
+
+ // calculate hamming windows
+ hamw = new float[XCORR_UPDATE_SEQUENCE];
+ hamming(hamw, XCORR_UPDATE_SEQUENCE);
+ hamw2 = new float[XCORR_UPDATE_SEQUENCE / 2];
+ hamming(hamw2, XCORR_UPDATE_SEQUENCE / 2);
+}
+
+
+BPMDetect::~BPMDetect()
+{
+ delete[] xcorr;
+ delete[] beatcorr_ringbuff;
+ delete[] hamw;
+ delete[] hamw2;
+ delete buffer;
+}
+
+
+/// convert to mono, low-pass filter & decimate to about 500 Hz.
+/// return number of outputted samples.
+///
+/// Decimation is used to remove the unnecessary frequencies and thus to reduce
+/// the amount of data needed to be processed as calculating autocorrelation
+/// function is a very-very heavy operation.
+///
+/// Anti-alias filtering is done simply by averaging the samples. This is really a
+/// poor-man's anti-alias filtering, but it's not so critical in this kind of application
+/// (it'd also be difficult to design a high-quality filter with steep cut-off at very
+/// narrow band)
+int BPMDetect::decimate(SAMPLETYPE *dest, const SAMPLETYPE *src, int numsamples)
+{
+ int count, outcount;
+ LONG_SAMPLETYPE out;
+
+ assert(channels > 0);
+ assert(decimateBy > 0);
+ outcount = 0;
+ for (count = 0; count < numsamples; count ++)
+ {
+ int j;
+
+ // convert to mono and accumulate
+ for (j = 0; j < channels; j ++)
+ {
+ decimateSum += src[j];
+ }
+ src += j;
+
+ decimateCount ++;
+ if (decimateCount >= decimateBy)
+ {
+ // Store every Nth sample only
+ out = (LONG_SAMPLETYPE)(decimateSum / (decimateBy * channels));
+ decimateSum = 0;
+ decimateCount = 0;
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ // check ranges for sure (shouldn't actually be necessary)
+ if (out > 32767)
+ {
+ out = 32767;
+ }
+ else if (out < -32768)
+ {
+ out = -32768;
+ }
+#endif // SOUNDTOUCH_INTEGER_SAMPLES
+ dest[outcount] = (SAMPLETYPE)out;
+ outcount ++;
+ }
+ }
+ return outcount;
+}
+
+
+// Calculates autocorrelation function of the sample history buffer
+void BPMDetect::updateXCorr(int process_samples)
+{
+ int offs;
+ SAMPLETYPE *pBuffer;
+
+ assert(buffer->numSamples() >= (uint)(process_samples + windowLen));
+ assert(process_samples == XCORR_UPDATE_SEQUENCE);
+
+ pBuffer = buffer->ptrBegin();
+
+ // calculate decay factor for xcorr filtering
+ float xcorr_decay = (float)pow(0.5, process_samples / (XCORR_DECAY_TIME_CONSTANT * TARGET_SRATE));
+
+ // prescale pbuffer
+ float tmp[XCORR_UPDATE_SEQUENCE];
+ for (int i = 0; i < process_samples; i++)
+ {
+ tmp[i] = hamw[i] * hamw[i] * pBuffer[i];
+ }
+
+ #pragma omp parallel for
+ for (offs = windowStart; offs < windowLen; offs ++)
+ {
+ float sum;
+ int i;
+
+ sum = 0;
+ for (i = 0; i < process_samples; i ++)
+ {
+ sum += tmp[i] * pBuffer[i + offs]; // scaling the sub-result shouldn't be necessary
+ }
+ xcorr[offs] *= xcorr_decay; // decay 'xcorr' here with suitable time constant.
+
+ xcorr[offs] += (float)fabs(sum);
+ }
+}
+
+
+// Detect individual beat positions
+void BPMDetect::updateBeatPos(int process_samples)
+{
+ SAMPLETYPE *pBuffer;
+
+ assert(buffer->numSamples() >= (uint)(process_samples + windowLen));
+
+ pBuffer = buffer->ptrBegin();
+ assert(process_samples == XCORR_UPDATE_SEQUENCE / 2);
+
+ // static double thr = 0.0003;
+ double posScale = (double)this->decimateBy / (double)this->sampleRate;
+ int resetDur = (int)(0.12 / posScale + 0.5);
+
+ // prescale pbuffer
+ float tmp[XCORR_UPDATE_SEQUENCE / 2];
+ for (int i = 0; i < process_samples; i++)
+ {
+ tmp[i] = hamw2[i] * hamw2[i] * pBuffer[i];
+ }
+
+ #pragma omp parallel for
+ for (int offs = windowStart; offs < windowLen; offs++)
+ {
+ float sum = 0;
+ for (int i = 0; i < process_samples; i++)
+ {
+ sum += tmp[i] * pBuffer[offs + i];
+ }
+ beatcorr_ringbuff[(beatcorr_ringbuffpos + offs) % windowLen] += (float)((sum > 0) ? sum : 0); // accumulate only positive correlations
+ }
+
+ int skipstep = XCORR_UPDATE_SEQUENCE / OVERLAP_FACTOR;
+
+ // compensate empty buffer at beginning by scaling coefficient
+ float scale = (float)windowLen / (float)(skipstep * init_scaler);
+ if (scale > 1.0f)
+ {
+ init_scaler++;
+ }
+ else
+ {
+ scale = 1.0f;
+ }
+
+ // detect beats
+ for (int i = 0; i < skipstep; i++)
+ {
+ float sum = beatcorr_ringbuff[beatcorr_ringbuffpos];
+ sum -= beat_lpf.update(sum);
+
+ if (sum > peakVal)
+ {
+ // found new local largest value
+ peakVal = sum;
+ peakPos = pos;
+ }
+ if (pos > peakPos + resetDur)
+ {
+ // largest value not updated for 200msec => accept as beat
+ peakPos += skipstep;
+ if (peakVal > 0)
+ {
+ // add detected beat to end of "beats" vector
+ BEAT temp = { (float)(peakPos * posScale), (float)(peakVal * scale) };
+ beats.push_back(temp);
+ }
+
+ peakVal = 0;
+ peakPos = pos;
+ }
+
+ beatcorr_ringbuff[beatcorr_ringbuffpos] = 0;
+ pos++;
+ beatcorr_ringbuffpos = (beatcorr_ringbuffpos + 1) % windowLen;
+ }
+}
+
+
+#define max(x,y) ((x) > (y) ? (x) : (y))
+
+void BPMDetect::inputSamples(const SAMPLETYPE *samples, int numSamples)
+{
+ SAMPLETYPE decimated[DECIMATED_BLOCK_SIZE];
+
+ // iterate so that max INPUT_BLOCK_SAMPLES processed per iteration
+ while (numSamples > 0)
+ {
+ int block;
+ int decSamples;
+
+ block = (numSamples > INPUT_BLOCK_SIZE) ? INPUT_BLOCK_SIZE : numSamples;
+
+ // decimate. note that converts to mono at the same time
+ decSamples = decimate(decimated, samples, block);
+ samples += block * channels;
+ numSamples -= block;
+
+ buffer->putSamples(decimated, decSamples);
+ }
+
+ // when the buffer has enough samples for processing...
+ int req = max(windowLen + XCORR_UPDATE_SEQUENCE, 2 * XCORR_UPDATE_SEQUENCE);
+ while ((int)buffer->numSamples() >= req)
+ {
+ // ... update autocorrelations...
+ updateXCorr(XCORR_UPDATE_SEQUENCE);
+ // ...update beat position calculation...
+ updateBeatPos(XCORR_UPDATE_SEQUENCE / 2);
+ // ... and remove proceessed samples from the buffer
+ int n = XCORR_UPDATE_SEQUENCE / OVERLAP_FACTOR;
+ buffer->receiveSamples(n);
+ }
+}
+
+
+void BPMDetect::removeBias()
+{
+ int i;
+
+ // Remove linear bias: calculate linear regression coefficient
+ // 1. calc mean of 'xcorr' and 'i'
+ double mean_i = 0;
+ double mean_x = 0;
+ for (i = windowStart; i < windowLen; i++)
+ {
+ mean_x += xcorr[i];
+ }
+ mean_x /= (windowLen - windowStart);
+ mean_i = 0.5 * (windowLen - 1 + windowStart);
+
+ // 2. calculate linear regression coefficient
+ double b = 0;
+ double div = 0;
+ for (i = windowStart; i < windowLen; i++)
+ {
+ double xt = xcorr[i] - mean_x;
+ double xi = i - mean_i;
+ b += xt * xi;
+ div += xi * xi;
+ }
+ b /= div;
+
+ // subtract linear regression and resolve min. value bias
+ float minval = FLT_MAX; // arbitrary large number
+ for (i = windowStart; i < windowLen; i ++)
+ {
+ xcorr[i] -= (float)(b * i);
+ if (xcorr[i] < minval)
+ {
+ minval = xcorr[i];
+ }
+ }
+
+ // subtract min.value
+ for (i = windowStart; i < windowLen; i ++)
+ {
+ xcorr[i] -= minval;
+ }
+}
+
+
+// Calculate N-point moving average for "source" values
+void MAFilter(float *dest, const float *source, int start, int end, int N)
+{
+ for (int i = start; i < end; i++)
+ {
+ int i1 = i - N / 2;
+ int i2 = i + N / 2 + 1;
+ if (i1 < start) i1 = start;
+ if (i2 > end) i2 = end;
+
+ double sum = 0;
+ for (int j = i1; j < i2; j ++)
+ {
+ sum += source[j];
+ }
+ dest[i] = (float)(sum / (i2 - i1));
+ }
+}
+
+
+float BPMDetect::getBpm()
+{
+ double peakPos;
+ double coeff;
+ PeakFinder peakFinder;
+
+ // remove bias from xcorr data
+ removeBias();
+
+ coeff = 60.0 * ((double)sampleRate / (double)decimateBy);
+
+ // save bpm debug data if debug data writing enabled
+ _SaveDebugData("soundtouch-bpm-xcorr.txt", xcorr, windowStart, windowLen, coeff);
+
+ // Smoothen by N-point moving-average
+ float *data = new float[windowLen];
+ memset(data, 0, sizeof(float) * windowLen);
+ MAFilter(data, xcorr, windowStart, windowLen, MOVING_AVERAGE_N);
+
+ // find peak position
+ peakPos = peakFinder.detectPeak(data, windowStart, windowLen);
+
+ // save bpm debug data if debug data writing enabled
+ _SaveDebugData("soundtouch-bpm-smoothed.txt", data, windowStart, windowLen, coeff);
+
+ delete[] data;
+
+ assert(decimateBy != 0);
+ if (peakPos < 1e-9) return 0.0; // detection failed.
+
+ _SaveDebugBeatPos("soundtouch-detected-beats.txt", beats);
+
+ // calculate BPM
+ float bpm = (float)(coeff / peakPos);
+ return (bpm >= MIN_BPM && bpm <= MAX_BPM_VALID) ? bpm : 0;
+}
+
+
+/// Get beat position arrays. Note: The array includes also really low beat detection values
+/// in absence of clear strong beats. Consumer may wish to filter low values away.
+/// - "pos" receive array of beat positions
+/// - "values" receive array of beat detection strengths
+/// - max_num indicates max.size of "pos" and "values" array.
+///
+/// You can query a suitable array sized by calling this with nullptr in "pos" & "values".
+///
+/// \return number of beats in the arrays.
+int BPMDetect::getBeats(float *pos, float *values, int max_num)
+{
+ int num = (int)beats.size();
+ if ((!pos) || (!values)) return num; // pos or values nullptr, return just size
+
+ for (int i = 0; (i < num) && (i < max_num); i++)
+ {
+ pos[i] = beats[i].pos;
+ values[i] = beats[i].strength;
+ }
+ return num;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp b/third_party/soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp
new file mode 100644
index 00000000..9e0d5b24
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp
@@ -0,0 +1,275 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// A buffer class for temporarily storaging sound samples, operates as a
+/// first-in-first-out pipe.
+///
+/// Samples are added to the end of the sample buffer with the 'putSamples'
+/// function, and are received from the beginning of the buffer by calling
+/// the 'receiveSamples' function. The class automatically removes the
+/// outputted samples from the buffer, as well as grows the buffer size
+/// whenever necessary.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <stdlib.h>
+#include <memory.h>
+#include <string.h>
+#include <assert.h>
+
+#include "FIFOSampleBuffer.h"
+
+using namespace soundtouch;
+
+// Constructor
+FIFOSampleBuffer::FIFOSampleBuffer(int numChannels)
+{
+ assert(numChannels > 0);
+ sizeInBytes = 0; // reasonable initial value
+ buffer = nullptr;
+ bufferUnaligned = nullptr;
+ samplesInBuffer = 0;
+ bufferPos = 0;
+ channels = (uint)numChannels;
+ ensureCapacity(32); // allocate initial capacity
+}
+
+
+// destructor
+FIFOSampleBuffer::~FIFOSampleBuffer()
+{
+ delete[] bufferUnaligned;
+ bufferUnaligned = nullptr;
+ buffer = nullptr;
+}
+
+
+// Sets number of channels, 1 = mono, 2 = stereo
+void FIFOSampleBuffer::setChannels(int numChannels)
+{
+ uint usedBytes;
+
+ if (!verifyNumberOfChannels(numChannels)) return;
+
+ usedBytes = channels * samplesInBuffer;
+ channels = (uint)numChannels;
+ samplesInBuffer = usedBytes / channels;
+}
+
+
+// if output location pointer 'bufferPos' isn't zero, 'rewinds' the buffer and
+// zeroes this pointer by copying samples from the 'bufferPos' pointer
+// location on to the beginning of the buffer.
+void FIFOSampleBuffer::rewind()
+{
+ if (buffer && bufferPos)
+ {
+ memmove(buffer, ptrBegin(), sizeof(SAMPLETYPE) * channels * samplesInBuffer);
+ bufferPos = 0;
+ }
+}
+
+
+// Adds 'numSamples' pcs of samples from the 'samples' memory position to
+// the sample buffer.
+void FIFOSampleBuffer::putSamples(const SAMPLETYPE *samples, uint nSamples)
+{
+ memcpy(ptrEnd(nSamples), samples, sizeof(SAMPLETYPE) * nSamples * channels);
+ samplesInBuffer += nSamples;
+}
+
+
+// Increases the number of samples in the buffer without copying any actual
+// samples.
+//
+// This function is used to update the number of samples in the sample buffer
+// when accessing the buffer directly with 'ptrEnd' function. Please be
+// careful though!
+void FIFOSampleBuffer::putSamples(uint nSamples)
+{
+ uint req;
+
+ req = samplesInBuffer + nSamples;
+ ensureCapacity(req);
+ samplesInBuffer += nSamples;
+}
+
+
+// Returns a pointer to the end of the used part of the sample buffer (i.e.
+// where the new samples are to be inserted). This function may be used for
+// inserting new samples into the sample buffer directly. Please be careful!
+//
+// Parameter 'slackCapacity' tells the function how much free capacity (in
+// terms of samples) there _at least_ should be, in order to the caller to
+// successfully insert all the required samples to the buffer. When necessary,
+// the function grows the buffer size to comply with this requirement.
+//
+// When using this function as means for inserting new samples, also remember
+// to increase the sample count afterwards, by calling the
+// 'putSamples(numSamples)' function.
+SAMPLETYPE *FIFOSampleBuffer::ptrEnd(uint slackCapacity)
+{
+ ensureCapacity(samplesInBuffer + slackCapacity);
+ return buffer + samplesInBuffer * channels;
+}
+
+
+// Returns a pointer to the beginning of the currently non-outputted samples.
+// This function is provided for accessing the output samples directly.
+// Please be careful!
+//
+// When using this function to output samples, also remember to 'remove' the
+// outputted samples from the buffer by calling the
+// 'receiveSamples(numSamples)' function
+SAMPLETYPE *FIFOSampleBuffer::ptrBegin()
+{
+ assert(buffer);
+ return buffer + bufferPos * channels;
+}
+
+
+// Ensures that the buffer has enough capacity, i.e. space for _at least_
+// 'capacityRequirement' number of samples. The buffer is grown in steps of
+// 4 kilobytes to eliminate the need for frequently growing up the buffer,
+// as well as to round the buffer size up to the virtual memory page size.
+void FIFOSampleBuffer::ensureCapacity(uint capacityRequirement)
+{
+ SAMPLETYPE *tempUnaligned, *temp;
+
+ if (capacityRequirement > getCapacity())
+ {
+ // enlarge the buffer in 4kbyte steps (round up to next 4k boundary)
+ sizeInBytes = (capacityRequirement * channels * sizeof(SAMPLETYPE) + 4095) & (uint)-4096;
+ assert(sizeInBytes % 2 == 0);
+ tempUnaligned = new SAMPLETYPE[sizeInBytes / sizeof(SAMPLETYPE) + 16 / sizeof(SAMPLETYPE)];
+ if (tempUnaligned == nullptr)
+ {
+ ST_THROW_RT_ERROR("Couldn't allocate memory!\n");
+ }
+ // Align the buffer to begin at 16byte cache line boundary for optimal performance
+ temp = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(tempUnaligned);
+ if (samplesInBuffer)
+ {
+ memcpy(temp, ptrBegin(), samplesInBuffer * channels * sizeof(SAMPLETYPE));
+ }
+ delete[] bufferUnaligned;
+ buffer = temp;
+ bufferUnaligned = tempUnaligned;
+ bufferPos = 0;
+ }
+ else
+ {
+ // simply rewind the buffer (if necessary)
+ rewind();
+ }
+}
+
+
+// Returns the current buffer capacity in terms of samples
+uint FIFOSampleBuffer::getCapacity() const
+{
+ return sizeInBytes / (channels * sizeof(SAMPLETYPE));
+}
+
+
+// Returns the number of samples currently in the buffer
+uint FIFOSampleBuffer::numSamples() const
+{
+ return samplesInBuffer;
+}
+
+
+// Output samples from beginning of the sample buffer. Copies demanded number
+// of samples to output and removes them from the sample buffer. If there
+// are less than 'numsample' samples in the buffer, returns all available.
+//
+// Returns number of samples copied.
+uint FIFOSampleBuffer::receiveSamples(SAMPLETYPE *output, uint maxSamples)
+{
+ uint num;
+
+ num = (maxSamples > samplesInBuffer) ? samplesInBuffer : maxSamples;
+
+ memcpy(output, ptrBegin(), channels * sizeof(SAMPLETYPE) * num);
+ return receiveSamples(num);
+}
+
+
+// Removes samples from the beginning of the sample buffer without copying them
+// anywhere. Used to reduce the number of samples in the buffer, when accessing
+// the sample buffer with the 'ptrBegin' function.
+uint FIFOSampleBuffer::receiveSamples(uint maxSamples)
+{
+ if (maxSamples >= samplesInBuffer)
+ {
+ uint temp;
+
+ temp = samplesInBuffer;
+ samplesInBuffer = 0;
+ return temp;
+ }
+
+ samplesInBuffer -= maxSamples;
+ bufferPos += maxSamples;
+
+ return maxSamples;
+}
+
+
+// Returns nonzero if the sample buffer is empty
+int FIFOSampleBuffer::isEmpty() const
+{
+ return (samplesInBuffer == 0) ? 1 : 0;
+}
+
+
+// Clears the sample buffer
+void FIFOSampleBuffer::clear()
+{
+ samplesInBuffer = 0;
+ bufferPos = 0;
+}
+
+
+/// allow trimming (downwards) amount of samples in pipeline.
+/// Returns adjusted amount of samples
+uint FIFOSampleBuffer::adjustAmountOfSamples(uint numSamples)
+{
+ if (numSamples < samplesInBuffer)
+ {
+ samplesInBuffer = numSamples;
+ }
+ return samplesInBuffer;
+}
+
+
+/// Add silence to end of buffer
+void FIFOSampleBuffer::addSilent(uint nSamples)
+{
+ memset(ptrEnd(nSamples), 0, sizeof(SAMPLETYPE) * nSamples * channels);
+ samplesInBuffer += nSamples;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/FIRFilter.cpp b/third_party/soundtouch/source/SoundTouch/FIRFilter.cpp
new file mode 100644
index 00000000..5937a03d
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/FIRFilter.cpp
@@ -0,0 +1,314 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// General FIR digital filter routines with MMX optimization.
+///
+/// Notes : MMX optimized functions reside in a separate, platform-specific file,
+/// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
+///
+/// This source file contains OpenMP optimizations that allow speeding up the
+/// corss-correlation algorithm by executing it in several threads / CPU cores
+/// in parallel. See the following article link for more detailed discussion
+/// about SoundTouch OpenMP optimizations:
+/// http://www.softwarecoven.com/parallel-computing-in-embedded-mobile-devices
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <memory.h>
+#include <assert.h>
+#include <math.h>
+#include <stdlib.h>
+#include "FIRFilter.h"
+#include "cpu_detect.h"
+
+using namespace soundtouch;
+
+/*****************************************************************************
+ *
+ * Implementation of the class 'FIRFilter'
+ *
+ *****************************************************************************/
+
+FIRFilter::FIRFilter()
+{
+ resultDivFactor = 0;
+ length = 0;
+ lengthDiv8 = 0;
+ filterCoeffs = nullptr;
+ filterCoeffsStereo = nullptr;
+}
+
+
+FIRFilter::~FIRFilter()
+{
+ delete[] filterCoeffs;
+ delete[] filterCoeffsStereo;
+}
+
+
+// Usual C-version of the filter routine for stereo sound
+uint FIRFilter::evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
+{
+ int j, end;
+ // hint compiler autovectorization that loop length is divisible by 8
+ uint ilength = length & -8;
+
+ assert((length != 0) && (length == ilength) && (src != nullptr) && (dest != nullptr) && (filterCoeffs != nullptr));
+ assert(numSamples >= ilength);
+
+ end = 2 * (numSamples - ilength);
+
+ #pragma omp parallel for
+ for (j = 0; j < end; j += 2)
+ {
+ const SAMPLETYPE *ptr;
+ LONG_SAMPLETYPE suml, sumr;
+
+ suml = sumr = 0;
+ ptr = src + j;
+
+ for (uint i = 0; i < ilength; i ++)
+ {
+ suml += ptr[2 * i] * filterCoeffsStereo[2 * i];
+ sumr += ptr[2 * i + 1] * filterCoeffsStereo[2 * i + 1];
+ }
+
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ suml >>= resultDivFactor;
+ sumr >>= resultDivFactor;
+ // saturate to 16 bit integer limits
+ suml = (suml < -32768) ? -32768 : (suml > 32767) ? 32767 : suml;
+ // saturate to 16 bit integer limits
+ sumr = (sumr < -32768) ? -32768 : (sumr > 32767) ? 32767 : sumr;
+#endif // SOUNDTOUCH_INTEGER_SAMPLES
+ dest[j] = (SAMPLETYPE)suml;
+ dest[j + 1] = (SAMPLETYPE)sumr;
+ }
+ return numSamples - ilength;
+}
+
+
+// Usual C-version of the filter routine for mono sound
+uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const
+{
+ int j, end;
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = length & -8;
+
+ assert(ilength != 0);
+
+ end = numSamples - ilength;
+ #pragma omp parallel for
+ for (j = 0; j < end; j ++)
+ {
+ const SAMPLETYPE *pSrc = src + j;
+ LONG_SAMPLETYPE sum;
+ int i;
+
+ sum = 0;
+ for (i = 0; i < ilength; i ++)
+ {
+ sum += pSrc[i] * filterCoeffs[i];
+ }
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ sum >>= resultDivFactor;
+ // saturate to 16 bit integer limits
+ sum = (sum < -32768) ? -32768 : (sum > 32767) ? 32767 : sum;
+#endif // SOUNDTOUCH_INTEGER_SAMPLES
+ dest[j] = (SAMPLETYPE)sum;
+ }
+ return end;
+}
+
+
+uint FIRFilter::evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels)
+{
+ int j, end;
+
+ assert(length != 0);
+ assert(src != nullptr);
+ assert(dest != nullptr);
+ assert(filterCoeffs != nullptr);
+ assert(numChannels <= SOUNDTOUCH_MAX_CHANNELS);
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = length & -8;
+
+ end = numChannels * (numSamples - ilength);
+
+ #pragma omp parallel for
+ for (j = 0; j < end; j += numChannels)
+ {
+ const SAMPLETYPE *ptr;
+ LONG_SAMPLETYPE sums[SOUNDTOUCH_MAX_CHANNELS];
+ uint c;
+ int i;
+
+ for (c = 0; c < numChannels; c ++)
+ {
+ sums[c] = 0;
+ }
+
+ ptr = src + j;
+
+ for (i = 0; i < ilength; i ++)
+ {
+ SAMPLETYPE coef=filterCoeffs[i];
+ for (c = 0; c < numChannels; c ++)
+ {
+ sums[c] += ptr[0] * coef;
+ ptr ++;
+ }
+ }
+
+ for (c = 0; c < numChannels; c ++)
+ {
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ sums[c] >>= resultDivFactor;
+#endif // SOUNDTOUCH_INTEGER_SAMPLES
+ dest[j+c] = (SAMPLETYPE)sums[c];
+ }
+ }
+ return numSamples - ilength;
+}
+
+
+// Set filter coeffiecients and length.
+//
+// Throws an exception if filter length isn't divisible by 8
+void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor)
+{
+ assert(newLength > 0);
+ if (newLength % 8) ST_THROW_RT_ERROR("FIR filter length not divisible by 8");
+
+ lengthDiv8 = newLength / 8;
+ length = lengthDiv8 * 8;
+ assert(length == newLength);
+
+ resultDivFactor = uResultDivFactor;
+
+ delete[] filterCoeffs;
+ filterCoeffs = new SAMPLETYPE[length];
+ delete[] filterCoeffsStereo;
+ filterCoeffsStereo = new SAMPLETYPE[length*2];
+
+#ifdef SOUNDTOUCH_FLOAT_SAMPLES
+ // scale coefficients already here if using floating samples
+ const double scale = ::pow(0.5, (int)resultDivFactor);;
+#else
+ const short scale = 1;
+#endif
+
+ for (uint i = 0; i < length; i ++)
+ {
+ filterCoeffs[i] = (SAMPLETYPE)(coeffs[i] * scale);
+ // create also stereo set of filter coefficients: this allows compiler
+ // to autovectorize filter evaluation much more efficiently
+ filterCoeffsStereo[2 * i] = (SAMPLETYPE)(coeffs[i] * scale);
+ filterCoeffsStereo[2 * i + 1] = (SAMPLETYPE)(coeffs[i] * scale);
+ }
+}
+
+
+uint FIRFilter::getLength() const
+{
+ return length;
+}
+
+
+// Applies the filter to the given sequence of samples.
+//
+// Note : The amount of outputted samples is by value of 'filter_length'
+// smaller than the amount of input samples.
+uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels)
+{
+ assert(length > 0);
+ assert(lengthDiv8 * 8 == length);
+
+ if (numSamples < length) return 0;
+
+#ifndef USE_MULTICH_ALWAYS
+ if (numChannels == 1)
+ {
+ return evaluateFilterMono(dest, src, numSamples);
+ }
+ else if (numChannels == 2)
+ {
+ return evaluateFilterStereo(dest, src, numSamples);
+ }
+ else
+#endif // USE_MULTICH_ALWAYS
+ {
+ assert(numChannels > 0);
+ return evaluateFilterMulti(dest, src, numSamples, numChannels);
+ }
+}
+
+
+// Operator 'new' is overloaded so that it automatically creates a suitable instance
+// depending on if we've a MMX-capable CPU available or not.
+void * FIRFilter::operator new(size_t)
+{
+ // Notice! don't use "new FIRFilter" directly, use "newInstance" to create a new instance instead!
+ ST_THROW_RT_ERROR("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!");
+ return newInstance();
+}
+
+
+FIRFilter * FIRFilter::newInstance()
+{
+ uint uExtensions;
+
+ uExtensions = detectCPUextensions();
+ (void)uExtensions;
+
+ // Check if MMX/SSE instruction set extensions supported by CPU
+
+#ifdef SOUNDTOUCH_ALLOW_MMX
+ // MMX routines available only with integer sample types
+ if (uExtensions & SUPPORT_MMX)
+ {
+ return ::new FIRFilterMMX;
+ }
+ else
+#endif // SOUNDTOUCH_ALLOW_MMX
+
+#ifdef SOUNDTOUCH_ALLOW_SSE
+ if (uExtensions & SUPPORT_SSE)
+ {
+ // SSE support
+ return ::new FIRFilterSSE;
+ }
+ else
+#endif // SOUNDTOUCH_ALLOW_SSE
+
+ {
+ // ISA optimizations not supported, use plain C version
+ return ::new FIRFilter;
+ }
+}
diff --git a/third_party/soundtouch/source/SoundTouch/FIRFilter.h b/third_party/soundtouch/source/SoundTouch/FIRFilter.h
new file mode 100644
index 00000000..0ae50831
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/FIRFilter.h
@@ -0,0 +1,137 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// General FIR digital filter routines with MMX optimization.
+///
+/// Note : MMX optimized functions reside in a separate, platform-specific file,
+/// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef FIRFilter_H
+#define FIRFilter_H
+
+#include <stddef.h>
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+class FIRFilter
+{
+protected:
+ // Number of FIR filter taps
+ uint length;
+ // Number of FIR filter taps divided by 8
+ uint lengthDiv8;
+
+ // Result divider factor in 2^k format
+ uint resultDivFactor;
+
+ // Memory for filter coefficients
+ SAMPLETYPE *filterCoeffs;
+ SAMPLETYPE *filterCoeffsStereo;
+
+ virtual uint evaluateFilterStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ uint numSamples) const;
+ virtual uint evaluateFilterMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ uint numSamples) const;
+ virtual uint evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels);
+
+public:
+ FIRFilter();
+ virtual ~FIRFilter();
+
+ /// Operator 'new' is overloaded so that it automatically creates a suitable instance
+ /// depending on if we've a MMX-capable CPU available or not.
+ static void * operator new(size_t s);
+
+ static FIRFilter *newInstance();
+
+ /// Applies the filter to the given sequence of samples.
+ /// Note : The amount of outputted samples is by value of 'filter_length'
+ /// smaller than the amount of input samples.
+ ///
+ /// \return Number of samples copied to 'dest'.
+ uint evaluate(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ uint numSamples,
+ uint numChannels);
+
+ uint getLength() const;
+
+ virtual void setCoefficients(const SAMPLETYPE *coeffs,
+ uint newLength,
+ uint uResultDivFactor);
+};
+
+
+// Optional subclasses that implement CPU-specific optimizations:
+
+#ifdef SOUNDTOUCH_ALLOW_MMX
+
+/// Class that implements MMX optimized functions exclusive for 16bit integer samples type.
+ class FIRFilterMMX : public FIRFilter
+ {
+ protected:
+ short *filterCoeffsUnalign;
+ short *filterCoeffsAlign;
+
+ virtual uint evaluateFilterStereo(short *dest, const short *src, uint numSamples) const override;
+ public:
+ FIRFilterMMX();
+ ~FIRFilterMMX();
+
+ virtual void setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor) override;
+ };
+
+#endif // SOUNDTOUCH_ALLOW_MMX
+
+
+#ifdef SOUNDTOUCH_ALLOW_SSE
+ /// Class that implements SSE optimized functions exclusive for floating point samples type.
+ class FIRFilterSSE : public FIRFilter
+ {
+ protected:
+ float *filterCoeffsUnalign;
+ float *filterCoeffsAlign;
+
+ virtual uint evaluateFilterStereo(float *dest, const float *src, uint numSamples) const override;
+ public:
+ FIRFilterSSE();
+ ~FIRFilterSSE();
+
+ virtual void setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor) override;
+ };
+
+#endif // SOUNDTOUCH_ALLOW_SSE
+
+}
+
+#endif // FIRFilter_H
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateCubic.cpp b/third_party/soundtouch/source/SoundTouch/InterpolateCubic.cpp
new file mode 100644
index 00000000..fe4c98a8
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateCubic.cpp
@@ -0,0 +1,196 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Cubic interpolation routine.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <stddef.h>
+#include <math.h>
+#include "InterpolateCubic.h"
+#include "STTypes.h"
+
+using namespace soundtouch;
+
+// cubic interpolation coefficients
+static const float _coeffs[]=
+{ -0.5f, 1.0f, -0.5f, 0.0f,
+ 1.5f, -2.5f, 0.0f, 1.0f,
+ -1.5f, 2.0f, 0.5f, 0.0f,
+ 0.5f, -0.5f, 0.0f, 0.0f};
+
+
+InterpolateCubic::InterpolateCubic()
+{
+ fract = 0;
+}
+
+
+void InterpolateCubic::resetRegisters()
+{
+ fract = 0;
+}
+
+
+/// Transpose mono audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateCubic::transposeMono(SAMPLETYPE *pdest,
+ const SAMPLETYPE *psrc,
+ int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 4;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ float out;
+ const float x3 = 1.0f;
+ const float x2 = (float)fract; // x
+ const float x1 = x2*x2; // x^2
+ const float x0 = x1*x2; // x^3
+ float y0, y1, y2, y3;
+
+ assert(fract < 1.0);
+
+ y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3;
+ y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3;
+ y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3;
+ y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3;
+
+ out = y0 * psrc[0] + y1 * psrc[1] + y2 * psrc[2] + y3 * psrc[3];
+
+ pdest[i] = (SAMPLETYPE)out;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ psrc += whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+/// Transpose stereo audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateCubic::transposeStereo(SAMPLETYPE *pdest,
+ const SAMPLETYPE *psrc,
+ int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 4;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ const float x3 = 1.0f;
+ const float x2 = (float)fract; // x
+ const float x1 = x2*x2; // x^2
+ const float x0 = x1*x2; // x^3
+ float y0, y1, y2, y3;
+ float out0, out1;
+
+ assert(fract < 1.0);
+
+ y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3;
+ y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3;
+ y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3;
+ y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3;
+
+ out0 = y0 * psrc[0] + y1 * psrc[2] + y2 * psrc[4] + y3 * psrc[6];
+ out1 = y0 * psrc[1] + y1 * psrc[3] + y2 * psrc[5] + y3 * psrc[7];
+
+ pdest[2*i] = (SAMPLETYPE)out0;
+ pdest[2*i+1] = (SAMPLETYPE)out1;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ psrc += 2*whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+/// Transpose multi-channel audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateCubic::transposeMulti(SAMPLETYPE *pdest,
+ const SAMPLETYPE *psrc,
+ int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 4;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ const float x3 = 1.0f;
+ const float x2 = (float)fract; // x
+ const float x1 = x2*x2; // x^2
+ const float x0 = x1*x2; // x^3
+ float y0, y1, y2, y3;
+
+ assert(fract < 1.0);
+
+ y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3;
+ y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3;
+ y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3;
+ y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3;
+
+ for (int c = 0; c < numChannels; c ++)
+ {
+ float out;
+ out = y0 * psrc[c] + y1 * psrc[c + numChannels] + y2 * psrc[c + 2 * numChannels] + y3 * psrc[c + 3 * numChannels];
+ pdest[0] = (SAMPLETYPE)out;
+ pdest ++;
+ }
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ psrc += numChannels*whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateCubic.h b/third_party/soundtouch/source/SoundTouch/InterpolateCubic.h
new file mode 100644
index 00000000..4eae60a6
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateCubic.h
@@ -0,0 +1,69 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Cubic interpolation routine.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _InterpolateCubic_H_
+#define _InterpolateCubic_H_
+
+#include "RateTransposer.h"
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+class InterpolateCubic : public TransposerBase
+{
+protected:
+ virtual int transposeMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ virtual int transposeStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ virtual int transposeMulti(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+
+ double fract;
+
+public:
+ InterpolateCubic();
+
+ virtual void resetRegisters() override;
+
+ virtual int getLatency() const override
+ {
+ return 1;
+ }
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateLinear.cpp b/third_party/soundtouch/source/SoundTouch/InterpolateLinear.cpp
new file mode 100644
index 00000000..b4571d68
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateLinear.cpp
@@ -0,0 +1,296 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Linear interpolation algorithm.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <assert.h>
+#include <stdlib.h>
+#include "InterpolateLinear.h"
+
+using namespace soundtouch;
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// InterpolateLinearInteger - integer arithmetic implementation
+//
+
+/// fixed-point interpolation routine precision
+#define SCALE 65536
+
+
+// Constructor
+InterpolateLinearInteger::InterpolateLinearInteger() : TransposerBase()
+{
+ // Notice: use local function calling syntax for sake of clarity,
+ // to indicate the fact that C++ constructor can't call virtual functions.
+ resetRegisters();
+ setRate(1.0f);
+}
+
+
+void InterpolateLinearInteger::resetRegisters()
+{
+ iFract = 0;
+}
+
+
+// Transposes the sample rate of the given samples using linear interpolation.
+// 'Mono' version of the routine. Returns the number of samples returned in
+// the "dest" buffer
+int InterpolateLinearInteger::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ LONG_SAMPLETYPE temp;
+
+ assert(iFract < SCALE);
+
+ temp = (SCALE - iFract) * src[0] + iFract * src[1];
+ dest[i] = (SAMPLETYPE)(temp / SCALE);
+ i++;
+
+ iFract += iRate;
+
+ int iWhole = iFract / SCALE;
+ iFract -= iWhole * SCALE;
+ srcCount += iWhole;
+ src += iWhole;
+ }
+ srcSamples = srcCount;
+
+ return i;
+}
+
+
+// Transposes the sample rate of the given samples using linear interpolation.
+// 'Stereo' version of the routine. Returns the number of samples returned in
+// the "dest" buffer
+int InterpolateLinearInteger::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ LONG_SAMPLETYPE temp0;
+ LONG_SAMPLETYPE temp1;
+
+ assert(iFract < SCALE);
+
+ temp0 = (SCALE - iFract) * src[0] + iFract * src[2];
+ temp1 = (SCALE - iFract) * src[1] + iFract * src[3];
+ dest[0] = (SAMPLETYPE)(temp0 / SCALE);
+ dest[1] = (SAMPLETYPE)(temp1 / SCALE);
+ dest += 2;
+ i++;
+
+ iFract += iRate;
+
+ int iWhole = iFract / SCALE;
+ iFract -= iWhole * SCALE;
+ srcCount += iWhole;
+ src += 2*iWhole;
+ }
+ srcSamples = srcCount;
+
+ return i;
+}
+
+
+int InterpolateLinearInteger::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ LONG_SAMPLETYPE temp, vol1;
+
+ assert(iFract < SCALE);
+ vol1 = (LONG_SAMPLETYPE)(SCALE - iFract);
+ for (int c = 0; c < numChannels; c ++)
+ {
+ temp = vol1 * src[c] + iFract * src[c + numChannels];
+ dest[0] = (SAMPLETYPE)(temp / SCALE);
+ dest ++;
+ }
+ i++;
+
+ iFract += iRate;
+
+ int iWhole = iFract / SCALE;
+ iFract -= iWhole * SCALE;
+ srcCount += iWhole;
+ src += iWhole * numChannels;
+ }
+ srcSamples = srcCount;
+
+ return i;
+}
+
+
+// Sets new target iRate. Normal iRate = 1.0, smaller values represent slower
+// iRate, larger faster iRates.
+void InterpolateLinearInteger::setRate(double newRate)
+{
+ iRate = (int)(newRate * SCALE + 0.5);
+ TransposerBase::setRate(newRate);
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// InterpolateLinearFloat - floating point arithmetic implementation
+//
+//////////////////////////////////////////////////////////////////////////////
+
+
+// Constructor
+InterpolateLinearFloat::InterpolateLinearFloat() : TransposerBase()
+{
+ // Notice: use local function calling syntax for sake of clarity,
+ // to indicate the fact that C++ constructor can't call virtual functions.
+ resetRegisters();
+ setRate(1.0);
+}
+
+
+void InterpolateLinearFloat::resetRegisters()
+{
+ fract = 0;
+}
+
+
+// Transposes the sample rate of the given samples using linear interpolation.
+// 'Mono' version of the routine. Returns the number of samples returned in
+// the "dest" buffer
+int InterpolateLinearFloat::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ double out;
+ assert(fract < 1.0);
+
+ out = (1.0 - fract) * src[0] + fract * src[1];
+ dest[i] = (SAMPLETYPE)out;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ src += whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+// Transposes the sample rate of the given samples using linear interpolation.
+// 'Mono' version of the routine. Returns the number of samples returned in
+// the "dest" buffer
+int InterpolateLinearFloat::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ double out0, out1;
+ assert(fract < 1.0);
+
+ out0 = (1.0 - fract) * src[0] + fract * src[2];
+ out1 = (1.0 - fract) * src[1] + fract * src[3];
+ dest[2*i] = (SAMPLETYPE)out0;
+ dest[2*i+1] = (SAMPLETYPE)out1;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ src += 2*whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+int InterpolateLinearFloat::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 1;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ float temp, vol1, fract_float;
+
+ vol1 = (float)(1.0 - fract);
+ fract_float = (float)fract;
+ for (int c = 0; c < numChannels; c ++)
+ {
+ temp = vol1 * src[c] + fract_float * src[c + numChannels];
+ *dest = (SAMPLETYPE)temp;
+ dest ++;
+ }
+ i++;
+
+ fract += rate;
+
+ int iWhole = (int)fract;
+ fract -= iWhole;
+ srcCount += iWhole;
+ src += iWhole * numChannels;
+ }
+ srcSamples = srcCount;
+
+ return i;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateLinear.h b/third_party/soundtouch/source/SoundTouch/InterpolateLinear.h
new file mode 100644
index 00000000..d0e5c908
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateLinear.h
@@ -0,0 +1,98 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Linear interpolation routine.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _InterpolateLinear_H_
+#define _InterpolateLinear_H_
+
+#include "RateTransposer.h"
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+/// Linear transposer class that uses integer arithmetic
+class InterpolateLinearInteger : public TransposerBase
+{
+protected:
+ int iFract;
+ int iRate;
+
+ virtual int transposeMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ virtual int transposeStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) override;
+public:
+ InterpolateLinearInteger();
+
+ /// Sets new target rate. Normal rate = 1.0, smaller values represent slower
+ /// rate, larger faster rates.
+ virtual void setRate(double newRate) override;
+
+ virtual void resetRegisters() override;
+
+ virtual int getLatency() const override
+ {
+ return 0;
+ }
+};
+
+
+/// Linear transposer class that uses floating point arithmetic
+class InterpolateLinearFloat : public TransposerBase
+{
+protected:
+ double fract;
+
+ virtual int transposeMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples);
+ virtual int transposeStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples);
+ virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples);
+
+public:
+ InterpolateLinearFloat();
+
+ virtual void resetRegisters();
+
+ int getLatency() const
+ {
+ return 0;
+ }
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateShannon.cpp b/third_party/soundtouch/source/SoundTouch/InterpolateShannon.cpp
new file mode 100644
index 00000000..ac0b81ab
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateShannon.cpp
@@ -0,0 +1,181 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sample interpolation routine using 8-tap band-limited Shannon interpolation
+/// with kaiser window.
+///
+/// Notice. This algorithm is remarkably much heavier than linear or cubic
+/// interpolation, and not remarkably better than cubic algorithm. Thus mostly
+/// for experimental purposes
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <math.h>
+#include "InterpolateShannon.h"
+#include "STTypes.h"
+
+using namespace soundtouch;
+
+
+/// Kaiser window with beta = 2.0
+/// Values scaled down by 5% to avoid overflows
+static const double _kaiser8[8] =
+{
+ 0.41778693317814,
+ 0.64888025049173,
+ 0.83508562409944,
+ 0.93887857733412,
+ 0.93887857733412,
+ 0.83508562409944,
+ 0.64888025049173,
+ 0.41778693317814
+};
+
+
+InterpolateShannon::InterpolateShannon()
+{
+ fract = 0;
+}
+
+
+void InterpolateShannon::resetRegisters()
+{
+ fract = 0;
+}
+
+
+#define PI 3.1415926536
+#define sinc(x) (sin(PI * (x)) / (PI * (x)))
+
+/// Transpose mono audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateShannon::transposeMono(SAMPLETYPE *pdest,
+ const SAMPLETYPE *psrc,
+ int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 8;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ double out;
+ assert(fract < 1.0);
+
+ out = psrc[0] * sinc(-3.0 - fract) * _kaiser8[0];
+ out += psrc[1] * sinc(-2.0 - fract) * _kaiser8[1];
+ out += psrc[2] * sinc(-1.0 - fract) * _kaiser8[2];
+ if (fract < 1e-6)
+ {
+ out += psrc[3] * _kaiser8[3]; // sinc(0) = 1
+ }
+ else
+ {
+ out += psrc[3] * sinc(- fract) * _kaiser8[3];
+ }
+ out += psrc[4] * sinc( 1.0 - fract) * _kaiser8[4];
+ out += psrc[5] * sinc( 2.0 - fract) * _kaiser8[5];
+ out += psrc[6] * sinc( 3.0 - fract) * _kaiser8[6];
+ out += psrc[7] * sinc( 4.0 - fract) * _kaiser8[7];
+
+ pdest[i] = (SAMPLETYPE)out;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ psrc += whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+/// Transpose stereo audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateShannon::transposeStereo(SAMPLETYPE *pdest,
+ const SAMPLETYPE *psrc,
+ int &srcSamples)
+{
+ int i;
+ int srcSampleEnd = srcSamples - 8;
+ int srcCount = 0;
+
+ i = 0;
+ while (srcCount < srcSampleEnd)
+ {
+ double out0, out1, w;
+ assert(fract < 1.0);
+
+ w = sinc(-3.0 - fract) * _kaiser8[0];
+ out0 = psrc[0] * w; out1 = psrc[1] * w;
+ w = sinc(-2.0 - fract) * _kaiser8[1];
+ out0 += psrc[2] * w; out1 += psrc[3] * w;
+ w = sinc(-1.0 - fract) * _kaiser8[2];
+ out0 += psrc[4] * w; out1 += psrc[5] * w;
+ w = _kaiser8[3] * ((fract < 1e-5) ? 1.0 : sinc(- fract)); // sinc(0) = 1
+ out0 += psrc[6] * w; out1 += psrc[7] * w;
+ w = sinc( 1.0 - fract) * _kaiser8[4];
+ out0 += psrc[8] * w; out1 += psrc[9] * w;
+ w = sinc( 2.0 - fract) * _kaiser8[5];
+ out0 += psrc[10] * w; out1 += psrc[11] * w;
+ w = sinc( 3.0 - fract) * _kaiser8[6];
+ out0 += psrc[12] * w; out1 += psrc[13] * w;
+ w = sinc( 4.0 - fract) * _kaiser8[7];
+ out0 += psrc[14] * w; out1 += psrc[15] * w;
+
+ pdest[2*i] = (SAMPLETYPE)out0;
+ pdest[2*i+1] = (SAMPLETYPE)out1;
+ i ++;
+
+ // update position fraction
+ fract += rate;
+ // update whole positions
+ int whole = (int)fract;
+ fract -= whole;
+ psrc += 2*whole;
+ srcCount += whole;
+ }
+ srcSamples = srcCount;
+ return i;
+}
+
+
+/// Transpose stereo audio. Returns number of produced output samples, and
+/// updates "srcSamples" to amount of consumed source samples
+int InterpolateShannon::transposeMulti(SAMPLETYPE *,
+ const SAMPLETYPE *,
+ int &)
+{
+ // not implemented
+ assert(false);
+ return 0;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/InterpolateShannon.h b/third_party/soundtouch/source/SoundTouch/InterpolateShannon.h
new file mode 100644
index 00000000..99bd7a62
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/InterpolateShannon.h
@@ -0,0 +1,74 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sample interpolation routine using 8-tap band-limited Shannon interpolation
+/// with kaiser window.
+///
+/// Notice. This algorithm is remarkably much heavier than linear or cubic
+/// interpolation, and not remarkably better than cubic algorithm. Thus mostly
+/// for experimental purposes
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _InterpolateShannon_H_
+#define _InterpolateShannon_H_
+
+#include "RateTransposer.h"
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+class InterpolateShannon : public TransposerBase
+{
+protected:
+ int transposeMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ int transposeStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+ int transposeMulti(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) override;
+
+ double fract;
+
+public:
+ InterpolateShannon();
+
+ void resetRegisters() override;
+
+ virtual int getLatency() const override
+ {
+ return 3;
+ }
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/source/SoundTouch/PeakFinder.cpp b/third_party/soundtouch/source/SoundTouch/PeakFinder.cpp
new file mode 100644
index 00000000..ce354ded
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/PeakFinder.cpp
@@ -0,0 +1,276 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Peak detection routine.
+///
+/// The routine detects highest value on an array of values and calculates the
+/// precise peak location as a mass-center of the 'hump' around the peak value.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <math.h>
+#include <assert.h>
+
+#include "PeakFinder.h"
+
+using namespace soundtouch;
+
+#define max(x, y) (((x) > (y)) ? (x) : (y))
+
+
+PeakFinder::PeakFinder()
+{
+ minPos = maxPos = 0;
+}
+
+
+// Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'.
+int PeakFinder::findTop(const float *data, int peakpos) const
+{
+ int i;
+ int start, end;
+ float refvalue;
+
+ refvalue = data[peakpos];
+
+ // seek within ±10 points
+ start = peakpos - 10;
+ if (start < minPos) start = minPos;
+ end = peakpos + 10;
+ if (end > maxPos) end = maxPos;
+
+ for (i = start; i <= end; i ++)
+ {
+ if (data[i] > refvalue)
+ {
+ peakpos = i;
+ refvalue = data[i];
+ }
+ }
+
+ // failure if max value is at edges of seek range => it's not peak, it's at slope.
+ if ((peakpos == start) || (peakpos == end)) return 0;
+
+ return peakpos;
+}
+
+
+// Finds 'ground level' of a peak hump by starting from 'peakpos' and proceeding
+// to direction defined by 'direction' until next 'hump' after minimum value will
+// begin
+int PeakFinder::findGround(const float *data, int peakpos, int direction) const
+{
+ int lowpos;
+ int pos;
+ int climb_count;
+ float refvalue;
+ float delta;
+
+ climb_count = 0;
+ refvalue = data[peakpos];
+ lowpos = peakpos;
+
+ pos = peakpos;
+
+ while ((pos > minPos+1) && (pos < maxPos-1))
+ {
+ int prevpos;
+
+ prevpos = pos;
+ pos += direction;
+
+ // calculate derivate
+ delta = data[pos] - data[prevpos];
+ if (delta <= 0)
+ {
+ // going downhill, ok
+ if (climb_count)
+ {
+ climb_count --; // decrease climb count
+ }
+
+ // check if new minimum found
+ if (data[pos] < refvalue)
+ {
+ // new minimum found
+ lowpos = pos;
+ refvalue = data[pos];
+ }
+ }
+ else
+ {
+ // going uphill, increase climbing counter
+ climb_count ++;
+ if (climb_count > 5) break; // we've been climbing too long => it's next uphill => quit
+ }
+ }
+ return lowpos;
+}
+
+
+// Find offset where the value crosses the given level, when starting from 'peakpos' and
+// proceeds to direction defined in 'direction'
+int PeakFinder::findCrossingLevel(const float *data, float level, int peakpos, int direction) const
+{
+ int pos;
+
+ [[maybe_unused]] float peaklevel = data[peakpos];
+ assert(peaklevel >= level);
+ pos = peakpos;
+ while ((pos >= minPos) && (pos + direction < maxPos))
+ {
+ if (data[pos + direction] < level) return pos; // crossing found
+ pos += direction;
+ }
+ return -1; // not found
+}
+
+
+// Calculates the center of mass location of 'data' array items between 'firstPos' and 'lastPos'
+double PeakFinder::calcMassCenter(const float *data, int firstPos, int lastPos) const
+{
+ int i;
+ float sum;
+ float wsum;
+
+ sum = 0;
+ wsum = 0;
+ for (i = firstPos; i <= lastPos; i ++)
+ {
+ sum += (float)i * data[i];
+ wsum += data[i];
+ }
+
+ if (wsum < 1e-6) return 0;
+ return sum / wsum;
+}
+
+
+/// get exact center of peak near given position by calculating local mass of center
+double PeakFinder::getPeakCenter(const float *data, int peakpos) const
+{
+ float peakLevel; // peak level
+ int crosspos1, crosspos2; // position where the peak 'hump' crosses cutting level
+ float cutLevel; // cutting value
+ float groundLevel; // ground level of the peak
+ int gp1, gp2; // bottom positions of the peak 'hump'
+
+ // find ground positions.
+ gp1 = findGround(data, peakpos, -1);
+ gp2 = findGround(data, peakpos, 1);
+
+ peakLevel = data[peakpos];
+
+ if (gp1 == gp2)
+ {
+ // avoid rounding errors when all are equal
+ assert(gp1 == peakpos);
+ cutLevel = groundLevel = peakLevel;
+ } else {
+ // get average of the ground levels
+ groundLevel = 0.5f * (data[gp1] + data[gp2]);
+
+ // calculate 70%-level of the peak
+ cutLevel = 0.70f * peakLevel + 0.30f * groundLevel;
+ }
+
+ // find mid-level crossings
+ crosspos1 = findCrossingLevel(data, cutLevel, peakpos, -1);
+ crosspos2 = findCrossingLevel(data, cutLevel, peakpos, 1);
+
+ if ((crosspos1 < 0) || (crosspos2 < 0)) return 0; // no crossing, no peak..
+
+ // calculate mass center of the peak surroundings
+ return calcMassCenter(data, crosspos1, crosspos2);
+}
+
+
+double PeakFinder::detectPeak(const float *data, int aminPos, int amaxPos)
+{
+
+ int i;
+ int peakpos; // position of peak level
+ double highPeak, peak;
+
+ this->minPos = aminPos;
+ this->maxPos = amaxPos;
+
+ // find absolute peak
+ peakpos = minPos;
+ peak = data[minPos];
+ for (i = minPos + 1; i < maxPos; i ++)
+ {
+ if (data[i] > peak)
+ {
+ peak = data[i];
+ peakpos = i;
+ }
+ }
+
+ // Calculate exact location of the highest peak mass center
+ highPeak = getPeakCenter(data, peakpos);
+ peak = highPeak;
+
+ // Now check if the highest peak were in fact harmonic of the true base beat peak
+ // - sometimes the highest peak can be Nth harmonic of the true base peak yet
+ // just a slightly higher than the true base
+
+ for (i = 1; i < 3; i ++)
+ {
+ double peaktmp, harmonic;
+ int i1,i2;
+
+ harmonic = (double)pow(2.0, i);
+ peakpos = (int)(highPeak / harmonic + 0.5f);
+ if (peakpos < minPos) break;
+ peakpos = findTop(data, peakpos); // seek true local maximum index
+ if (peakpos == 0) continue; // no local max here
+
+ // calculate mass-center of possible harmonic peak
+ peaktmp = getPeakCenter(data, peakpos);
+
+ // accept harmonic peak if
+ // (a) it is found
+ // (b) is within ±4% of the expected harmonic interval
+ // (c) has at least half x-corr value of the max. peak
+
+ double diff = harmonic * peaktmp / highPeak;
+ if ((diff < 0.96) || (diff > 1.04)) continue; // peak too afar from expected
+
+ // now compare to highest detected peak
+ i1 = (int)(highPeak + 0.5);
+ i2 = (int)(peaktmp + 0.5);
+ if (data[i2] >= 0.4*data[i1])
+ {
+ // The harmonic is at least half as high primary peak,
+ // thus use the harmonic peak instead
+ peak = peaktmp;
+ }
+ }
+
+ return peak;
+}
diff --git a/third_party/soundtouch/source/SoundTouch/PeakFinder.h b/third_party/soundtouch/source/SoundTouch/PeakFinder.h
new file mode 100644
index 00000000..106eac80
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/PeakFinder.h
@@ -0,0 +1,90 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// The routine detects highest value on an array of values and calculates the
+/// precise peak location as a mass-center of the 'hump' around the peak value.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _PeakFinder_H_
+#define _PeakFinder_H_
+
+namespace soundtouch
+{
+
+class PeakFinder
+{
+protected:
+ /// Min, max allowed peak positions within the data vector
+ int minPos, maxPos;
+
+ /// Calculates the mass center between given vector items.
+ double calcMassCenter(const float *data, ///< Data vector.
+ int firstPos, ///< Index of first vector item belonging to the peak.
+ int lastPos ///< Index of last vector item belonging to the peak.
+ ) const;
+
+ /// Finds the data vector index where the monotoniously decreasing signal crosses the
+ /// given level.
+ int findCrossingLevel(const float *data, ///< Data vector.
+ float level, ///< Goal crossing level.
+ int peakpos, ///< Peak position index within the data vector.
+ int direction /// Direction where to proceed from the peak: 1 = right, -1 = left.
+ ) const;
+
+ // Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'.
+ int findTop(const float *data, int peakpos) const;
+
+
+ /// Finds the 'ground' level, i.e. smallest level between two neighbouring peaks, to right-
+ /// or left-hand side of the given peak position.
+ int findGround(const float *data, /// Data vector.
+ int peakpos, /// Peak position index within the data vector.
+ int direction /// Direction where to proceed from the peak: 1 = right, -1 = left.
+ ) const;
+
+ /// get exact center of peak near given position by calculating local mass of center
+ double getPeakCenter(const float *data, int peakpos) const;
+
+public:
+ /// Constructor.
+ PeakFinder();
+
+ /// Detect exact peak position of the data vector by finding the largest peak 'hump'
+ /// and calculating the mass-center location of the peak hump.
+ ///
+ /// \return The location of the largest base harmonic peak hump.
+ double detectPeak(const float *data, /// Data vector to be analyzed. The data vector has
+ /// to be at least 'maxPos' items long.
+ int minPos, ///< Min allowed peak location within the vector data.
+ int maxPos ///< Max allowed peak location within the vector data.
+ );
+};
+
+}
+
+#endif // _PeakFinder_H_
diff --git a/third_party/soundtouch/source/SoundTouch/RateTransposer.cpp b/third_party/soundtouch/source/SoundTouch/RateTransposer.cpp
new file mode 100644
index 00000000..2b921186
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/RateTransposer.cpp
@@ -0,0 +1,313 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sample rate transposer. Changes sample rate by using linear interpolation
+/// together with anti-alias filtering (first order interpolation with anti-
+/// alias filtering should be quite adequate for this application)
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <memory.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "RateTransposer.h"
+#include "InterpolateLinear.h"
+#include "InterpolateCubic.h"
+#include "InterpolateShannon.h"
+#include "AAFilter.h"
+
+using namespace soundtouch;
+
+// Define default interpolation algorithm here
+TransposerBase::ALGORITHM TransposerBase::algorithm = TransposerBase::CUBIC;
+
+
+// Constructor
+RateTransposer::RateTransposer() : FIFOProcessor(&outputBuffer)
+{
+ bUseAAFilter =
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ true;
+#else
+ // Disable Anti-alias filter if desirable to avoid click at rate change zero value crossover
+ false;
+#endif
+
+ // Instantiates the anti-alias filter
+ pAAFilter = new AAFilter(64);
+ pTransposer = TransposerBase::newInstance();
+ clear();
+}
+
+
+RateTransposer::~RateTransposer()
+{
+ delete pAAFilter;
+ delete pTransposer;
+}
+
+
+/// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable
+void RateTransposer::enableAAFilter(bool newMode)
+{
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ // Disable Anti-alias filter if desirable to avoid click at rate change zero value crossover
+ bUseAAFilter = newMode;
+ clear();
+#endif
+}
+
+
+/// Returns nonzero if anti-alias filter is enabled.
+bool RateTransposer::isAAFilterEnabled() const
+{
+ return bUseAAFilter;
+}
+
+
+AAFilter *RateTransposer::getAAFilter()
+{
+ return pAAFilter;
+}
+
+
+// Sets new target iRate. Normal iRate = 1.0, smaller values represent slower
+// iRate, larger faster iRates.
+void RateTransposer::setRate(double newRate)
+{
+ double fCutoff;
+
+ pTransposer->setRate(newRate);
+
+ // design a new anti-alias filter
+ if (newRate > 1.0)
+ {
+ fCutoff = 0.5 / newRate;
+ }
+ else
+ {
+ fCutoff = 0.5 * newRate;
+ }
+ pAAFilter->setCutoffFreq(fCutoff);
+}
+
+
+// Adds 'nSamples' pcs of samples from the 'samples' memory position into
+// the input of the object.
+void RateTransposer::putSamples(const SAMPLETYPE *samples, uint nSamples)
+{
+ processSamples(samples, nSamples);
+}
+
+
+// Transposes sample rate by applying anti-alias filter to prevent folding.
+// Returns amount of samples returned in the "dest" buffer.
+// The maximum amount of samples that can be returned at a time is set by
+// the 'set_returnBuffer_size' function.
+void RateTransposer::processSamples(const SAMPLETYPE *src, uint nSamples)
+{
+ if (nSamples == 0) return;
+
+ // Store samples to input buffer
+ inputBuffer.putSamples(src, nSamples);
+
+ // If anti-alias filter is turned off, simply transpose without applying
+ // the filter
+ if (bUseAAFilter == false)
+ {
+ (void)pTransposer->transpose(outputBuffer, inputBuffer);
+ return;
+ }
+
+ assert(pAAFilter);
+
+ // Transpose with anti-alias filter
+ if (pTransposer->rate < 1.0f)
+ {
+ // If the parameter 'Rate' value is smaller than 1, first transpose
+ // the samples and then apply the anti-alias filter to remove aliasing.
+
+ // Transpose the samples, store the result to end of "midBuffer"
+ pTransposer->transpose(midBuffer, inputBuffer);
+
+ // Apply the anti-alias filter for transposed samples in midBuffer
+ pAAFilter->evaluate(outputBuffer, midBuffer);
+ }
+ else
+ {
+ // If the parameter 'Rate' value is larger than 1, first apply the
+ // anti-alias filter to remove high frequencies (prevent them from folding
+ // over the lover frequencies), then transpose.
+
+ // Apply the anti-alias filter for samples in inputBuffer
+ pAAFilter->evaluate(midBuffer, inputBuffer);
+
+ // Transpose the AA-filtered samples in "midBuffer"
+ pTransposer->transpose(outputBuffer, midBuffer);
+ }
+}
+
+
+// Sets the number of channels, 1 = mono, 2 = stereo
+void RateTransposer::setChannels(int nChannels)
+{
+ if (!verifyNumberOfChannels(nChannels) ||
+ (pTransposer->numChannels == nChannels)) return;
+
+ pTransposer->setChannels(nChannels);
+ inputBuffer.setChannels(nChannels);
+ midBuffer.setChannels(nChannels);
+ outputBuffer.setChannels(nChannels);
+}
+
+
+// Clears all the samples in the object
+void RateTransposer::clear()
+{
+ outputBuffer.clear();
+ midBuffer.clear();
+ inputBuffer.clear();
+ pTransposer->resetRegisters();
+
+ // prefill buffer to avoid losing first samples at beginning of stream
+ int prefill = getLatency();
+ inputBuffer.addSilent(prefill);
+}
+
+
+// Returns nonzero if there aren't any samples available for outputting.
+int RateTransposer::isEmpty() const
+{
+ int res;
+
+ res = FIFOProcessor::isEmpty();
+ if (res == 0) return 0;
+ return inputBuffer.isEmpty();
+}
+
+
+/// Return approximate initial input-output latency
+int RateTransposer::getLatency() const
+{
+ return pTransposer->getLatency() +
+ ((bUseAAFilter) ? (pAAFilter->getLength() / 2) : 0);
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// TransposerBase - Base class for interpolation
+//
+
+// static function to set interpolation algorithm
+void TransposerBase::setAlgorithm(TransposerBase::ALGORITHM a)
+{
+ TransposerBase::algorithm = a;
+}
+
+
+// Transposes the sample rate of the given samples using linear interpolation.
+// Returns the number of samples returned in the "dest" buffer
+int TransposerBase::transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src)
+{
+ int numSrcSamples = src.numSamples();
+ int sizeDemand = (int)((double)numSrcSamples / rate) + 8;
+ int numOutput;
+ SAMPLETYPE *psrc = src.ptrBegin();
+ SAMPLETYPE *pdest = dest.ptrEnd(sizeDemand);
+
+#ifndef USE_MULTICH_ALWAYS
+ if (numChannels == 1)
+ {
+ numOutput = transposeMono(pdest, psrc, numSrcSamples);
+ }
+ else if (numChannels == 2)
+ {
+ numOutput = transposeStereo(pdest, psrc, numSrcSamples);
+ }
+ else
+#endif // USE_MULTICH_ALWAYS
+ {
+ assert(numChannels > 0);
+ numOutput = transposeMulti(pdest, psrc, numSrcSamples);
+ }
+ dest.putSamples(numOutput);
+ src.receiveSamples(numSrcSamples);
+ return numOutput;
+}
+
+
+TransposerBase::TransposerBase()
+{
+ numChannels = 0;
+ rate = 1.0f;
+}
+
+
+TransposerBase::~TransposerBase()
+{
+}
+
+
+void TransposerBase::setChannels(int channels)
+{
+ numChannels = channels;
+ resetRegisters();
+}
+
+
+void TransposerBase::setRate(double newRate)
+{
+ rate = newRate;
+}
+
+
+// static factory function
+TransposerBase *TransposerBase::newInstance()
+{
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ // Notice: For integer arithmetic support only linear algorithm (due to simplest calculus)
+ return ::new InterpolateLinearInteger;
+#else
+ switch (algorithm)
+ {
+ case LINEAR:
+ return new InterpolateLinearFloat;
+
+ case CUBIC:
+ return new InterpolateCubic;
+
+ case SHANNON:
+ return new InterpolateShannon;
+
+ default:
+ assert(false);
+ return nullptr;
+ }
+#endif
+}
diff --git a/third_party/soundtouch/source/SoundTouch/RateTransposer.h b/third_party/soundtouch/source/SoundTouch/RateTransposer.h
new file mode 100644
index 00000000..32bbadc1
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/RateTransposer.h
@@ -0,0 +1,164 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sample rate transposer. Changes sample rate by using linear interpolation
+/// together with anti-alias filtering (first order interpolation with anti-
+/// alias filtering should be quite adequate for this application).
+///
+/// Use either of the derived classes of 'RateTransposerInteger' or
+/// 'RateTransposerFloat' for corresponding integer/floating point tranposing
+/// algorithm implementation.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef RateTransposer_H
+#define RateTransposer_H
+
+#include <stddef.h>
+#include "AAFilter.h"
+#include "FIFOSamplePipe.h"
+#include "FIFOSampleBuffer.h"
+
+#include "STTypes.h"
+
+namespace soundtouch
+{
+
+/// Abstract base class for transposer implementations (linear, advanced vs integer, float etc)
+class TransposerBase
+{
+public:
+ enum ALGORITHM {
+ LINEAR = 0,
+ CUBIC,
+ SHANNON
+ };
+
+protected:
+ virtual int transposeMono(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) = 0;
+ virtual int transposeStereo(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) = 0;
+ virtual int transposeMulti(SAMPLETYPE *dest,
+ const SAMPLETYPE *src,
+ int &srcSamples) = 0;
+
+ static ALGORITHM algorithm;
+
+public:
+ double rate;
+ int numChannels;
+
+ TransposerBase();
+ virtual ~TransposerBase();
+
+ virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src);
+ virtual void setRate(double newRate);
+ virtual void setChannels(int channels);
+ virtual int getLatency() const = 0;
+
+ virtual void resetRegisters() = 0;
+
+ // static factory function
+ static TransposerBase *newInstance();
+
+ // static function to set interpolation algorithm
+ static void setAlgorithm(ALGORITHM a);
+};
+
+
+/// A common linear samplerate transposer class.
+///
+class RateTransposer : public FIFOProcessor
+{
+protected:
+ /// Anti-alias filter object
+ AAFilter *pAAFilter;
+ TransposerBase *pTransposer;
+
+ /// Buffer for collecting samples to feed the anti-alias filter between
+ /// two batches
+ FIFOSampleBuffer inputBuffer;
+
+ /// Buffer for keeping samples between transposing & anti-alias filter
+ FIFOSampleBuffer midBuffer;
+
+ /// Output sample buffer
+ FIFOSampleBuffer outputBuffer;
+
+ bool bUseAAFilter;
+
+
+ /// Transposes sample rate by applying anti-alias filter to prevent folding.
+ /// Returns amount of samples returned in the "dest" buffer.
+ /// The maximum amount of samples that can be returned at a time is set by
+ /// the 'set_returnBuffer_size' function.
+ void processSamples(const SAMPLETYPE *src,
+ uint numSamples);
+
+public:
+ RateTransposer();
+ virtual ~RateTransposer() override;
+
+ /// Returns the output buffer object
+ FIFOSamplePipe *getOutput() { return &outputBuffer; };
+
+ /// Return anti-alias filter object
+ AAFilter *getAAFilter();
+
+ /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable
+ void enableAAFilter(bool newMode);
+
+ /// Returns nonzero if anti-alias filter is enabled.
+ bool isAAFilterEnabled() const;
+
+ /// Sets new target rate. Normal rate = 1.0, smaller values represent slower
+ /// rate, larger faster rates.
+ virtual void setRate(double newRate);
+
+ /// Sets the number of channels, 1 = mono, 2 = stereo
+ void setChannels(int channels);
+
+ /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
+ /// the input of the object.
+ void putSamples(const SAMPLETYPE *samples, uint numSamples) override;
+
+ /// Clears all the samples in the object
+ void clear() override;
+
+ /// Returns nonzero if there aren't any samples available for outputting.
+ int isEmpty() const override;
+
+ /// Return approximate initial input-output latency
+ int getLatency() const;
+};
+
+}
+
+#endif
diff --git a/third_party/soundtouch/source/SoundTouch/SoundTouch.cpp b/third_party/soundtouch/source/SoundTouch/SoundTouch.cpp
new file mode 100644
index 00000000..0eda80ae
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/SoundTouch.cpp
@@ -0,0 +1,538 @@
+//////////////////////////////////////////////////////////////////////////////
+///
+/// SoundTouch - main class for tempo/pitch/rate adjusting routines.
+///
+/// Notes:
+/// - Initialize the SoundTouch object instance by setting up the sound stream
+/// parameters with functions 'setSampleRate' and 'setChannels', then set
+/// desired tempo/pitch/rate settings with the corresponding functions.
+///
+/// - The SoundTouch class behaves like a first-in-first-out pipeline: The
+/// samples that are to be processed are fed into one of the pipe by calling
+/// function 'putSamples', while the ready processed samples can be read
+/// from the other end of the pipeline with function 'receiveSamples'.
+///
+/// - The SoundTouch processing classes require certain sized 'batches' of
+/// samples in order to process the sound. For this reason the classes buffer
+/// incoming samples until there are enough of samples available for
+/// processing, then they carry out the processing step and consequently
+/// make the processed samples available for outputting.
+///
+/// - For the above reason, the processing routines introduce a certain
+/// 'latency' between the input and output, so that the samples input to
+/// SoundTouch may not be immediately available in the output, and neither
+/// the amount of outputtable samples may not immediately be in direct
+/// relationship with the amount of previously input samples.
+///
+/// - The tempo/pitch/rate control parameters can be altered during processing.
+/// Please notice though that they aren't currently protected by semaphores,
+/// so in multi-thread application external semaphore protection may be
+/// required.
+///
+/// - This class utilizes classes 'TDStretch' for tempo change (without modifying
+/// pitch) and 'RateTransposer' for changing the playback rate (that is, both
+/// tempo and pitch in the same ratio) of the sound. The third available control
+/// 'pitch' (change pitch but maintain tempo) is produced by a combination of
+/// combining the two other controls.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <assert.h>
+#include <stdlib.h>
+#include <memory.h>
+#include <math.h>
+#include <stdio.h>
+
+#include "SoundTouch.h"
+#include "TDStretch.h"
+#include "RateTransposer.h"
+#include "cpu_detect.h"
+
+using namespace soundtouch;
+
+/// test if two floating point numbers are equal
+#define TEST_FLOAT_EQUAL(a, b) (fabs(a - b) < 1e-10)
+
+
+/// Print library version string for autoconf
+extern "C" void soundtouch_ac_test()
+{
+ printf("SoundTouch Version: %s\n",SOUNDTOUCH_VERSION);
+}
+
+
+SoundTouch::SoundTouch()
+{
+ // Initialize rate transposer and tempo changer instances
+
+ pRateTransposer = new RateTransposer();
+ pTDStretch = TDStretch::newInstance();
+
+ setOutPipe(pTDStretch);
+
+ rate = tempo = 0;
+
+ virtualPitch =
+ virtualRate =
+ virtualTempo = 1.0;
+
+ calcEffectiveRateAndTempo();
+
+ samplesExpectedOut = 0;
+ samplesOutput = 0;
+
+ channels = 0;
+ bSrateSet = false;
+}
+
+
+SoundTouch::~SoundTouch()
+{
+ delete pRateTransposer;
+ delete pTDStretch;
+}
+
+
+/// Get SoundTouch library version string
+const char *SoundTouch::getVersionString()
+{
+ static const char *_version = SOUNDTOUCH_VERSION;
+
+ return _version;
+}
+
+
+/// Get SoundTouch library version Id
+uint SoundTouch::getVersionId()
+{
+ return SOUNDTOUCH_VERSION_ID;
+}
+
+
+// Sets the number of channels, 1 = mono, 2 = stereo
+void SoundTouch::setChannels(uint numChannels)
+{
+ if (!verifyNumberOfChannels(numChannels)) return;
+
+ channels = numChannels;
+ pRateTransposer->setChannels((int)numChannels);
+ pTDStretch->setChannels((int)numChannels);
+}
+
+
+// Sets new rate control value. Normal rate = 1.0, smaller values
+// represent slower rate, larger faster rates.
+void SoundTouch::setRate(double newRate)
+{
+ virtualRate = newRate;
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets new rate control value as a difference in percents compared
+// to the original rate (-50 .. +100 %)
+void SoundTouch::setRateChange(double newRate)
+{
+ virtualRate = 1.0 + 0.01 * newRate;
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets new tempo control value. Normal tempo = 1.0, smaller values
+// represent slower tempo, larger faster tempo.
+void SoundTouch::setTempo(double newTempo)
+{
+ virtualTempo = newTempo;
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets new tempo control value as a difference in percents compared
+// to the original tempo (-50 .. +100 %)
+void SoundTouch::setTempoChange(double newTempo)
+{
+ virtualTempo = 1.0 + 0.01 * newTempo;
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets new pitch control value. Original pitch = 1.0, smaller values
+// represent lower pitches, larger values higher pitch.
+void SoundTouch::setPitch(double newPitch)
+{
+ virtualPitch = newPitch;
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets pitch change in octaves compared to the original pitch
+// (-1.00 .. +1.00)
+void SoundTouch::setPitchOctaves(double newPitch)
+{
+ virtualPitch = exp(0.69314718056 * newPitch);
+ calcEffectiveRateAndTempo();
+}
+
+
+// Sets pitch change in semi-tones compared to the original pitch
+// (-12 .. +12)
+void SoundTouch::setPitchSemiTones(int newPitch)
+{
+ setPitchOctaves((double)newPitch / 12.0);
+}
+
+
+void SoundTouch::setPitchSemiTones(double newPitch)
+{
+ setPitchOctaves(newPitch / 12.0);
+}
+
+
+// Calculates 'effective' rate and tempo values from the
+// nominal control values.
+void SoundTouch::calcEffectiveRateAndTempo()
+{
+ double oldTempo = tempo;
+ double oldRate = rate;
+
+ tempo = virtualTempo / virtualPitch;
+ rate = virtualPitch * virtualRate;
+
+ if (!TEST_FLOAT_EQUAL(rate,oldRate)) pRateTransposer->setRate(rate);
+ if (!TEST_FLOAT_EQUAL(tempo, oldTempo)) pTDStretch->setTempo(tempo);
+
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ if (rate <= 1.0f)
+ {
+ if (output != pTDStretch)
+ {
+ FIFOSamplePipe *tempoOut;
+
+ assert(output == pRateTransposer);
+ // move samples in the current output buffer to the output of pTDStretch
+ tempoOut = pTDStretch->getOutput();
+ tempoOut->moveSamples(*output);
+ // move samples in pitch transposer's store buffer to tempo changer's input
+ // deprecated : pTDStretch->moveSamples(*pRateTransposer->getStore());
+
+ output = pTDStretch;
+ }
+ }
+ else
+#endif
+ {
+ if (output != pRateTransposer)
+ {
+ FIFOSamplePipe *transOut;
+
+ assert(output == pTDStretch);
+ // move samples in the current output buffer to the output of pRateTransposer
+ transOut = pRateTransposer->getOutput();
+ transOut->moveSamples(*output);
+ // move samples in tempo changer's input to pitch transposer's input
+ pRateTransposer->moveSamples(*pTDStretch->getInput());
+
+ output = pRateTransposer;
+ }
+ }
+}
+
+
+// Sets sample rate.
+void SoundTouch::setSampleRate(uint srate)
+{
+ // set sample rate, leave other tempo changer parameters as they are.
+ pTDStretch->setParameters((int)srate);
+ bSrateSet = true;
+}
+
+
+// Adds 'numSamples' pcs of samples from the 'samples' memory position into
+// the input of the object.
+void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)
+{
+ if (bSrateSet == false)
+ {
+ ST_THROW_RT_ERROR("SoundTouch : Sample rate not defined");
+ }
+ else if (channels == 0)
+ {
+ ST_THROW_RT_ERROR("SoundTouch : Number of channels not defined");
+ }
+
+ // accumulate how many samples are expected out from processing, given the current
+ // processing setting
+ samplesExpectedOut += (double)nSamples / ((double)rate * (double)tempo);
+
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ if (rate <= 1.0f)
+ {
+ // transpose the rate down, output the transposed sound to tempo changer buffer
+ assert(output == pTDStretch);
+ pRateTransposer->putSamples(samples, nSamples);
+ pTDStretch->moveSamples(*pRateTransposer);
+ }
+ else
+#endif
+ {
+ // evaluate the tempo changer, then transpose the rate up,
+ assert(output == pRateTransposer);
+ pTDStretch->putSamples(samples, nSamples);
+ pRateTransposer->moveSamples(*pTDStretch);
+ }
+}
+
+
+// Flushes the last samples from the processing pipeline to the output.
+// Clears also the internal processing buffers.
+//
+// Note: This function is meant for extracting the last samples of a sound
+// stream. This function may introduce additional blank samples in the end
+// of the sound stream, and thus it's not recommended to call this function
+// in the middle of a sound stream.
+void SoundTouch::flush()
+{
+ int i;
+ int numStillExpected;
+ SAMPLETYPE *buff = new SAMPLETYPE[128 * channels];
+
+ // how many samples are still expected to output
+ numStillExpected = (int)((long)(samplesExpectedOut + 0.5) - samplesOutput);
+ if (numStillExpected < 0) numStillExpected = 0;
+
+ memset(buff, 0, 128 * channels * sizeof(SAMPLETYPE));
+ // "Push" the last active samples out from the processing pipeline by
+ // feeding blank samples into the processing pipeline until new,
+ // processed samples appear in the output (not however, more than
+ // 24ksamples in any case)
+ for (i = 0; (numStillExpected > (int)numSamples()) && (i < 200); i ++)
+ {
+ putSamples(buff, 128);
+ }
+
+ adjustAmountOfSamples(numStillExpected);
+
+ delete[] buff;
+
+ // Clear input buffers
+ pTDStretch->clearInput();
+ // yet leave the output intouched as that's where the
+ // flushed samples are!
+}
+
+
+// Changes a setting controlling the processing system behaviour. See the
+// 'SETTING_...' defines for available setting ID's.
+bool SoundTouch::setSetting(int settingId, int value)
+{
+ int sampleRate, sequenceMs, seekWindowMs, overlapMs;
+
+ // read current tdstretch routine parameters
+ pTDStretch->getParameters(&sampleRate, &sequenceMs, &seekWindowMs, &overlapMs);
+
+ switch (settingId)
+ {
+ case SETTING_USE_AA_FILTER :
+ // enables / disabless anti-alias filter
+ pRateTransposer->enableAAFilter((value != 0) ? true : false);
+ return true;
+
+ case SETTING_AA_FILTER_LENGTH :
+ // sets anti-alias filter length
+ pRateTransposer->getAAFilter()->setLength(value);
+ return true;
+
+ case SETTING_USE_QUICKSEEK :
+ // enables / disables tempo routine quick seeking algorithm
+ pTDStretch->enableQuickSeek((value != 0) ? true : false);
+ return true;
+
+ case SETTING_SEQUENCE_MS:
+ // change time-stretch sequence duration parameter
+ pTDStretch->setParameters(sampleRate, value, seekWindowMs, overlapMs);
+ return true;
+
+ case SETTING_SEEKWINDOW_MS:
+ // change time-stretch seek window length parameter
+ pTDStretch->setParameters(sampleRate, sequenceMs, value, overlapMs);
+ return true;
+
+ case SETTING_OVERLAP_MS:
+ // change time-stretch overlap length parameter
+ pTDStretch->setParameters(sampleRate, sequenceMs, seekWindowMs, value);
+ return true;
+
+ default :
+ return false;
+ }
+}
+
+
+// Reads a setting controlling the processing system behaviour. See the
+// 'SETTING_...' defines for available setting ID's.
+//
+// Returns the setting value.
+int SoundTouch::getSetting(int settingId) const
+{
+ int temp;
+
+ switch (settingId)
+ {
+ case SETTING_USE_AA_FILTER :
+ return (uint)pRateTransposer->isAAFilterEnabled();
+
+ case SETTING_AA_FILTER_LENGTH :
+ return pRateTransposer->getAAFilter()->getLength();
+
+ case SETTING_USE_QUICKSEEK :
+ return (uint)pTDStretch->isQuickSeekEnabled();
+
+ case SETTING_SEQUENCE_MS:
+ pTDStretch->getParameters(nullptr, &temp, nullptr, nullptr);
+ return temp;
+
+ case SETTING_SEEKWINDOW_MS:
+ pTDStretch->getParameters(nullptr, nullptr, &temp, nullptr);
+ return temp;
+
+ case SETTING_OVERLAP_MS:
+ pTDStretch->getParameters(nullptr, nullptr, nullptr, &temp);
+ return temp;
+
+ case SETTING_NOMINAL_INPUT_SEQUENCE :
+ {
+ int size = pTDStretch->getInputSampleReq();
+
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ if (rate <= 1.0)
+ {
+ // transposing done before timestretch, which impacts latency
+ return (int)(size * rate + 0.5);
+ }
+#endif
+ return size;
+ }
+
+ case SETTING_NOMINAL_OUTPUT_SEQUENCE :
+ {
+ int size = pTDStretch->getOutputBatchSize();
+
+ if (rate > 1.0)
+ {
+ // transposing done after timestretch, which impacts latency
+ return (int)(size / rate + 0.5);
+ }
+ return size;
+ }
+
+ case SETTING_INITIAL_LATENCY:
+ {
+ double latency = pTDStretch->getLatency();
+ int latency_tr = pRateTransposer->getLatency();
+
+#ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
+ if (rate <= 1.0)
+ {
+ // transposing done before timestretch, which impacts latency
+ latency = (latency + latency_tr) * rate;
+ }
+ else
+#endif
+ {
+ latency += (double)latency_tr / rate;
+ }
+
+ return (int)(latency + 0.5);
+ }
+
+ default :
+ return 0;
+ }
+}
+
+
+// Clears all the samples in the object's output and internal processing
+// buffers.
+void SoundTouch::clear()
+{
+ samplesExpectedOut = 0;
+ samplesOutput = 0;
+ pRateTransposer->clear();
+ pTDStretch->clear();
+}
+
+
+/// Returns number of samples currently unprocessed.
+uint SoundTouch::numUnprocessedSamples() const
+{
+ FIFOSamplePipe * psp;
+ if (pTDStretch)
+ {
+ psp = pTDStretch->getInput();
+ if (psp)
+ {
+ return psp->numSamples();
+ }
+ }
+ return 0;
+}
+
+
+/// Output samples from beginning of the sample buffer. Copies requested samples to
+/// output buffer and removes them from the sample buffer. If there are less than
+/// 'numsample' samples in the buffer, returns all that available.
+///
+/// \return Number of samples returned.
+uint SoundTouch::receiveSamples(SAMPLETYPE *output, uint maxSamples)
+{
+ uint ret = FIFOProcessor::receiveSamples(output, maxSamples);
+ samplesOutput += (long)ret;
+ return ret;
+}
+
+
+/// Adjusts book-keeping so that given number of samples are removed from beginning of the
+/// sample buffer without copying them anywhere.
+///
+/// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
+/// with 'ptrBegin' function.
+uint SoundTouch::receiveSamples(uint maxSamples)
+{
+ uint ret = FIFOProcessor::receiveSamples(maxSamples);
+ samplesOutput += (long)ret;
+ return ret;
+}
+
+
+/// Get ratio between input and output audio durations, useful for calculating
+/// processed output duration: if you'll process a stream of N samples, then
+/// you can expect to get out N * getInputOutputSampleRatio() samples.
+double SoundTouch::getInputOutputSampleRatio()
+{
+ return 1.0 / (tempo * rate);
+}
diff --git a/third_party/soundtouch/source/SoundTouch/TDStretch.cpp b/third_party/soundtouch/source/SoundTouch/TDStretch.cpp
new file mode 100644
index 00000000..93ac181e
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/TDStretch.cpp
@@ -0,0 +1,1088 @@
+///////////////////////////////////////////////////////////////////////////////
+///
+/// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo
+/// while maintaining the original pitch by using a time domain WSOLA-like
+/// method with several performance-increasing tweaks.
+///
+/// Notes : MMX optimized functions reside in a separate, platform-specific
+/// file, e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp'.
+///
+/// This source file contains OpenMP optimizations that allow speeding up the
+/// corss-correlation algorithm by executing it in several threads / CPU cores
+/// in parallel. See the following article link for more detailed discussion
+/// about SoundTouch OpenMP optimizations:
+/// http://www.softwarecoven.com/parallel-computing-in-embedded-mobile-devices
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <string.h>
+#include <limits.h>
+#include <assert.h>
+#include <math.h>
+#include <float.h>
+
+#include "STTypes.h"
+#include "cpu_detect.h"
+#include "TDStretch.h"
+
+using namespace soundtouch;
+
+#define max(x, y) (((x) > (y)) ? (x) : (y))
+
+/*****************************************************************************
+ *
+ * Implementation of the class 'TDStretch'
+ *
+ *****************************************************************************/
+
+
+TDStretch::TDStretch() : FIFOProcessor(&outputBuffer)
+{
+ bQuickSeek = false;
+ channels = 2;
+
+ pMidBuffer = nullptr;
+ pMidBufferUnaligned = nullptr;
+ overlapLength = 0;
+
+ bAutoSeqSetting = true;
+ bAutoSeekSetting = true;
+
+ tempo = 1.0f;
+ setParameters(44100, DEFAULT_SEQUENCE_MS, DEFAULT_SEEKWINDOW_MS, DEFAULT_OVERLAP_MS);
+ setTempo(1.0f);
+
+ clear();
+}
+
+
+
+TDStretch::~TDStretch()
+{
+ delete[] pMidBufferUnaligned;
+}
+
+
+
+// Sets routine control parameters. These control are certain time constants
+// defining how the sound is stretched to the desired duration.
+//
+// 'sampleRate' = sample rate of the sound
+// 'sequenceMS' = one processing sequence length in milliseconds (default = 82 ms)
+// 'seekwindowMS' = seeking window length for scanning the best overlapping
+// position (default = 28 ms)
+// 'overlapMS' = overlapping length (default = 12 ms)
+
+void TDStretch::setParameters(int aSampleRate, int aSequenceMS,
+ int aSeekWindowMS, int aOverlapMS)
+{
+ // accept only positive parameter values - if zero or negative, use old values instead
+ if (aSampleRate > 0)
+ {
+ if (aSampleRate > 192000) ST_THROW_RT_ERROR("Error: Excessive samplerate");
+ this->sampleRate = aSampleRate;
+ }
+
+ if (aOverlapMS > 0) this->overlapMs = aOverlapMS;
+
+ if (aSequenceMS > 0)
+ {
+ this->sequenceMs = aSequenceMS;
+ bAutoSeqSetting = false;
+ }
+ else if (aSequenceMS == 0)
+ {
+ // if zero, use automatic setting
+ bAutoSeqSetting = true;
+ }
+
+ if (aSeekWindowMS > 0)
+ {
+ this->seekWindowMs = aSeekWindowMS;
+ bAutoSeekSetting = false;
+ }
+ else if (aSeekWindowMS == 0)
+ {
+ // if zero, use automatic setting
+ bAutoSeekSetting = true;
+ }
+
+ calcSeqParameters();
+
+ calculateOverlapLength(overlapMs);
+
+ // set tempo to recalculate 'sampleReq'
+ setTempo(tempo);
+}
+
+
+
+/// Get routine control parameters, see setParameters() function.
+/// Any of the parameters to this function can be nullptr, in such case corresponding parameter
+/// value isn't returned.
+void TDStretch::getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const
+{
+ if (pSampleRate)
+ {
+ *pSampleRate = sampleRate;
+ }
+
+ if (pSequenceMs)
+ {
+ *pSequenceMs = (bAutoSeqSetting) ? (USE_AUTO_SEQUENCE_LEN) : sequenceMs;
+ }
+
+ if (pSeekWindowMs)
+ {
+ *pSeekWindowMs = (bAutoSeekSetting) ? (USE_AUTO_SEEKWINDOW_LEN) : seekWindowMs;
+ }
+
+ if (pOverlapMs)
+ {
+ *pOverlapMs = overlapMs;
+ }
+}
+
+
+// Overlaps samples in 'midBuffer' with the samples in 'pInput'
+void TDStretch::overlapMono(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput) const
+{
+ int i;
+ SAMPLETYPE m1, m2;
+
+ m1 = (SAMPLETYPE)0;
+ m2 = (SAMPLETYPE)overlapLength;
+
+ for (i = 0; i < overlapLength ; i ++)
+ {
+ pOutput[i] = (pInput[i] * m1 + pMidBuffer[i] * m2 ) / overlapLength;
+ m1 += 1;
+ m2 -= 1;
+ }
+}
+
+
+
+void TDStretch::clearMidBuffer()
+{
+ memset(pMidBuffer, 0, channels * sizeof(SAMPLETYPE) * overlapLength);
+}
+
+
+void TDStretch::clearInput()
+{
+ inputBuffer.clear();
+ clearMidBuffer();
+ isBeginning = true;
+ maxnorm = 0;
+ maxnormf = 1e8;
+ skipFract = 0;
+}
+
+
+// Clears the sample buffers
+void TDStretch::clear()
+{
+ outputBuffer.clear();
+ clearInput();
+}
+
+
+
+// Enables/disables the quick position seeking algorithm. Zero to disable, nonzero
+// to enable
+void TDStretch::enableQuickSeek(bool enable)
+{
+ bQuickSeek = enable;
+}
+
+
+// Returns nonzero if the quick seeking algorithm is enabled.
+bool TDStretch::isQuickSeekEnabled() const
+{
+ return bQuickSeek;
+}
+
+
+// Seeks for the optimal overlap-mixing position.
+int TDStretch::seekBestOverlapPosition(const SAMPLETYPE *refPos)
+{
+ if (bQuickSeek)
+ {
+ return seekBestOverlapPositionQuick(refPos);
+ }
+ else
+ {
+ return seekBestOverlapPositionFull(refPos);
+ }
+}
+
+
+// Overlaps samples in 'midBuffer' with the samples in 'pInputBuffer' at position
+// of 'ovlPos'.
+inline void TDStretch::overlap(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput, uint ovlPos) const
+{
+#ifndef USE_MULTICH_ALWAYS
+ if (channels == 1)
+ {
+ // mono sound.
+ overlapMono(pOutput, pInput + ovlPos);
+ }
+ else if (channels == 2)
+ {
+ // stereo sound
+ overlapStereo(pOutput, pInput + 2 * ovlPos);
+ }
+ else
+#endif // USE_MULTICH_ALWAYS
+ {
+ assert(channels > 0);
+ overlapMulti(pOutput, pInput + channels * ovlPos);
+ }
+}
+
+
+// Seeks for the optimal overlap-mixing position. The 'stereo' version of the
+// routine
+//
+// The best position is determined as the position where the two overlapped
+// sample sequences are 'most alike', in terms of the highest cross-correlation
+// value over the overlapping period
+int TDStretch::seekBestOverlapPositionFull(const SAMPLETYPE *refPos)
+{
+ int bestOffs;
+ double bestCorr;
+ int i;
+ double norm;
+
+ bestCorr = -FLT_MAX;
+ bestOffs = 0;
+
+ // Scans for the best correlation value by testing each possible position
+ // over the permitted range.
+ bestCorr = calcCrossCorr(refPos, pMidBuffer, norm);
+ bestCorr = (bestCorr + 0.1) * 0.75;
+
+ #pragma omp parallel for
+ for (i = 1; i < seekLength; i ++)
+ {
+ double corr;
+ // Calculates correlation value for the mixing position corresponding to 'i'
+#if defined(_OPENMP) || defined(ST_SIMD_AVOID_UNALIGNED)
+ // in parallel OpenMP mode, can't use norm accumulator version as parallel executor won't
+ // iterate the loop in sequential order
+ // in SIMD mode, avoid accumulator version to allow avoiding unaligned positions
+ corr = calcCrossCorr(refPos + channels * i, pMidBuffer, norm);
+#else
+ // In non-parallel version call "calcCrossCorrAccumulate" that is otherwise same
+ // as "calcCrossCorr", but saves time by reusing & updating previously stored
+ // "norm" value
+ corr = calcCrossCorrAccumulate(refPos + channels * i, pMidBuffer, norm);
+#endif
+ // heuristic rule to slightly favour values close to mid of the range
+ double tmp = (double)(2 * i - seekLength) / (double)seekLength;
+ corr = ((corr + 0.1) * (1.0 - 0.25 * tmp * tmp));
+
+ // Checks for the highest correlation value
+ if (corr > bestCorr)
+ {
+ // For optimal performance, enter critical section only in case that best value found.
+ // in such case repeat 'if' condition as it's possible that parallel execution may have
+ // updated the bestCorr value in the mean time
+ #pragma omp critical
+ if (corr > bestCorr)
+ {
+ bestCorr = corr;
+ bestOffs = i;
+ }
+ }
+ }
+
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ adaptNormalizer();
+#endif
+
+ // clear cross correlation routine state if necessary (is so e.g. in MMX routines).
+ clearCrossCorrState();
+
+ return bestOffs;
+}
+
+
+// Quick seek algorithm for improved runtime-performance: First roughly scans through the
+// correlation area, and then scan surroundings of two best preliminary correlation candidates
+// with improved precision
+//
+// Based on testing:
+// - This algorithm gives on average 99% as good match as the full algorithm
+// - this quick seek algorithm finds the best match on ~90% of cases
+// - on those 10% of cases when this algorithm doesn't find best match,
+// it still finds on average ~90% match vs. the best possible match
+int TDStretch::seekBestOverlapPositionQuick(const SAMPLETYPE *refPos)
+{
+#define _MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define SCANSTEP 16
+#define SCANWIND 8
+
+ int bestOffs;
+ int i;
+ int bestOffs2;
+ float bestCorr, corr;
+ float bestCorr2;
+ double norm;
+
+ // note: 'float' types used in this function in case that the platform would need to use software-fp
+
+ bestCorr =
+ bestCorr2 = -FLT_MAX;
+ bestOffs =
+ bestOffs2 = SCANWIND;
+
+ // Scans for the best correlation value by testing each possible position
+ // over the permitted range. Look for two best matches on the first pass to
+ // increase possibility of ideal match.
+ //
+ // Begin from "SCANSTEP" instead of SCANWIND to make the calculation
+ // catch the 'middlepoint' of seekLength vector as that's the a-priori
+ // expected best match position
+ //
+ // Roughly:
+ // - 15% of cases find best result directly on the first round,
+ // - 75% cases find better match on 2nd round around the best match from 1st round
+ // - 10% cases find better match on 2nd round around the 2nd-best-match from 1st round
+ for (i = SCANSTEP; i < seekLength - SCANWIND - 1; i += SCANSTEP)
+ {
+ // Calculates correlation value for the mixing position corresponding
+ // to 'i'
+ corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
+ // heuristic rule to slightly favour values close to mid of the seek range
+ float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
+ corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
+
+ // Checks for the highest correlation value
+ if (corr > bestCorr)
+ {
+ // found new best match. keep the previous best as 2nd best match
+ bestCorr2 = bestCorr;
+ bestOffs2 = bestOffs;
+ bestCorr = corr;
+ bestOffs = i;
+ }
+ else if (corr > bestCorr2)
+ {
+ // not new best, but still new 2nd best match
+ bestCorr2 = corr;
+ bestOffs2 = i;
+ }
+ }
+
+ // Scans surroundings of the found best match with small stepping
+ int end = _MIN(bestOffs + SCANWIND + 1, seekLength);
+ for (i = bestOffs - SCANWIND; i < end; i++)
+ {
+ if (i == bestOffs) continue; // this offset already calculated, thus skip
+
+ // Calculates correlation value for the mixing position corresponding
+ // to 'i'
+ corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
+ // heuristic rule to slightly favour values close to mid of the range
+ float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
+ corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
+
+ // Checks for the highest correlation value
+ if (corr > bestCorr)
+ {
+ bestCorr = corr;
+ bestOffs = i;
+ }
+ }
+
+ // Scans surroundings of the 2nd best match with small stepping
+ end = _MIN(bestOffs2 + SCANWIND + 1, seekLength);
+ for (i = bestOffs2 - SCANWIND; i < end; i++)
+ {
+ if (i == bestOffs2) continue; // this offset already calculated, thus skip
+
+ // Calculates correlation value for the mixing position corresponding
+ // to 'i'
+ corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm);
+ // heuristic rule to slightly favour values close to mid of the range
+ float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength;
+ corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp));
+
+ // Checks for the highest correlation value
+ if (corr > bestCorr)
+ {
+ bestCorr = corr;
+ bestOffs = i;
+ }
+ }
+
+ // clear cross correlation routine state if necessary (is so e.g. in MMX routines).
+ clearCrossCorrState();
+
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+ adaptNormalizer();
+#endif
+
+ return bestOffs;
+}
+
+
+
+
+/// For integer algorithm: adapt normalization factor divider with music so that
+/// it'll not be pessimistically restrictive that can degrade quality on quieter sections
+/// yet won't cause integer overflows either
+void TDStretch::adaptNormalizer()
+{
+ // Do not adapt normalizer over too silent sequences to avoid averaging filter depleting to
+ // too low values during pauses in music
+ if ((maxnorm > 1000) || (maxnormf > 40000000))
+ {
+ //norm averaging filter
+ maxnormf = 0.9f * maxnormf + 0.1f * (float)maxnorm;
+
+ if ((maxnorm > 800000000) && (overlapDividerBitsNorm < 16))
+ {
+ // large values, so increase divider
+ overlapDividerBitsNorm++;
+ if (maxnorm > 1600000000) overlapDividerBitsNorm++; // extra large value => extra increase
+ }
+ else if ((maxnormf < 1000000) && (overlapDividerBitsNorm > 0))
+ {
+ // extra small values, decrease divider
+ overlapDividerBitsNorm--;
+ }
+ }
+
+ maxnorm = 0;
+}
+
+
+/// clear cross correlation routine state if necessary
+void TDStretch::clearCrossCorrState()
+{
+ // default implementation is empty.
+}
+
+
+/// Calculates processing sequence length according to tempo setting
+void TDStretch::calcSeqParameters()
+{
+ // Adjust tempo param according to tempo, so that variating processing sequence length is used
+ // at various tempo settings, between the given low...top limits
+ #define AUTOSEQ_TEMPO_LOW 0.5 // auto setting low tempo range (-50%)
+ #define AUTOSEQ_TEMPO_TOP 2.0 // auto setting top tempo range (+100%)
+
+ // sequence-ms setting values at above low & top tempo
+ #define AUTOSEQ_AT_MIN 90.0
+ #define AUTOSEQ_AT_MAX 40.0
+ #define AUTOSEQ_K ((AUTOSEQ_AT_MAX - AUTOSEQ_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW))
+ #define AUTOSEQ_C (AUTOSEQ_AT_MIN - (AUTOSEQ_K) * (AUTOSEQ_TEMPO_LOW))
+
+ // seek-window-ms setting values at above low & top tempoq
+ #define AUTOSEEK_AT_MIN 20.0
+ #define AUTOSEEK_AT_MAX 15.0
+ #define AUTOSEEK_K ((AUTOSEEK_AT_MAX - AUTOSEEK_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW))
+ #define AUTOSEEK_C (AUTOSEEK_AT_MIN - (AUTOSEEK_K) * (AUTOSEQ_TEMPO_LOW))
+
+ #define CHECK_LIMITS(x, mi, ma) (((x) < (mi)) ? (mi) : (((x) > (ma)) ? (ma) : (x)))
+
+ double seq, seek;
+
+ if (bAutoSeqSetting)
+ {
+ seq = AUTOSEQ_C + AUTOSEQ_K * tempo;
+ seq = CHECK_LIMITS(seq, AUTOSEQ_AT_MAX, AUTOSEQ_AT_MIN);
+ sequenceMs = (int)(seq + 0.5);
+ }
+
+ if (bAutoSeekSetting)
+ {
+ seek = AUTOSEEK_C + AUTOSEEK_K * tempo;
+ seek = CHECK_LIMITS(seek, AUTOSEEK_AT_MAX, AUTOSEEK_AT_MIN);
+ seekWindowMs = (int)(seek + 0.5);
+ }
+
+ // Update seek window lengths
+ seekWindowLength = (sampleRate * sequenceMs) / 1000;
+ if (seekWindowLength < 2 * overlapLength)
+ {
+ seekWindowLength = 2 * overlapLength;
+ }
+ seekLength = (sampleRate * seekWindowMs) / 1000;
+}
+
+
+
+// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower
+// tempo, larger faster tempo.
+void TDStretch::setTempo(double newTempo)
+{
+ int intskip;
+
+ tempo = newTempo;
+
+ // Calculate new sequence duration
+ calcSeqParameters();
+
+ // Calculate ideal skip length (according to tempo value)
+ nominalSkip = tempo * (seekWindowLength - overlapLength);
+ intskip = (int)(nominalSkip + 0.5);
+
+ // Calculate how many samples are needed in the 'inputBuffer' to
+ // process another batch of samples
+ //sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength / 2;
+ sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength;
+}
+
+
+
+// Sets the number of channels, 1 = mono, 2 = stereo
+void TDStretch::setChannels(int numChannels)
+{
+ if (!verifyNumberOfChannels(numChannels) ||
+ (channels == numChannels)) return;
+
+ channels = numChannels;
+ inputBuffer.setChannels(channels);
+ outputBuffer.setChannels(channels);
+
+ // re-init overlap/buffer
+ overlapLength=0;
+ setParameters(sampleRate);
+}
+
+
+// nominal tempo, no need for processing, just pass the samples through
+// to outputBuffer
+/*
+void TDStretch::processNominalTempo()
+{
+ assert(tempo == 1.0f);
+
+ if (bMidBufferDirty)
+ {
+ // If there are samples in pMidBuffer waiting for overlapping,
+ // do a single sliding overlapping with them in order to prevent a
+ // clicking distortion in the output sound
+ if (inputBuffer.numSamples() < overlapLength)
+ {
+ // wait until we've got overlapLength input samples
+ return;
+ }
+ // Mix the samples in the beginning of 'inputBuffer' with the
+ // samples in 'midBuffer' using sliding overlapping
+ overlap(outputBuffer.ptrEnd(overlapLength), inputBuffer.ptrBegin(), 0);
+ outputBuffer.putSamples(overlapLength);
+ inputBuffer.receiveSamples(overlapLength);
+ clearMidBuffer();
+ // now we've caught the nominal sample flow and may switch to
+ // bypass mode
+ }
+
+ // Simply bypass samples from input to output
+ outputBuffer.moveSamples(inputBuffer);
+}
+*/
+
+
+// Processes as many processing frames of the samples 'inputBuffer', store
+// the result into 'outputBuffer'
+void TDStretch::processSamples()
+{
+ int ovlSkip;
+ int offset = 0;
+ int temp;
+
+ /* Removed this small optimization - can introduce a click to sound when tempo setting
+ crosses the nominal value
+ if (tempo == 1.0f)
+ {
+ // tempo not changed from the original, so bypass the processing
+ processNominalTempo();
+ return;
+ }
+ */
+
+ // Process samples as long as there are enough samples in 'inputBuffer'
+ // to form a processing frame.
+ while ((int)inputBuffer.numSamples() >= sampleReq)
+ {
+ if (isBeginning == false)
+ {
+ // apart from the very beginning of the track,
+ // scan for the best overlapping position & do overlap-add
+ offset = seekBestOverlapPosition(inputBuffer.ptrBegin());
+
+ // Mix the samples in the 'inputBuffer' at position of 'offset' with the
+ // samples in 'midBuffer' using sliding overlapping
+ // ... first partially overlap with the end of the previous sequence
+ // (that's in 'midBuffer')
+ overlap(outputBuffer.ptrEnd((uint)overlapLength), inputBuffer.ptrBegin(), (uint)offset);
+ outputBuffer.putSamples((uint)overlapLength);
+ offset += overlapLength;
+ }
+ else
+ {
+ // Adjust processing offset at beginning of track by not perform initial overlapping
+ // and compensating that in the 'input buffer skip' calculation
+ isBeginning = false;
+ int skip = (int)(tempo * overlapLength + 0.5 * seekLength + 0.5);
+
+ #ifdef ST_SIMD_AVOID_UNALIGNED
+ // in SIMD mode, round the skip amount to value corresponding to aligned memory address
+ if (channels == 1)
+ {
+ skip &= -4;
+ }
+ else if (channels == 2)
+ {
+ skip &= -2;
+ }
+ #endif
+ skipFract -= skip;
+ if (skipFract <= -nominalSkip)
+ {
+ skipFract = -nominalSkip;
+ }
+ }
+
+ // ... then copy sequence samples from 'inputBuffer' to output:
+
+ // crosscheck that we don't have buffer overflow...
+ if ((int)inputBuffer.numSamples() < (offset + seekWindowLength - overlapLength))
+ {
+ continue; // just in case, shouldn't really happen
+ }
+
+ // length of sequence
+ temp = (seekWindowLength - 2 * overlapLength);
+ outputBuffer.putSamples(inputBuffer.ptrBegin() + channels * offset, (uint)temp);
+
+ // Copies the end of the current sequence from 'inputBuffer' to
+ // 'midBuffer' for being mixed with the beginning of the next
+ // processing sequence and so on
+ assert((offset + temp + overlapLength) <= (int)inputBuffer.numSamples());
+ memcpy(pMidBuffer, inputBuffer.ptrBegin() + channels * (offset + temp),
+ channels * sizeof(SAMPLETYPE) * overlapLength);
+
+ // Remove the processed samples from the input buffer. Update
+ // the difference between integer & nominal skip step to 'skipFract'
+ // in order to prevent the error from accumulating over time.
+ skipFract += nominalSkip; // real skip size
+ ovlSkip = (int)skipFract; // rounded to integer skip
+ skipFract -= ovlSkip; // maintain the fraction part, i.e. real vs. integer skip
+ inputBuffer.receiveSamples((uint)ovlSkip);
+ }
+}
+
+
+// Adds 'numsamples' pcs of samples from the 'samples' memory position into
+// the input of the object.
+void TDStretch::putSamples(const SAMPLETYPE *samples, uint nSamples)
+{
+ // Add the samples into the input buffer
+ inputBuffer.putSamples(samples, nSamples);
+ // Process the samples in input buffer
+ processSamples();
+}
+
+
+
+/// Set new overlap length parameter & reallocate RefMidBuffer if necessary.
+void TDStretch::acceptNewOverlapLength(int newOverlapLength)
+{
+ int prevOvl;
+
+ assert(newOverlapLength >= 0);
+ prevOvl = overlapLength;
+ overlapLength = newOverlapLength;
+
+ if (overlapLength > prevOvl)
+ {
+ delete[] pMidBufferUnaligned;
+
+ pMidBufferUnaligned = new SAMPLETYPE[overlapLength * channels + 16 / sizeof(SAMPLETYPE)];
+ // ensure that 'pMidBuffer' is aligned to 16 byte boundary for efficiency
+ pMidBuffer = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(pMidBufferUnaligned);
+
+ clearMidBuffer();
+ }
+}
+
+
+// Operator 'new' is overloaded so that it automatically creates a suitable instance
+// depending on if we've a MMX/SSE/etc-capable CPU available or not.
+void * TDStretch::operator new(size_t)
+{
+ // Notice! don't use "new TDStretch" directly, use "newInstance" to create a new instance instead!
+ ST_THROW_RT_ERROR("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!");
+ return newInstance();
+}
+
+
+TDStretch * TDStretch::newInstance()
+{
+ uint uExtensions;
+
+ uExtensions = detectCPUextensions();
+ (void)uExtensions;
+
+ // Check if MMX/SSE instruction set extensions supported by CPU
+
+#ifdef SOUNDTOUCH_ALLOW_MMX
+ // MMX routines available only with integer sample types
+ if (uExtensions & SUPPORT_MMX)
+ {
+ return ::new TDStretchMMX;
+ }
+ else
+#endif // SOUNDTOUCH_ALLOW_MMX
+
+
+#ifdef SOUNDTOUCH_ALLOW_SSE
+ if (uExtensions & SUPPORT_SSE)
+ {
+ // SSE support
+ return ::new TDStretchSSE;
+ }
+ else
+#endif // SOUNDTOUCH_ALLOW_SSE
+
+ {
+ // ISA optimizations not supported, use plain C version
+ return ::new TDStretch;
+ }
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Integer arithmetic specific algorithm implementations.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifdef SOUNDTOUCH_INTEGER_SAMPLES
+
+// Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Stereo'
+// version of the routine.
+void TDStretch::overlapStereo(short *poutput, const short *input) const
+{
+ int i;
+ short temp;
+ int cnt2;
+
+ for (i = 0; i < overlapLength ; i ++)
+ {
+ temp = (short)(overlapLength - i);
+ cnt2 = 2 * i;
+ poutput[cnt2] = (input[cnt2] * i + pMidBuffer[cnt2] * temp ) / overlapLength;
+ poutput[cnt2 + 1] = (input[cnt2 + 1] * i + pMidBuffer[cnt2 + 1] * temp ) / overlapLength;
+ }
+}
+
+
+// Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Multi'
+// version of the routine.
+void TDStretch::overlapMulti(short *poutput, const short *input) const
+{
+ short m1;
+ int i = 0;
+
+ for (m1 = 0; m1 < overlapLength; m1 ++)
+ {
+ short m2 = (short)(overlapLength - m1);
+ for (int c = 0; c < channels; c ++)
+ {
+ poutput[i] = (input[i] * m1 + pMidBuffer[i] * m2) / overlapLength;
+ i++;
+ }
+ }
+}
+
+// Calculates the x having the closest 2^x value for the given value
+static int _getClosest2Power(double value)
+{
+ return (int)(log(value) / log(2.0) + 0.5);
+}
+
+
+/// Calculates overlap period length in samples.
+/// Integer version rounds overlap length to closest power of 2
+/// for a divide scaling operation.
+void TDStretch::calculateOverlapLength(int aoverlapMs)
+{
+ int newOvl;
+
+ assert(aoverlapMs >= 0);
+
+ // calculate overlap length so that it's power of 2 - thus it's easy to do
+ // integer division by right-shifting. Term "-1" at end is to account for
+ // the extra most significatnt bit left unused in result by signed multiplication
+ overlapDividerBitsPure = _getClosest2Power((sampleRate * aoverlapMs) / 1000.0) - 1;
+ if (overlapDividerBitsPure > 9) overlapDividerBitsPure = 9;
+ if (overlapDividerBitsPure < 3) overlapDividerBitsPure = 3;
+ newOvl = (int)pow(2.0, (int)overlapDividerBitsPure + 1); // +1 => account for -1 above
+
+ acceptNewOverlapLength(newOvl);
+
+ overlapDividerBitsNorm = overlapDividerBitsPure;
+
+ // calculate sloping divider so that crosscorrelation operation won't
+ // overflow 32-bit register. Max. sum of the crosscorrelation sum without
+ // divider would be 2^30*(N^3-N)/3, where N = overlap length
+ slopingDivider = (newOvl * newOvl - 1) / 3;
+}
+
+
+double TDStretch::calcCrossCorr(const short *mixingPos, const short *compare, double &norm)
+{
+ long corr;
+ unsigned long lnorm;
+ int i;
+
+ #ifdef ST_SIMD_AVOID_UNALIGNED
+ // in SIMD mode skip 'mixingPos' positions that aren't aligned to 16-byte boundary
+ if (((ulongptr)mixingPos) & 15) return -1e50;
+ #endif
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = (channels * overlapLength) & -8;
+
+ corr = lnorm = 0;
+ // Same routine for stereo and mono
+ for (i = 0; i < ilength; i += 2)
+ {
+ corr += (mixingPos[i] * compare[i] +
+ mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm;
+ lnorm += (mixingPos[i] * mixingPos[i] +
+ mixingPos[i + 1] * mixingPos[i + 1]) >> overlapDividerBitsNorm;
+ // do intermediate scalings to avoid integer overflow
+ }
+
+ if (lnorm > maxnorm)
+ {
+ // modify 'maxnorm' inside critical section to avoid multi-access conflict if in OpenMP mode
+ #pragma omp critical
+ if (lnorm > maxnorm)
+ {
+ maxnorm = lnorm;
+ }
+ }
+ // Normalize result by dividing by sqrt(norm) - this step is easiest
+ // done using floating point operation
+ norm = (double)lnorm;
+ return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm);
+}
+
+
+/// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
+double TDStretch::calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm)
+{
+ long corr;
+ long lnorm;
+ int i;
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = (channels * overlapLength) & -8;
+
+ // cancel first normalizer tap from previous round
+ lnorm = 0;
+ for (i = 1; i <= channels; i ++)
+ {
+ lnorm -= (mixingPos[-i] * mixingPos[-i]) >> overlapDividerBitsNorm;
+ }
+
+ corr = 0;
+ // Same routine for stereo and mono.
+ for (i = 0; i < ilength; i += 2)
+ {
+ corr += (mixingPos[i] * compare[i] +
+ mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm;
+ }
+
+ // update normalizer with last samples of this round
+ for (int j = 0; j < channels; j ++)
+ {
+ i --;
+ lnorm += (mixingPos[i] * mixingPos[i]) >> overlapDividerBitsNorm;
+ }
+
+ norm += (double)lnorm;
+ if (norm > maxnorm)
+ {
+ maxnorm = (unsigned long)norm;
+ }
+
+ // Normalize result by dividing by sqrt(norm) - this step is easiest
+ // done using floating point operation
+ return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm);
+}
+
+#endif // SOUNDTOUCH_INTEGER_SAMPLES
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Floating point arithmetic specific algorithm implementations.
+//
+
+#ifdef SOUNDTOUCH_FLOAT_SAMPLES
+
+// Overlaps samples in 'midBuffer' with the samples in 'pInput'
+void TDStretch::overlapStereo(float *pOutput, const float *pInput) const
+{
+ int i;
+ float fScale;
+ float f1;
+ float f2;
+
+ fScale = 1.0f / (float)overlapLength;
+
+ f1 = 0;
+ f2 = 1.0f;
+
+ for (i = 0; i < 2 * (int)overlapLength ; i += 2)
+ {
+ pOutput[i + 0] = pInput[i + 0] * f1 + pMidBuffer[i + 0] * f2;
+ pOutput[i + 1] = pInput[i + 1] * f1 + pMidBuffer[i + 1] * f2;
+
+ f1 += fScale;
+ f2 -= fScale;
+ }
+}
+
+
+// Overlaps samples in 'midBuffer' with the samples in 'input'.
+void TDStretch::overlapMulti(float *pOutput, const float *pInput) const
+{
+ int i;
+ float fScale;
+ float f1;
+ float f2;
+
+ fScale = 1.0f / (float)overlapLength;
+
+ f1 = 0;
+ f2 = 1.0f;
+
+ i=0;
+ for (int i2 = 0; i2 < overlapLength; i2 ++)
+ {
+ // note: Could optimize this slightly by taking into account that always channels > 2
+ for (int c = 0; c < channels; c ++)
+ {
+ pOutput[i] = pInput[i] * f1 + pMidBuffer[i] * f2;
+ i++;
+ }
+ f1 += fScale;
+ f2 -= fScale;
+ }
+}
+
+
+/// Calculates overlapInMsec period length in samples.
+void TDStretch::calculateOverlapLength(int overlapInMsec)
+{
+ int newOvl;
+
+ assert(overlapInMsec >= 0);
+ newOvl = (sampleRate * overlapInMsec) / 1000;
+ if (newOvl < 16) newOvl = 16;
+
+ // must be divisible by 8
+ newOvl -= newOvl % 8;
+
+ acceptNewOverlapLength(newOvl);
+}
+
+
+/// Calculate cross-correlation
+double TDStretch::calcCrossCorr(const float *mixingPos, const float *compare, double &anorm)
+{
+ float corr;
+ float norm;
+ int i;
+
+ #ifdef ST_SIMD_AVOID_UNALIGNED
+ // in SIMD mode skip 'mixingPos' positions that aren't aligned to 16-byte boundary
+ if (((ulongptr)mixingPos) & 15) return -1e50;
+ #endif
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = (channels * overlapLength) & -8;
+
+ corr = norm = 0;
+ // Same routine for stereo and mono
+ for (i = 0; i < ilength; i ++)
+ {
+ corr += mixingPos[i] * compare[i];
+ norm += mixingPos[i] * mixingPos[i];
+ }
+
+ anorm = norm;
+ return corr / sqrt((norm < 1e-9 ? 1.0 : norm));
+}
+
+
+/// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
+double TDStretch::calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm)
+{
+ float corr;
+ int i;
+
+ corr = 0;
+
+ // cancel first normalizer tap from previous round
+ for (i = 1; i <= channels; i ++)
+ {
+ norm -= mixingPos[-i] * mixingPos[-i];
+ }
+
+ // hint compiler autovectorization that loop length is divisible by 8
+ int ilength = (channels * overlapLength) & -8;
+
+ // Same routine for stereo and mono
+ for (i = 0; i < ilength; i ++)
+ {
+ corr += mixingPos[i] * compare[i];
+ }
+
+ // update normalizer with last samples of this round
+ for (int j = 0; j < channels; j ++)
+ {
+ i --;
+ norm += mixingPos[i] * mixingPos[i];
+ }
+
+ return corr / sqrt((norm < 1e-9 ? 1.0 : norm));
+}
+
+
+#endif // SOUNDTOUCH_FLOAT_SAMPLES
diff --git a/third_party/soundtouch/source/SoundTouch/TDStretch.h b/third_party/soundtouch/source/SoundTouch/TDStretch.h
new file mode 100644
index 00000000..483dd536
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/TDStretch.h
@@ -0,0 +1,279 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo
+/// while maintaining the original pitch by using a time domain WSOLA-like method
+/// with several performance-increasing tweaks.
+///
+/// Note : MMX/SSE optimized functions reside in separate, platform-specific files
+/// 'mmx_optimized.cpp' and 'sse_optimized.cpp'
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef TDStretch_H
+#define TDStretch_H
+
+#include <stddef.h>
+#include "STTypes.h"
+#include "RateTransposer.h"
+#include "FIFOSamplePipe.h"
+
+namespace soundtouch
+{
+
+/// Default values for sound processing parameters:
+/// Notice that the default parameters are tuned for contemporary popular music
+/// processing. For speech processing applications these parameters suit better:
+/// #define DEFAULT_SEQUENCE_MS 40
+/// #define DEFAULT_SEEKWINDOW_MS 15
+/// #define DEFAULT_OVERLAP_MS 8
+///
+
+/// Default length of a single processing sequence, in milliseconds. This determines to how
+/// long sequences the original sound is chopped in the time-stretch algorithm.
+///
+/// The larger this value is, the lesser sequences are used in processing. In principle
+/// a bigger value sounds better when slowing down tempo, but worse when increasing tempo
+/// and vice versa.
+///
+/// Increasing this value reduces computational burden & vice versa.
+//#define DEFAULT_SEQUENCE_MS 40
+#define DEFAULT_SEQUENCE_MS USE_AUTO_SEQUENCE_LEN
+
+/// Giving this value for the sequence length sets automatic parameter value
+/// according to tempo setting (recommended)
+#define USE_AUTO_SEQUENCE_LEN 0
+
+/// Seeking window default length in milliseconds for algorithm that finds the best possible
+/// overlapping location. This determines from how wide window the algorithm may look for an
+/// optimal joining location when mixing the sound sequences back together.
+///
+/// The bigger this window setting is, the higher the possibility to find a better mixing
+/// position will become, but at the same time large values may cause a "drifting" artifact
+/// because consequent sequences will be taken at more uneven intervals.
+///
+/// If there's a disturbing artifact that sounds as if a constant frequency was drifting
+/// around, try reducing this setting.
+///
+/// Increasing this value increases computational burden & vice versa.
+//#define DEFAULT_SEEKWINDOW_MS 15
+#define DEFAULT_SEEKWINDOW_MS USE_AUTO_SEEKWINDOW_LEN
+
+/// Giving this value for the seek window length sets automatic parameter value
+/// according to tempo setting (recommended)
+#define USE_AUTO_SEEKWINDOW_LEN 0
+
+/// Overlap length in milliseconds. When the chopped sound sequences are mixed back together,
+/// to form a continuous sound stream, this parameter defines over how long period the two
+/// consecutive sequences are let to overlap each other.
+///
+/// This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting
+/// by a large amount, you might wish to try a smaller value on this.
+///
+/// Increasing this value increases computational burden & vice versa.
+#define DEFAULT_OVERLAP_MS 8
+
+
+/// Class that does the time-stretch (tempo change) effect for the processed
+/// sound.
+class TDStretch : public FIFOProcessor
+{
+protected:
+ int channels;
+ int sampleReq;
+
+ int overlapLength;
+ int seekLength;
+ int seekWindowLength;
+ int overlapDividerBitsNorm;
+ int overlapDividerBitsPure;
+ int slopingDivider;
+ int sampleRate;
+ int sequenceMs;
+ int seekWindowMs;
+ int overlapMs;
+
+ unsigned long maxnorm;
+ float maxnormf;
+
+ double tempo;
+ double nominalSkip;
+ double skipFract;
+
+ bool bQuickSeek;
+ bool bAutoSeqSetting;
+ bool bAutoSeekSetting;
+ bool isBeginning;
+
+ SAMPLETYPE *pMidBuffer;
+ SAMPLETYPE *pMidBufferUnaligned;
+
+ FIFOSampleBuffer outputBuffer;
+ FIFOSampleBuffer inputBuffer;
+
+ void acceptNewOverlapLength(int newOverlapLength);
+
+ virtual void clearCrossCorrState();
+ void calculateOverlapLength(int overlapMs);
+
+ virtual double calcCrossCorr(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm);
+ virtual double calcCrossCorrAccumulate(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm);
+
+ virtual int seekBestOverlapPositionFull(const SAMPLETYPE *refPos);
+ virtual int seekBestOverlapPositionQuick(const SAMPLETYPE *refPos);
+ virtual int seekBestOverlapPosition(const SAMPLETYPE *refPos);
+
+ virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const;
+ virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const;
+ virtual void overlapMulti(SAMPLETYPE *output, const SAMPLETYPE *input) const;
+
+ void clearMidBuffer();
+ void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const;
+
+ void calcSeqParameters();
+ void adaptNormalizer();
+
+ /// Changes the tempo of the given sound samples.
+ /// Returns amount of samples returned in the "output" buffer.
+ /// The maximum amount of samples that can be returned at a time is set by
+ /// the 'set_returnBuffer_size' function.
+ void processSamples();
+
+public:
+ TDStretch();
+ virtual ~TDStretch() override;
+
+ /// Operator 'new' is overloaded so that it automatically creates a suitable instance
+ /// depending on if we've a MMX/SSE/etc-capable CPU available or not.
+ static void *operator new(size_t s);
+
+ /// Use this function instead of "new" operator to create a new instance of this class.
+ /// This function automatically chooses a correct feature set depending on if the CPU
+ /// supports MMX/SSE/etc extensions.
+ static TDStretch *newInstance();
+
+ /// Returns the output buffer object
+ FIFOSamplePipe *getOutput() { return &outputBuffer; };
+
+ /// Returns the input buffer object
+ FIFOSamplePipe *getInput() { return &inputBuffer; };
+
+ /// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower
+ /// tempo, larger faster tempo.
+ void setTempo(double newTempo);
+
+ /// Returns nonzero if there aren't any samples available for outputting.
+ virtual void clear() override;
+
+ /// Clears the input buffer
+ void clearInput();
+
+ /// Sets the number of channels, 1 = mono, 2 = stereo
+ void setChannels(int numChannels);
+
+ /// Enables/disables the quick position seeking algorithm. Zero to disable,
+ /// nonzero to enable
+ void enableQuickSeek(bool enable);
+
+ /// Returns nonzero if the quick seeking algorithm is enabled.
+ bool isQuickSeekEnabled() const;
+
+ /// Sets routine control parameters. These control are certain time constants
+ /// defining how the sound is stretched to the desired duration.
+ //
+ /// 'sampleRate' = sample rate of the sound
+ /// 'sequenceMS' = one processing sequence length in milliseconds
+ /// 'seekwindowMS' = seeking window length for scanning the best overlapping
+ /// position
+ /// 'overlapMS' = overlapping length
+ void setParameters(int sampleRate, ///< Samplerate of sound being processed (Hz)
+ int sequenceMS = -1, ///< Single processing sequence length (ms)
+ int seekwindowMS = -1, ///< Offset seeking window length (ms)
+ int overlapMS = -1 ///< Sequence overlapping length (ms)
+ );
+
+ /// Get routine control parameters, see setParameters() function.
+ /// Any of the parameters to this function can be nullptr, in such case corresponding parameter
+ /// value isn't returned.
+ void getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const;
+
+ /// Adds 'numsamples' pcs of samples from the 'samples' memory position into
+ /// the input of the object.
+ virtual void putSamples(
+ const SAMPLETYPE *samples, ///< Input sample data
+ uint numSamples ///< Number of samples in 'samples' so that one sample
+ ///< contains both channels if stereo
+ ) override;
+
+ /// return nominal input sample requirement for triggering a processing batch
+ int getInputSampleReq() const
+ {
+ return (int)(nominalSkip + 0.5);
+ }
+
+ /// return nominal output sample amount when running a processing batch
+ int getOutputBatchSize() const
+ {
+ return seekWindowLength - overlapLength;
+ }
+
+ /// return approximate initial input-output latency
+ int getLatency() const
+ {
+ return sampleReq;
+ }
+};
+
+
+// Implementation-specific class declarations:
+
+#ifdef SOUNDTOUCH_ALLOW_MMX
+ /// Class that implements MMX optimized routines for 16bit integer samples type.
+ class TDStretchMMX : public TDStretch
+ {
+ protected:
+ double calcCrossCorr(const short *mixingPos, const short *compare, double &norm) override;
+ double calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm) override;
+ virtual void overlapStereo(short *output, const short *input) const override;
+ virtual void clearCrossCorrState() override;
+ };
+#endif /// SOUNDTOUCH_ALLOW_MMX
+
+
+#ifdef SOUNDTOUCH_ALLOW_SSE
+ /// Class that implements SSE optimized routines for floating point samples type.
+ class TDStretchSSE : public TDStretch
+ {
+ protected:
+ double calcCrossCorr(const float *mixingPos, const float *compare, double &norm) override;
+ double calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm) override;
+ };
+
+#endif /// SOUNDTOUCH_ALLOW_SSE
+
+}
+#endif /// TDStretch_H
diff --git a/third_party/soundtouch/source/SoundTouch/cpu_detect.h b/third_party/soundtouch/source/SoundTouch/cpu_detect.h
new file mode 100644
index 00000000..0794c448
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/cpu_detect.h
@@ -0,0 +1,55 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// A header file for detecting the Intel MMX instructions set extension.
+///
+/// Please see 'mmx_win.cpp', 'mmx_cpp.cpp' and 'mmx_non_x86.cpp' for the
+/// routine implementations for x86 Windows, x86 gnu version and non-x86
+/// platforms, respectively.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#ifndef _CPU_DETECT_H_
+#define _CPU_DETECT_H_
+
+#include "STTypes.h"
+
+#define SUPPORT_MMX 0x0001
+#define SUPPORT_3DNOW 0x0002
+#define SUPPORT_ALTIVEC 0x0004
+#define SUPPORT_SSE 0x0008
+#define SUPPORT_SSE2 0x0010
+
+/// Checks which instruction set extensions are supported by the CPU.
+///
+/// \return A bitmask of supported extensions, see SUPPORT_... defines.
+uint detectCPUextensions(void);
+
+/// Disables given set of instruction extensions. See SUPPORT_... defines.
+void disableExtensions(uint wDisableMask);
+
+#endif // _CPU_DETECT_H_
diff --git a/third_party/soundtouch/source/SoundTouch/cpu_detect_x86.cpp b/third_party/soundtouch/source/SoundTouch/cpu_detect_x86.cpp
new file mode 100644
index 00000000..18d88e9e
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/cpu_detect_x86.cpp
@@ -0,0 +1,130 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// Generic version of the x86 CPU extension detection routine.
+///
+/// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp'
+/// for the Microsoft compiler version.
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include "cpu_detect.h"
+#include "STTypes.h"
+
+
+#if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
+
+ #if defined(__GNUC__) && defined(__i386__)
+ // gcc
+ #include "cpuid.h"
+ #elif defined(_M_IX86)
+ // windows non-gcc
+ #include <intrin.h>
+ #endif
+
+ #define bit_MMX (1 << 23)
+ #define bit_SSE (1 << 25)
+ #define bit_SSE2 (1 << 26)
+#endif
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// processor instructions extension detection routines
+//
+//////////////////////////////////////////////////////////////////////////////
+
+// Flag variable indicating whick ISA extensions are disabled (for debugging)
+static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions
+
+// Disables given set of instruction extensions. See SUPPORT_... defines.
+void disableExtensions(uint dwDisableMask)
+{
+ _dwDisabledISA = dwDisableMask;
+}
+
+
+/// Checks which instruction set extensions are supported by the CPU.
+uint detectCPUextensions(void)
+{
+/// If building for a 64bit system (no Itanium) and the user wants optimizations.
+/// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19.
+/// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
+#if ((defined(__GNUC__) && defined(__x86_64__)) \
+ || defined(_M_X64)) \
+ && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
+ return 0x19 & ~_dwDisabledISA;
+
+/// If building for a 32bit system and the user wants optimizations.
+/// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
+#elif ((defined(__GNUC__) && defined(__i386__)) \
+ || defined(_M_IX86)) \
+ && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
+
+ if (_dwDisabledISA == 0xffffffff) return 0;
+
+ uint res = 0;
+
+#if defined(__GNUC__)
+ // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support.
+ uint eax, ebx, ecx, edx; // unsigned int is the standard type. uint is defined by the compiler and not guaranteed to be portable.
+
+ // Check if no cpuid support.
+ if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions.
+
+ if (edx & bit_MMX) res = res | SUPPORT_MMX;
+ if (edx & bit_SSE) res = res | SUPPORT_SSE;
+ if (edx & bit_SSE2) res = res | SUPPORT_SSE2;
+
+#else
+ // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required
+ // for __cpuid intrinsic support.
+ int reg[4] = {-1};
+
+ // Check if no cpuid support.
+ __cpuid(reg,0);
+ if ((unsigned int)reg[0] == 0) return 0; // always disable extensions.
+
+ __cpuid(reg,1);
+ if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX;
+ if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE;
+ if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2;
+
+#endif
+
+ return res & ~_dwDisabledISA;
+
+#else
+
+/// One of these is true:
+/// 1) We don't want optimizations.
+/// 2) Using an unsupported compiler.
+/// 3) Running on a non-x86 platform.
+ return 0;
+
+#endif
+}
diff --git a/third_party/soundtouch/source/SoundTouch/mmx_optimized.cpp b/third_party/soundtouch/source/SoundTouch/mmx_optimized.cpp
new file mode 100644
index 00000000..2fdeeac9
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/mmx_optimized.cpp
@@ -0,0 +1,396 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// MMX optimized routines. All MMX optimized functions have been gathered into
+/// this single source code file, regardless to their class or original source
+/// code file, in order to ease porting the library to other compiler and
+/// processor platforms.
+///
+/// The MMX-optimizations are programmed using MMX compiler intrinsics that
+/// are supported both by Microsoft Visual C++ and GCC compilers, so this file
+/// should compile with both toolsets.
+///
+/// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++
+/// 6.0 processor pack" update to support compiler intrinsic syntax. The update
+/// is available for download at Microsoft Developers Network, see here:
+/// http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include "STTypes.h"
+
+#ifdef SOUNDTOUCH_ALLOW_MMX
+// MMX routines available only with integer sample type
+
+using namespace soundtouch;
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// implementation of MMX optimized functions of class 'TDStretchMMX'
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "TDStretch.h"
+#include <mmintrin.h>
+#include <limits.h>
+#include <math.h>
+
+
+// Calculates cross correlation of two buffers
+double TDStretchMMX::calcCrossCorr(const short *pV1, const short *pV2, double &dnorm)
+{
+ const __m64 *pVec1, *pVec2;
+ __m64 shifter;
+ __m64 accu, normaccu;
+ long corr, norm;
+ int i;
+
+ pVec1 = (__m64*)pV1;
+ pVec2 = (__m64*)pV2;
+
+ shifter = _m_from_int(overlapDividerBitsNorm);
+ normaccu = accu = _mm_setzero_si64();
+
+ // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples
+ // during each round for improved CPU-level parallellization.
+ for (i = 0; i < channels * overlapLength / 16; i ++)
+ {
+ __m64 temp, temp2;
+
+ // dictionary of instructions:
+ // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
+ // _mm_add_pi32 : 2*32bit add
+ // _m_psrad : 32bit right-shift
+
+ temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter));
+ temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec1[0]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec1[1]), shifter));
+ accu = _mm_add_pi32(accu, temp);
+ normaccu = _mm_add_pi32(normaccu, temp2);
+
+ temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter));
+ temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec1[2]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec1[3]), shifter));
+ accu = _mm_add_pi32(accu, temp);
+ normaccu = _mm_add_pi32(normaccu, temp2);
+
+ pVec1 += 4;
+ pVec2 += 4;
+ }
+
+ // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
+ // and finally store the result into the variable "corr"
+
+ accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
+ corr = _m_to_int(accu);
+
+ normaccu = _mm_add_pi32(normaccu, _mm_srli_si64(normaccu, 32));
+ norm = _m_to_int(normaccu);
+
+ // Clear MMS state
+ _m_empty();
+
+ if (norm > (long)maxnorm)
+ {
+ // modify 'maxnorm' inside critical section to avoid multi-access conflict if in OpenMP mode
+ #pragma omp critical
+ if (norm > (long)maxnorm)
+ {
+ maxnorm = norm;
+ }
+ }
+
+ // Normalize result by dividing by sqrt(norm) - this step is easiest
+ // done using floating point operation
+ dnorm = (double)norm;
+
+ return (double)corr / sqrt(dnorm < 1e-9 ? 1.0 : dnorm);
+ // Note: Warning about the missing EMMS instruction is harmless
+ // as it'll be called elsewhere.
+}
+
+
+/// Update cross-correlation by accumulating "norm" coefficient by previously calculated value
+double TDStretchMMX::calcCrossCorrAccumulate(const short *pV1, const short *pV2, double &dnorm)
+{
+ const __m64 *pVec1, *pVec2;
+ __m64 shifter;
+ __m64 accu;
+ long corr, lnorm;
+ int i;
+
+ // cancel first normalizer tap from previous round
+ lnorm = 0;
+ for (i = 1; i <= channels; i ++)
+ {
+ lnorm -= (pV1[-i] * pV1[-i]) >> overlapDividerBitsNorm;
+ }
+
+ pVec1 = (__m64*)pV1;
+ pVec2 = (__m64*)pV2;
+
+ shifter = _m_from_int(overlapDividerBitsNorm);
+ accu = _mm_setzero_si64();
+
+ // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples
+ // during each round for improved CPU-level parallellization.
+ for (i = 0; i < channels * overlapLength / 16; i ++)
+ {
+ __m64 temp;
+
+ // dictionary of instructions:
+ // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3]
+ // _mm_add_pi32 : 2*32bit add
+ // _m_psrad : 32bit right-shift
+
+ temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter));
+ accu = _mm_add_pi32(accu, temp);
+
+ temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter),
+ _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter));
+ accu = _mm_add_pi32(accu, temp);
+
+ pVec1 += 4;
+ pVec2 += 4;
+ }
+
+ // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1
+ // and finally store the result into the variable "corr"
+
+ accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32));
+ corr = _m_to_int(accu);
+
+ // Clear MMS state
+ _m_empty();
+
+ // update normalizer with last samples of this round
+ pV1 = (short *)pVec1;
+ for (int j = 1; j <= channels; j ++)
+ {
+ lnorm += (pV1[-j] * pV1[-j]) >> overlapDividerBitsNorm;
+ }
+ dnorm += (double)lnorm;
+
+ if (lnorm > (long)maxnorm)
+ {
+ maxnorm = lnorm;
+ }
+
+ // Normalize result by dividing by sqrt(norm) - this step is easiest
+ // done using floating point operation
+ return (double)corr / sqrt((dnorm < 1e-9) ? 1.0 : dnorm);
+}
+
+
+void TDStretchMMX::clearCrossCorrState()
+{
+ // Clear MMS state
+ _m_empty();
+ //_asm EMMS;
+}
+
+
+// MMX-optimized version of the function overlapStereo
+void TDStretchMMX::overlapStereo(short *output, const short *input) const
+{
+ const __m64 *pVinput, *pVMidBuf;
+ __m64 *pVdest;
+ __m64 mix1, mix2, adder, shifter;
+ int i;
+
+ pVinput = (const __m64*)input;
+ pVMidBuf = (const __m64*)pMidBuffer;
+ pVdest = (__m64*)output;
+
+ // mix1 = mixer values for 1st stereo sample
+ // mix1 = mixer values for 2nd stereo sample
+ // adder = adder for updating mixer values after each round
+
+ mix1 = _mm_set_pi16(0, overlapLength, 0, overlapLength);
+ adder = _mm_set_pi16(1, -1, 1, -1);
+ mix2 = _mm_add_pi16(mix1, adder);
+ adder = _mm_add_pi16(adder, adder);
+
+ // Overlaplength-division by shifter. "+1" is to account for "-1" deduced in
+ // overlapDividerBits calculation earlier.
+ shifter = _m_from_int(overlapDividerBitsPure + 1);
+
+ for (i = 0; i < overlapLength / 4; i ++)
+ {
+ __m64 temp1, temp2;
+
+ // load & shuffle data so that input & mixbuffer data samples are paired
+ temp1 = _mm_unpacklo_pi16(pVMidBuf[0], pVinput[0]); // = i0l m0l i0r m0r
+ temp2 = _mm_unpackhi_pi16(pVMidBuf[0], pVinput[0]); // = i1l m1l i1r m1r
+
+ // temp = (temp .* mix) >> shifter
+ temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
+ temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
+ pVdest[0] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
+
+ // update mix += adder
+ mix1 = _mm_add_pi16(mix1, adder);
+ mix2 = _mm_add_pi16(mix2, adder);
+
+ // --- second round begins here ---
+
+ // load & shuffle data so that input & mixbuffer data samples are paired
+ temp1 = _mm_unpacklo_pi16(pVMidBuf[1], pVinput[1]); // = i2l m2l i2r m2r
+ temp2 = _mm_unpackhi_pi16(pVMidBuf[1], pVinput[1]); // = i3l m3l i3r m3r
+
+ // temp = (temp .* mix) >> shifter
+ temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter);
+ temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter);
+ pVdest[1] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit
+
+ // update mix += adder
+ mix1 = _mm_add_pi16(mix1, adder);
+ mix2 = _mm_add_pi16(mix2, adder);
+
+ pVinput += 2;
+ pVMidBuf += 2;
+ pVdest += 2;
+ }
+
+ _m_empty(); // clear MMS state
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// implementation of MMX optimized functions of class 'FIRFilter'
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "FIRFilter.h"
+
+
+FIRFilterMMX::FIRFilterMMX() : FIRFilter()
+{
+ filterCoeffsAlign = nullptr;
+ filterCoeffsUnalign = nullptr;
+}
+
+
+FIRFilterMMX::~FIRFilterMMX()
+{
+ delete[] filterCoeffsUnalign;
+}
+
+
+// (overloaded) Calculates filter coefficients for MMX routine
+void FIRFilterMMX::setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor)
+{
+ uint i;
+ FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
+
+ // Ensure that filter coeffs array is aligned to 16-byte boundary
+ delete[] filterCoeffsUnalign;
+ filterCoeffsUnalign = new short[2 * newLength + 8];
+ filterCoeffsAlign = (short *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign);
+
+ // rearrange the filter coefficients for mmx routines
+ for (i = 0;i < length; i += 4)
+ {
+ filterCoeffsAlign[2 * i + 0] = coeffs[i + 0];
+ filterCoeffsAlign[2 * i + 1] = coeffs[i + 2];
+ filterCoeffsAlign[2 * i + 2] = coeffs[i + 0];
+ filterCoeffsAlign[2 * i + 3] = coeffs[i + 2];
+
+ filterCoeffsAlign[2 * i + 4] = coeffs[i + 1];
+ filterCoeffsAlign[2 * i + 5] = coeffs[i + 3];
+ filterCoeffsAlign[2 * i + 6] = coeffs[i + 1];
+ filterCoeffsAlign[2 * i + 7] = coeffs[i + 3];
+ }
+}
+
+
+// mmx-optimized version of the filter routine for stereo sound
+uint FIRFilterMMX::evaluateFilterStereo(short *dest, const short *src, uint numSamples) const
+{
+ // Create stack copies of the needed member variables for asm routines :
+ uint i, j;
+ __m64 *pVdest = (__m64*)dest;
+
+ if (length < 2) return 0;
+
+ for (i = 0; i < (numSamples - length) / 2; i ++)
+ {
+ __m64 accu1;
+ __m64 accu2;
+ const __m64 *pVsrc = (const __m64*)src;
+ const __m64 *pVfilter = (const __m64*)filterCoeffsAlign;
+
+ accu1 = accu2 = _mm_setzero_si64();
+ for (j = 0; j < lengthDiv8 * 2; j ++)
+ {
+ __m64 temp1, temp2;
+
+ temp1 = _mm_unpacklo_pi16(pVsrc[0], pVsrc[1]); // = l2 l0 r2 r0
+ temp2 = _mm_unpackhi_pi16(pVsrc[0], pVsrc[1]); // = l3 l1 r3 r1
+
+ accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp1, pVfilter[0])); // += l2*f2+l0*f0 r2*f2+r0*f0
+ accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp2, pVfilter[1])); // += l3*f3+l1*f1 r3*f3+r1*f1
+
+ temp1 = _mm_unpacklo_pi16(pVsrc[1], pVsrc[2]); // = l4 l2 r4 r2
+
+ accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp2, pVfilter[0])); // += l3*f2+l1*f0 r3*f2+r1*f0
+ accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp1, pVfilter[1])); // += l4*f3+l2*f1 r4*f3+r2*f1
+
+ // accu1 += l2*f2+l0*f0 r2*f2+r0*f0
+ // += l3*f3+l1*f1 r3*f3+r1*f1
+
+ // accu2 += l3*f2+l1*f0 r3*f2+r1*f0
+ // l4*f3+l2*f1 r4*f3+r2*f1
+
+ pVfilter += 2;
+ pVsrc += 2;
+ }
+ // accu >>= resultDivFactor
+ accu1 = _mm_srai_pi32(accu1, resultDivFactor);
+ accu2 = _mm_srai_pi32(accu2, resultDivFactor);
+
+ // pack 2*2*32bits => 4*16 bits
+ pVdest[0] = _mm_packs_pi32(accu1, accu2);
+ src += 4;
+ pVdest ++;
+ }
+
+ _m_empty(); // clear emms state
+
+ return (numSamples & 0xfffffffe) - length;
+}
+
+#else
+
+// workaround to not complain about empty module
+bool _dontcomplain_mmx_empty;
+
+#endif // SOUNDTOUCH_ALLOW_MMX
diff --git a/third_party/soundtouch/source/SoundTouch/sse_optimized.cpp b/third_party/soundtouch/source/SoundTouch/sse_optimized.cpp
new file mode 100644
index 00000000..3cd0b344
--- /dev/null
+++ b/third_party/soundtouch/source/SoundTouch/sse_optimized.cpp
@@ -0,0 +1,362 @@
+////////////////////////////////////////////////////////////////////////////////
+///
+/// SSE optimized routines for Pentium-III, Athlon-XP and later CPUs. All SSE
+/// optimized functions have been gathered into this single source
+/// code file, regardless to their class or original source code file, in order
+/// to ease porting the library to other compiler and processor platforms.
+///
+/// The SSE-optimizations are programmed using SSE compiler intrinsics that
+/// are supported both by Microsoft Visual C++ and GCC compilers, so this file
+/// should compile with both toolsets.
+///
+/// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++
+/// 6.0 processor pack" update to support SSE instruction set. The update is
+/// available for download at Microsoft Developers Network, see here:
+/// http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx
+///
+/// If the above URL is expired or removed, go to "http://msdn.microsoft.com" and
+/// perform a search with keywords "processor pack".
+///
+/// Author : Copyright (c) Olli Parviainen
+/// Author e-mail : oparviai 'at' iki.fi
+/// SoundTouch WWW: http://www.surina.net/soundtouch
+///
+////////////////////////////////////////////////////////////////////////////////
+//
+// License :
+//
+// SoundTouch audio processing library
+// Copyright (c) Olli Parviainen
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include "cpu_detect.h"
+#include "STTypes.h"
+
+using namespace soundtouch;
+
+#ifdef SOUNDTOUCH_ALLOW_SSE
+
+// SSE routines available only with float sample type
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// implementation of SSE optimized functions of class 'TDStretchSSE'
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "TDStretch.h"
+#include <xmmintrin.h>
+#include <math.h>
+
+// Calculates cross correlation of two buffers
+double TDStretchSSE::calcCrossCorr(const float *pV1, const float *pV2, double &anorm)
+{
+ int i;
+ const float *pVec1;
+ const __m128 *pVec2;
+ __m128 vSum, vNorm;
+
+ // Note. It means a major slow-down if the routine needs to tolerate
+ // unaligned __m128 memory accesses. It's way faster if we can skip
+ // unaligned slots and use _mm_load_ps instruction instead of _mm_loadu_ps.
+ // This can mean up to ~ 10-fold difference (incl. part of which is
+ // due to skipping every second round for stereo sound though).
+ //
+ // Compile-time define SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION is provided
+ // for choosing if this little cheating is allowed.
+
+#ifdef ST_SIMD_AVOID_UNALIGNED
+ // Little cheating allowed, return valid correlation only for
+ // aligned locations, meaning every second round for stereo sound.
+
+ #define _MM_LOAD _mm_load_ps
+
+ if (((ulongptr)pV1) & 15) return -1e50; // skip unaligned locations
+
+#else
+ // No cheating allowed, use unaligned load & take the resulting
+ // performance hit.
+ #define _MM_LOAD _mm_loadu_ps
+#endif
+
+ // ensure overlapLength is divisible by 8
+ assert((overlapLength % 8) == 0);
+
+ // Calculates the cross-correlation value between 'pV1' and 'pV2' vectors
+ // Note: pV2 _must_ be aligned to 16-bit boundary, pV1 need not.
+ pVec1 = (const float*)pV1;
+ pVec2 = (const __m128*)pV2;
+ vSum = vNorm = _mm_setzero_ps();
+
+ // Unroll the loop by factor of 4 * 4 operations. Use same routine for
+ // stereo & mono, for mono it just means twice the amount of unrolling.
+ for (i = 0; i < channels * overlapLength / 16; i ++)
+ {
+ __m128 vTemp;
+ // vSum += pV1[0..3] * pV2[0..3]
+ vTemp = _MM_LOAD(pVec1);
+ vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp ,pVec2[0]));
+ vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp));
+
+ // vSum += pV1[4..7] * pV2[4..7]
+ vTemp = _MM_LOAD(pVec1 + 4);
+ vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[1]));
+ vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp));
+
+ // vSum += pV1[8..11] * pV2[8..11]
+ vTemp = _MM_LOAD(pVec1 + 8);
+ vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[2]));
+ vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp));
+
+ // vSum += pV1[12..15] * pV2[12..15]
+ vTemp = _MM_LOAD(pVec1 + 12);
+ vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[3]));
+ vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp));
+
+ pVec1 += 16;
+ pVec2 += 4;
+ }
+
+ // return value = vSum[0] + vSum[1] + vSum[2] + vSum[3]
+ float *pvNorm = (float*)&vNorm;
+ float norm = (pvNorm[0] + pvNorm[1] + pvNorm[2] + pvNorm[3]);
+ anorm = norm;
+
+ float *pvSum = (float*)&vSum;
+ return (double)(pvSum[0] + pvSum[1] + pvSum[2] + pvSum[3]) / sqrt(norm < 1e-9 ? 1.0 : norm);
+
+ /* This is approximately corresponding routine in C-language yet without normalization:
+ double corr, norm;
+ uint i;
+
+ // Calculates the cross-correlation value between 'pV1' and 'pV2' vectors
+ corr = norm = 0.0;
+ for (i = 0; i < channels * overlapLength / 16; i ++)
+ {
+ corr += pV1[0] * pV2[0] +
+ pV1[1] * pV2[1] +
+ pV1[2] * pV2[2] +
+ pV1[3] * pV2[3] +
+ pV1[4] * pV2[4] +
+ pV1[5] * pV2[5] +
+ pV1[6] * pV2[6] +
+ pV1[7] * pV2[7] +
+ pV1[8] * pV2[8] +
+ pV1[9] * pV2[9] +
+ pV1[10] * pV2[10] +
+ pV1[11] * pV2[11] +
+ pV1[12] * pV2[12] +
+ pV1[13] * pV2[13] +
+ pV1[14] * pV2[14] +
+ pV1[15] * pV2[15];
+
+ for (j = 0; j < 15; j ++) norm += pV1[j] * pV1[j];
+
+ pV1 += 16;
+ pV2 += 16;
+ }
+ return corr / sqrt(norm);
+ */
+}
+
+
+
+double TDStretchSSE::calcCrossCorrAccumulate(const float *pV1, const float *pV2, double &norm)
+{
+ // call usual calcCrossCorr function because SSE does not show big benefit of
+ // accumulating "norm" value, and also the "norm" rolling algorithm would get
+ // complicated due to SSE-specific alignment-vs-nonexact correlation rules.
+ return calcCrossCorr(pV1, pV2, norm);
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// implementation of SSE optimized functions of class 'FIRFilter'
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "FIRFilter.h"
+
+FIRFilterSSE::FIRFilterSSE() : FIRFilter()
+{
+ filterCoeffsAlign = nullptr;
+ filterCoeffsUnalign = nullptr;
+}
+
+
+FIRFilterSSE::~FIRFilterSSE()
+{
+ delete[] filterCoeffsUnalign;
+ filterCoeffsAlign = nullptr;
+ filterCoeffsUnalign = nullptr;
+}
+
+
+// (overloaded) Calculates filter coefficients for SSE routine
+void FIRFilterSSE::setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor)
+{
+ FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor);
+
+ // Scale the filter coefficients so that it won't be necessary to scale the filtering result
+ // also rearrange coefficients suitably for SSE
+ // Ensure that filter coeffs array is aligned to 16-byte boundary
+ delete[] filterCoeffsUnalign;
+ filterCoeffsUnalign = new float[2 * newLength + 4];
+ filterCoeffsAlign = (float *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign);
+
+ const float scale = static_cast<float>(::pow(0.5, (int)resultDivFactor));
+
+ // rearrange the filter coefficients for sse routines
+ for (auto i = 0U; i < newLength; i ++)
+ {
+ filterCoeffsAlign[2 * i + 0] =
+ filterCoeffsAlign[2 * i + 1] = coeffs[i] * scale;
+ }
+}
+
+
+
+// SSE-optimized version of the filter routine for stereo sound
+uint FIRFilterSSE::evaluateFilterStereo(float *dest, const float *source, uint numSamples) const
+{
+ int count = (int)((numSamples - length) & (uint)-2);
+ int j;
+
+ assert(count % 2 == 0);
+
+ if (count < 2) return 0;
+
+ assert(source != nullptr);
+ assert(dest != nullptr);
+ assert((length % 8) == 0);
+ assert(filterCoeffsAlign != nullptr);
+ assert(((ulongptr)filterCoeffsAlign) % 16 == 0);
+
+ // filter is evaluated for two stereo samples with each iteration, thus use of 'j += 2'
+ #pragma omp parallel for
+ for (j = 0; j < count; j += 2)
+ {
+ const float *pSrc;
+ float *pDest;
+ const __m128 *pFil;
+ __m128 sum1, sum2;
+ uint i;
+
+ pSrc = (const float*)source + j * 2; // source audio data
+ pDest = dest + j * 2; // destination audio data
+ pFil = (const __m128*)filterCoeffsAlign; // filter coefficients. NOTE: Assumes coefficients
+ // are aligned to 16-byte boundary
+ sum1 = sum2 = _mm_setzero_ps();
+
+ for (i = 0; i < length / 8; i ++)
+ {
+ // Unroll loop for efficiency & calculate filter for 2*2 stereo samples
+ // at each pass
+
+ // sum1 is accu for 2*2 filtered stereo sound data at the primary sound data offset
+ // sum2 is accu for 2*2 filtered stereo sound data for the next sound sample offset.
+
+ sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc) , pFil[0]));
+ sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 2), pFil[0]));
+
+ sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 4), pFil[1]));
+ sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 6), pFil[1]));
+
+ sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 8) , pFil[2]));
+ sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 10), pFil[2]));
+
+ sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 12), pFil[3]));
+ sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 14), pFil[3]));
+
+ pSrc += 16;
+ pFil += 4;
+ }
+
+ // Now sum1 and sum2 both have a filtered 2-channel sample each, but we still need
+ // to sum the two hi- and lo-floats of these registers together.
+
+ // post-shuffle & add the filtered values and store to dest.
+ _mm_storeu_ps(pDest, _mm_add_ps(
+ _mm_shuffle_ps(sum1, sum2, _MM_SHUFFLE(1,0,3,2)), // s2_1 s2_0 s1_3 s1_2
+ _mm_shuffle_ps(sum1, sum2, _MM_SHUFFLE(3,2,1,0)) // s2_3 s2_2 s1_1 s1_0
+ ));
+ }
+
+ // Ideas for further improvement:
+ // 1. If it could be guaranteed that 'source' were always aligned to 16-byte
+ // boundary, a faster aligned '_mm_load_ps' instruction could be used.
+ // 2. If it could be guaranteed that 'dest' were always aligned to 16-byte
+ // boundary, a faster '_mm_store_ps' instruction could be used.
+
+ return (uint)count;
+
+ /* original routine in C-language. please notice the C-version has differently
+ organized coefficients though.
+ double suml1, suml2;
+ double sumr1, sumr2;
+ uint i, j;
+
+ for (j = 0; j < count; j += 2)
+ {
+ const float *ptr;
+ const float *pFil;
+
+ suml1 = sumr1 = 0.0;
+ suml2 = sumr2 = 0.0;
+ ptr = src;
+ pFil = filterCoeffs;
+ for (i = 0; i < lengthLocal; i ++)
+ {
+ // unroll loop for efficiency.
+
+ suml1 += ptr[0] * pFil[0] +
+ ptr[2] * pFil[2] +
+ ptr[4] * pFil[4] +
+ ptr[6] * pFil[6];
+
+ sumr1 += ptr[1] * pFil[1] +
+ ptr[3] * pFil[3] +
+ ptr[5] * pFil[5] +
+ ptr[7] * pFil[7];
+
+ suml2 += ptr[8] * pFil[0] +
+ ptr[10] * pFil[2] +
+ ptr[12] * pFil[4] +
+ ptr[14] * pFil[6];
+
+ sumr2 += ptr[9] * pFil[1] +
+ ptr[11] * pFil[3] +
+ ptr[13] * pFil[5] +
+ ptr[15] * pFil[7];
+
+ ptr += 16;
+ pFil += 8;
+ }
+ dest[0] = (float)suml1;
+ dest[1] = (float)sumr1;
+ dest[2] = (float)suml2;
+ dest[3] = (float)sumr2;
+
+ src += 4;
+ dest += 4;
+ }
+ */
+}
+
+#endif // SOUNDTOUCH_ALLOW_SSE