Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
333201e1ec |
+7
-2
@@ -8,9 +8,14 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Version
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added (experimental — `audio/speaker-rate-trim` branch)
|
||||
### Changed
|
||||
|
||||
- **`SpkTrim` setting — fine speaker clock-drift trim to chase the periodic crackle (#7).** The DS5's DAC and the USB host's audio clock are independent ~48 kHz crystals; the residual ppm mismatch slowly drifts the controller's audio buffer, which underruns and crackles. The speaker path now delivers `48000 + trim` samples/s (via a zero-order-hold duplicate/drop accumulator) instead of exactly 48000, to null that drift. New `speaker_rate_trim` config field (stored 0–200 = **−100…+100 Hz**, default **0 Hz** = exact no-op), swept on the OLED **Settings** screen with the D-pad ▶◀ at 1 Hz/step. **Experimental / diagnostic:** if one value silences the crackle and *holds*, the drift was a static offset and this is the fix; if it nulls then creeps back, that proves the drift is dynamic and needs adaptive resampling. Tune with `scripts/sine_ch12.py` playing a steady tone and time the interval between crackles.
|
||||
- **`polling_rate_mode` now defaults to realtime / 1000 Hz** (was 250 Hz) so a fresh flash matches a wired DualSense's 1 ms input latency instead of polling 4× slower. Mode 2 also bypasses the report-throttling used by the 250/500 Hz modes, forwarding every BT input report immediately. Existing saved configs keep their value; this only affects fresh flashes / Reset-to-defaults. (Out-of-box "acts like USB" pass.)
|
||||
- **`bt_mic_enable` now defaults to Off** (was On) — the DualSense BT microphone path has a confirmed playback-rate bug (issue #10: recordings come out ~2× too fast / half-duration). Until that's root-caused on hardware, the mic is opt-in: fresh flashes / Reset-to-defaults boot with it off, and it can be turned on from the OLED **Settings** screen (`BT Mic`) or the web config. Existing saved configs keep their current value. (Web config default should be flipped to match in `DS5Dongle-OLED-Config-Web`.)
|
||||
|
||||
### Known issues
|
||||
|
||||
- **BT microphone records ~2× too fast (#10).** The mic playout stage is wall-clock paced at 10 ms / 480 samples into a 48 kHz endpoint, which should be real-time, so the 2× points at the on-wire frame size/rate differing from the assumed 480 samples. Needs the OLED **Diagnostics** `Mic dec=` (samples per `opus_decode`) and `Mic in/s` (arrival rate) read on hardware to make the fix deterministic — `Mic dec=240` would confirm a half-rate stream. Mic is opt-in (off by default) in the meantime.
|
||||
|
||||
---
|
||||
|
||||
|
||||
-106
@@ -1,106 +0,0 @@
|
||||
# HANDOFF — audio work in progress
|
||||
|
||||
_Last updated: 2026-06-04 (written on the laptop; continuing on the main PC)_
|
||||
|
||||
This is a working handoff so the next session — on the main PC, or a fresh Claude Code
|
||||
session — can pick up exactly where we left off. **All the real work is committed and pushed
|
||||
to `origin`; nothing is stuck on the laptop.** This file lives on the `audio/speaker-rate-trim`
|
||||
branch, so after `git checkout audio/speaker-rate-trim` it's right here.
|
||||
|
||||
---
|
||||
|
||||
## TL;DR
|
||||
|
||||
- The frustration that kicked this off: **audio (speaker/mic/HD haptics) is the priority**, not OLED features. Goal: make the dongle behave **as close to a wired-USB DualSense as possible**, with flaky extras opt-in.
|
||||
- We confirmed the open GitHub issues are real, synced the tree up to **v0.6.11** (it was stale at 0.6.4), and split the work onto two feature branches.
|
||||
- **Main thing to do next:** build `audio/speaker-rate-trim` on the main PC, flash it, and run the **`SpkTrim` sweep test** (below) to find out whether the speaker crackle is a fixed clock offset (curable with the new knob) or live drift (needs adaptive resampling).
|
||||
|
||||
---
|
||||
|
||||
## Branch map
|
||||
|
||||
| Branch | Commit | State | Needs |
|
||||
|---|---|---|---|
|
||||
| `master` | `878a742` | untouched, = `origin/master` = **v0.6.11** | — |
|
||||
| `defaults/usb-faithful` | `333201e` | pushed | **HIL soak-test** before merging to master |
|
||||
| `audio/speaker-rate-trim` | `3b157cb` | pushed | **build + flash + sweep test** (not yet built) |
|
||||
|
||||
Both feature branches are pushed to `origin` (the fork) and tracking. `upstream` / `upstream-fork` were **not** touched.
|
||||
|
||||
---
|
||||
|
||||
## What each branch contains
|
||||
|
||||
### `defaults/usb-faithful` — out-of-box "acts like USB" defaults
|
||||
A full audit found the firmware is already ~USB-faithful for everything that matters (input is byte-for-byte passthrough; lightbar defaults to host control; etc.). Only two defaults were flipped:
|
||||
- **`polling_rate_mode` 0 → 2** (250 Hz → **1000 Hz / realtime**) — matches a wired DS5's 1 ms latency. ⚠️ Realtime also drops the report-throttle, so **this needs a 30–60 min hardware soak-test** (watch for BT drops / OLED Diag `BT31 in/s` collapse) before it merges to master. Fallback if unstable: `polling_rate_mode = 1` (500 Hz).
|
||||
- **`bt_mic_enable` 1 → 0** (mic **off** by default) — the BT mic has a known 2× playback bug (#10), so it's opt-in until fixed.
|
||||
|
||||
Both only affect fresh flashes / Reset-to-defaults; existing saved configs keep their values.
|
||||
|
||||
### `audio/speaker-rate-trim` — experimental crackle fix (the active thread)
|
||||
New **`SpkTrim`** setting that trims the speaker sample rate to null clock drift.
|
||||
|
||||
**Why:** the speaker path is 1:1 (no resampler) — it delivers samples at the **USB host's 48 kHz clock** while the DS5 consumes at **its own 48 kHz crystal**. Two independent clocks drift apart → the DS5's audio buffer slowly underruns → the periodic **crackle (#7)**. 0.6.11's "retiming" fixed the *average* rate but can't track *drift*.
|
||||
|
||||
**What it does:** delivers `48000 + trim` samples/s via a zero-order-hold duplicate/drop accumulator (`src/audio.cpp`, in the speaker accumulation loop). At ppm scale, a single duplicated/dropped sample is inaudible vs a 480-sample gap.
|
||||
|
||||
**Files changed:** `src/config.h` (field), `src/config.cpp` (clamp), `src/audio.cpp` (the trim), `src/oled.cpp` (Settings UI), `CHANGELOG.md`.
|
||||
|
||||
**Config field:** `speaker_rate_trim`, stored `0..200` = **−100…+100 Hz**, default **100 (= 0 Hz, an exact no-op)**. So a fresh flash sounds **identical to 0.6.11** until you sweep it — safe to flash.
|
||||
|
||||
**UI:** OLED **Settings** screen, item **`SpkTrim ±NHz`** (just above "Reset to defaults"), swept with the D-pad **▶ / ◀** at **1 Hz/step**. (Reset/Wipe moved to settings indices 16/17 via named constants.)
|
||||
|
||||
---
|
||||
|
||||
## Next steps on the main PC
|
||||
|
||||
```bash
|
||||
git fetch origin
|
||||
git checkout audio/speaker-rate-trim
|
||||
|
||||
# ensure TinyUSB is still pinned (CLAUDE.md — 0.18 that ships with SDK 2.2.0 won't compile):
|
||||
( cd "$PICO_SDK_PATH/lib/tinyusb" && git checkout 0.20.0 )
|
||||
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DPICO_SDK_PATH="$PICO_SDK_PATH"
|
||||
cmake --build build --target ds5-bridge # → build/ds5-bridge-oled.uf2
|
||||
# BOOTSEL the board, copy build/ds5-bridge-oled.uf2 onto the RP2350 mount
|
||||
```
|
||||
|
||||
### The sweep test (the whole point)
|
||||
1. OLED **Settings** → scroll to **`SpkTrim`** (reads `+0Hz`).
|
||||
2. Play a steady tone from the host: `python3 scripts/sine_ch12.py 600` (10 min of 440 Hz on the speaker channels).
|
||||
3. Nudge `SpkTrim` with the D-pad ▶/◀ and **time the interval between crackles**. A longer interval = closer to the DS5's true rate. Walk it toward the value where the crackle stops. (The crackle interval is itself a tachometer: a click every ~16 s ≈ ~600 ppm of clock error.)
|
||||
4. **Decision read — leave the good value running 10–15 min:**
|
||||
- **Stays clean → static offset.** The knob is the fix; we lock in a default and ship it.
|
||||
- **Creeps back → dynamic drift.** Proof we need **adaptive resampling** (continuously measure drift and vary the trim), which is the bigger but "correct" fix. The DS5 gives no buffer back-channel, so by-ear / crackle-interval is the right instrument.
|
||||
|
||||
---
|
||||
|
||||
## Other open threads (not blocking the sweep)
|
||||
|
||||
- **`defaults/usb-faithful` soak-test** — validate 1000 Hz polling stability, then merge to master (or fall back to 500 Hz).
|
||||
- **#10 mic 2× playback** — needs OLED **Diagnostics** `Mic dec=` (samples per `opus_decode`; `240` would confirm a half-rate stream) and `Mic in/s` (arrival rate) read with the mic enabled. Mic is off-by-default now, so it's no longer an out-of-box problem.
|
||||
- **#11 constant low-level haptics** — root-caused to the **#6 adaptive-trigger fix** (`71cead4`): 0.6.11 now honors host trigger-FFB, and the dongle re-asserts `state[]` continuously via the `0x36` audio frames, so a game's idle trigger effect buzzes constantly. **Not** auto-haptics (already off). Decisive read: watch the OLED Diag **`trig`** / **`host02`** counters during the buzz — climbing = host streaming FFB (add an opt-out toggle); static = stale latch (clear FFB params when allow-bit is 0, in `src/state_mgr.cpp`).
|
||||
|
||||
---
|
||||
|
||||
## Why audio is hard (context for any new session)
|
||||
|
||||
The DualSense's Bluetooth audio is **not A2DP / not normal Bluetooth audio** — Sony tunnels Opus-encoded audio inside the HID controller reports (`0x31`/`0x36`) over a proprietary, undocumented protocol with no clock negotiation. A normal PC/phone paired to a DS5 over BT gets **zero** controller audio; **only the PS5** implements it (Sony built both ends + a dedicated radio). This dongle reverse-engineers that on a CYW43 chip over emulated SPI (hence the load-bearing 320 MHz overclock) with general-purpose BTstack. So the gap from PS5-quality is **fidelity, not feasibility** — the crackle and mic-2× are timing/rate bugs, not walls.
|
||||
|
||||
---
|
||||
|
||||
## Gotchas / reminders
|
||||
|
||||
- **Push only to `origin`** — never `upstream` / `upstream-fork`.
|
||||
- **TinyUSB must be 0.20.0** or the build fails on `TUD_AUDIO_EP_SIZE`.
|
||||
- Build target is `ds5-bridge`; output is `build/ds5-bridge-oled.uf2` (custom name).
|
||||
- `SpkTrim = 0` is a byte-exact no-op, so flashing this branch can't regress audio.
|
||||
- The `0x36` packet layout and the `state[]` re-assertion are **load-bearing** (see CLAUDE.md) — don't "simplify" the audio frame.
|
||||
|
||||
---
|
||||
|
||||
## One-paste kickoff for a fresh Claude session
|
||||
|
||||
> I'm on branch `audio/speaker-rate-trim`. Read `HANDOFF.md`, the latest commit message, and the `[Unreleased]` section of `CHANGELOG.md`. We added an experimental `SpkTrim` setting to chase the speaker crackle (#7) by trimming the speaker sample rate to null clock drift between the host and the DualSense. Help me build the UF2, flash it, and run the sweep test in HANDOFF.md — find the `SpkTrim` value where the crackle stops, then check whether it holds (static offset = fix) or creeps back (dynamic drift = needs adaptive resampling).
|
||||
+2
-23
@@ -264,20 +264,12 @@ void audio_loop() {
|
||||
|
||||
static float audio_buf[480 * 2];
|
||||
static uint audio_buf_pos = 0;
|
||||
static double spk_acc = 0.0; // speaker rate-trim accumulator (persists across calls)
|
||||
// 2. 从4ch中提取ch3/ch4,转换为float输入重采样器
|
||||
WDL_ResampleSample *in_buf;
|
||||
int nframes = resampler.ResamplePrepare(frames, OUTPUT_CHANNELS, &in_buf);
|
||||
|
||||
const float audio_gain = mute[0] ? 0.0f : powf(10.0f, get_config().speaker_volume / 20.0f);
|
||||
const float haptics_gain = get_config().haptics_gain;
|
||||
|
||||
// EXPERIMENTAL speaker clock-drift trim (audio/speaker-rate-trim). Deliver
|
||||
// 48000 + trim samples/s to the DS5 instead of exactly 48000, to null the slow
|
||||
// inter-crystal drift behind the crackle (#7). trim = 0 → ratio == 1.0 → exact
|
||||
// no-op (byte-identical to pre-trim, since 48000/48000 is exact in double).
|
||||
const int spk_trim_hz = (int)get_config().speaker_rate_trim - 100; // -100..+100 Hz
|
||||
const double spk_ratio = (48000.0 + spk_trim_hz) / 48000.0;
|
||||
uint16_t spk_max = g_peak_spk;
|
||||
uint16_t hap_max = g_peak_hap;
|
||||
|
||||
@@ -316,19 +308,8 @@ void audio_loop() {
|
||||
if (b > hap_max) hap_max = b;
|
||||
}
|
||||
#if !DISABLE_SPEAKER_PROC
|
||||
{
|
||||
const float spk_l = raw[i * INPUT_CHANNELS] / 32768.0f * audio_gain;
|
||||
const float spk_r = raw[i * INPUT_CHANNELS + 1] / 32768.0f * audio_gain;
|
||||
// Rate-trim: emit `spk_ratio` output samples per input sample on average
|
||||
// (usually 1; occasionally 2 when trim>0, or 0 when trim<0). Zero-order
|
||||
// hold — at ppm-scale trims a single duplicated/dropped sample is inaudible
|
||||
// vs a full 480-sample buffer underrun.
|
||||
spk_acc += spk_ratio;
|
||||
int emit = (int)spk_acc;
|
||||
spk_acc -= emit;
|
||||
while (emit-- > 0) {
|
||||
audio_buf[audio_buf_pos++] = spk_l;
|
||||
audio_buf[audio_buf_pos++] = spk_r;
|
||||
audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS] / 32768.0f * audio_gain;
|
||||
audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS + 1] / 32768.0f * audio_gain;
|
||||
if (audio_buf_pos == 480 * 2) {
|
||||
static audio_raw_element element{};
|
||||
memcpy(element.data, audio_buf, 480 * 2 * 4);
|
||||
@@ -341,8 +322,6 @@ void audio_loop() {
|
||||
}
|
||||
audio_buf_pos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
float h_l = raw[i * INPUT_CHANNELS + 2] / 32768.0f * haptics_gain;
|
||||
float h_r = raw[i * INPUT_CHANNELS + 3] / 32768.0f * haptics_gain;
|
||||
|
||||
+12
-10
@@ -67,9 +67,11 @@ void config_valid() {
|
||||
body->disable_pico_led = 0;
|
||||
printf("[Config] disable_pico_led is invalid\n");
|
||||
}
|
||||
if (body->polling_rate_mode > 2) {
|
||||
body->polling_rate_mode = 0;
|
||||
printf("[Config] polling_rate_mode is invalid\n");
|
||||
if (body->polling_rate_mode > 2) { // 0xFF erased / out of range → default
|
||||
body->polling_rate_mode = 2; // realtime / 1000 Hz — match a wired DS5's
|
||||
// 1 ms polling (was 0 / 250 Hz). Existing
|
||||
// saved configs keep their value.
|
||||
printf("[Config] polling_rate_mode invalid, defaulting to 2 (1000 Hz)\n");
|
||||
}
|
||||
if (body->audio_buffer_length < 16 || body->audio_buffer_length > 128) {
|
||||
body->audio_buffer_length = 16; // low buffer avoids the DS5's periodic re-buffer gap
|
||||
@@ -109,9 +111,13 @@ void config_valid() {
|
||||
body->screen_off_timeout = 15; // mirrors the original 15-min off tier
|
||||
printf("[Config] screen_off_timeout invalid, defaulting to 15 min\n");
|
||||
}
|
||||
if (body->bt_mic_enable > 1) { // 0xFF erased / upgrade → default ON
|
||||
body->bt_mic_enable = 1;
|
||||
printf("[Config] bt_mic_enable invalid, defaulting to 1 (on)\n");
|
||||
if (body->bt_mic_enable > 1) { // 0xFF erased / upgrade → default OFF
|
||||
body->bt_mic_enable = 0; // opt-in: the BT mic path has a known
|
||||
// 2x-playback-rate bug (#10), so it is
|
||||
// off until explicitly enabled (Settings
|
||||
// screen / web config). Existing saved
|
||||
// configs keep their value.
|
||||
printf("[Config] bt_mic_enable invalid, defaulting to 0 (off)\n");
|
||||
}
|
||||
if (body->screen_brightness > 3) { // kBrightLevels has 4 entries (0..3)
|
||||
body->screen_brightness = 0; // full brightness
|
||||
@@ -121,10 +127,6 @@ void config_valid() {
|
||||
body->controller_wakes_display = 1;
|
||||
printf("[Config] controller_wakes_display invalid, defaulting to 1 (on)\n");
|
||||
}
|
||||
if (body->speaker_rate_trim > 200) { // 0xFF erased / upgrade → 0 Hz (centered)
|
||||
body->speaker_rate_trim = 100; // 100 encodes 0 Hz; actual trim = stored - 100
|
||||
printf("[Config] speaker_rate_trim invalid, defaulting to 100 (0 Hz)\n");
|
||||
}
|
||||
if (body->config_version != CONFIG_VERSION) {
|
||||
body->config_version = CONFIG_VERSION;
|
||||
printf("[Config] Warning: Config may breaking change\n");
|
||||
|
||||
@@ -54,14 +54,6 @@ struct __attribute__((packed)) Config_body {
|
||||
// preserves the original "any controller activity wakes the screen"
|
||||
// behavior. Issues #8 (dim timeout never fired during play) and #9.
|
||||
uint8_t controller_wakes_display;
|
||||
// EXPERIMENTAL (audio/speaker-rate-trim branch): fine speaker clock-drift trim.
|
||||
// The DS5's DAC and the USB host's audio clock are independent ~48 kHz crystals;
|
||||
// the residual ppm mismatch slowly drifts the DS5's buffer and produces the
|
||||
// periodic crackle (#7). This nudges our delivered rate to 48000 + (trim-100) Hz
|
||||
// to null that drift. Stored 0..200, offset by 100 so an erased-flash 0xFF clamps
|
||||
// to 100 = 0 Hz (an exact no-op, byte-identical to pre-trim). Swept on the OLED
|
||||
// Settings screen with the D-pad ▶◀, 1 Hz/step. Default 100 (0 Hz).
|
||||
uint8_t speaker_rate_trim;
|
||||
};
|
||||
|
||||
struct __attribute__((packed)) Config {
|
||||
|
||||
+5
-13
@@ -130,7 +130,7 @@ constexpr int kLbModeHost = 8;
|
||||
constexpr int kNumLbModes = 9;
|
||||
|
||||
// Settings screen state
|
||||
constexpr int kNumSettingsItems = 18; // 8 fields + 3 auto-haptic + 2 screen-timeout + BT mic + Ctrl-wake + Spk-trim + Reset + Wipe
|
||||
constexpr int kNumSettingsItems = 17; // 8 fields + 3 auto-haptic + 2 screen-timeout + BT mic + Ctrl-wake + Reset + Wipe
|
||||
constexpr int kSettingsAutoHapEnaIdx = 8;
|
||||
constexpr int kSettingsAutoHapGainIdx = 9;
|
||||
constexpr int kSettingsAutoHapLpIdx = 10;
|
||||
@@ -138,9 +138,8 @@ constexpr int kSettingsScrDimIdx = 11;
|
||||
constexpr int kSettingsScrOffIdx = 12;
|
||||
constexpr int kSettingsBtMicIdx = 13;
|
||||
constexpr int kSettingsCtrlWakeIdx = 14;
|
||||
constexpr int kSettingsSpkTrimIdx = 15;
|
||||
constexpr int kSettingsResetIdx = 16;
|
||||
constexpr int kSettingsWipeSlotsIdx = 17;
|
||||
constexpr int kSettingsResetIdx = 15;
|
||||
constexpr int kSettingsWipeSlotsIdx = 16;
|
||||
Config_body settings_local{};
|
||||
int settings_sel = 0;
|
||||
bool settings_dirty = false;
|
||||
@@ -1600,12 +1599,6 @@ void settings_adjust(int delta) {
|
||||
}
|
||||
case 13: c.bt_mic_enable ^= 1; break; // BT mic on/off
|
||||
case 14: c.controller_wakes_display ^= 1; break; // controller activity wakes OLED on/off
|
||||
case 15: { // speaker_rate_trim stored 0..200 = -100..+100 Hz, 1 Hz/step
|
||||
int v = (int)c.speaker_rate_trim + delta;
|
||||
if (v < 0) v = 0; if (v > 200) v = 200;
|
||||
c.speaker_rate_trim = (uint8_t)v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1709,9 +1702,8 @@ __attribute__((noinline)) void format_settings_item(int idx, char* line, size_t
|
||||
break;
|
||||
case 13: snprintf(line, n, "%s BT Mic %s", cur, c.bt_mic_enable ? "on" : "off"); break;
|
||||
case 14: snprintf(line, n, "%s CtrlWake %s", cur, c.controller_wakes_display ? "on" : "off"); break;
|
||||
case 15: snprintf(line, n, "%s SpkTrim %+dHz", cur, (int)c.speaker_rate_trim - 100); break;
|
||||
case 16: snprintf(line, n, "%s Reset to defaults", cur); break;
|
||||
case 17: snprintf(line, n, "%s Wipe all slots", cur); break;
|
||||
case 15: snprintf(line, n, "%s Reset to defaults", cur); break;
|
||||
case 16: snprintf(line, n, "%s Wipe all slots", cur); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user