Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
chore: lossless plays a matching local FLAC in place of Spotify's sound (off, no settings row yet)
My-Name-Is-Jefff5c1cb0d0d50
diff --git a/AGENTS.md b/AGENTS.mdindex 02709553..8f9859cd 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -59,6 +59,13 @@ Rules:## Working on it+- Lossless (`Shared/Lossless/`, `SGLossless.h`; off unless `spotifyglass.lossless` is set): a local FLAC or ALAC of+ the same recording plays in place of Spotify's sound. The anchor comes from cross-correlating 4 s of Spotify's+ output with the file, never from Spotify's position reports, and every seek, skip, pause or route change drops it+ (`SGSourceEvents.x`). Spotify still streams its own copy, so the switch lowers its quality only while the next song+ has a file. Sources (providers, downloads) are a design only: `docs/lossless-sources.md`. Do not take Sing's+ `SGPlayerSetStage` slot. After audio edits, run the handoff harness (`harness/lossless/handoff-build.sh`).+- Build: `make install` (signs and pushes to the phone), `make quick` (a tweak-only change, in seconds), `make release` (IPA only). Tweak only:`env -u MAKELEVEL gmake -C tweak clean package`.- Look at Spotify's views through recorded trees (`make session` records clean ones intodiff --git a/docs/lossless-addons.md b/docs/lossless-addons.mdnew file mode 100644index 00000000..08c72737--- /dev/null+++ b/docs/lossless-addons.md@@ -0,0 +1,90 @@+# HTTP addon compatibility research++Follow-up to [Lossless sources and automatic upgrades](lossless-sources.md), checked 2026-10-08 against+the links and September announcements supplied by the user. **No provider is validated in Vitrine.**+These are provider candidates for the same background-upgrade coordinator, not separate player designs.+Server-side JavaScript is compatible with the decision to execute no downloaded provider code in the app.+The [playback investigation](lossless-playback.md) records Vitrine's native decoder and integration+limits. Provider range support alone does not prove progressive decoding or timestamp seeking.+The [audio prototype](lossless-prototype.md) does not yet connect to any of these providers.++## Provider candidates++| Candidate | Evidence | Implication |+| --- | --- | --- |+| [Plex/Jellyfin addon](https://github.com/rairulyle/bitchord-selfhosted-addon) | Inspected source at `2a20c3ac76327f56a4189df66c79292462e154f3`: manifest/search/stream endpoints and original-file proxy with range forwarding | Best first controlled integration target when a test library is available; each configured server is a separate source |+| [Telegram Music Addon](https://github.com/Imnotshashwat/Telegram-Music-Addon) | Inspected source at `cb336d2cce2b77676a450f11587edf42985cc17a`: JSON endpoints, ISRC resolution and range-aware audio route | Second candidate; requires the user's own channel and hosted server. WebDAV support does not require Vitrine to implement WebDAV |+| Deezer addon | User supplied `https://deezer.nom.deno.net/` and an announcement describing an account/free-pool requirement; the browser tool could not access the page | Current authentication, returned formats, availability and Vitrine access remain unverified |+| Unified addon | User supplied several setup URLs and announcements about Tidal account/pool requirements, CJK matching fixes, Atmos and client-version restrictions; the browser tool could not inspect the pages | Do not infer the current host, access requirements, matching quality or third-party-client eligibility from old announcements |+| NullBeat | User supplied an announcement naming paid TorBox access, 5 requests/minute and 60 TorBox caches/hour | Treat these as announced limits for this provider, not verified current limits or global addon defaults; operator permission for BitChord does not establish permission for Vitrine |+| [Amazon Music API](https://github.com/itsmeadarsh2008/amazon-music-api), [HiFi API](https://github.com/binimum/hifi-api) | Public repositories describe backend APIs, not proof of the BitChord manifest/search/stream contract | Backend building blocks only; need a separately maintained adapter and supported access. They are not paste-and-play addons |++The Unified locations supplied were `https://addon.unified.dpdns.org/pages/setup.html`,+`https://unified-addon.netlify.app/pages/setup.html`, `https://unified-addon.vercel.app/`, and+`https://unified.dpdns.org/`. Keep these as research leads only. Failed access through the browser tool+does not establish that a service is down. No credentials, donated accounts, stream requests or media+downloads were used in this research.++## Verified implementation details++The Plex/Jellyfin addon's+[routes](https://github.com/rairulyle/bitchord-selfhosted-addon/blob/2a20c3ac76327f56a4189df66c79292462e154f3/internal/server/server.go)+are `/{slug}/{secret}/manifest.json`, `/search`, `/stream/{id}`, `/file/{id}` and `/art/{id}`, all under+that source prefix. Its+[mapper](https://github.com/rairulyle/bitchord-selfhosted-addon/blob/2a20c3ac76327f56a4189df66c79292462e154f3/internal/server/mappers.go)+returns `/file/{id}` with codec, container, `manifest: "none"`, rate/depth and bitrate. Its+[proxy](https://github.com/rairulyle/bitchord-selfhosted-addon/blob/2a20c3ac76327f56a4189df66c79292462e154f3/internal/server/proxy.go)+forwards `Range`/`If-Range`, relevant response headers, and 200/206/416 statuses. Media-server credentials+are handled server-side; the source URL itself grants access and must be treated as a credential.+Its search/stream handlers serve the original file rather than negotiating a lower-quality transcode.+The reviewed mapper does not return ISRC, so strict metadata matching is still needed.++Telegram's pinned+[server](https://github.com/Imnotshashwat/Telegram-Music-Addon/blob/cb336d2cce2b77676a450f11587edf42985cc17a/index.js)+declares `search`, `stream`, `isrc`; search rows include direct `streamURL` values. It implements+`/resolve-isrc?isrc=…`, `/stream/{id}` and `/audio/{id}`. The audio handler parses byte ranges and emits+206/416; HEAD is handled too. Secret-prefix authentication is supported, but the server also permits+unprotected operation if no secret is configured. Do not assume a particular deployment is private.+Its stream handler reports the stored file and logs the requested quality; that does not establish+conversion to the requested tier. It also fills missing rate/depth with defaults. Decoder verification+must distinguish provider hints from measured format. These are static observations, not live playback+or a security audit of either server.++## Additions to the client design++The [Eclipse guide](https://eclipsemusic.app/docs) shares the basic JSON contract and documents optional+`isrc` capability: `/resolve-isrc?isrc=…` returns `trackId` (or `id`), then the client resolves the stream;+404 or a null ID means no match. It also describes `expiresAt` as a Unix timestamp and settings passed+as query parameters, with `perNetwork` selecting the active network's value. These extensions can fit+the Vitrine client without embedding provider code. Retain Vitrine's duration/edition validation even+after an ISRC lookup; do not inherit a provider's certainty without checking the actual media.++1. **Import the generated addon link.** A setup page, GitHub repository and JSON index are distinct+ inputs. Open setup in the external browser; the user pastes its generated root/manifest URL. Do not+ turn an HTML setup URL into a guessed API root. Preserve path secrets and server slugs, including+ case and encoding, when appending endpoints. Store secret-bearing URLs in Keychain, with a separate+ opaque source ID for preferences; redact them from diagnostics and settings exports.+2. **Identify the client honestly.** Use Vitrine's own user agent/version. A BitChord-only or+ version-restricted service needs operator-supported Vitrine access; do not masquerade as BitChord+ to pass its checks. Expose a useful unsupported-client reason and leave Spotify playing.+3. **Enforce a budget per provider.** Share manifest and lookup work; avoid automatic multi-query,+ whole-queue or playlist prefetch. A 5-request/minute service may spend several requests on one+ candidate. Budget health checks, retries and preparation too; a stream-resolution call can trigger+ backend caching before audio is requested. Represent request and cache quotas separately. If an+ announced limit's scope is uncertain, use a conservative configured budget until documented.+ Honor `Retry-After`; a cooldown leaves current playback alone. Do not probe these services at scale.+4. **Enforce ceilings locally.** Original-file providers may ignore a requested lower tier. Reject+ above-ceiling candidates before full buffering and retain Spotify. For the initial lossless-only+ upgrade, a cellular lossy ceiling can simply suppress external preparation. `atmos: "auto"` is a+ provider default in Telegram's manifest: explicitly request stereo where supported, then validate+ the response. Atmos availability does not establish lossless audio.+5. **Test seeking against the served bytes.** Add fixtures for path-secret prefixes, direct search+ URLs, exact/missing ISRC, absent `encrypted`, unknown rate/depth, ignored quality/Atmos preferences,+ valid and invalid ranges, 416, servers that ignore Range and return 200, revoked source links,+ incorrect Content-Range, expiry and cooldown. Byte-range support enables efficient fetching but+ does not itself prove time-accurate seeking or aligned playback. Do not make an unconditional HEAD+ request to a possibly single-use media URL.++Start with fixtures modeled on these source-verified contracts, then a controlled personal-library+server. Hosted-provider compatibility remains a separate check, with no bundled source credentials or+default hosted endpoint. The unresolved Spotify handoff described in the main design still applies.diff --git a/docs/lossless-playback.md b/docs/lossless-playback.mdnew file mode 100644index 00000000..3c8ad95b--- /dev/null+++ b/docs/lossless-playback.md@@ -0,0 +1,154 @@+# Lossless playback inside Vitrine++Investigation and native decoder experiment, 2026-10-08. **No production playback integration yet.**+Read with the [provider design](lossless-sources.md) and [addon compatibility notes](lossless-addons.md).+Implementation follow-up: [the inactive native prototype](lossless-prototype.md) now includes a+complete-file decoder and pre-Sing source selector with a real AudioUnit handoff harness. The remaining+production integration and streaming limits below still apply.++## Recommendation++Use HTTP addons, with a user-configured Plex/Jellyfin addon as the first real integration. Keep Spotify+as queue/control owner and investigate replacing its decoded audio **before Sing**. First prove that+handoff with a completely prepared, generated file. Native file decoding now has a reproducible+simulator baseline; live progressive decoding and Spotify synchronization do not.++Spotify can start immediately while a matched alternative prepares in the background. Waiting for a+bounded temporary file is acceptable for the first experiment because Spotify continues playing. It+is not a promise of a quick upgrade on every connection. Check expected size, allowed network, remaining+track duration and buffering permissions before fetching. Keep a bounded investigation of Spotify's+native external-source route alongside this work: a working native handoff could avoid the PCM+approach's continuing Spotify download and decoder, but static evidence does not prove it works.++## Native decoding: measured evidence++Added [harness/lossless](../harness/lossless/README.md), an isolated arm64 simulator app using four+generated 12-second stereo assets. No third-party media, account, Spotify instance or phone is used.++| Question | Observed on iOS 27.0 simulator | Consequence |+| --- | --- | --- |+| Native FLAC/ALAC files? | Complete frames and waveform-checked seeks for 16/44.1 FLAC, 24/96 FLAC and 24/48 ALAC | `ExtAudioFile` is a usable first decoder experiment; no JavaScript or FFmpeg runtime required |+| Convert to Spotify's client rate? | All four assets convert to 44.1 kHz with the expected frame count | Decode into the negotiated client format; conversion does not preserve original hi-res rate |+| Remote PCM via `AVAssetReader`? | Metadata loads over successful HTTP 206 requests; decoding refuses all four with -11838 | Do not assume local decoding generalizes to HTTP URLs |+| Incremental FLAC? | Parser succeeds and native packet conversion yields correct samples, but is 816/1,024 frames short at EOF | Promising, incomplete; investigate EOF before a streaming claim |+| Incremental ALAC? | Fast-start M4A succeeds; tail-metadata M4A returns `optm` | Container layout matters; not every `.m4a` is progressively streamable |+| Bit depth from compressed ASBD? | `mBitsPerChannel` is zero for these assets | Validate codec/container metadata separately; Float32 output does not prove source depth |++Raw output and reproduction commands are in the harness. Its packet test collects all input before+converting, so it does not establish bounded buffering, underflow recovery or progressive seek. This+ran on one simulator runtime, not iOS 16.1/26 or a phone. Local decode timings are not playback-startup+or device-throughput measurements. The deployment target alone does not prove older runtime support.++The installed Apple SDK's `AVPlayerItemSampleBufferOutput.h` marks that API available from iOS 27+and currently supports HLS items only. Apple's+[API documentation](https://developer.apple.com/documentation/avfoundation/avplayeritemsamplebufferoutput)+also limits it to decoded PCM from HLS. It cannot be the iOS 16.1/26 direct-FLAC baseline.++## Exact PCM integration point++Inspect [SpeedPitch.x](../tweak/Sources/Shared/Player/SpeedPitch.x): `pullSource`, `pullMixer`,+`pullChain`, `feed`; and [Sing.x](../tweak/Sources/Shared/Sing/Sing.x): `stage`, `musicOutputChanged`,+`SGSingLeadOf`.++Flow exercised by the inactive prototype; normal Spotify playback does not enable source selection:++```text+Spotify mixer -> original PCM + source advancement+ |+prepared external PCM -> source selector -> Sing -> speed/pitch -> RemoteIO+ -> render-notify effects/haptics+```++`SGPlayerSetStage` is one atomic function pointer, already registered by Sing even when its engine is+inactive. Do not register lossless as another stage. The prototype adds a distinct source selector+inside Shared/Player's `pullSource`, reached both with no stage and through the `pullMixer` callback+passed to the existing stage. The decoder lives in Shared/Lossless; its future coordinator will+publish prepared buffers through that seam.++The first prototype should still pull the original mixer for each source span, retain its valid/silent+frame information, and choose external frames only for a validated corresponding span. This is a+hypothesis for preserving Spotify's advancement, **not proof of its clock contract**. It continues+Spotify's traffic and decoding. Never replace frames beyond a native stall, pending seek, unknown+track transition or EOF just because external data exists. Never wait for a worker during render.++Constraints from the current code:++- `pullSource` advances a per-output sample counter. It is not track position and cannot identify+ repeat, seek or crossfade. Bind the external cursor to a verified track/seek occurrence and source+ frame anchor; carry that generation through every decoded buffer.+- Sing pulls seconds ahead and corrects the public position for that lead. Feeding its input from the+ *audible* position would replay old audio. Commit at the aligned next **source** frame; change the+ source badge when new audio actually emerges. Do not discard buffered audio to hurry a badge update.+- Speed/pitch consumes input at a different rate from wall time. Advance by frames actually pulled,+ not a main-thread timer. Sing's seek/landing behavior needs a shared discontinuity signal; the track+ observer does not report every seek.+- The seam accepts Float32, noninterleaved, at most two channels with matching mixer/output client+ rates. Sing further requires stereo 44.1 kHz. The harness tested interleaved decoder output; planar+ conversion and actual stage composition still need tests. A 96 kHz source converted to 44.1 kHz is+ a lossless source with resampled output, not preserved hi-res output.+- Selection is after Spotify's mixer/EQ. Overwriting its samples can bypass upstream gain,+ normalization, fade and duck behavior. Measure and reproduce necessary gains explicitly; exclude+ crossfade/Automix initially. Vitrine's downstream processing is a separate stage.+- Follow `SGPlayerWatchMusicOutput`, disposal and format changes. Keep buffers alive through render.+ Preserve native sound information used to choose the music output; replacement sound must not keep+ an obsolete output selected.+- Network, file I/O, codec work, allocation, Objective-C messages and waits stay on workers. Render+ reads preallocated PCM and an immutable generation descriptor. Recovery must account for frames+ Sing already holds rather than abruptly mixing clock generations.++An independent `AVPlayer` would need another design for Spotify's queue, lyrics, lock-screen commands,+sleep timer, interruptions and Connect. It is not the preferred production shortcut. Phone-side PCM+replacement cannot improve a remote Connect renderer; keep that mode with Spotify.++## Spotify binary: a real lead, not a proven hook++Inspected the available decrypted **9.1.78** executable, SHA-256+`2d78c32e16c941776c1de2d38f06a61fbc0aac6a865c6a68ce4180cf1001d671`.+Used symbols, string cross-references and bounded ARM64 disassembly; did not modify or run the binary.++- `SPTEsperantoPlayer` exposes `playContext:options:loggingParams:origin:` and context preparation.+ `SPTPlayerContext`, `SPTPlayerContextPage` and `SPTPlayerTrack` have dictionary construction and+ serialization. These are investigation entry points, not a demonstrated stream-replacement API.+- `media.external_url` has code references including `0x1095ce8ec` and `0x1095ce97c`. This establishes+ a metadata-key code path, not that overriding `-[SPTPlayerTrack metadata]` changes the decoder.+- The binary contains `ResolveAudioURL` and `rurl`; timeline-conductor code has separate branches+ logging external URIs (`0x1096a1da4`) and HLS audio URLs (`0x1096a1e34`), checked in disassembly.+ This supports investigating native external media, but establishes neither music-track eligibility+ nor required fields, authorization behavior, codecs or handoff.++Addresses are unslid evidence for that exact executable, not offsets to ship. Verify 9.1.88 separately.+AVPlayer symbols in Canvas/video do not establish the main music player's implementation.++The next native-source experiment should use a diagnostic build and generated audio: trace the context+representation to the selected source, identify necessary fields, and test whether changing the+rendition retains recording identity, queue, position and local controls. Starting a separate context+or episode does not solve in-place music upgrades. Do not invent selectors or rewrite entitlements.++## Concrete implementation slices++1. **Controlled PCM handoff harness.** Extend the real AudioUnit chain from `harness/speed`, with a+ known original signal and a native-decoded alternate. Prove source selection before a stand-in+ stage, original decoder advancement, seek/skip/format cancellation, underflow/fallback, pause,+ two outputs and disposal during render. Then test actual Sing. This is the highest-risk unresolved+ dependency; prove it before shipping a settings switch.+2. **Provider configuration and diagnostics.** Native HTTP/JSON models, isolated session, Keychain,+ source ordering, per-network ceilings and “Test source.” Use fixtures first, then a user's own+ Plex/Jellyfin source. No bundled credential pools or playlist-sized searches. Distinguish a+ successful recording match and media validation from mere connectivity.+3. **Opt-in prepared-file upgrade on device.** A bounded transient file, native worker decode to+ the client format, generation-tagged PCM and one playback owner. Prepare while Spotify plays;+ switch only when recording, position, format, policy and transition checks agree. No offline+ library management in this slice. If buffering permissions/budgets rule out a complete file, keep+ Spotify playing until a supported streaming path exists.+4. **Progressive media.** Resolve the FLAC EOF deficit; test a bounded URLSession/parser/converter+ pipeline, cancellation, truncation, seek tables/range reopens and resets. ALAC needs a compatible+ container or a separately tested random-access reader. Range support alone does not map time to+ compressed byte offsets. Local-file decoding does not prove reading a file still being downloaded.+5. **Automatic upgrades and both looks.** Add coordinator and source/output status after the seam+ passes. Test rapid A/B/A, repeat, pause/seek during preparation, network/route changes, recovery,+ background, lyrics/Live Activity, speed/pitch, Sing and haptics. Measure audible discontinuity,+ drift, total bytes for both sources, CPU, memory and recovery time.++The follow-up prototype adds a tested audio seam and native decoder, not an addon client, lossless+settings, active Spotify replacement or production streaming engine. Synchronized playback ownership+remains the main uncertainty; progressive transport is another unresolved part.diff --git a/docs/lossless-prototype.md b/docs/lossless-prototype.mdnew file mode 100644index 00000000..20d1c7f8--- /dev/null+++ b/docs/lossless-prototype.md@@ -0,0 +1,250 @@+# Native lossless prototype++**History.** This records the first slice (2026-10-08). The live engine no longer decodes whole files or+takes its anchor from Spotify's position: see the Lossless section of [tweaks.md](tweaks.md). `SGPreparedPCM` and+`SGPCMReplacement` remain for the handoff harness.++Implemented in `feat/lossless-native-prototype`, based on `e2677b1`, 2026-10-08.+This is the first audio-integration slice of [the playback design](lossless-playback.md).+**Normal Spotify playback does not enable it.** There is no provider client, settings switch,+track matcher, automatic-upgrade coordinator or network fetch in this slice.++## Implemented++- `Shared/Lossless/SGPreparedPCM.{h,m}`: native complete-file FLAC/ALAC decoding to stereo planar+ Float32 at the requested client rate. Checks codec, channels, rate, declared versus decoded length,+ finite samples, cancellation and allocation limits. Rejects network URLs. Encoded file limit:+ 64 MiB; decoded PCM limit: caller-selected up to 32 MiB (about 95 seconds at 44.1 kHz stereo).+ This whole-file prototype deliberately rejects longer assets; production playback needs a bounded+ worker-fed buffer rather than raising this limit without measuring memory.+- `Shared/Lossless/SGPCMReplacement.{h,m}`: immutable, retained PCM snapshots, explicit output and+ generation identity, source-to-asset frame anchors, whole-span replacement and native fallback for+ stale/wrong output, rate, shape, position or EOF. Readers use atomic admission; storage is reclaimed+ off render after admitted readers leave. Publication is refused when retirement is backlogged;+ at most three snapshot owners are retained by the replacement manager. A caller can own additional+ decoder results, so this is not a process-wide memory cap.+- `Shared/Player/SpeedPitch.{h,x}`: a separate source-selector callback after successful non-silent+ music-mixer pulls and before the existing stage. It leaves `SGPlayerSetStage` with Sing. The native+ mixer is still pulled once, including when its output is replaced. Native samples determine output+ ownership; replacement audio cannot keep an obsolete output selected by being louder.+- Output restart, selection, connection, format change, stop and disposal invalidate source epochs.+ Native mixer silence/errors also invalidate: the output counter can advance without recording+ advancement, so recovery requires a new proven anchor. This conservatively includes natural silence.+ Format changes invalidate before `AudioUnitSetProperty`, since its property listener may arrive+ later. `SGSourceEvents.x` now invalidates on terminal seek/skip/stop commands, reported track,+ pause/resume, inferred position discontinuities/repeat, Sing on/off and route changes. These+ invalidations run only in driver (FLEX) builds. A future playback owner still needs its own policy and+ source cancellation, proven recording identity and seek-landing coordination.++## Calling contract++1. Decode a complete, trusted local file on a worker using `SGPreparedPCM`. Cancellation is cooperative+ between native reads; it cannot interrupt a stuck Core Audio call. This does not validate HTTP or+ progressive transport, and it does not create an offline library.+2. Call `SGPCMReplacementEnable` once off render. It registers the process-lifetime source callback;+ no constructor calls it in the tweak. The harness installs an instrumented wrapper instead.+3. With the correct music output running, capture the current epoch. Establish a **source-frame**+ anchor for the same recording/seek occurrence, then publish via `SGPCMReplacementInstall`.+ The output-local sample counter is not a track position. Reading Spotify's audible position while+ Sing has look-ahead does not establish this anchor. No production anchor has been proven yet.+4. Cancel on discontinuity. Once invalidated, callbacks leave native PCM untouched; an already+ admitted callback may finish its block. Invalidating cannot remove old audio that Sing or the+ time/pitch unit already buffered. Recovery must eventually coordinate those buffers and clocks.++The render path does no allocation, file I/O, codec work, Objective-C messaging, mutex acquisition or+waiting. It validates an immutable descriptor and copies PCM. Publication/reclamation use a control+mutex off render. Do not relax sequential consistency of reader admission and pointer exchange without+re-proving reclamation: a reader must increment before loading the active snapshot.++This is not sample-transparent output: substitution occurs after Spotify's mixer/EQ, potentially+bypassing normalization, fades and ducking. Existing Vitrine stages follow it, and the original decoder+still consumes data. Native stalling causes native fallback, not continued external playback. There is+no crossfade between sources. The prototype's whole-span fallback can itself be audible.++## Validation++[harness/lossless/handoff.m](../harness/lossless/handoff.m) compiles the production decoder, replacement+manager and Logos `SpeedPitch.x` with a real converter -> mixer -> RemoteIO chain. Generated test audio+only, with a stand-in processing stage; it does not run Spotify or Sing's ML engine.++```sh+THEOS="$HOME/theos" sh harness/lossless/handoff-build.sh+xcrun simctl install booted harness/lossless/build/handoff/LosslessHandoff.app+xcrun simctl launch --console booted dev.vitrine.lossless-handoff > /tmp/lossless-handoff.log 2>&1+```++Require `RESULT failures=0` and no `FAIL` lines. A 30-second watchdog catches an incomplete run. The+harness emits quiet audio through the simulator. The direct checks cover complete decode, waveform,+rate conversion, exact/insufficient budget, cancelled preparation, remote-URL rejection, frame anchors,+invalid source guards, storage retention/release and concurrent publication/cancellation. The live+chain covers no-stage and existing-stage paths, native advancement, speed, cancellation, pause/resume,+native silence and recovery without reusing its old anchor, format invalidation and mixer disposal.++Run the existing `harness/speed` `two` scenario as a regression with the selector unregistered. That+checks multiple outputs and rate/ownership behavior, not simultaneous active lossless substitution on+two outputs. The new direct test separately rejects a wrong output identity.++Full tweak package build (does not install):++```sh+THEOS="$HOME/theos" CLANG_MODULE_CACHE_PATH=/tmp/vitrine-lossless-module-cache \+ env -u MAKELEVEL gmake -C tweak package -j4 \+ 'ADDITIONAL_SWIFTFLAGS=-module-cache-path /tmp/vitrine-lossless-module-cache'+```++A fresh worktree also needs its ignored `SGFlagList.m` generated from the target IPA with+`scripts/extract-flags.py`. The decoder/handoff harness needs no IPA or provider account.++On 2026-10-08, the iOS 27.0 arm64 simulator handoff run completed with zero failures (59 checks,+including AudioUnit setup checks). The full arm64 tweak package builds. The existing `two` regression+checks 12 multi-output scenarios with the selector inactive. Recorded results are in+[the harness results](../harness/lossless/results/). No device installation,+physical-device handoff, actual Sing look-ahead, recording match, provider compatibility, seamless+switch, end-to-end latency or network bandwidth improvement is established by these simulator checks.++## Next dependency++Prove the source-frame anchor and validate the new application-level discontinuity observation on+a phone, including actual Sing look-ahead and buffering. Separately add the HTTP fixture client and+bounded media preparation. Keep the prototype inactive in normal playback until those paths pass;+do not infer production readiness from the source selector or the simulator waveform checks.++## Source-frame anchor diagnostic++Phase 1 adds measurement only. It neither registers the selector nor replaces any audio, and it has+no provider client or network fetch. The capture code and ring storage compile only with `SG_DRIVER=1`,+the flag `scripts/pipeline.sh` sets for FLEX builds. Logging additionally requires `FLEXManager` at+runtime, like Sing's dev-model slot (currently in `SGSingModel.m`, not `SGSingLoader.m`). Do not install+this branch merely to enable lossless: replacement remains inactive.++Build the diagnostic package without installing:++```sh+THEOS="$HOME/theos" CLANG_MODULE_CACHE_PATH=/private/tmp/vitrine-anchor-module-cache \+ env -u MAKELEVEL gmake -C tweak package -j4 SG_DRIVER=1 \+ 'ADDITIONAL_SWIFTFLAGS=-module-cache-path /private/tmp/vitrine-anchor-module-cache'+```++The package must be used with a FLEX-carrying app for the runtime check to pass. A non-FLEX package+omits the sampler, diagnostic getters and logger. Keep one canonical spelling of the module cache+path: reusing a cache through both `/tmp` and `/private/tmp` can make Swift report duplicate Darwin+modules. The original package command above was attempted; a fresh canonical cache resolved that+local build failure.++`Shared/Player/SpeedPitch.x` samples each music mixer pull before source selection and Sing. Each+output has its own 256-entry single-producer/single-consumer ring, with release/acquire publication+and compile-time checks for lock-free atomics. Sampling uses+`N = max(1, round(rate * speed / (10 * frames)))`, plus every changed epoch. This is roughly ten+samples per wall second during steady playback; Sing's refill can burst. Silent and failed pulls+are included and invalidate the diagnostic epoch even with no selector installed. A full ring drops+new entries rather than waiting; `dropped` is cumulative. The render additions do no allocation,+locking, logging, Objective-C messaging or waiting. Rings are never reset when output slots are reused.++One main-thread timer drains all rings once a second. Application events also drain and log immediately+on main; events received on another thread invalidate immediately and dispatch the log asynchronously.+`event_host`/`event_epoch` retain the event's origin, while `control_host` describes when state was read.+Every line begins `lossless-anchor:` and carries `v=1`, a process `session` and a monotonic `seq`.+Track URIs are percent-escaped. There is no inference that a command succeeded: `evidence=command`+(or `next_command`/`previous_command`) is a request, `reported` is observed state, and `inferred` is+clock-based evidence. The all-report observer sees position-only reports that the original+`playerStateDidChange:` deliberately filters out.++The fields come from:++| Fields | Source and meaning |+| --- | --- |+| `source_frame`, `frames`, `rate`, `host`, `silent`, `status`, `sample_epoch`, `output` | The native pull's starting output-local frame, span, client rate, `mach_absolute_time()` immediately before `AudioUnitRender`, its silence/error result, epoch captured before the pull and output identity. This counter is not a recording position. |+| `epoch`, `event_epoch` | Current source epoch and the epoch after an application invalidation. They can differ from the latest render sample. |+| `position_ms`, `uri`, `playing`, `paused` | `SGPlayerState()` from the existing player-state observer. Position uses `positionAsOfTimestamp` while paused and `position` otherwise. Sing's existing correction applies. |+| `raw_ms` | `SGSingSourcePosition`, a wrapper around Sing's existing thread-local `rawPosition` path, bypassing only Sing's correction. SpeedPitch's playback-speed hook still applies. |+| `karaoke_ms`, `karaoke_uri` | `SGKaraokePositionMs()` and `SGKaraokePlayingTrack()`, through KaraokeSource's captured player's state. The id may differ from the observer while reports arrive. No lyrics-delay preference is applied. |+| `speed`, `pitch`, `follows` | `SGPlayerSpeed()`, `SGPlayerPitch()` and `SGPlayerPitchFollowsSpeed()`. Their changes are labeled `speed_pitch` on the next timer tick. |+| `tp_engaged`, `tp_latency_ms` | SpeedPitch's engagement flag and `SGTimePitchLatency()` for the selected pull/in-place unit, read off render under its build lock. Zero when disengaged; this is the unit's reported latency, not a measurement of the whole output route or Varispeed. |+| `sing_on`, `sing_active`, `sing_state` | The requested mic setting, whether the last stage call entered the compatible Sing engine, and `SGSingCurrentState()`. A stopped output can leave the last-call flag set; consult sample age. Engine entry does not mean the ML worker is separating. |+| `sing_lead_ms`, `sing_correction_ms` | `SGSingHeldLead()` and `SGSingLeadOf(state)`. The latter is the correction for that state's clock line, and need not equal current held lead. |+| `age_ms`, `read_ms`, `dropped` | Age of the latest render sample, duration of the control reads, and total ring drops. Control reads and render samples are not an atomic snapshot. |++No unique Spotify recording-occurrence id or sample-exact seek-landing marker was found in the+exposed state. A same-URI backwards clock jump near the prior end, with repeat-one enabled, logs+`repeat` with `evidence=inferred`; other clock changes over 500 ms log `position_jump`. A seek to zero+near the end can also look like repeat. Commands and the later observed transition are separate+boundaries. Small remote seeks or repeats that never produce distinguishable state reports can be+missed. A callback for a route notification has the route reason, not proof of an acoustic change.+No independent fixed Sing end-to-end latency is exposed; its held lead and correction are available.++### Phone capture and procedure++After the maintainer reviews, builds and installs a FLEX app, capture its syslog with the locally+available `pymobiledevice3 syslog live` command. That CLI was not installed in this sandbox; no phone+capture or installation was attempted here. The exact filter is:++```sh+grep -F 'lossless-anchor:' phone-syslog.log > lossless-anchor.log+uv run harness/lossless/anchor.py lossless-anchor.log+uv run harness/lossless/anchor.py --selftest+```++Do not filter on process name: signing can change it. The prefix is inside SGLog's public text, so a+full syslog capture works too. Keep the full capture for comparison with Sing's own logs.++1. Start with a local phone output, one music track, Sing off, normal speed/pitch, crossfade/Automix and+ other effects off. Record at least 30 seconds of steady playback before each change, and 30 seconds+ after it settles. Confirm `sample=1`, a fresh `age_ms`, and matching sample/current epochs.+2. Seek forward, then backward, including a seek while paused. Pause for five seconds and resume.+ Check `seek`, reported `pause`/`resume`, any observed `position_jump`, and fresh epochs afterward.+3. Skip next, skip previous, then let repeat-one wrap naturally on the same track. A skip request and+ track report are distinct lines; a same-track repeat is an inferred boundary, not proven identity.+4. Turn Sing on with an already available model and vocals below As sung. Wait for active processing+ and a settled lead; record it, then turn Sing off and record the retained lead draining. Do not+ download a model as part of this measurement-only phase.+5. Set speed to 1.5x and back to 1x. Record the Pitch follows speed setting; repeat with it off if+ comparing the time-stretch unit. Repeat the seek/pause checks with Sing and 1.5x together.+6. Connect AirPods, record, disconnect them, and resume if Spotify pauses. Repeat with Sing on. Check+ `route` and pause/resume epochs and note the route externally; the unit latency is not Bluetooth's+ acoustic latency. Include a foreground/background interval to expose delayed control sampling.++### Reading the analysis++The stdlib-only script splits at every event, URI, epoch, output, rate, speed or dropped-count change,+including empty event segments. It rejects paused, silent, failed, missing, stale (>250 ms), slow+control-read (>20 ms), reused and mismatched-epoch samples. The default settling exclusion is one+second after a boundary (`--settle` changes it); Sing may need much longer. Karaoke fits additionally+require a matching track id. Fewer than three usable samples or no frame span reports `insufficient`.++For each of the corrected observer, uncorrected observer and Karaoke clocks, it projects the control+position back to render time using the recorded speed, then fits the fixed-slope model+`position = a + source_frame / rate`. `a_ms` is the mean offset; `jitter_ms` is RMS residual about that+fixed offset. A regression of that residual against source seconds gives `drift_ppm`; `detrended_ms`+shows scatter after removing that linear trend. The reference for `a` is each segment's mean frame,+so drift affects comparisons between segments. Short spans are not a reliable ppm estimate.++`jump_ms` compares offsets with the last fitted segment in the same process. `across` lists all+intervening event kinds, including events without enough data to fit. A cluster such as seek, pause+and route cannot be attributed to just one cause. Frame-counter resets change the numerical offset;+a jump is not necessarily a jump in audible audio. Different tracks need separate recording anchors.+The script reports each event transition rather than averaging unlike recordings by event kind.++These are asynchronously paired block-level observations. They can reject a stable-anchor hypothesis+or motivate a tighter probe, but even low jitter does not prove a source-frame anchor, acoustic+alignment, seamless replacement, HTTP decoding, hi-res output or Sing buffer continuity. No measured+phone result exists in this change.++### Checks for this phase++Both non-FLEX and FLEX arm64 tweak packages built on 2026-10-08, and the non-FLEX dylib holds none of the+diagnostic's strings or classes. On the iOS 27.0 arm64 simulator, the lossless handoff run passed 59 checks+with `RESULT failures=0`, and the speed harness `two` scenario reported 0 failures. Both runs are recorded in+[the harness results](../harness/lossless/results/).++The analyzer self-check passes known offsets, 100 ppm drift, jumps, all named event boundaries,+stale/silent/error/paused samples, epoch/output changes, duplicate capture lines and missing lyrics.+The actual ring implementation also passed wraparound, full-ring drops and a 200,000-entry concurrent+producer/consumer check on the Mac:++```sh+clang -std=c11 -Wall -Wextra -Werror -Wno-unused-parameter -O2 -pthread \+ harness/lossless/anchor-ring.c -o /tmp/vitrine-anchor-ring+/tmp/vitrine-anchor-ring+```++Layer and whitespace checks pass. No commit, push, phone installation or main-checkout edit was made.diff --git a/docs/lossless-sources.md b/docs/lossless-sources.mdnew file mode 100644index 00000000..90abdfd3--- /dev/null+++ b/docs/lossless-sources.md@@ -0,0 +1,245 @@+# Lossless sources and automatic upgrades++Research and proposed implementation, 2026-10-08. **End-to-end support is not implemented.** The target is BitChord's+“Start instantly. Land on lossless” behavior: begin playback without waiting for a lossless lookup,+prepare a better rendition in the background, and switch at the current position when it is ready.+Keep separate Wi-Fi and cellular quality ceilings. The user's direction is to avoid downloaded+JavaScript in the app; use HTTP/JSON sources.++The [HTTP addon compatibility follow-up](lossless-addons.md) covers the user-supplied Plex/Jellyfin,+Telegram, Deezer, Unified and NullBeat candidates, Eclipse's optional ISRC contract, secret-bearing setup+links, per-provider quotas and client-access restrictions. Read it alongside this design.+The [Vitrine playback investigation](lossless-playback.md) adds binary evidence, the exact pre-Sing+integration point and measured native decoder limits. Its [harness](../harness/lossless/README.md)+proves local file decoding on one simulator runtime, not a working Spotify handoff.+The [native prototype](lossless-prototype.md) adds an inactive pre-Sing source selector and bounded+complete-file decoder. Its AudioUnit harness now checks replacement and lifecycle fallback; live+Spotify anchoring, providers and automatic upgrades remain to be implemented and verified.++## What upstream actually does++Source inspected at BitChord [`2c599a6`](https://github.com/kushagrasinghx/BitChord/tree/2c599a6cd7a195227a11dab5769f88da8fd08194)+and Monochrome [`eb602ad`](https://github.com/monochrome-music/monochrome/tree/eb602ad4f8f74e0307f29906fc78a32f08a34f87).+These are source observations, not measurements of a running app or proof that a provider serves Vitrine.++| Mechanism | What runs on the client | Relevance to Vitrine |+| --- | --- | --- |+| BitChord JavaScript modules | Fetch a JSON index, download JavaScript, execute it in QuickJS | Do not embed this mechanism |+| BitChord HTTP addons | Read JSON from manifest, search and stream endpoints | Recommended provider contract |+| BitChord automatic upgrade | Match another recording, prepare its audio, replace the current rendition while preserving position | Desired playback behavior, independent of the provider contract |+| Monochrome | Web client with its own catalogue/stream APIs and browser playback | Backend-adapter reference; not a BitChord module protocol |++BitChord's [module host](https://github.com/kushagrasinghx/BitChord/tree/2c599a6cd7a195227a11dab5769f88da8fd08194/app/src/main/java/com/music/bitchord/data/sources/module)+reads arbitrary `category:*` arrays, excluding artwork/testing, and deduplicates module IDs. A descriptor+supplies `id`, `name`, `download`, and capability tags (`tags` or `labels`). Relative downloads resolve+against the index location. The downloaded code exports `searchTracks(query, limit, context)` and+`getTrackStreamUrl(id, quality, context)`; settings use `context.settings.<key>.value`. Search returns+`tracks`; resolution returns `streamUrl` plus track format metadata. QuickJS receives fetch and timer+bridges and polyfills. This executes third-party code, including initialization code, rather than+merely reading configuration. The current registry removes its retired built-in module but preserves+user-added module sources. A known working module index was not supplied or tested here.++BitChord's [HTTP addon guide](https://bitchord.kushagrasingh.in/docs) describes a separate JSON protocol.+Its [implementation](https://github.com/kushagrasinghx/BitChord/tree/2c599a6cd7a195227a11dab5769f88da8fd08194/app/src/main/java/com/music/bitchord/data/sources/addon)+confirms that downloaded JavaScript is unnecessary for search, resolution, quality selection or upgrades.++The [resolver](https://github.com/kushagrasinghx/BitChord/blob/2c599a6cd7a195227a11dab5769f88da8fd08194/app/src/main/java/com/music/bitchord/data/sources/SourceResolver.kt)+uses the current connection's quality ceiling. The+[matcher](https://github.com/kushagrasinghx/BitChord/blob/2c599a6cd7a195227a11dab5769f88da8fd08194/shared/src/jvmSharedMain/kotlin/com/music/bitchord/data/sources/TrackMatcher.kt)+compares title, recording/version markers, credited artists, duration and edition metadata. A search+result is a candidate, not sufficient reason to replace the playing recording.++[QualityUpgrade](https://github.com/kushagrasinghx/BitChord/blob/2c599a6cd7a195227a11dab5769f88da8fd08194/app/src/main/java/com/music/bitchord/playback/QualityUpgrade.kt)+tracks background candidates. In+[PlaybackService](https://github.com/kushagrasinghx/BitChord/blob/2c599a6cd7a195227a11dab5769f88da8fd08194/app/src/main/java/com/music/bitchord/playback/PlaybackService.kt),+`swapIn` first auditions/buffers the replacement, rechecks the current item and remaining duration,+avoids an active crossfade, then replaces the media item with a distinct rendition URI, seeks to the+freshly read position, prepares playback and watches for failure. This is possible because BitChord+owns its player. Its source comments acknowledge a playback break: the marketing description is not+evidence of a gapless or sample-aligned transition, and its latency claims are not Vitrine measurements.++Monochrome's current [`getStreamUrl`](https://github.com/monochrome-music/monochrome/blob/eb602ad4f8f74e0307f29906fc78a32f08a34f87/js/api.js)+uses [`tracks-api.js`](https://github.com/monochrome-music/monochrome/blob/eb602ad4f8f74e0307f29906fc78a32f08a34f87/js/tracks-api.js)+outside developer mode. That client searches `/search/tracks?q=…&limit=…`, matches external catalogue+metadata, and constructs `/track/{id}` on its configured service. It labels that path FLAC; its `quality`+argument is recorded as requested quality but is not sent by that URL builder. Do not treat this as+proof of decoded quality or enforcement of a cellular ceiling. Developer mode uses a configured HiFi+endpoint and `/trackManifests/`; other code handles base64 manifests, direct URLs and DASH/HLS through+browser playback. Do not assume all those older/alternate paths are today's default resolver.++Monochrome's [README](https://github.com/monochrome-music/monochrome/blob/eb602ad4f8f74e0307f29906fc78a32f08a34f87/README.md)+also warns about restrictions on its hosted API. Its frontend being public does not establish a stable+third-party service contract. Prefer a user-configured addon or separately hosted adapter with supported+backend access. No provider addresses, service credentials or executable modules should ship as defaults.+BitChord's root license is GPL-3.0 and Monochrome's is Apache-2.0; preserve applicable notices if code is+later reused. This research change copies neither implementation into Vitrine.++## Proposed HTTP compatibility scope++The following is a proposed Vitrine subset, not a claim of complete BitChord compatibility:++| Request | Response and handling |+| --- | --- |+| `GET /manifest.json` | Require a nonempty `id`; read name, version, resources, settings defaults/options and policy switches. Preserve any base-path prefix. Reject a module index with an explanation; never follow its JavaScript downloads. |+| `GET /search?q=…&quality=…` | Read `tracks` with provider IDs, title, artist, album, duration in seconds and format hints. Ignore unknown fields. Validate types, sizes and finite positive durations. |+| `GET /stream/{encoded-id}?quality=…` | Read `url`, codec, container, manifest transport, encryption and audio-format hints. A `404` is a miss; an empty URL or JSON error is not playable. |+| `GET /resolve-isrc?isrc=…` (optional) | For a source declaring `isrc`, accept `trackId` or `id`, then resolve and validate the media; 404/null means no match. This is an Eclipse extension also implemented by the inspected Telegram addon. |++Quality tiers are `LOSSLESS`, `HIGH`, `LOW`, mapped to a manifest's declared option values when present.+Search rows can carry `streamURL`, allowing resolution to skip `/stream`. Support that optimization only+through the same URL, policy and media validation as a resolved stream. BitChord accepts aliases such as+`fileCodec`, `containerFormat`, `mediaType`, and `format: "dash"`; normalize these explicitly and reject+conflicting transport declarations. `atmos` is separate from lossless quality; immersive playback is+outside the first implementation, so decline immersive renditions and request a supported stereo copy.++Persist `allowDownloads` (default allowed) and `checkValidLossless` (default off). Downloads are outside+the first implementation regardless of that permission. The output gate is learned on the first+manifest request; afterward apply it before health checks, searches, resolution and media requests,+including requests for previously queued results. Unknown routes do not qualify. Define and device-test+an iOS route policy; do not copy Android's Bluetooth codec list. BitChord deliberately includes some+lossy Bluetooth codecs in this gate, so its meaning is not proof of bit-perfect output.++Start with direct, unencrypted FLAC/ALAC assets whose exact container and seeking behavior pass the+iOS harness. HLS is a later compatibility test; DASH is unsupported until a separate transport path or+server-side remux is implemented and tested. Do not send an MPD to a direct-file decoder, silently+transcode lossless to AAC, accept DRM payloads, or claim support from a filename extension. A hosted+adapter can translate a Monochrome/backend API or run a module outside the phone and expose the JSON+contract; operating and trusting that adapter remains a separate concern.++Use asynchronous asset inspection and audio format descriptions to establish what the native decoder+can read; Apple's [loading-media guidance](https://developer.apple.com/documentation/avfoundation/loading-media-data-asynchronously)+documents that inspection. Actual decode, seek and handoff tests remain necessary for each container,+transport and supported iOS version.++## What Vitrine already has, and what is missing++| Existing code | Can reuse | Does not establish |+| --- | --- | --- |+| `Shared/Player/PlayerState.{h,x}` | Main-thread track/state observation, current state | Observers do not report every position change; no external stream setter |+| `Headers/SPTPlayer.h` | Pause, resume, seek, queue commands and position/duration | A verified API to load a provider URL into Spotify's player |+| `Shared/Haptics/SystemMusicHaptics.x`, `SGHapticTrack.h` | `SGSpotifyISRC` recording lookup | Provider access or permission to send Spotify's request headers elsewhere |+| `Shared/Player/SpeedPitch.{h,x}` | Music-output tracking and a PCM stage seam | Encoded FLAC decoding, sample-rate preservation or a complete replacement clock |+| `Shared/Sing/Sing.x` | Existing consumer of `SGPlayerSetStage` | A second independent stage slot; registering another would replace Sing's stage |+| `Shared/Lyrics/KaraokeSource.x` | Lyrics use Spotify's position and seek commands | Automatic synchronization with an independent player |+| `Shared/LocalFiles/` | Metadata/cover edits and lyrics for files Spotify already plays | Importing an arbitrary URL as a playable Spotify track |+| `Redesigned/Kit/SGRDownload.m` | Mirrors Spotify's download controls/state | An external-audio cache or downloader |+| `Shared/AdBlock/Premium.m` | Existing product-state behavior | Lossless/offline server entitlement; an external source is a separate audio path |++The resolver and shared settings belong under a proposed `Shared/Lossless/`, with keys beginning+`spotifyglass.lossless.` and no look gate. `App/Pages.m` composes the settings entry. Each look owns any+player badge/menu presentation. Preserve `Core <- Settings <- Shared <- Native | Redesigned <- App`.+Reuse the state observer instead of adding another feature-specific player-state hook. If recording+identity helpers move out of Haptics, preserve existing lyrics and haptics callers.++## Playback design to prove++There is no verified Spotify URL-substitution hook in the reviewed code. Do not invent one or patch+Spotify's entitlement/quality responses to pretend this feature exists. Investigate three paths:++1. **Spotify source handoff:** best integration if a real stream-source boundary can be demonstrated+ against the supported binary and on device. It is presently unproven.+2. **Decoded PCM substitution:** use a worker-fed bounded buffer before the existing processing chain.+ This offers reuse of speed/pitch, effects and haptics, but requires a composable stage with Sing,+ explicit sample-rate conversion, render-safe ownership, and synchronized seek/clock/EOF handling.+ Continuing to pull Spotify to advance its clock would still consume Spotify's stream and bandwidth.+3. **Independent native player:** useful for proving provider decoding first; production use requires+ routing all controls, queue progression, lock screen, interruptions and clocks through one playback+ owner. Merely pausing Spotify and starting another player leaves Spotify's state and lyrics behind.++Prove the playback seam using a known recording and a controlled alternate file before implementing+automatic replacement. Keep the feature off until one path passes the device checks below. Do not label+any of these paths implemented based on a parser test or successful URL lookup.+Standalone native file decoding now has a simulator baseline; see the playback investigation. The next+experiment is a prepared-file PCM source selector before Sing, alongside bounded investigation of+Spotify's external-source path. Preserve Sing's stage slot. An independent production player has the+largest control/state scope. Remote `AVAssetReader` and complete local files are not interchangeable:+the former refused the tested HTTP media, and incremental paths have separate container/EOF limits.++The automatic-upgrade coordinator should have explicit states:++`Spotify playing + resolving -> candidate matched -> preparing -> ready -> switching -> external playing`++Before switching, any miss, timeout, rejection or cancellation leaves Spotify playing. After switching,+a media failure enters recovery: prepare Spotify at the current audible position, transfer ownership+once ready, and clear external state. If recovery cannot play, surface the actual error. Never leave a+permanent mute or two audible players. A skip or source removal invalidates all prior work.++- Start Spotify immediately. Provider lookup must never delay its normal start. A validated cached+ rendition can participate in an eventual startup race, once the playback seam supports it; a quick+ metadata response alone is not ready audio. Keep one owner of playback commands and one audible path.+- Match the recording before buffering. Prefer matching ISRCs when both sides provide them, then verify+ duration and edition; reject conflicting ISRCs. The baseline addon contract does not promise ISRCs.+ Otherwise require strong title, artists, album/version and duration agreement. Unknown or ambiguous+ matches keep Spotify. Matching length does not prove sample alignment between masters.+- Give each playback occurrence a generation, even when repeat plays the same URI. Carry it with source+ configuration revision, seek revision and network policy through every lookup, decode and callback.+ Recheck immediately before committing. A paused track must stay paused; a seek invalidates prepared+ audio for the old position. Drop stale publication and cancel obsolete network/decode work explicitly.+- Prepare silently while Spotify continues. Validate decoded format, duration, seek accuracy and buffered+ headroom around the moving playback position. Read that position again at commit. Avoid late-song,+ crossfade and Automix transitions; defer or skip when their state cannot be established reliably.+ No claim of an inaudible transition until measured on device with aligned and differently mastered files.+- Separate Wi-Fi/cellular ceilings apply to lookup, prefetch and media requests, including retries and+ redirects. Recheck on path changes and before handoff. Cancel disallowed network work immediately;+ already buffered audio can finish without new requests while recovery is prepared. Unknown network+ types use the stricter policy. These settings govern Vitrine's extra traffic; Spotify's own data-use+ controls still govern its stream. Account for both streams when measuring usage.+- A returned FLAC above a lossy ceiling is refused, not accepted because the source ignored `quality`.+ Do not compare unlike lossy codecs by bitrate alone or replace known native lossless with an unproven+ alternative. Initially restrict upgrades to verified lossless candidates from a known lossy baseline.+- Keep network, decoding, allocations, locks and Objective-C messaging out of the audio render callback.+ Output disposal and route changes must not free buffers still in use. Output gates and policy changes+ invalidate pending upgrades as well as new requests.++## Network and quality boundaries++Use a dedicated network session with no Spotify cookies, authorization headers or shared authentication+state. Store provider credentials separately in Keychain; redact complete signed/token-bearing URLs,+including paths, query strings and provider error text. Show users that a configured source receives+track metadata. Require HTTPS, validate every redirect, and reject non-network schemes and unexpected+local/private destinations by default. A user-configured LAN provider needs an explicit scoped policy.+Validate manifests and media URLs as untrusted data; JSON removes code execution, not network risks.++Bound response sizes, nesting, candidate count, concurrent requests, decode memory, total wall-clock+time and retries. Honor `429`/`Retry-After`, distinguish auth/configuration errors from misses, and use+backoff rather than retry loops per player-state event. Source removal, reset and disable must stop its+requests and clear its credentials, pending work and cached answers. Do not rely only on stale-result+checks to stop bandwidth use. Apply limits to artwork and media manifests/segments as well as API calls.++BitChord caches manifests/searches for ten minutes and stream answers for five, with shared in-flight+calls. For Vitrine, key by source identity/configuration, recording, rendition, requested quality and+policy; respect a provider's earlier expiry and single-use URLs. A signed URL is not a permanent+track identity. Keep transport errors out of successful caches and evict expired/failed stream entries.+Transient playback buffering must be bounded and must not become offline storage for a streaming-only+provider. Offline downloads need a separate design and permission enforcement.++Display source codec/rate/depth separately from output route/rate and active processing. A FLAC label+does not establish an unaltered source master; decoder verification establishes the delivered format.+DSP, speed, pitch, Sing, normalization and resampling can change the output. Apple's+[audio-session guidance](https://developer.apple.com/library/archive/qa/qa1631/_index.html) says preferred+sample rates are requests: inspect the actual active rate. Never promise bit-perfect output from the+source label, a cable, or a settings switch alone.++## Implementation sequence and acceptance evidence++1. Build `Shared/Lossless` provider models, HTTP client, matcher and shared settings against a local+ fixture service. Test root/manifest URLs with prefixes, encoded IDs, optional fields, malformed and+ oversized JSON, wrong editions, duplicate IDs across providers, expiry, redirects, auth, rate limits,+ cancellation, source reset/removal and quality ceilings. A JS index must be rejected without fetching+ or executing its code. Fixture audio should be owned or generated test material.+2. Add a decoder/playback harness for direct FLAC/ALAC. Measure startup, seeking, buffer size, cancellation+ and actual formats. Separately investigate the Spotify handoff seam; do not publish a working-feature+ claim from standalone playback. Prove one audible owner and recovery before enabling automation.+3. Add the coordinator and per-network policy. Exercise rapid A/B/A skips, repeat of one URI, seek/pause+ during preparation and handoff, late completions, EOF, 403 after resolution, stalls after handoff,+ network/route changes, background/lock screen, calls and Connect transfer. Remote Connect playback+ stays with Spotify initially; phone-side substitution cannot upgrade a remote renderer.+4. Validate both looks on supported Spotify versions and iOS targets. Exercise lyrics, Live Activity,+ lock-screen controls, sleep timer, speed/pitch, effects, Sing and haptics together. Run the layer check+ and relevant harnesses; measure time to first sound, time to upgrade, audible gap, drift, total bytes,+ CPU/memory and recovery time. Report measured results separately from targets.++The worktree now includes documentation, decoder and AudioUnit harnesses, and an inactive source selector+in Shared/Player. No provider client, JavaScript runtime or network settings were added. No Spotify/device+handoff was validated. The HTTP contract is sufficiently documented for a fixture-backed client; the+next major prerequisite is proven Spotify recording/frame anchoring, followed by bounded streaming.diff --git a/docs/tweaks.md b/docs/tweaks.mdindex e1bfa702..bd32185b 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -594,6 +594,28 @@ change shows after Spotify restarts; the tab editor on the Navbar page is the exout again, as are the Home gradient's color, strength and height, but not the switch that turns it on, and Vibrationsand Live Activity. The root page in `App/ModSettings.x` links the Appearance page and the page of each part of Spotify, and only the stored look's.+## Lossless++Off unless `spotifyglass.lossless` is set; there is no settings row yet. A FLAC or ALAC file in Spotify's+Documents/Lossless folder (Files app › Spotify › Lossless) plays in place of Spotify's sound for the same recording.++- **Which file:** the folder's index (`.index.plist`, track URI to file name), else a file whose title tag (or+ name) is the track's title and whose length is within 2 s. Artists are not compared. A file goes in the index+ once its sound matched (`SGLosslessStore.m`).+- **Where in the file:** at each track start, seek and resume, 4 s of Spotify's output are matched against the+ file near Spotify's position: first on a mono copy at 1/16 of the rate, then exactly on a full-rate window. A+ normalized correlation under 0.8 is another recording or master, and Spotify's sound stays. The file plays+ from that frame at the gain that matches Spotify's loudness (`SGLosslessEngine.m`).+- **Playback:** a static 2^19-frame ring that a worker thread fills ahead of where Spotify plays. A block the+ ring does not hold yet keeps Spotify's sound. The swap is in Shared/Player's source selector, before Sing.+- **Data:** Spotify still downloads and decodes its own copy, and it downloads a song as it starts. So while+ the next song in its queue has a file, Spotify's quality is set to low, and the user's value comes back+ afterwards, also at the next launch (`LosslessQuality.x`).+- **Not yet:** Karaoke and speeds other than 1 keep Spotify's sound; the first 4 s after each start, seek or+ resume are Spotify's; a silent block drops the anchor until the next try. Sources that download files are a+ design only: [Lossless sources](lossless-sources.md), [addons](lossless-addons.md),+ [playback investigation](lossless-playback.md), [prototype history](lossless-prototype.md).+## Make targetsmake build # out/vitrine-<version>.ipa with FLEX in itdiff --git a/harness/lossless/README.md b/harness/lossless/README.mdnew file mode 100644index 00000000..7eea5a4c--- /dev/null+++ b/harness/lossless/README.md@@ -0,0 +1,85 @@+# Native lossless decoder probe++An isolated simulator app using generated sine waves. It does not load Spotify, contact providers,+play audible audio, or change production playback. Requires Xcode with an arm64 iOS simulator,+FFmpeg and Python 3.9+. Build products default to the ignored `build/` directory; override+`LOSSLESS_PROBE_OUT` to keep them elsewhere.++```sh+sh harness/lossless/build.sh+python3 harness/lossless/serve.py harness/lossless/build/LosslessProbe.app+```++In another terminal, with the intended simulator booted:++```sh+xcrun simctl install booted harness/lossless/build/LosslessProbe.app+SIMCTL_CHILD_LOSSLESS_HTTP_BASE=http://localhost:18764 \+ xcrun simctl launch --console booted dev.vitrine.lossless-probe > /tmp/lossless-probe.log 2>&1+python3 harness/lossless/check.py /tmp/lossless-probe.log+```++Omit `SIMCTL_CHILD_LOSSLESS_HTTP_BASE` for local tests only. The server binds loopback and serves only+four generated fixture names, supporting HEAD, byte ranges, 206 and 416. Stop it with Ctrl-C afterward.+The app exits after its probes, with a 60-second watchdog. Check the log with `check.py`: exiting+successfully only means the probe finished, not that all APIs supported the media.++## What is exercised++- 12-second stereo FLAC at 16-bit/44.1 kHz and 24-bit/96 kHz; ALAC at 24-bit/48 kHz in M4A with the+ `moov` box at the end and an otherwise identical fast-start copy with it at the beginning.+- `AudioFileStream` in 257-byte input chunks. Collected packets and the magic cookie then go through+ `AudioConverter` to Float32 PCM. This intentionally keeps all packets in memory: **it is not a+ bounded streaming implementation**, network-underflow test or proof of seeking compressed streams.+- `ExtAudioFile` whole-file decode at native rate and 44.1 kHz, exact total-frame checks, and a seek+ to file frame `floor(3.125 * sourceRate)`. The next 1,024 native-rate frames are compared to the known+ sine waves; converted seeks check successful reads, not waveform alignment or converter latency.+- `AVAssetReader` over local and loopback HTTP assets, requesting one second from 3.125 seconds.+ Native-rate timestamp/frame bounds are checked locally. HTTP behavior is reported, not required+ to fail on every OS.++## Observed on 2026-10-08++iOS 27.0 arm64 simulator, SDK 27.0, minimum deployment target 16.1. **Not run on iOS 16.1 or 26, or on+a physical phone.** The deployment target alone does not establish those runtimes' capabilities.+Raw structured output: [results/ios27-simulator.jsonl](results/ios27-simulator.jsonl).++| Fixture | Local native decode | Seek max absolute error | Incremental parser / packet decode |+| --- | --- | --- | --- |+| CD FLAC | 529,200 frames | 0.00001525 | Parser succeeds; packet path gives 528,384 frames, 816 short |+| 96 kHz FLAC | 1,152,000 frames | 0.000000119 | Parser succeeds; packet path gives 1,150,976 frames, 1,024 short |+| ALAC, tail metadata | 576,000 frames | 0.000000119 | Parser refuses with `optm` / 1869640813 (not optimized) |+| ALAC, fast start | 576,000 frames | 0.000000119 | Parser and packet decode return all 576,000 frames |++All four full-file conversions to 44.1 kHz returned 529,200 frames. All four local asset-reader seeks+completed within one sample of the requested start/duration. All four HTTP assets loaded tracks but+`startReading` returned false with `AVFoundationErrorDomain -11838`+(`AVErrorOperationNotSupportedForAsset`); the fixture server recorded successful 206 responses.++The FLAC packet-path tail deficit is unresolved; do not silently pad it or call streaming complete.+The source ASBD reported zero bits per channel for these compressed formats. That value cannot be used+as a verified bit-depth badge. Fixture bit depth is known from generation; future source validation+needs codec/container metadata and decode inspection, separate from Float32 output precision.++These results support a complete temporary-file decoder as the first controlled playback experiment.+They do not prove Spotify clock continuity, stage composition, seamless switching, network streaming,+low memory use, background playback, hi-res output, or any provider's compatibility. See+[the integration investigation](../../docs/lossless-playback.md) for those remaining steps.++## Audio handoff prototype++`handoff-build.sh` builds a second app from the worktree's production `SGPreparedPCM`,+`SGPCMReplacement` and `SpeedPitch.x`. It replaces generated native PCM before a stand-in stage,+using a real converter/mixer/RemoteIO chain. It emits quiet audio. See+[the prototype's calling contract and tests](../../docs/lossless-prototype.md).++```sh+THEOS="$HOME/theos" sh harness/lossless/handoff-build.sh+xcrun simctl install booted harness/lossless/build/handoff/LosslessHandoff.app+xcrun simctl launch --console booted dev.vitrine.lossless-handoff > /tmp/lossless-handoff.log 2>&1+```++Require a final `RESULT failures=0`, not just successful launch. The app has a 30-second watchdog.+The default decoder probe above and its `check.py` remain separate; they use a different log format.+The handoff checks do not run actual Spotify or Sing's ML/look-ahead engine. The production source+selector has no automatic registration or settings switch.diff --git a/harness/lossless/anchor-ring.c b/harness/lossless/anchor-ring.cnew file mode 100644index 00000000..1e664b3f--- /dev/null+++ b/harness/lossless/anchor-ring.c@@ -0,0 +1,42 @@+// The render handoff stays ordered through overflow, index wrap and a concurrent drain.+#define SG_DRIVER 1+#include "../../tweak/Sources/Shared/Lossless/SGAnchorRing.h"+#include <assert.h>+#include <limits.h>+#include <pthread.h>+#include <stdio.h>+#include <sched.h>++static SGAnchorRing ring;+enum { count = 200000 };+static void *produce(void *unused) {+ for (unsigned i = 0; i < count; ++i) {+ SGAnchorSample sample = {.epoch = i, .sourceFrame = i * 1024., .frames = 1024};+ while (!SGAnchorPush(&ring, sample)) sched_yield();+ }+ return NULL;+}+int main(void) {+ SGAnchorSample sample;+ assert(!SGAnchorPop(&ring, &sample));+ atomic_store(&ring.read, UINT_MAX - 100);+ atomic_store(&ring.write, UINT_MAX - 100);+ for (unsigned i = 0; i < kSGAnchorCapacity; ++i)+ assert(SGAnchorPush(&ring, (SGAnchorSample){.epoch = i}));+ assert(!SGAnchorPush(&ring, (SGAnchorSample){0}));+ assert(atomic_load(&ring.dropped) == 1);+ for (unsigned i = 0; i < kSGAnchorCapacity; ++i) {+ assert(SGAnchorPop(&ring, &sample));+ assert(sample.epoch == i);+ }+ assert(!SGAnchorPop(&ring, &sample));+ pthread_t producer;+ assert(!pthread_create(&producer, NULL, produce, NULL));+ for (unsigned i = 0; i < count; ++i) {+ while (!SGAnchorPop(&ring, &sample)) sched_yield();+ assert(sample.epoch == i && sample.sourceFrame == i * 1024. && sample.frames == 1024);+ }+ assert(!pthread_join(producer, NULL));+ assert(!SGAnchorPop(&ring, &sample));+ puts("ANCHOR RING PASS");+}diff --git a/harness/lossless/anchor.py b/harness/lossless/anchor.pynew file mode 100644index 00000000..7782f975--- /dev/null+++ b/harness/lossless/anchor.py@@ -0,0 +1,234 @@+#!/usr/bin/env python3+"""Fit observed clock offsets. This does not establish sample-accurate recording alignment."""+import argparse+import math+import re+import statistics+import sys+from dataclasses import dataclass, field+from pathlib import Path++PREFIX = "lossless-anchor:"+ANSI = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")+FIELDS = re.compile(r"([a-z_]+)=([^\s]+)")+CLOCKS = ("position_ms", "raw_ms", "karaoke_ms")+++def parse(lines):+ for number, line in enumerate(lines, 1):+ line = ANSI.sub("", line)+ if PREFIX not in line:+ continue+ row = dict(FIELDS.findall(line.split(PREFIX, 1)[1]))+ try:+ for name in ("session", "seq", "kind", "uri", "epoch", "sample_epoch", "output"):+ if name not in row:+ raise ValueError(f"missing {name}")+ for name in ("control_host", "host", "source_frame", "rate", "age_ms", "speed",+ "read_ms", "sample", "silent", "status", "playing", "paused", "dropped", *CLOCKS):+ row[name] = float(row[name])+ if not math.isfinite(row[name]):+ raise ValueError(f"nonfinite {name}")+ if row.get("v") != "1":+ raise ValueError("unsupported version")+ except (KeyError, ValueError) as error:+ raise ValueError(f"line {number}: {error}") from error+ yield row+++@dataclass+class Occurrence:+ session: str+ reason: str+ uri: str+ start: float+ rows: list = field(default_factory=list)+ rejected: int = 0+++def occurrences(rows, settle=1.0, max_age=250.0):+ groups = []+ previous = None+ seen = set()+ for row in rows:+ identity = (row["session"], row["seq"])+ if identity in seen:+ continue # Some syslog captures carry the same SGLog line twice.+ seen.add(identity)+ reason = None+ if previous is None or row["session"] != previous["session"]:+ reason = row["kind"] if row["kind"] != "sample" else "capture_start"+ elif row["control_host"] < previous["control_host"]:+ raise ValueError("control timestamps went backwards within a session")+ elif row["kind"] != "sample":+ reason = row["kind"]+ else:+ for key in ("uri", "epoch", "output", "rate", "speed", "dropped"):+ if row[key] != previous[key]:+ reason = key+ break+ if reason:+ groups.append(Occurrence(row["session"], reason, row["uri"], row["control_host"]))+ group = groups[-1]+ valid = (row["kind"] == "sample" and row["sample"] == 1 and row["silent"] == 0+ and row["status"] == 0 and row["playing"] == 1 and row["paused"] == 0+ and row["sample_epoch"] == row["epoch"] and row["rate"] > 0 and row["speed"] > 0+ and 0 <= row["age_ms"] <= max_age and 0 <= row["read_ms"] <= 20+ and row["control_host"] - group.start >= settle and row["uri"] != "-")+ if valid and group.rows and row["host"] <= group.rows[-1]["host"]:+ valid = False+ if valid:+ group.rows.append(row)+ else:+ group.rejected += 1+ previous = row+ return groups+++def fit(group, clock):+ pairs = []+ for row in group.rows:+ if row[clock] < 0:+ continue+ # Karaoke names a bare track id, whereas the observer exposes a full URI.+ if clock == "karaoke_ms" and row.get("karaoke_uri", "-") not in (row["uri"], row["uri"].rsplit(":", 1)[-1]):+ continue+ x = row["source_frame"] / row["rate"]+ # Project the control read back to the start of the native pull. Read-ahead bursts,+ # Core Audio's block timing and public clock corrections remain measurement error.+ y = row[clock] / 1000 - (row["control_host"] - row["host"]) * row["speed"]+ pairs.append((x, y - x))+ if len(pairs) < 3:+ return None+ xs, offsets = zip(*pairs)+ span = max(xs) - min(xs)+ if span <= 0:+ return None+ mean_x, a = statistics.mean(xs), statistics.mean(offsets)+ denominator = sum((x - mean_x) ** 2 for x in xs)+ slope = sum((x - mean_x) * (offset - a) for x, offset in pairs) / denominator+ rms = math.sqrt(statistics.mean((offset - a) ** 2 for offset in offsets)) * 1000+ detrended = math.sqrt(statistics.mean((offset - a - slope * (x - mean_x)) ** 2 for x, offset in pairs)) * 1000+ return dict(n=len(pairs), a_ms=a * 1000, jitter_ms=rms, detrended_ms=detrended,+ drift_ppm=slope * 1e6, span_s=span)+++def report(groups, out=sys.stdout):+ last = {clock: None for clock in CLOCKS}+ pending = {clock: [] for clock in CLOCKS}+ for number, group in enumerate(groups, 1):+ print(f"occurrence={number} session={group.session} event={group.reason} uri={group.uri} "+ f"accepted={len(group.rows)} rejected={group.rejected}", file=out)+ for clock in CLOCKS:+ pending[clock].append(group.reason)+ result = fit(group, clock)+ if result is None:+ print(f" clock={clock} fit=insufficient", file=out)+ continue+ prior = last[clock]+ same_session = prior and prior[0] == group.session+ jump = f"{result['a_ms'] - prior[1]:.3f}" if same_session else "unknown"+ kinds = "+".join(pending[clock]) if same_session else "baseline"+ print(f" clock={clock} n={result['n']} span_s={result['span_s']:.3f} "+ f"a_ms={result['a_ms']:.3f} jitter_ms={result['jitter_ms']:.3f} "+ f"detrended_ms={result['detrended_ms']:.3f} drift_ppm={result['drift_ppm']:.3f} "+ f"jump_ms={jump} across={kinds}", file=out)+ last[clock] = (group.session, result["a_ms"])+ pending[clock] = []+++def selftest():+ import io+ def line(seq, t, kind="sample", offset=2, drift=0, **changes):+ frame = t * 48000+ row = dict(v=1, session=1, seq=seq, kind=kind, uri="spotify:track:test", karaoke_uri="test",+ epoch=1, sample_epoch=1, output=42, control_host=t + .05, host=t,+ source_frame=frame, frames=480, rate=48000, age_ms=50, speed=1.5, read_ms=.1,+ sample=1, silent=0, status=0, playing=1, paused=0, dropped=0)+ for clock in CLOCKS:+ row[clock] = (t + offset + t * drift + .05 * 1.5) * 1000+ row.update(changes)+ return "syslog [spotifyglass] " + PREFIX + " " + " ".join(f"{k}={v}" for k, v in row.items())+ lines = [line(1, 0, "start")]+ lines += [line(i + 1, i, drift=100e-6) for i in range(1, 11)]+ lines += [line(12, 11, "seek", epoch=2), line(13, 11.1, sample_epoch=1, epoch=2)]+ lines += [line(i + 3, i, offset=5, epoch=2, sample_epoch=2) for i in range(12, 22)]+ groups = occurrences(parse(lines))+ assert len(groups) == 2+ a, b = (fit(g, "raw_ms") for g in groups)+ assert abs(a["drift_ppm"] - 100) < 1e-6 and a["detrended_ms"] < 1e-6+ assert abs(b["a_ms"] - 5000) < 1e-6 and abs(b["drift_ppm"]) < 1e-6+ assert groups[1].rejected == 2+ stream = io.StringIO()+ report(groups, stream)+ assert "across=seek" in stream.getvalue() and "jump_ms=2999.450" in stream.getvalue()+ # Pauses and event clusters must survive even if they cannot themselves be fitted.+ events = ("pause", "resume", "skip", "track", "repeat", "sing_on", "sing_off", "route", "speed_pitch")+ rows = list(parse([line(i, i, kind) for i, kind in enumerate(events)]))+ assert [g.reason for g in occurrences(rows)] == list(events)+ assert all(fit(g, "raw_ms") is None for g in occurrences(rows))+ for changes in ({"silent": 1}, {"status": -1}, {"paused": 1}, {"playing": 0},+ {"age_ms": 251}, {"sample_epoch": 2}, {"sample": 0}, {"read_ms": 30}):+ bad = occurrences(parse([line(1, 0, "start"), line(2, 2, **changes)]))+ assert not bad[-1].rows+ assert len(occurrences(parse(lines + [lines[-1]]))[-1].rows) == 10+ for key in ("epoch", "output", "rate", "speed", "dropped", "uri"):+ changed = "different" if key == "uri" else 2+ gs = occurrences(parse([line(1, 0, "start"), line(2, 2, **{key: changed})]))+ assert gs[-1].reason == key+ # A new process, unavailable lyrics, nonfinite data and empty captures.+ assert occurrences([]) == []+ assert len(list(parse(["\x1b[32m" + lines[0] + "\x1b[0m"]))) == 1+ # Drift-free noise has a known fixed-offset RMS; a silent event cluster keeps its labels.+ noisy = [line(0, 0, "start")]+ noisy += [line(i, i, offset=2 + (0.01 if i % 2 else -0.01)) for i in range(1, 11)]+ noise_fit = fit(occurrences(parse(noisy))[0], "raw_ms")+ assert abs(noise_fit["a_ms"] - 2000) < 1e-6 and abs(noise_fit["jitter_ms"] - 10) < 1e-6+ clustered = lines[:11] + [line(12, 11, "pause"), line(13, 12, "resume")]+ clustered += [line(i + 3, i, offset=3) for i in range(13, 23)]+ stream = io.StringIO()+ report(occurrences(parse(clustered)), stream)+ assert "across=pause+resume" in stream.getvalue()+ restarted = lines[:11] + [line(i + 1, i, session=2) for i in range(11)]+ stream = io.StringIO()+ report(occurrences(parse(restarted)), stream)+ assert "jump_ms=unknown across=baseline" in stream.getvalue().split("session=2", 1)[1]+ gs = occurrences(parse([line(i, i, karaoke_uri="other") for i in range(5)]))+ assert fit(gs[0], "karaoke_ms") is None+ try:+ list(parse([line(1, 0, raw_ms="nan")]))+ raise AssertionError("nonfinite input accepted")+ except ValueError:+ pass+ print("SELFTEST PASS")+++def main():+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("capture", nargs="?", help="syslog file, or - for stdin")+ parser.add_argument("--selftest", action="store_true")+ parser.add_argument("--settle", type=float, default=1.0, help="seconds excluded after each boundary (default 1)")+ parser.add_argument("--max-age-ms", type=float, default=250.0)+ args = parser.parse_args()+ if args.selftest:+ selftest()+ return+ if not args.capture:+ parser.error("give a capture or --selftest")+ if not all(math.isfinite(v) and v >= 0 for v in (args.settle, args.max_age_ms)):+ parser.error("limits must be nonnegative")+ try:+ if args.capture == "-":+ groups = occurrences(parse(sys.stdin), args.settle, args.max_age_ms)+ else:+ with Path(args.capture).open(errors="replace") as source:+ groups = occurrences(parse(source), args.settle, args.max_age_ms)+ if not groups:+ raise ValueError("no lossless-anchor lines found")+ report(groups)+ except (ValueError, OSError) as error:+ parser.exit(1, f"anchor: {error}\n")+++if __name__ == "__main__":+ main()diff --git a/harness/lossless/build.sh b/harness/lossless/build.shnew file mode 100644index 00000000..7e2e0cf7--- /dev/null+++ b/harness/lossless/build.sh@@ -0,0 +1,44 @@+#!/bin/sh+set -eu+HERE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)+OUT=${LOSSLESS_PROBE_OUT:-"$HERE/build"}+APP="$OUT/LosslessProbe.app"+mkdir -p "$APP"+SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)+xcrun --sdk iphonesimulator clang -target arm64-apple-ios16.1-simulator -fobjc-arc -O1 \+ -isysroot "$SDK" -Wno-deprecated-declarations "$HERE/main.m" "$HERE/../scene.m" \+ -framework UIKit -framework Foundation -framework AVFoundation -framework AudioToolbox \+ -framework CoreMedia -o "$APP/LosslessProbe"+for NAME in cd hires alac; do+ case "$NAME" in+ cd) RATE=44100; CODEC=flac; FORMAT=s16; EXT=flac ;;+ hires) RATE=96000; CODEC=flac; FORMAT=s32; EXT=flac ;;+ alac) RATE=48000; CODEC=alac; FORMAT=s32p; EXT=m4a ;;+ esac+ ffmpeg -v error -y -f lavfi \+ -i "aevalsrc=0.08*sin(2*PI*440*t)|0.06*sin(2*PI*997*t):s=$RATE:d=12" \+ -c:a "$CODEC" -sample_fmt "$FORMAT" "$APP/$NAME.$EXT"+done+ffmpeg -v error -y -i "$APP/alac.m4a" -c:a copy -movflags +faststart "$APP/alac-fast.m4a"+cat > "$APP/Info.plist" <<'PLIST'+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0"><dict>+<key>CFBundleExecutable</key><string>LosslessProbe</string>+<key>CFBundleIdentifier</key><string>dev.vitrine.lossless-probe</string>+<key>CFBundleName</key><string>LosslessProbe</string>+<key>CFBundleVersion</key><string>1</string>+<key>CFBundleShortVersionString</key><string>1.0</string>+<key>UILaunchScreen</key><dict/>+<key>NSAppTransportSecurity</key><dict><key>NSAllowsLocalNetworking</key><true/></dict>+<key>UIApplicationSceneManifest</key><dict>+ <key>UISceneConfigurations</key><dict>+ <key>UIWindowSceneSessionRoleApplication</key><array><dict>+ <key>UISceneConfigurationName</key><string>Default</string>+ <key>UISceneDelegateClassName</key><string>SGRHarnessScene</string>+ </dict></array>+ </dict>+</dict>+</dict></plist>+PLIST+echo "$APP"diff --git a/harness/lossless/check.py b/harness/lossless/check.pynew file mode 100644index 00000000..7a2f4142--- /dev/null+++ b/harness/lossless/check.py@@ -0,0 +1,43 @@+#!/usr/bin/env python3+"""Verify the generated-fixture local decode/seek baseline; report streaming observations separately."""+import json+import math+from pathlib import Path+import sys++rows = [json.loads(line.removeprefix("LOSSLESS ")) for line in Path(sys.argv[1]).read_text().splitlines()+ if line.startswith("LOSSLESS ")]+if not rows or rows[-1].get("test") != "complete":+ raise SystemExit("FAIL: probe did not complete")+rates = {"cd.flac": 44100, "hires.flac": 96000, "alac.m4a": 48000, "alac-fast.m4a": 48000}+checks = 0+for name, rate in rates.items():+ local = [r for r in rows if r.get("file") == name and r["test"] == "local-decode-seek"]+ assert len(local) == 2, (name, "missing local decode")+ assert {r["clientRate"] for r in local} == {rate, 44100}, (name, "wrong rates")+ for row in local:+ assert row["status"] == row["seekStatus"] == 0, row+ assert row["sourceRate"] == rate, row+ assert row["frames"] == 12 * row["clientRate"], row+ assert math.isfinite(row["energy"]) and row["energy"] > 0, row+ assert row["seekFrames"] == 1024, row+ if row["clientRate"] == rate:+ assert math.isfinite(row["seekMaxError"]) and row["seekMaxError"] < 0.00004, row+ checks += 1+ local_reader = [r for r in rows if r.get("file") == name and r["test"] == "asset-reader" and not r["remote"]]+ assert len(local_reader) == 1, (name, "missing reader")+ row = local_reader[0]+ assert row["started"] and row["readerStatus"] == 2, row+ assert abs(row["firstPTS"] - 3.125) <= 1/rate and abs(row["frames"] - rate) <= 1, row+ checks += 1+ for row in rows:+ if row.get("file") != name:+ continue+ if row["test"] == "parsed-packet-decode":+ print(f"OBSERVATION {name}: packet decode status={row['status']}, "+ f"frames={row['frames']}/{12*rate}, max error={row['maxError']:.3g}")+ elif row["test"] == "incremental-parser":+ print(f"OBSERVATION {name}: parser status={row['status']}, ready={row['ready']}")+ elif row["test"] == "asset-reader" and row["remote"]:+ print(f"OBSERVATION {name}: remote reader started={row['started']}, error={row['errorCode']}")+print(f"PASS: {checks} local decode/seek cases. Streaming observations are not streaming acceptance tests.")diff --git a/harness/lossless/handoff-build.sh b/harness/lossless/handoff-build.shnew file mode 100644index 00000000..b66536da--- /dev/null+++ b/harness/lossless/handoff-build.sh@@ -0,0 +1,30 @@+#!/bin/sh+set -eu+HERE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)+SRC="$HERE/../../tweak/Sources"+OUT="$HERE/build/handoff"+APP="$OUT/LosslessHandoff.app"+mkdir -p "$APP" "$OUT/gen"+"${THEOS:?Set THEOS}/bin/logos.pl" -c generator=internal "$SRC/Shared/Player/SpeedPitch.x" > "$OUT/gen/SpeedPitch.m"+SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)+xcrun --sdk iphonesimulator clang -target arm64-apple-ios17.0-simulator -fobjc-arc -O1 -g \+ -isysroot "$SDK" -I"$SRC" -I"$SRC/Shared/Player" -Wno-deprecated-declarations \+ "$HERE/handoff.m" "$HERE/../scene.m" "$OUT/gen/SpeedPitch.m" \+ "$SRC/Shared/Lossless/SGPreparedPCM.m" "$SRC/Shared/Lossless/SGPCMReplacement.m" \+ "$SRC/Shared/Player/SGTimePitch.m" "$SRC/Core/SGRebind.m" "$SRC/Core/SGLog.m" \+ "$SRC/Core/SGPrefs.m" "$SRC/Core/SGUIMode.m" "$SRC/Core/SGFlagForce.m" \+ -framework UIKit -framework Foundation -framework AVFoundation -framework AudioToolbox -framework QuartzCore \+ -o "$APP/LosslessHandoff"+ffmpeg -v error -y -f lavfi -i 'aevalsrc=0.08*sin(2*PI*440*t)|0.06*sin(2*PI*997*t):s=44100:d=12' -c:a flac -sample_fmt s16 "$APP/fixture.flac"+ffmpeg -v error -y -i "$APP/fixture.flac" -c:a alac -sample_fmt s32p "$APP/fixture.m4a"+python3 - "$APP" <<'PY'+from pathlib import Path+import plistlib,sys+p=Path(sys.argv[1])+info={'CFBundleExecutable':'LosslessHandoff','CFBundleIdentifier':'dev.vitrine.lossless-handoff',+ 'CFBundleName':'LosslessHandoff','CFBundleVersion':'1','CFBundleShortVersionString':'1.0','UILaunchScreen':{},+ 'UIApplicationSceneManifest':{'UISceneConfigurations':{'UIWindowSceneSessionRoleApplication':[+ {'UISceneConfigurationName':'Default','UISceneDelegateClassName':'SGRHarnessScene'}]}}}+(p/'Info.plist').write_bytes(plistlib.dumps(info))+PY+printf '%s\n' "$APP"diff --git a/harness/lossless/handoff.m b/harness/lossless/handoff.mnew file mode 100644index 00000000..459412d3--- /dev/null+++ b/harness/lossless/handoff.m@@ -0,0 +1,274 @@+#import <UIKit/UIKit.h>+#import <AVFoundation/AVFoundation.h>+#import <stdatomic.h>+#import "Shared/Lossless/SGPCMReplacement.h"++// SpeedPitch.x reads the player for its clock away from 1x; the harness has no player.+void *SGPlayerState(void) { return NULL; }++static int failures;+static SGPreparedPCM *asset;+static atomic_uint_fast64_t decoded, selected, stageFrames, badSamples, lastSourceEnd;+static atomic_bool mute;+static float expectedFirst;+static BOOL replacedBlock;+static AudioUnit output, mixer, converter;+static uint64_t stallEpoch, stallSelected;++static void expect(BOOL ok, const char *name) {+ printf("%s %s\n", ok ? "PASS" : "FAIL", name); fflush(stdout);+ if (!ok) ++failures;+}+static void check(OSStatus status, const char *name) { expect(status == noErr, name); }+@interface SPTPlayerState : NSObject+@property(nonatomic) double positionAsOfTimestamp;+@property(nonatomic,strong) NSDate *timestamp;+@end+@implementation SPTPlayerState+- (double)playbackSpeed { return 1; }+- (double)position { return self.positionAsOfTimestamp-self.timestamp.timeIntervalSinceNow; }+@end++static OSStatus original(void *ref, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *time,+ UInt32 bus, UInt32 frames, AudioBufferList *data) {+ atomic_fetch_add(&decoded, frames);+ BOOL silent = atomic_load(&mute);+ for (UInt32 b=0;b<data->mNumberBuffers;++b) for (UInt32 i=0;i<frames;++i)+ ((float *)data->mBuffers[b].mData)[i] = silent ? 0 : 0.02;+ if (silent) *flags |= kAudioUnitRenderAction_OutputIsSilence;+ return noErr;+}+static void selector(AudioUnit unit, uint64_t epoch, double sourceFrame, double rate, UInt32 frames, AudioBufferList *data) {+ uint64_t before = SGPCMReplacementFrames();+ SGPCMReplacementApply(unit,epoch,sourceFrame,rate,frames,data);+ replacedBlock = SGPCMReplacementFrames() > before;+ if (replacedBlock) {+ atomic_fetch_add(&selected,frames);+ // The decoder must still advance; source epoch/counter are the real SpeedPitch callback's.+ uint64_t native=atomic_load(&decoded), end=(uint64_t)sourceFrame+frames;+ if (native < end || native-end > 4096) atomic_fetch_add(&badSamples,1);+ const float *left=asset.left, *right=asset.right;+ for (UInt32 i=0;i<frames;++i) {+ if (((float *)data->mBuffers[0].mData)[i] != left[(uint64_t)sourceFrame+i]+ || ((float *)data->mBuffers[1].mData)[i] != right[(uint64_t)sourceFrame+i])+ atomic_fetch_add(&badSamples,1);+ }+ expectedFirst=((float *)data->mBuffers[0].mData)[0];+ atomic_store(&lastSourceEnd,(uint64_t)sourceFrame+frames);+ }+}+// A stand-in stage: inspect the actual pull callback Sing receives, then alter its samples.+static OSStatus stage(UInt32 frames, AudioBufferList *data, SGPlayerPull pull, void *context) {+ replacedBlock=NO;+ OSStatus status=pull(context,frames,data,NULL);+ if (!status && replacedBlock) {+ if (((float *)data->mBuffers[0].mData)[0] != expectedFirst) atomic_fetch_add(&badSamples,1);+ atomic_fetch_add(&stageFrames,frames);+ for (UInt32 b=0;b<data->mNumberBuffers;++b) for (UInt32 i=0;i<frames;++i)+ ((float *)data->mBuffers[b].mData)[i]*=0.5;+ }+ return status;+}+static AudioUnit make(OSType type, OSType subtype) {+ AudioComponentDescription description={type,subtype,kAudioUnitManufacturer_Apple,0,0};+ AudioUnit unit=NULL;+ check(AudioComponentInstanceNew(AudioComponentFindNext(NULL,&description),&unit),"create audio unit");+ return unit;+}+static void setup(void) {+ [AVAudioSession.sharedInstance setCategory:AVAudioSessionCategoryPlayback error:nil];+ [AVAudioSession.sharedInstance setActive:YES error:nil];+ converter=make(kAudioUnitType_FormatConverter,kAudioUnitSubType_AUConverter);+ mixer=make(kAudioUnitType_Mixer,kAudioUnitSubType_MultiChannelMixer);+ output=make(kAudioUnitType_Output,kAudioUnitSubType_RemoteIO);+ AURenderCallbackStruct callback={original,NULL};+ check(AudioUnitSetProperty(converter,kAudioUnitProperty_SetRenderCallback,kAudioUnitScope_Input,0,&callback,sizeof callback),"native input callback");+ AudioStreamBasicDescription format={44100,kAudioFormatLinearPCM,kAudioFormatFlagsNativeFloatPacked|kAudioFormatFlagIsNonInterleaved,4,1,4,2,32,0};+ AudioUnit units[]={converter,mixer,output};+ for(int i=0;i<3;++i) {+ check(AudioUnitSetProperty(units[i],kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,&format,sizeof format),"input format");+ if(i<2) check(AudioUnitSetProperty(units[i],kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output,0,&format,sizeof format),"output format");+ UInt32 slice=4096;+ check(AudioUnitSetProperty(units[i],kAudioUnitProperty_MaximumFramesPerSlice,kAudioUnitScope_Global,0,&slice,sizeof slice),"slice");+ }+ AudioUnitConnection a={converter,0,0}, b={mixer,0,0};+ check(AudioUnitSetProperty(mixer,kAudioUnitProperty_MakeConnection,kAudioUnitScope_Input,0,&a,sizeof a),"connect converter");+ check(AudioUnitSetProperty(output,kAudioUnitProperty_MakeConnection,kAudioUnitScope_Input,0,&b,sizeof b),"connect mixer");+ for(int i=0;i<3;++i) check(AudioUnitInitialize(units[i]),"initialize");+ check(AudioOutputUnitStart(output),"start");+}+static void later(double seconds, dispatch_block_t action) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(seconds*NSEC_PER_SEC)),dispatch_get_main_queue(),action);+}+static void directTests(void) {+ NSError *error=nil;+ NSURL *url=[NSBundle.mainBundle.bundleURL URLByAppendingPathComponent:@"fixture.flac"];+ SGPCMDecodeCancellation *token=[SGPCMDecodeCancellation new];+ asset=[SGPreparedPCM prepareURL:url sampleRate:44100 maxBytes:8*1024*1024 cancellation:token error:&error];+ expect(asset && !error && asset.frameCount==529200,"native FLAC -> complete planar PCM");+ if(!asset) exit(1);+ double maxError=0;+ for(uint64_t i=0;i<asset.frameCount;++i) {+ maxError=fmax(maxError,fabs(asset.left[i]-0.08*sin(2*M_PI*440*i/44100.0)));+ maxError=fmax(maxError,fabs(asset.right[i]-0.06*sin(2*M_PI*997*i/44100.0)));+ }+ expect(maxError<0.00004,"decoded stereo waveform matches generated recording");+ SGPreparedPCM *exact=[SGPreparedPCM prepareURL:url sampleRate:44100 maxBytes:529200*8 cancellation:token error:&error];+ expect(exact && !error && exact.frameCount==529200,"a file exactly filling its PCM budget is accepted");+ SGPreparedPCM *alac=[SGPreparedPCM prepareURL:[NSBundle.mainBundle.bundleURL URLByAppendingPathComponent:@"fixture.m4a"]+ sampleRate:48000 maxBytes:8*1024*1024 cancellation:token error:&error];+ expect(alac && !error && alac.frameCount==576000,"ALAC converts to planar 48 kHz");+ expect(![SGPreparedPCM prepareURL:url sampleRate:44100 maxBytes:8192 cancellation:token error:&error],"PCM budget rejects oversized decode");+ [token cancel];+ expect(![SGPreparedPCM prepareURL:url sampleRate:44100 maxBytes:8*1024*1024 cancellation:token error:&error]+ && error.code==1,"cancelled preparation returns no asset");+ token=[SGPCMDecodeCancellation new];+ expect(![SGPreparedPCM prepareURL:[NSURL URLWithString:@"https://invalid.example/audio.flac"] sampleRate:44100 maxBytes:8192 cancellation:token error:&error],"decoder rejects network URL");+ SGPCMReplacementEnable();+ AudioUnit fake=(AudioUnit)(uintptr_t)0x1234;+ uint64_t epoch=SGPlayerSourceEpoch();+ expect(SGPCMReplacementInstall(asset,fake,epoch,100,200),"publish source/asset anchor");+ float l[256],r[256];+ struct {AudioBufferList list;AudioBuffer second;} buffers={{2,{{1,sizeof l,l}}},{1,sizeof r,r}};+ for(int i=0;i<256;++i) l[i]=r[i]=0.02;+ SGPCMReplacementApply(fake,epoch,110,44100,256,&buffers.list);+ expect(!memcmp(l,asset.left+210,sizeof l) && !memcmp(r,asset.right+210,sizeof r),"frame-aligned replacement, both channels");+ for(int test=0;test<7;++test) {+ for(int i=0;i<256;++i) l[i]=r[i]=0.02;+ uint64_t e=epoch; double frame=110,rate=44100; AudioUnit unit=fake;+ if(test==0) e--;+ if(test==1) unit=NULL;+ if(test==2) rate=48000;+ if(test==3) frame=NAN;+ if(test==4) frame=99;+ if(test==5) frame=asset.frameCount-200+100-128;+ if(test==6) buffers.second.mDataByteSize=4;+ SGPCMReplacementApply(unit,e,frame,rate,256,&buffers.list);+ BOOL untouched=YES;+ for(int i=0;i<256;++i) untouched &= l[i]==(float)0.02 && r[i]==(float)0.02;+ expect(untouched,"invalid generation/output/rate/anchor/EOF/layout preserves native span");+ buffers.second.mDataByteSize=sizeof r;+ }+ SGPCMReplacementCancel();+ expect(!SGPCMReplacementInstall(asset,fake,epoch,0,0),"late publication rejected after cancellation");+ __weak SGPreparedPCM *weakAsset;+ @autoreleasepool {+ SGPreparedPCM *temporary=[SGPreparedPCM prepareURL:url sampleRate:44100 maxBytes:8*1024*1024 cancellation:token error:&error];+ weakAsset=temporary;+ expect(SGPCMReplacementInstall(temporary,fake,SGPlayerSourceEpoch(),0,0),"publish independently owned PCM");+ }+ expect(weakAsset!=nil,"published snapshot retains decoded storage");+ SGPCMReplacementCancel();+ expect(weakAsset==nil,"cancel releases storage after readers leave");+ // Race live readers against replacement, cancellation and reclamation; every returned block must+ // be wholly native or wholly one of two valid asset spans, never torn between generations.+ dispatch_group_t group=dispatch_group_create();+ __block atomic_int torn; atomic_init(&torn,0);+ dispatch_group_async(group,dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{+ float a[256],b[256];+ struct {AudioBufferList list;AudioBuffer second;} block={{2,{{1,sizeof a,a}}},{1,sizeof b,b}};+ for(int i=0;i<20000;++i) {+ for(int j=0;j<256;++j) a[j]=b[j]=0.02;+ SGPCMReplacementApply(fake,SGPlayerSourceEpoch(),0,44100,256,&block.list);+ BOOL native=YES;+ for(int j=0;j<256;++j) native &= a[j]==(float)0.02 && b[j]==(float)0.02;+ BOOL first=!memcmp(a,asset.left,sizeof a)&&!memcmp(b,asset.right,sizeof b);+ BOOL second=!memcmp(a,asset.left+500,sizeof a)&&!memcmp(b,asset.right+500,sizeof b);+ if(!native&&!first&&!second) atomic_fetch_add(&torn,1);+ }+ });+ for(int i=0;i<2000;++i) {+ if(i%3==0) SGPCMReplacementCancel();+ SGPCMReplacementInstall(asset,fake,SGPlayerSourceEpoch(),0,i%2?500:0);+ }+ dispatch_group_wait(group,DISPATCH_TIME_FOREVER);+ expect(atomic_load(&torn)==0,"concurrent publication/cancel keeps whole snapshots");+ SGPCMReplacementCancel();+}+static void realTests(void) {+ SGPlayerSetSourceSelector(selector);+ setup();+ later(0.5,^{+ expect(SGPlayerSpeedAllowed() && SGPlayerMusicOutput()==output,"real mixer-to-RemoteIO seam reached");+ expect(SGPCMReplacementInstall(asset,output,SGPlayerSourceEpoch(),0,0),"prepare real output source");+ });+ later(1.2,^{+ expect(atomic_load(&selected)>0,"replacement runs with no Sing stage registered");+ SGPlayerSetStage(stage);+ });+ later(2.0,^{+ expect(atomic_load(&stageFrames)>0,"existing stage receives replacement PCM");+ expect(atomic_load(&badSamples)==0,"source waveform and native advancement agree");+ SGSetPlayerSpeed(1.5);+ });+ later(2.8,^{+ expect(atomic_load(&badSamples)==0,"speed processing pulls the same source before the stage");+ SGSetPlayerSpeed(1);+ SGPCMReplacementCancel();+ later(0.1,^{+ uint64_t at=atomic_load(&selected);+ later(0.15,^{expect(atomic_load(&selected)==at,"cancelled source stops replacement");});+ });+ });+ later(3.3,^{+ uint64_t epoch=SGPlayerSourceEpoch();+ expect(SGPCMReplacementInstall(asset,output,epoch,0,0),"re-arm current occurrence");+ SGPlayerInvalidateSource(); // the playback owner must do this on seek/skip, even on the same URI+ expect(!SGPCMReplacementInstall(asset,output,epoch,0,0),"seek/repeat generation rejects late preparation");+ });+ later(3.8,^{+ SGPCMReplacementCancel();+ check(AudioOutputUnitStop(output),"pause original output");+ uint64_t d=atomic_load(&decoded);+ later(0.2,^{expect(atomic_load(&decoded)==d,"pause stops native decoder advancement");});+ });+ later(4.2,^{+ check(AudioOutputUnitStart(output),"resume original output");+ expect(SGPCMReplacementInstall(asset,output,SGPlayerSourceEpoch(),0,0),"re-arm after output restart");+ stallEpoch=SGPlayerSourceEpoch();+ atomic_store(&mute,true);+ });+ later(4.6,^{+ uint64_t at=atomic_load(&selected);+ expect(SGPlayerSourceEpoch()!=stallEpoch,"native stall invalidates the recording/frame anchor");+ later(0.2,^{+ expect(atomic_load(&selected)==at,"native silent/stalled spans cannot be replaced");+ stallSelected=atomic_load(&selected);+ atomic_store(&mute,false);+ });+ });+ later(5.0,^{+ expect(atomic_load(&selected)==stallSelected,"native recovery cannot reuse the pre-stall anchor");+ uint64_t epoch=SGPlayerSourceEpoch();+ AudioStreamBasicDescription format={44100,kAudioFormatLinearPCM,kAudioFormatFlagsNativeFloatPacked|kAudioFormatFlagIsNonInterleaved,4,1,4,2,32,0};+ check(AudioUnitSetProperty(output,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,&format,sizeof format),"format notification");+ expect(SGPlayerSourceEpoch()!=epoch,"format notification invalidates prepared source");+ });+ later(5.5,^{+ uint64_t epoch=SGPlayerSourceEpoch();+ check(AudioComponentInstanceDispose(mixer),"dispose mixer during running output"); mixer=NULL;+ expect(SGPlayerSourceEpoch()!=epoch,"mixer disposal invalidates source");+ });+ later(6.0,^{+ SGPCMReplacementCancel(); SGPlayerSetStage(NULL); SGPlayerSetSourceSelector(NULL);+ AudioOutputUnitStop(output); AudioComponentInstanceDispose(output); AudioComponentInstanceDispose(converter);+ printf("RESULT failures=%d nativeFrames=%llu replacementFrames=%llu stageFrames=%llu\n",failures,+ (unsigned long long)atomic_load(&decoded),(unsigned long long)atomic_load(&selected),+ (unsigned long long)atomic_load(&stageFrames)); fflush(stdout); exit(failures?1:0);+ });+}+@interface LosslessHandoff : UIResponder <UIApplicationDelegate>+@property(nonatomic,strong) UIWindow *window;+@end+@implementation LosslessHandoff+- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options {+ self.window=[[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];+ self.window.rootViewController=[UIViewController new];+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{+ directTests(); dispatch_async(dispatch_get_main_queue(),^{realTests();});+ });+ later(30,^{printf("FAIL watchdog\n"); fflush(stdout); exit(124);});+ return YES;+}+@end+int main(int argc,char **argv) { @autoreleasepool{return UIApplicationMain(argc,argv,nil,@"LosslessHandoff");} }diff --git a/harness/lossless/main.m b/harness/lossless/main.mnew file mode 100644index 00000000..279cb5c5--- /dev/null+++ b/harness/lossless/main.m@@ -0,0 +1,218 @@+// Native decoder capability probe. Generated fixtures only; no Spotify hooks or credentials.+#import <UIKit/UIKit.h>+#import <AVFoundation/AVFoundation.h>+#import <AudioToolbox/AudioToolbox.h>+#include <math.h>++static void report(NSDictionary *row) {+ NSData *json = [NSJSONSerialization dataWithJSONObject:row options:NSJSONWritingSortedKeys error:nil];+ printf("LOSSLESS %s\n", [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding].UTF8String);+ fflush(stdout);+}++typedef struct {+ BOOL ready, described; UInt64 packets, bytes; AudioStreamBasicDescription format;+ __unsafe_unretained NSMutableData *payload, *descriptions;+ NSUInteger cursor; AudioStreamPacketDescription current;+} ParseState;+static void property(void *context, AudioFileStreamID stream, AudioFileStreamPropertyID prop, UInt32 *flags) {+ ParseState *s = context;+ if (prop == kAudioFileStreamProperty_ReadyToProducePackets) s->ready = YES;+ if (prop == kAudioFileStreamProperty_DataFormat) {+ UInt32 size = sizeof(s->format);+ AudioFileStreamGetProperty(stream, prop, &size, &s->format);+ }+}+static void packets(void *context, UInt32 bytes, UInt32 count, const void *data,+ AudioStreamPacketDescription *descriptions) {+ ParseState *s = context; s->packets += count; s->bytes += bytes;+ NSUInteger base = s->payload.length;+ [s->payload appendBytes:data length:bytes];+ for (UInt32 i=0; i<count; ++i) {+ AudioStreamPacketDescription description;+ if (descriptions) description = descriptions[i];+ else if (s->format.mBytesPerPacket)+ description = (AudioStreamPacketDescription){i*s->format.mBytesPerPacket, 0, s->format.mBytesPerPacket};+ else { s->described = NO; return; }+ description.mStartOffset += base;+ [s->descriptions appendBytes:&description length:sizeof(description)];+ }+}+static OSStatus converterInput(AudioConverterRef converter, UInt32 *count, AudioBufferList *data,+ AudioStreamPacketDescription **descriptions, void *context) {+ ParseState *s = context;+ NSUInteger total = s->descriptions.length / sizeof(AudioStreamPacketDescription);+ if (s->cursor == total) { *count = 0; return noErr; }+ s->current = ((const AudioStreamPacketDescription *)s->descriptions.bytes)[s->cursor++];+ data->mNumberBuffers = 1;+ data->mBuffers[0] = (AudioBuffer){s->format.mChannelsPerFrame, s->current.mDataByteSize,+ (char *)s->payload.bytes + s->current.mStartOffset};+ s->current.mStartOffset = 0;+ *count = 1;+ if (descriptions) *descriptions = &s->current;+ return noErr;+}+static void convertPackets(ParseState *s, AudioFileStreamID stream, NSString *name) {+ AudioStreamBasicDescription target = {.mSampleRate=s->format.mSampleRate, .mFormatID=kAudioFormatLinearPCM,+ .mFormatFlags=kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked, .mBytesPerPacket=8,+ .mFramesPerPacket=1, .mBytesPerFrame=8, .mChannelsPerFrame=2, .mBitsPerChannel=32};+ AudioConverterRef converter = NULL;+ OSStatus status = AudioConverterNew(&s->format, &target, &converter);+ UInt32 size = 0; Boolean writable = false; OSStatus cookieStatus = 0;+ if (!status && !AudioFileStreamGetPropertyInfo(stream, kAudioFileStreamProperty_MagicCookieData, &size, &writable) && size) {+ NSMutableData *cookie = [NSMutableData dataWithLength:size];+ cookieStatus = AudioFileStreamGetProperty(stream, kAudioFileStreamProperty_MagicCookieData, &size, cookie.mutableBytes);+ if (!cookieStatus) cookieStatus = AudioConverterSetProperty(converter, kAudioConverterDecompressionMagicCookie, size, cookie.bytes);+ status = cookieStatus;+ }+ UInt64 total = 0; double maxError = 0; float samples[4096*2];+ while (!status) {+ UInt32 count = 4096;+ AudioBufferList list = {.mNumberBuffers=1, .mBuffers={{2,sizeof(samples),samples}}};+ status = AudioConverterFillComplexBuffer(converter, converterInput, s, &count, &list, NULL);+ if (status || !count) break;+ for (UInt32 i=0; i<count; ++i) {+ double t = (total+i)/target.mSampleRate;+ maxError = fmax(maxError, fabs(samples[2*i]-0.08*sin(2*M_PI*440*t)));+ maxError = fmax(maxError, fabs(samples[2*i+1]-0.06*sin(2*M_PI*997*t)));+ }+ total += count;+ }+ if (converter) AudioConverterDispose(converter);+ report(@{@"test":@"parsed-packet-decode", @"file":name, @"status":@(status),+ @"cookieStatus":@(cookieStatus), @"frames":@(total), @"maxError":@(maxError)});+}+static void parse(NSURL *url) {+ NSData *data = [NSData dataWithContentsOfURL:url];+ __attribute__((objc_precise_lifetime)) NSMutableData *payload = [NSMutableData data], *descriptions = [NSMutableData data];+ ParseState state = {.described=YES, .payload=payload, .descriptions=descriptions}; AudioFileStreamID stream = NULL;+ AudioFileTypeID hint = [url.pathExtension isEqualToString:@"flac"] ? kAudioFileFLACType : kAudioFileM4AType;+ OSStatus status = data ? AudioFileStreamOpen(&state, property, packets, hint, &stream) : -1;+ NSUInteger consumed = 0;+ while (!status && consumed < data.length) {+ UInt32 count = (UInt32)MIN(257, data.length - consumed);+ status = AudioFileStreamParseBytes(stream, count, (const char *)data.bytes + consumed, 0);+ consumed += count;+ }+ report(@{@"test": @"incremental-parser", @"file": url.lastPathComponent, @"status": @(status),+ @"ready": @(state.ready), @"packets": @(state.packets), @"inputBytes": @(consumed),+ @"packetBytes": @(state.bytes), @"rate": @(state.format.mSampleRate),+ @"formatID": @(state.format.mFormatID), @"framesPerPacket": @(state.format.mFramesPerPacket),+ @"bits": @(state.format.mBitsPerChannel), @"channels": @(state.format.mChannelsPerFrame)});+ if (!status && state.ready && state.described && state.packets) convertPackets(&state, stream, url.lastPathComponent);+ if (stream) AudioFileStreamClose(stream);+}++static void decode(NSURL *url, double requestedRate) {+ ExtAudioFileRef file = NULL;+ OSStatus status = ExtAudioFileOpenURL((__bridge CFURLRef)url, &file);+ AudioStreamBasicDescription source = {0}; UInt32 size = sizeof(source);+ if (!status) status = ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileDataFormat, &size, &source);+ double rate = requestedRate ?: source.mSampleRate;+ AudioStreamBasicDescription target = {.mSampleRate=rate, .mFormatID=kAudioFormatLinearPCM,+ .mFormatFlags=kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked, .mBytesPerPacket=8,+ .mFramesPerPacket=1, .mBytesPerFrame=8, .mChannelsPerFrame=2, .mBitsPerChannel=32};+ if (!status) status = ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, sizeof(target), &target);+ float samples[4096 * 2]; UInt64 total = 0; double energy = 0;+ CFAbsoluteTime started = CFAbsoluteTimeGetCurrent();+ while (!status) {+ AudioBufferList list = {.mNumberBuffers=1, .mBuffers={{2, sizeof(samples), samples}}};+ UInt32 frames = 4096; status = ExtAudioFileRead(file, &frames, &list);+ if (status || !frames) break;+ total += frames;+ for (UInt32 i=0; i<frames*2; ++i) energy += samples[i] * samples[i];+ }+ double elapsed = CFAbsoluteTimeGetCurrent() - started;+ // Seek in FILE frames, even when the client rate differs. Check the native-rate decoded waveform.+ SInt64 seekFrame = (SInt64)(3.125 * source.mSampleRate);+ OSStatus seekStatus = status ?: ExtAudioFileSeek(file, seekFrame);+ UInt32 seekFrames = 1024; double maxError = 0;+ if (!seekStatus) {+ AudioBufferList list = {.mNumberBuffers=1, .mBuffers={{2, sizeof(samples), samples}}};+ seekStatus = ExtAudioFileRead(file, &seekFrames, &list);+ if (rate == source.mSampleRate) for (UInt32 i=0; i<seekFrames; ++i) {+ double t = (seekFrame + i) / source.mSampleRate;+ maxError = fmax(maxError, fabs(samples[2*i] - 0.08*sin(2*M_PI*440*t)));+ maxError = fmax(maxError, fabs(samples[2*i+1] - 0.06*sin(2*M_PI*997*t)));+ }+ }+ if (file) ExtAudioFileDispose(file);+ report(@{@"test": @"local-decode-seek", @"file": url.lastPathComponent, @"status": @(status),+ @"sourceRate": @(source.mSampleRate), @"sourceBits": @(source.mBitsPerChannel),+ @"clientRate": @(rate), @"frames": @(total), @"energy": @(energy), @"decodeSeconds": @(elapsed),+ @"seekStatus": @(seekStatus), @"seekFrames": @(seekFrames),+ @"seekMaxError": rate == source.mSampleRate ? @(maxError) : NSNull.null});+}++static void reader(NSURL *url) {+ // This runs on a worker, including asynchronous asset loading; no render thread is involved.+ AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];+ dispatch_semaphore_t loaded = dispatch_semaphore_create(0);+ [asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{ dispatch_semaphore_signal(loaded); }];+ if (dispatch_semaphore_wait(loaded, dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_SEC))) {+ [asset cancelLoading]; report(@{@"test":@"asset-reader", @"file":url.lastPathComponent,+ @"remote":@(!url.isFileURL), @"error":@"load-timeout"}); return;+ }+ NSError *error = nil;+ AVKeyValueStatus loadStatus = [asset statusOfValueForKey:@"tracks" error:&error];+ AVAssetTrack *track = loadStatus == AVKeyValueStatusLoaded ? [asset tracksWithMediaType:AVMediaTypeAudio].firstObject : nil;+ AVAssetReader *reader = track ? [[AVAssetReader alloc] initWithAsset:asset error:&error] : nil;+ AVAssetReaderTrackOutput *output = nil;+ if (reader) {+ output = [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:@{+ AVFormatIDKey:@(kAudioFormatLinearPCM), AVLinearPCMIsFloatKey:@YES, AVLinearPCMBitDepthKey:@32,+ AVLinearPCMIsNonInterleaved:@NO}];+ if ([reader canAddOutput:output]) [reader addOutput:output];+ else { [reader cancelReading]; reader = nil; }+ }+ BOOL started = NO; UInt64 frames = 0; double firstPTS = -1, sampleRate = 0;+ if (reader) {+ reader.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(3.125, 1000000), CMTimeMakeWithSeconds(1, 1000000));+ started = [reader startReading];+ if (started) {+ CMSampleBufferRef sample;+ while ((sample = [output copyNextSampleBuffer])) {+ if (firstPTS < 0) firstPTS = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sample));+ frames += CMSampleBufferGetNumSamples(sample);+ const AudioStreamBasicDescription *format = CMAudioFormatDescriptionGetStreamBasicDescription(CMSampleBufferGetFormatDescription(sample));+ if (format) sampleRate = format->mSampleRate;+ CFRelease(sample);+ }+ }+ error = reader.error ?: error;+ }+ report(@{@"test":@"asset-reader", @"file":url.lastPathComponent, @"remote":@(!url.isFileURL),+ @"loadStatus":@(loadStatus), @"started":@(started), @"readerStatus":@(reader.status),+ @"frames":@(frames), @"rate":@(sampleRate), @"firstPTS":@(firstPTS),+ @"errorDomain": error.domain ?: @"", @"errorCode":@(error.code),+ @"error":error.localizedDescription ?: @""});+}++@interface LosslessProbe : UIResponder <UIApplicationDelegate>+@property(nonatomic,strong) UIWindow *window;+@end+@implementation LosslessProbe+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {+ self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];+ self.window.rootViewController = [UIViewController new];+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ @autoreleasepool {+ report(@{@"test":@"environment", @"os":UIDevice.currentDevice.systemVersion,+ @"scope":@"simulator native decoding only; no Spotify playback"});+ NSString *base = NSProcessInfo.processInfo.environment[@"LOSSLESS_HTTP_BASE"];+ for (NSString *name in @[@"cd.flac", @"hires.flac", @"alac.m4a", @"alac-fast.m4a"]) {+ NSURL *url = [NSBundle.mainBundle.bundleURL URLByAppendingPathComponent:name];+ parse(url); decode(url, 0); decode(url, 44100); reader(url);+ if (base.length) reader([[NSURL URLWithString:base] URLByAppendingPathComponent:name]);+ }+ report(@{@"test":@"complete"});+ exit(0);+ }+ });+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 60*NSEC_PER_SEC), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ report(@{@"test":@"watchdog-timeout"}); exit(124);+ });+ return YES;+}+@end+int main(int argc, char **argv) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, @"LosslessProbe"); } }diff --git a/harness/lossless/results/handoff-ios27.txt b/harness/lossless/results/handoff-ios27.txtnew file mode 100644index 00000000..cdd879f9--- /dev/null+++ b/harness/lossless/results/handoff-ios27.txt@@ -0,0 +1,61 @@+iOS 27.0 arm64 simulator; generated local audio; stand-in stage, not actual Sing+PASS native FLAC -> complete planar PCM+PASS decoded stereo waveform matches generated recording+PASS a file exactly filling its PCM budget is accepted+PASS ALAC converts to planar 48 kHz+PASS PCM budget rejects oversized decode+PASS cancelled preparation returns no asset+PASS decoder rejects network URL+PASS publish source/asset anchor+PASS frame-aligned replacement, both channels+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS invalid generation/output/rate/anchor/EOF/layout preserves native span+PASS late publication rejected after cancellation+PASS publish independently owned PCM+PASS published snapshot retains decoded storage+PASS cancel releases storage after readers leave+PASS concurrent publication/cancel keeps whole snapshots+PASS create audio unit+PASS create audio unit+PASS create audio unit+PASS native input callback+PASS input format+PASS output format+PASS slice+PASS input format+PASS output format+PASS slice+PASS input format+PASS slice+PASS connect converter+PASS connect mixer+PASS initialize+PASS initialize+PASS initialize+PASS start+PASS real mixer-to-RemoteIO seam reached+PASS prepare real output source+PASS replacement runs with no Sing stage registered+PASS existing stage receives replacement PCM+PASS source waveform and native advancement agree+PASS speed processing pulls the same source before the stage+PASS cancelled source stops replacement+PASS re-arm current occurrence+PASS seek/repeat generation rejects late preparation+PASS pause original output+PASS pause stops native decoder advancement+PASS resume original output+PASS re-arm after output restart+PASS native stall invalidates the recording/frame anchor+PASS native silent/stalled spans cannot be replaced+PASS native recovery cannot reuse the pre-stall anchor+PASS format notification+PASS format notification invalidates prepared source+PASS dispose mixer during running output+PASS mixer disposal invalidates source+RESULT failures=0 nativeFrames=239905 replacementFrames=119012 stageFrames=87965diff --git a/harness/lossless/results/ios27-simulator.jsonl b/harness/lossless/results/ios27-simulator.jsonlnew file mode 100644index 00000000..3e2909da--- /dev/null+++ b/harness/lossless/results/ios27-simulator.jsonl@@ -0,0 +1,25 @@+{"os":"27.0","scope":"simulator native decoding only; no Spotify playback","test":"environment"}+{"bits":0,"channels":2,"file":"cd.flac","formatID":1718378851,"framesPerPacket":1024,"inputBytes":350832,"packetBytes":342072,"packets":516,"rate":44100,"ready":true,"status":0,"test":"incremental-parser"}+{"cookieStatus":0,"file":"cd.flac","frames":528384,"maxError":1.5255911489695016e-05,"status":0,"test":"parsed-packet-decode"}+{"clientRate":44100,"decodeSeconds":0.0094369649887084961,"energy":2645.9880860298872,"file":"cd.flac","frames":529200,"seekFrames":1024,"seekMaxError":1.5246675121477912e-05,"seekStatus":0,"sourceBits":0,"sourceRate":44100,"status":0,"test":"local-decode-seek"}+{"clientRate":44100,"decodeSeconds":0.009144902229309082,"energy":2645.9880860298872,"file":"cd.flac","frames":529200,"seekFrames":1024,"seekMaxError":1.5246675121477912e-05,"seekStatus":0,"sourceBits":0,"sourceRate":44100,"status":0,"test":"local-decode-seek"}+{"error":"","errorCode":0,"errorDomain":"","file":"cd.flac","firstPTS":3.1249886621315195,"frames":44101,"loadStatus":2,"rate":44100,"readerStatus":2,"remote":0,"started":true,"test":"asset-reader"}+{"error":"Operation Stopped","errorCode":-11838,"errorDomain":"AVFoundationErrorDomain","file":"cd.flac","firstPTS":-1,"frames":0,"loadStatus":2,"rate":0,"readerStatus":0,"remote":1,"started":false,"test":"asset-reader"}+{"bits":0,"channels":2,"file":"hires.flac","formatID":1718378851,"framesPerPacket":1024,"inputBytes":2016400,"packetBytes":2006388,"packets":1124,"rate":96000,"ready":true,"status":0,"test":"incremental-parser"}+{"cookieStatus":0,"file":"hires.flac","frames":1150976,"maxError":1.1897570397146673e-07,"status":0,"test":"parsed-packet-decode"}+{"clientRate":96000,"decodeSeconds":0.02408301830291748,"energy":5759.9999065709244,"file":"hires.flac","frames":1152000,"seekFrames":1024,"seekMaxError":1.1895324905609224e-07,"seekStatus":0,"sourceBits":0,"sourceRate":96000,"status":0,"test":"local-decode-seek"}+{"clientRate":44100,"decodeSeconds":0.024996042251586914,"energy":2645.8942000093202,"file":"hires.flac","frames":529200,"seekFrames":1024,"seekMaxError":null,"seekStatus":0,"sourceBits":0,"sourceRate":96000,"status":0,"test":"local-decode-seek"}+{"error":"","errorCode":0,"errorDomain":"","file":"hires.flac","firstPTS":3.125,"frames":96000,"loadStatus":2,"rate":96000,"readerStatus":2,"remote":0,"started":true,"test":"asset-reader"}+{"error":"Operation Stopped","errorCode":-11838,"errorDomain":"AVFoundationErrorDomain","file":"hires.flac","firstPTS":-1,"frames":0,"loadStatus":2,"rate":0,"readerStatus":0,"remote":1,"started":false,"test":"asset-reader"}+{"bits":0,"channels":0,"file":"alac.m4a","formatID":0,"framesPerPacket":0,"inputBytes":257,"packetBytes":0,"packets":0,"rate":0,"ready":false,"status":1869640813,"test":"incremental-parser"}+{"clientRate":48000,"decodeSeconds":0.018272042274475098,"energy":2879.9999679101998,"file":"alac.m4a","frames":576000,"seekFrames":1024,"seekMaxError":1.189455438024583e-07,"seekStatus":0,"sourceBits":0,"sourceRate":48000,"status":0,"test":"local-decode-seek"}+{"clientRate":44100,"decodeSeconds":0.016273975372314453,"energy":2646.0126319793831,"file":"alac.m4a","frames":529200,"seekFrames":1024,"seekMaxError":null,"seekStatus":0,"sourceBits":0,"sourceRate":48000,"status":0,"test":"local-decode-seek"}+{"error":"","errorCode":0,"errorDomain":"","file":"alac.m4a","firstPTS":3.125,"frames":48000,"loadStatus":2,"rate":48000,"readerStatus":2,"remote":0,"started":true,"test":"asset-reader"}+{"error":"Operation Stopped","errorCode":-11838,"errorDomain":"AVFoundationErrorDomain","file":"alac.m4a","firstPTS":-1,"frames":0,"loadStatus":2,"rate":0,"readerStatus":0,"remote":1,"started":false,"test":"asset-reader"}+{"bits":0,"channels":2,"file":"alac-fast.m4a","formatID":1634492771,"framesPerPacket":4096,"inputBytes":1591403,"packetBytes":1590089,"packets":141,"rate":48000,"ready":true,"status":0,"test":"incremental-parser"}+{"cookieStatus":0,"file":"alac-fast.m4a","frames":576000,"maxError":1.1897183664903324e-07,"status":0,"test":"parsed-packet-decode"}+{"clientRate":48000,"decodeSeconds":0.014691948890686035,"energy":2879.9999679101998,"file":"alac-fast.m4a","frames":576000,"seekFrames":1024,"seekMaxError":1.189455438024583e-07,"seekStatus":0,"sourceBits":0,"sourceRate":48000,"status":0,"test":"local-decode-seek"}+{"clientRate":44100,"decodeSeconds":0.016265988349914551,"energy":2646.0126319793831,"file":"alac-fast.m4a","frames":529200,"seekFrames":1024,"seekMaxError":null,"seekStatus":0,"sourceBits":0,"sourceRate":48000,"status":0,"test":"local-decode-seek"}+{"error":"","errorCode":0,"errorDomain":"","file":"alac-fast.m4a","firstPTS":3.125,"frames":48000,"loadStatus":2,"rate":48000,"readerStatus":2,"remote":0,"started":true,"test":"asset-reader"}+{"error":"Operation Stopped","errorCode":-11838,"errorDomain":"AVFoundationErrorDomain","file":"alac-fast.m4a","firstPTS":-1,"frames":0,"loadStatus":2,"rate":0,"readerStatus":0,"remote":1,"started":false,"test":"asset-reader"}+{"test":"complete"}diff --git a/harness/lossless/results/speed-two-ios27.txt b/harness/lossless/results/speed-two-ios27.txtnew file mode 100644index 00000000..8300ef0c--- /dev/null+++ b/harness/lossless/results/speed-two-ios27.txt@@ -0,0 +1,14 @@+iOS 27.0 arm64 simulator; source selector unregistered+PASS one chain, normal 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 0.00x (want 0.00); the processors on 44.1+PASS 48 kHz chain started too, normal 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 1.00x (want 1.00); the processors on 48+PASS 1.5x: only the 48 kHz chain 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 1.50x (want 1.50); the processors on 48+PASS 48 kHz stopped: 1.5x moves to 44.1 44.1 kHz chain drained 1.50x (want 1.50), 48 kHz chain 0.00x (want 0.00); the processors on 44.1+PASS 48 kHz started again: back to it 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 1.50x (want 1.50); the processors on 48+PASS a callback-fed unit started: 1.5x stays 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 1.50x (want 1.50); the processors on 48+PASS 48 kHz silent for a while: 1.5x to 44.1 44.1 kHz chain drained 1.50x (want 1.50), 48 kHz chain 1.00x (want 1.00); the processors on 44.1+PASS 48 kHz sound again: stays on 44.1 44.1 kHz chain drained 1.50x (want 1.50), 48 kHz chain 1.00x (want 1.00); the processors on 44.1+PASS 48 kHz mixer disposed: 44.1 unchanged 44.1 kHz chain drained 1.50x (want 1.50), 48 kHz chain 0.00x (want 0.00); the processors on 44.1+PASS 44.1 stopped, 48 output disposed 44.1 kHz chain drained 0.00x (want 0.00), 48 kHz chain 0.00x (want 0.00); the processors on neither+PASS 44.1 started again, still 1.5x 44.1 kHz chain drained 1.50x (want 1.50), 48 kHz chain 0.00x (want 0.00); the processors on 44.1+PASS back to normal 44.1 kHz chain drained 1.00x (want 1.00), 48 kHz chain 0.00x (want 0.00); the processors on 44.1+two chains: 0 failuresdiff --git a/harness/lossless/serve.py b/harness/lossless/serve.pynew file mode 100644index 00000000..4442180d--- /dev/null+++ b/harness/lossless/serve.py@@ -0,0 +1,73 @@+#!/usr/bin/env python3+"""Loopback-only generated-media fixture with byte ranges. No external media or credentials."""+import argparse+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from pathlib import Path+import re++parser = argparse.ArgumentParser()+parser.add_argument("directory", type=Path)+parser.add_argument("--port", type=int, default=18764)+args = parser.parse_args()+types = {"cd.flac": "audio/flac", "hires.flac": "audio/flac",+ "alac.m4a": "audio/mp4", "alac-fast.m4a": "audio/mp4"}+++class Media(BaseHTTPRequestHandler):+ def do_HEAD(self):+ self.media(False)++ def do_GET(self):+ self.media(True)++ def media(self, body):+ name = self.path.removeprefix("/")+ if name not in types or not (args.directory / name).is_file():+ self.send_error(404)+ return+ path = args.directory / name+ length = path.stat().st_size+ start, end, status = 0, length - 1, 200+ requested = self.headers.get("Range")+ if requested:+ match = re.fullmatch(r"bytes=(\d*)-(\d*)", requested)+ valid = match and any(match.groups())+ if valid:+ left, right = match.groups()+ if left:+ start = int(left)+ end = min(int(right), length - 1) if right else length - 1+ else:+ start = max(0, length - int(right))+ valid = 0 <= start <= end < length+ if not valid:+ self.send_response(416)+ self.send_header("Content-Range", f"bytes */{length}")+ self.send_header("Content-Length", "0")+ self.end_headers()+ return+ status = 206+ self.send_response(status)+ self.send_header("Content-Type", types[name])+ self.send_header("Accept-Ranges", "bytes")+ self.send_header("Content-Length", str(end - start + 1))+ if status == 206:+ self.send_header("Content-Range", f"bytes {start}-{end}/{length}")+ self.end_headers()+ if body:+ try:+ with path.open("rb") as stream:+ stream.seek(start)+ remaining = end - start + 1+ while remaining:+ block = stream.read(min(65536, remaining))+ if not block:+ break+ self.wfile.write(block)+ remaining -= len(block)+ except (BrokenPipeError, ConnectionResetError):+ pass+++print(f"Generated fixtures at http://localhost:{args.port}/", flush=True)+ThreadingHTTPServer(("127.0.0.1", args.port), Media).serve_forever()diff --git a/scripts/phone.py b/scripts/phone.pyindex 24e17a76..fac84d2b 100755--- a/scripts/phone.py+++ b/scripts/phone.py@@ -14,6 +14,9 @@scripts/phone.py menu.pick "Sleep timer"scripts/phone.py play | pause | nextscripts/phone.py seek 42 | seek --by -15 # to a position, or by an amount as Spotify's relative seeks do+ scripts/phone.py speed 1.5 # playback speed, 1 when normal+ scripts/phone.py sing 1 # Karaoke on (0 off)+ scripts/phone.py lossless anchor --path P | lossless check | lossless stop # a local FLAC in place of Spotify's soundscripts/phone.py tab 0 | tab Searchscripts/phone.py settings.openscripts/phone.py settings.page "Appearance"@@ -67,7 +70,7 @@ def token():sys.exit(f"no PHONE_TOKEN and no token in {LOG}: run once over the cable, or read it from the app's log")return found[-1]# The argument a command takes without a flag.-POSITIONAL = {"type": "text", "menu.pick": "title", "seek": "seconds", "tab": "arg", "settings.page": "title"}+POSITIONAL = {"type": "text", "menu.pick": "title", "seek": "seconds", "speed": "arg", "sing": "arg", "lossless": "arg", "tab": "arg", "settings.page": "title"}def port_open():diff --git a/tweak/Sources/Diagnostics/Diagnostics.h b/tweak/Sources/Diagnostics/Diagnostics.hindex c8b61db6..f75c9ec6 100644--- a/tweak/Sources/Diagnostics/Diagnostics.h+++ b/tweak/Sources/Diagnostics/Diagnostics.h@@ -18,4 +18,11 @@ BOOL SGDriverHandles(NSString *command);NSData *SGDriverRun(NSString *command, NSDictionary<NSString *, NSString *> *params);// Defined in App/ModSettings.x: declared low, defined high.void SGOpenModSettings(UIView *source);+// Defined in Shared/Player/SpeedPitch.x.+double SGPlayerSpeed(void);+void SGSetPlayerSpeed(double speed);+// Defined in Shared/Sing/Sing.x.+void SGSetSingOn(BOOL on);+// Defined in Shared/Lossless/LosslessProbe.m: lossless anchor --path P, check, stop. Nil with *error on failure.+NSDictionary *SGLosslessProbe(NSString *verb, NSDictionary *p, NSString **error);#endifdiff --git a/tweak/Sources/Diagnostics/Driver.m b/tweak/Sources/Diagnostics/Driver.mindex 79006874..2011d670 100644--- a/tweak/Sources/Diagnostics/Driver.m+++ b/tweak/Sources/Diagnostics/Driver.m@@ -815,6 +815,31 @@ static NSDictionary<NSString *, SGCommand> *commands(void) {return by ? @{@"by" : @(by.doubleValue)} : @{@"seconds" : @(seconds)};});},+ // Playback speed, 1 when normal: speed 1.5.+ @"speed" : ^NSDictionary *(NSDictionary *p) {+ double speed = [(p[@"value"] ?: p[@"arg"]) doubleValue];+ if (speed <= 0) fail(@"speed takes a rate, 1 when normal");+ return onMain(^id {+ SGSetPlayerSpeed(speed);+ return @{@"speed" : @(SGPlayerSpeed())};+ });+ },+ // A local lossless file in place of Spotify's sound for the same recording: lossless anchor --path P,+ // lossless check, lossless stop. Runs off main: matching takes a few seconds.+ @"lossless" : ^NSDictionary *(NSDictionary *p) {+ NSString *error;+ NSDictionary *result = SGLosslessProbe(p[@"arg"] ?: @"check", p, &error);+ if (!result) fail(@"%@", error);+ return result;+ },+ // Karaoke on or off: sing 1, sing 0.+ @"sing" : ^NSDictionary *(NSDictionary *p) {+ BOOL on = [(p[@"value"] ?: p[@"arg"]) boolValue];+ return onMain(^id {+ SGSetSingOn(on);+ return @{@"sing" : @(on)};+ });+ },@"play" : ^NSDictionary *(NSDictionary *p) {return onMain(^id {[requirePlayer() resume:nil];diff --git a/tweak/Sources/Shared/Lossless/Lossless.x b/tweak/Sources/Shared/Lossless/Lossless.xnew file mode 100644index 00000000..fb405f4c--- /dev/null+++ b/tweak/Sources/Shared/Lossless/Lossless.x@@ -0,0 +1,108 @@+// Anchors the playing track's lossless file again whenever the anchor stops holding: a new track, a seek, a resume.+// Only at normal speed with Karaoke off; anything else leaves Spotify's own sound.+#import "Core/SGCore.h"+#import <objc/runtime.h>+#import "Shared/Player/PlayerState.h"+#import "Shared/Sing/Sing.h"+#import "Shared/Player/SpeedPitch.h"+#import "SGLossless.h"+#import "SGLosslessStore.h"+#import "LosslessQuality.h"++static const int kTries = 3; // failed anchors in a row before waiting for the next change of source+static const double kRetrySeconds = 2;++@interface SGLosslessCoordinator : NSObject <SGPlayerStateObserver>+@end++@implementation SGLosslessCoordinator {+ NSString *_track, *_path;+ BOOL _busy, _looked, _nextHas;+ NSString *_next;+ NSDate *_folderChanged;+ int _failures;+ uint64_t _gaveUpEpoch;+ double _retryAt;+}++- (void)playerStateDidChange:(SPTPlayerState *)state {+ [self tick];+}++- (void)tick {+ SPTPlayerState *state = SGPlayerState();+ NSString *track = SGURIString(state.track.URI);+ if (![track isEqualToString:_track]) {+ if (_path) SGLosslessStop(YES);+ _track = track;+ _path = nil;+ _looked = NO;+ _failures = 0;+ _gaveUpEpoch = 0;+ }+ // A track's first report can come before its title and length: look again until both are known.+ if (track && !_looked) {+ NSString *title = state.track.trackTitle;+ _path = SGLosslessStoreFile(track, title ?: @"", state.duration);+ _looked = _path || (title.length && state.duration > 0);+ if (_looked) SGLog(@"lossless: %@ (%.1f s): %@", track, state.duration, _path.lastPathComponent ?: @"no file");+ }+ // Spotify fetches a song as it starts, so its quality is set while the one before plays.+ id future = [state respondsToSelector:@selector(future)] ? state.future : nil;+ id upcoming = [future isKindOfClass:NSArray.class] ? [(NSArray *)future firstObject] : nil;+ SPTPlayerTrack *next = [upcoming isKindOfClass:objc_getClass("SPTPlayerTrack")] ? upcoming : nil;+ NSString *nextURI = SGURIString(next.URI);+ // Again when the folder changes: a file can arrive while the song before plays.+ NSDate *changed = [NSFileManager.defaultManager attributesOfItemAtPath:SGLosslessStoreDirectory() error:NULL].fileModificationDate;+ if (![nextURI isEqualToString:_next] || ![changed isEqualToDate:_folderChanged]) {+ _next = nextURI;+ _folderChanged = changed;+ _nextHas = nextURI && SGLosslessStoreHas(nextURI, next.trackTitle ?: @"");+ }+ SGLosslessLowerQuality(_nextHas);++ if (!_path || _busy || SGLosslessPlaying()) return;+ if (!state.isPlaying || state.isPaused || fabs(state.playbackSpeed - 1) > 0.001 || SGSingOn()) return;+ uint64_t epoch = SGPlayerSourceEpoch();+ if (_gaveUpEpoch && _gaveUpEpoch == epoch) return;+ double now = CFAbsoluteTimeGetCurrent();+ if (now < _retryAt) return;++ _busy = YES;+ NSString *path = _path;+ double hint = state.position;+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{+ NSString *error;+ NSDictionary *anchored = SGLosslessAnchor(path, hint, &error);+ dispatch_async(dispatch_get_main_queue(), ^{+ self->_busy = NO;+ if (![track isEqualToString:self->_track]) return;+ if (anchored) {+ self->_failures = 0;+ SGLosslessStoreRemember(track, path);+ SGLog(@"lossless: playing %@ from %.2f s", path.lastPathComponent, [anchored[@"file_seconds"] doubleValue]);+ return;+ }+ self->_retryAt = CFAbsoluteTimeGetCurrent() + kRetrySeconds;+ if (++self->_failures >= kTries) {+ self->_failures = 0;+ self->_gaveUpEpoch = SGPlayerSourceEpoch();+ SGLog(@"lossless: gave up on %@ until the source changes: %@", path.lastPathComponent, error);+ } else {+ SGLog(@"lossless: no anchor (%@)", error);+ }+ });+ });+}+@end++%ctor {+ if (!SGHidden(SGKeyLossless)) return;+ dispatch_async(dispatch_get_main_queue(), ^{+ static SGLosslessCoordinator *coordinator;+ coordinator = [SGLosslessCoordinator new];+ SGAddPlayerStateObserver(coordinator);+ // Position reports are not change callbacks, and a seek or resume shows only in the source's epoch.+ [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer *timer) { [coordinator tick]; }];+ });+}diff --git a/tweak/Sources/Shared/Lossless/LosslessProbe.m b/tweak/Sources/Shared/Lossless/LosslessProbe.mnew file mode 100644index 00000000..92613494--- /dev/null+++ b/tweak/Sources/Shared/Lossless/LosslessProbe.m@@ -0,0 +1,63 @@+// FLEX builds only: the lossless engine and Spotify's streaming quality, driven from the Mac (scripts/phone.py).+#if SG_DRIVER+#import "Diagnostics/Diagnostics.h"+#import "Core/SGCore.h"+#import "SGLossless.h"+#import "LosslessQuality.h"+#import "Shared/Player/PlayerState.h"+#import <objc/runtime.h>+#import <objc/message.h>++// Spotify's streaming quality settings and the playing track's quality; --set N sets both settings to N.+static NSDictionary *quality(NSDictionary *p, NSString **error) {+ NSMutableDictionary *result = [NSMutableDictionary dictionary];+ Class preferences = NSClassFromString(@"_TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation");+ for (NSString *name in @[@"audioPlayBitrate", @"audioPlayBitrateNonMetered", @"setAudioPlayBitrateNonMetered:"]) {+ Method m = class_getInstanceMethod(preferences, NSSelectorFromString(name));+ result[[@"type_" stringByAppendingString:name]] = m ? @(method_getTypeEncoding(m)) : @"missing";+ }+ __block NSInteger metered = -1, nonMetered = -1;+ __block BOOL read = NO;+ dispatch_sync(dispatch_get_main_queue(), ^{+ if (p[@"set"]) SGSetStreamQuality([p[@"set"] integerValue], [p[@"set"] integerValue]);+ read = SGStreamQuality(&metered, &nonMetered);+ id state = SGPlayerState();+ id q = [state respondsToSelector:NSSelectorFromString(@"playbackQuality")] ? [state valueForKey:@"playbackQuality"] : nil;+ result[@"track"] = [[state valueForKeyPath:@"track.URI"] description] ?: @"";+ id future = [state respondsToSelector:NSSelectorFromString(@"future")] ? [state valueForKey:@"future"] : nil;+ id next = [future isKindOfClass:NSArray.class] ? [(NSArray *)future firstObject] : nil;+ result[@"next"] = [[next valueForKey:@"URI"] description] ?: @"";+ result[@"playback_quality"] = [q description] ?: @"none";+ SEL originName = NSSelectorFromString(@"fileOriginToString:");+ if ([q respondsToSelector:originName]) {+ for (long long o = 0; o < 6; o++) {+ id name = ((id (*)(id, SEL, long long))objc_msgSend)(q, originName, o);+ result[[NSString stringWithFormat:@"origin_name_%lld", o]] = [name description] ?: @"nil";+ }+ }+ for (NSString *key in @[@"bitrateLevel", @"targetBitrateLevel", @"targetBitrateAvailable", @"fileOrigin", @"hifiStatus", @"qualityRestrictionReason", @"highestAvailableQuality"]) {+ @try { result[[@"q_" stringByAppendingString:key]] = [[q valueForKey:key] description] ?: @"nil"; } @catch (NSException *e) { result[[@"q_" stringByAppendingString:key]] = @"?"; }+ }+ });+ if (!read) { *error = @"Spotify's preferences object was not captured"; return nil; }+ result[@"metered"] = @(metered);+ result[@"non_metered"] = @(nonMetered);+ return result;+}++NSDictionary *SGLosslessProbe(NSString *verb, NSDictionary *p, NSString **error) {+ if ([verb isEqualToString:@"anchor"]) {+ NSString *path = p[@"path"];+ if (!path.length) { *error = @"lossless anchor takes --path to a FLAC or ALAC file in the app's container"; return nil; }+ return SGLosslessAnchor(path, p[@"hint"] ? [p[@"hint"] doubleValue] : -1, error);+ }+ if ([verb isEqualToString:@"check"]) return SGLosslessCheck(error);+ if ([verb isEqualToString:@"quality"]) return quality(p, error);+ if ([verb isEqualToString:@"stop"]) {+ SGLosslessStop(YES);+ return @{};+ }+ *error = @"lossless takes anchor, check, quality or stop";+ return nil;+}+#endifdiff --git a/tweak/Sources/Shared/Lossless/LosslessQuality.h b/tweak/Sources/Shared/Lossless/LosslessQuality.hnew file mode 100644index 00000000..daa2fca5--- /dev/null+++ b/tweak/Sources/Shared/Lossless/LosslessQuality.h@@ -0,0 +1,10 @@+#import <Foundation/Foundation.h>++// Spotify's streaming quality settings, cellular (metered) and Wi-Fi (non-metered), as its own levels.+// NO until Spotify has made its preferences object.+BOOL SGStreamQuality(NSInteger *metered, NSInteger *nonMetered);+BOOL SGSetStreamQuality(NSInteger metered, NSInteger nonMetered);+// Main thread. Spotify's lowest quality while `lower` (where low is not offered, Spotify plays its next lowest),+// the user's own otherwise. The user's quality is kept across launches and put back at the next launch, even with+// lossless off. A quality the user picks while it is lowered stands.+void SGLosslessLowerQuality(BOOL lower);diff --git a/tweak/Sources/Shared/Lossless/LosslessQuality.x b/tweak/Sources/Shared/Lossless/LosslessQuality.xnew file mode 100644index 00000000..85540bfa--- /dev/null+++ b/tweak/Sources/Shared/Lossless/LosslessQuality.x@@ -0,0 +1,67 @@+// Spotify's streaming quality, read and set through its own preferences object (captured as Spotify makes it).+#import "Core/SGCore.h"+#import "LosslessQuality.h"+#import "SGLossless.h"++#define SGKeyLosslessSavedQuality @"spotifyglass.lossless.savedQuality" // [cellular, Wi-Fi] while lowered+static const NSInteger kLowest = 1;++@interface _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation : NSObject+- (NSInteger)audioPlayBitrate;+- (NSInteger)audioPlayBitrateNonMetered;+- (void)setAudioPlayBitrate:(NSInteger)level;+- (void)setAudioPlayBitrateNonMetered:(NSInteger)level;+@end++static __weak _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation *sg_preferences;++%hook _TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation+- (id)initWithScheduler:(id)scheduler {+ id made = %orig;+ sg_preferences = made;+ // Lowered when Spotify last quit, and lossless is off now: nothing else would put it back.+ if (!SGHidden(SGKeyLossless)) dispatch_async(dispatch_get_main_queue(), ^{ SGLosslessLowerQuality(NO); });+ return made;+}+%end++BOOL SGStreamQuality(NSInteger *metered, NSInteger *nonMetered) {+ __auto_type preferences = sg_preferences;+ if (!preferences) return NO;+ *metered = preferences.audioPlayBitrate;+ *nonMetered = preferences.audioPlayBitrateNonMetered;+ return YES;+}++BOOL SGSetStreamQuality(NSInteger metered, NSInteger nonMetered) {+ __auto_type preferences = sg_preferences;+ if (!preferences) return NO;+ preferences.audioPlayBitrate = metered;+ preferences.audioPlayBitrateNonMetered = nonMetered;+ return YES;+}++void SGLosslessLowerQuality(BOOL lower) {+ NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;+ NSArray<NSNumber *> *saved = [defaults arrayForKey:SGKeyLosslessSavedQuality];+ NSInteger metered, nonMetered;+ if (!SGStreamQuality(&metered, &nonMetered)) return;+ if (lower && !saved) {+ if (metered == kLowest && nonMetered == kLowest) return;+ [defaults setObject:@[@(metered), @(nonMetered)] forKey:SGKeyLosslessSavedQuality];+ SGSetStreamQuality(kLowest, kLowest);+ SGLog(@"lossless: Spotify's quality lowered for the next track (was %ld, %ld)", (long)metered, (long)nonMetered);+ } else if (!lower && saved.count == 2) {+ if (metered == kLowest && nonMetered == kLowest) SGSetStreamQuality(saved[0].integerValue, saved[1].integerValue);+ [defaults removeObjectForKey:SGKeyLosslessSavedQuality];+ SGLog(@"lossless: Spotify's quality back to %ld, %ld", (long)saved[0].integerValue, (long)saved[1].integerValue);+ }+}++%ctor {+ #if !SG_DRIVER+ if (!SGHidden(SGKeyLossless) && ![NSUserDefaults.standardUserDefaults arrayForKey:SGKeyLosslessSavedQuality]) return;+ #endif+ %init;+ SGRequireClasses(@[@"_TtC31Preferences_CorePreferencesImpl28SPTPreferencesImplementation"]);+}diff --git a/tweak/Sources/Shared/Lossless/SGAnchor.h b/tweak/Sources/Shared/Lossless/SGAnchor.hnew file mode 100644index 00000000..12a33b21--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGAnchor.h@@ -0,0 +1,13 @@+#pragma once+#import <Foundation/Foundation.h>+#import "SGAnchorRing.h"++#if SG_DRIVER+// Main thread only. The event's epoch and host time are captured where it happened.+void SGAnchorLog(NSString *kind, NSString *evidence, uint64_t epoch, uint64_t host);+// SpeedPitch owns the rings; only the diagnostic's main-thread drain calls this.+bool SGPlayerAnchorRead(SGAnchorSample *latest, unsigned *dropped);+double SGPlayerAnchorLatency(bool *engaged);+#else+static inline void SGAnchorLog(NSString *kind, NSString *evidence, uint64_t epoch, uint64_t host) {}+#endifdiff --git a/tweak/Sources/Shared/Lossless/SGAnchor.m b/tweak/Sources/Shared/Lossless/SGAnchor.mnew file mode 100644index 00000000..b91f35ae--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGAnchor.m@@ -0,0 +1,77 @@+#if SG_DRIVER+#import <mach/mach_time.h>+#import "Core/SGLog.h"+#import "Shared/Player/PlayerState.h"+#import "Shared/Player/SpeedPitch.h"+#import "Shared/Sing/Sing.h"+#import "Shared/Lyrics/Lyrics.h"+#import "SGAnchor.h"++static SGAnchorSample sg_latest;+static BOOL sg_haveSample;+static double sg_tickSeconds;+static uint64_t sg_session, sg_sequence;+static BOOL sg_enabled;++static NSString *field(NSString *value) {+ static NSCharacterSet *allowed;+ if (!allowed) allowed = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:._-"];+ return [value stringByAddingPercentEncodingWithAllowedCharacters:allowed] ?: @"-";+}++void SGAnchorLog(NSString *kind, NSString *evidence, uint64_t epoch, uint64_t host) {+ if (!sg_enabled) return;+ unsigned dropped;+ SGAnchorSample sample;+ if (SGPlayerAnchorRead(&sample, &dropped)) { sg_latest = sample; sg_haveSample = YES; }+ uint64_t before = mach_absolute_time();+ SPTPlayerState *state = SGPlayerState();+ double position = state ? (state.isPaused ? state.positionAsOfTimestamp : state.position) : -1;+ double raw = state ? SGSingSourcePosition(state) : -1;+ NSInteger karaoke = SGKaraokePositionMs();+ double lead = SGSingHeldLead(), correction = state ? SGSingLeadOf(state) : 0;+ bool engaged;+ double latency = SGPlayerAnchorLatency(&engaged);+ uint64_t after = mach_absolute_time();+ double control = ((double)before + (double)(after - before) / 2) * sg_tickSeconds;+ double sampleTime = sg_haveSample ? sg_latest.host * sg_tickSeconds : 0;+ SGLog(@"lossless-anchor: v=1 session=%llu seq=%llu kind=%@ evidence=%@ event_host=%.6f event_epoch=%llu control_host=%.6f read_ms=%.3f epoch=%llu sample=%d sample_epoch=%llu output=%llu source_frame=%.0f frames=%u rate=%.0f host=%.6f age_ms=%.3f silent=%d status=%d dropped=%u uri=%@ karaoke_uri=%@ position_ms=%.3f raw_ms=%.3f asof_ms=%.3f stamp=%.3f karaoke_ms=%ld playing=%d paused=%d speed=%.6f pitch=%.3f follows=%d tp_engaged=%d tp_latency_ms=%.3f sing_on=%d sing_active=%d sing_state=%ld sing_lead_ms=%.3f sing_correction_ms=%.3f",+ (unsigned long long)sg_session, (unsigned long long)++sg_sequence, kind, evidence,+ host * sg_tickSeconds, (unsigned long long)epoch, control, (after - before) * sg_tickSeconds * 1000,+ (unsigned long long)SGPlayerSourceEpoch(), sg_haveSample, (unsigned long long)sg_latest.epoch,+ (unsigned long long)sg_latest.output, sg_latest.sourceFrame, sg_latest.frames, sg_latest.rate,+ sampleTime, sg_haveSample ? (control - sampleTime) * 1000 : -1, sg_latest.silent, sg_latest.status, dropped,+ field(SGURIString(state.track.URI)), field(SGKaraokePlayingTrack()), position * 1000, raw * 1000, state ? state.positionAsOfTimestamp * 1000 : -1,+ state.timestamp ? state.timestamp.timeIntervalSinceReferenceDate : 0, (long)karaoke,+ state.isPlaying, state.isPaused, SGPlayerSpeed(), SGPlayerPitch(), SGPlayerPitchFollowsSpeed(), engaged, latency * 1000,+ SGSingOn(), SGSingAnchorStageActive(), (long)SGSingCurrentState(), lead * 1000, correction * 1000);+}++__attribute__((constructor)) static void start(void) {+ dispatch_async(dispatch_get_main_queue(), ^{+ // The compile flag follows FLEX packaging; the runtime check matches Sing's dev model slot.+ if (!NSClassFromString(@"FLEXManager")) return;+ mach_timebase_info_data_t timebase;+ mach_timebase_info(&timebase);+ sg_tickSeconds = (double)timebase.numer / timebase.denom / 1e9;+ sg_session = mach_absolute_time();+ sg_enabled = YES;+ SGAnchorLog(@"start", @"diagnostic", SGPlayerSourceEpoch(), sg_session);+ static dispatch_source_t timer;+ timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());+ dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, NSEC_PER_MSEC * 50);+ dispatch_source_set_event_handler(timer, ^{+ static double speed, pitch;+ static BOOL follows;+ double nextSpeed = SGPlayerSpeed(), nextPitch = SGPlayerPitch();+ BOOL nextFollows = SGPlayerPitchFollowsSpeed();+ BOOL changed = speed && (nextSpeed != speed || nextPitch != pitch || nextFollows != follows);+ speed = nextSpeed; pitch = nextPitch; follows = nextFollows;+ if (changed) SGPlayerInvalidateSource();+ SGAnchorLog(changed ? @"speed_pitch" : @"sample", changed ? @"polled" : @"timer",+ SGPlayerSourceEpoch(), mach_absolute_time());+ });+ dispatch_resume(timer);+ });+}+#endifdiff --git a/tweak/Sources/Shared/Lossless/SGAnchorRing.h b/tweak/Sources/Shared/Lossless/SGAnchorRing.hnew file mode 100644index 00000000..7b06798b--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGAnchorRing.h@@ -0,0 +1,46 @@+#pragma once+#include <stdatomic.h>+#include <stdbool.h>+#include <stdint.h>++#if SG_DRIVER+// One producer per output, one control-thread reader. Slots are never reset or freed.+// A full ring drops the new sample. The reader reports that loss with its next line.+_Static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "anchor flags must be lock-free");+_Static_assert(ATOMIC_LLONG_LOCK_FREE == 2, "anchor epochs must be lock-free");+_Static_assert(ATOMIC_INT_LOCK_FREE == 2, "anchor indices must be lock-free");+enum { kSGAnchorCapacity = 256 };+typedef struct {+ double sourceFrame, rate;+ uint64_t host, epoch, output;+ uint32_t frames;+ int32_t status;+ bool silent;+} SGAnchorSample;+typedef struct {+ SGAnchorSample samples[kSGAnchorCapacity];+ atomic_uint read, write, dropped;+ uint64_t lastEpoch;+ unsigned pulls;+} SGAnchorRing;++static inline bool SGAnchorPush(SGAnchorRing *ring, SGAnchorSample sample) {+ unsigned write = atomic_load_explicit(&ring->write, memory_order_relaxed);+ unsigned read = atomic_load_explicit(&ring->read, memory_order_acquire);+ if (write - read == kSGAnchorCapacity) {+ atomic_fetch_add_explicit(&ring->dropped, 1, memory_order_relaxed);+ return false;+ }+ ring->samples[write % kSGAnchorCapacity] = sample;+ atomic_store_explicit(&ring->write, write + 1, memory_order_release);+ return true;+}++static inline bool SGAnchorPop(SGAnchorRing *ring, SGAnchorSample *sample) {+ unsigned read = atomic_load_explicit(&ring->read, memory_order_relaxed);+ if (read == atomic_load_explicit(&ring->write, memory_order_acquire)) return false;+ *sample = ring->samples[read % kSGAnchorCapacity];+ atomic_store_explicit(&ring->read, read + 1, memory_order_release);+ return true;+}+#endifdiff --git a/tweak/Sources/Shared/Lossless/SGLossless.h b/tweak/Sources/Shared/Lossless/SGLossless.hnew file mode 100644index 00000000..e4765f14--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGLossless.h@@ -0,0 +1,22 @@+// Lossless: a local FLAC or ALAC file of the same recording plays in place of Spotify's sound.+//+// SGLosslessEngine.m finds where Spotify plays in the file by matching its sound, and swaps the file in+// SGLosslessStore.m which file belongs to which Spotify track+// Lossless.x anchors again at each track start, seek or resume (the switch below)+// SGSourceEvents.x drops the anchor at a seek, skip, pause or route change+#import <Foundation/Foundation.h>++#define SGKeyLossless @"spotifyglass.lossless" // off unless set++NS_ASSUME_NONNULL_BEGIN+// Off the main and render threads; blocks about 4 s while it records Spotify's sound. Decodes `path` once and keeps+// it until another path or SGLosslessStop(YES). The search looks within 8 s of hintSeconds, or the whole file when+// hintSeconds is negative. Nil with *error when Spotify's sound does not match the file or the source changed.+NSDictionary *_Nullable SGLosslessAnchor(NSString *path, double hintSeconds, NSString *_Nullable *_Nonnull error);+// Off the main thread: the offset in frames between Spotify's sound and the file where the anchor puts it.+NSDictionary *_Nullable SGLosslessCheck(NSString *_Nullable *_Nonnull error);+// Spotify's own sound again; `release` also frees the decoded file.+void SGLosslessStop(BOOL release);+// Whether the file plays now: anchored, and nothing has changed the source since.+BOOL SGLosslessPlaying(void);+NS_ASSUME_NONNULL_ENDdiff --git a/tweak/Sources/Shared/Lossless/SGLosslessEngine.m b/tweak/Sources/Shared/Lossless/SGLosslessEngine.mnew file mode 100644index 00000000..21553fd6--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGLosslessEngine.m@@ -0,0 +1,443 @@+// The anchor comes from the sound itself: a few seconds of Spotify's own output are matched against the file by+// cross-correlation, so Spotify's position reports (1.2 s off after a false stall at 1.5x) only narrow the search.+//+// The file is never decoded whole. The search runs on a copy at 1/16 of the rate (about 2 MB for a 3 minute song)+// and is made exact on a short window read at full rate. Playback reads from a ring that a worker fills ahead of+// where Spotify plays; a block the ring does not hold yet keeps Spotify's sound.+#import "SGLossless.h"+#import "Core/SGCore.h"+#import "Shared/Player/SpeedPitch.h"+#import <Accelerate/Accelerate.h>+#import <AudioToolbox/AudioToolbox.h>+#import <stdatomic.h>++enum { kIdle, kArmed, kFull };+enum { kCoarse = 16 }; // decimation for the coarse search+enum { kRing = 1 << 19 }; // frames the ring holds, about 12 s at 44.1 kHz+enum { kChunk = 8192 }; // frames per read+static const double kAnchorSeconds = 4, kCheckSeconds = 1, kHintWindow = 8;+static const float kMinMatch = 0.8f; // normalized correlation below this is another recording or master++#pragma mark - capture (render thread writes while armed)++static float *sg_cap;+static uint32_t sg_capLength;+static _Atomic uint32_t sg_capFill;+static _Atomic int sg_capState;+static uint64_t sg_capStart, sg_capEpoch, sg_capNext;+static AudioUnit sg_capUnit;+static double sg_capRate;++static void capture(AudioUnit output, uint64_t epoch, double sourceFrame, double rate, UInt32 frames, AudioBufferList *data) {+ if (atomic_load(&sg_capState) != kArmed) return;+ const float *l = data->mBuffers[0].mData, *r = data->mBuffers[1].mData;+ uint32_t fill = atomic_load(&sg_capFill);+ if (fill && ((uint64_t)sourceFrame != sg_capNext || epoch != sg_capEpoch || output != sg_capUnit)) fill = 0;+ if (!fill) { sg_capStart = (uint64_t)sourceFrame; sg_capEpoch = epoch; sg_capUnit = output; sg_capRate = rate; }+ UInt32 n = MIN(frames, sg_capLength - fill);+ for (UInt32 i = 0; i < n; i++) sg_cap[fill + i] = 0.5f * (l[i] + r[i]);+ sg_capNext = (uint64_t)sourceFrame + frames;+ atomic_store(&sg_capFill, fill + n);+ if (fill + n == sg_capLength) atomic_store(&sg_capState, kFull);+}++#pragma mark - the ring (static, never freed: a render call never reads freed memory)++static float sg_ringL[kRing], sg_ringR[kRing];+// File frames [start, end) are in the ring, frame f at f % kRing. The writer moves both forward only.+static _Atomic uint64_t sg_ringStart, sg_ringEnd;+// Where the render thread reads, so the writer never overwrites it.+static _Atomic uint64_t sg_readAt;++static bool ringRead(uint64_t at, UInt32 frames, float *l, float *r) {+ atomic_store_explicit(&sg_readAt, at, memory_order_relaxed);+ uint64_t start = atomic_load(&sg_ringStart), end = atomic_load(&sg_ringEnd);+ if (at < start || at + frames > end || frames > kRing) return false;+ uint64_t i = at % kRing;+ UInt32 first = (UInt32)MIN((uint64_t)frames, kRing - i);+ memcpy(l, sg_ringL + i, first * sizeof(float));+ memcpy(r, sg_ringR + i, first * sizeof(float));+ memcpy(l + first, sg_ringL, (frames - first) * sizeof(float));+ memcpy(r + first, sg_ringR, (frames - first) * sizeof(float));+ return true;+}++#pragma mark - what plays (set off render only while no reader is admitted)++static atomic_bool sg_live;+static atomic_uint sg_readers;+static uint64_t sg_playEpoch, sg_playSource, sg_playAsset;+static AudioUnit sg_playUnit;+static double sg_playRate;+static _Atomic uint32_t sg_gainBits;+static _Atomic uint64_t sg_replaced;++static void selector(AudioUnit output, uint64_t epoch, double sourceFrame, double rate, UInt32 frames, AudioBufferList *data) {+ if (data->mNumberBuffers != 2 || !data->mBuffers[0].mData || !data->mBuffers[1].mData+ || data->mBuffers[0].mDataByteSize < frames * sizeof(float) || data->mBuffers[1].mDataByteSize < frames * sizeof(float)) return;+ capture(output, epoch, sourceFrame, rate, frames, data);+ atomic_fetch_add(&sg_readers, 1);+ if (atomic_load(&sg_live) && epoch == sg_playEpoch && epoch == SGPlayerSourceEpoch() && output == sg_playUnit+ && rate == sg_playRate && sourceFrame >= sg_playSource) {+ uint64_t at = sg_playAsset + ((uint64_t)sourceFrame - sg_playSource);+ float *l = data->mBuffers[0].mData, *r = data->mBuffers[1].mData;+ if (ringRead(at, frames, l, r)) {+ uint32_t bits = atomic_load(&sg_gainBits);+ float gain;+ memcpy(&gain, &bits, sizeof gain);+ vDSP_vsmul(l, 1, &gain, l, 1, frames);+ vDSP_vsmul(r, 1, &gain, r, 1, frames);+ atomic_fetch_add_explicit(&sg_replaced, frames, memory_order_relaxed);+ }+ }+ atomic_fetch_sub(&sg_readers, 1);+}++// Off render: no reader is inside the play fields after this returns.+static void quiesce(void) {+ atomic_store(&sg_live, false);+ while (atomic_load(&sg_readers)) usleep(100);+}++#pragma mark - the file++typedef struct {+ ExtAudioFileRef file;+ double fileRate, rate;+ uint64_t frames; // at the client rate+} Reader;++static void closeReader(Reader *reader) {+ if (reader->file) ExtAudioFileDispose(reader->file);+ *reader = (Reader){0};+}++static BOOL openReader(Reader *reader, NSString *path, double rate, NSString **error) {+ closeReader(reader);+ ExtAudioFileRef file;+ if (ExtAudioFileOpenURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], &file)) { *error = @"the file does not open"; return NO; }+ AudioStreamBasicDescription source = {0};+ UInt32 size = sizeof source;+ SInt64 frames = 0;+ OSStatus status = ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileDataFormat, &size, &source);+ size = sizeof frames;+ if (!status) status = ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileLengthFrames, &size, &frames);+ BOOL lossless = source.mFormatID == kAudioFormatFLAC || source.mFormatID == kAudioFormatAppleLossless;+ AudioStreamBasicDescription client = {rate, kAudioFormatLinearPCM,+ kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved, 4, 1, 4, 2, 32, 0};+ if (!status && lossless && source.mChannelsPerFrame == 2 && frames > 0)+ status = ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, sizeof client, &client);+ if (status || !lossless || source.mChannelsPerFrame != 2 || frames <= 0) {+ ExtAudioFileDispose(file);+ *error = @"not stereo FLAC or ALAC";+ return NO;+ }+ *reader = (Reader){file, source.mSampleRate, rate, (uint64_t)floor(frames * rate / source.mSampleRate)};+ return YES;+}++// Up to `count` frames from file frame `at` into l and r; the frames read (0 at the end).+static UInt32 readAt(Reader *reader, uint64_t at, UInt32 count, float *l, float *r, BOOL seek) {+ if (seek && ExtAudioFileSeek(reader->file, (SInt64)llround(at * reader->fileRate / reader->rate))) return 0;+ struct { AudioBufferList list; AudioBuffer second; } buffers = {{2, {{1, count * 4, l}}}, {1, count * 4, r}};+ UInt32 frames = count;+ if (ExtAudioFileRead(reader->file, &frames, &buffers.list)) return 0;+ return frames;+}++// Mono of file frames [from, from + count), silence outside the file. The caller frees it.+static float *readMono(Reader *reader, int64_t from, uint32_t count) {+ float *mono = calloc(count, sizeof(float)), *l = malloc(kChunk * sizeof(float)), *r = malloc(kChunk * sizeof(float));+ int64_t at = MAX(from, 0);+ BOOL seek = YES;+ while (at < from + count && (uint64_t)at < reader->frames) {+ UInt32 want = (UInt32)MIN((int64_t)kChunk, from + count - at);+ UInt32 got = readAt(reader, at, want, l, r, seek);+ if (!got) break;+ seek = NO;+ for (UInt32 i = 0; i < got; i++) mono[at - from + i] = 0.5f * (l[i] + r[i]);+ at += got;+ }+ free(l); free(r);+ return mono;+}++#pragma mark - the anchor's queue owns these++static dispatch_queue_t sg_queue;+static NSString *sg_path;+static Reader sg_reader; // for searches and checks+static float *sg_coarse; // the file's mono at 1/kCoarse of the rate+static uint64_t sg_coarseCount;+static _Atomic uint64_t sg_anchorEpoch;+static _Atomic uint64_t sg_generation; // the writer stops when this changes+static atomic_uint sg_writers; // writers still running++static void drop(void) {+ closeReader(&sg_reader);+ free(sg_coarse);+ sg_coarse = NULL;+ sg_coarseCount = 0;+ sg_path = nil;+}++static BOOL load(NSString *path, double rate, NSString **error) {+ if (sg_coarse && [sg_path isEqualToString:path] && sg_reader.rate == rate) return YES;+ drop();+ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();+ if (!openReader(&sg_reader, path, rate, error)) return NO;+ sg_coarseCount = sg_reader.frames / kCoarse;+ sg_coarse = calloc(sg_coarseCount + 1, sizeof(float));+ float *l = malloc(kChunk * sizeof(float)), *r = malloc(kChunk * sizeof(float));+ float sum = 0;+ uint64_t at = 0;+ BOOL seek = YES;+ for (UInt32 got; (got = readAt(&sg_reader, at, kChunk, l, r, seek)); at += got) {+ seek = NO;+ for (UInt32 i = 0; i < got; i++) {+ uint64_t f = at + i;+ sum += 0.5f * (l[i] + r[i]);+ if (f % kCoarse == kCoarse - 1) {+ if (f / kCoarse < sg_coarseCount) sg_coarse[f / kCoarse] = sum / kCoarse;+ sum = 0;+ }+ }+ }+ free(l); free(r);+ sg_path = path;+ SGLog(@"lossless: read %@, %llu frames at %.0f Hz in %.2f s", path.lastPathComponent, at, rate, CFAbsoluteTimeGetCurrent() - start);+ if (at + kChunk < sg_reader.frames) { *error = @"the file ended early"; drop(); return NO; }+ return YES;+}++static float dot(const float *a, const float *b, vDSP_Length n) {+ float d;+ vDSP_dotpr(a, 1, b, 1, &d, n);+ return d;+}++// Normalized correlation of `capture` against `window` (both n long), and the gain that maps the file onto it.+static float match(const float *capture, const float *window, vDSP_Length n, float *gain) {+ float cc = dot(capture, window, n), aa = dot(window, window, n), ss = dot(capture, capture, n);+ if (gain) *gain = aa > 0 ? cc / aa : 0;+ return aa > 0 && ss > 0 ? cc / sqrtf(aa * ss) : 0;+}++// The file frame within [from, to] whose following frames best match the capture, read at full rate.+static uint64_t refine(int64_t from, int64_t to, float *score, float *gain) {+ from = MAX(from, 0);+ to = MAX(from, MIN(to, (int64_t)sg_reader.frames - (int64_t)sg_capLength));+ uint32_t span = (uint32_t)(to - from) + sg_capLength;+ float *window = readMono(&sg_reader, from, span);+ uint64_t best = from;+ *score = -2;+ for (int64_t lag = from; lag <= to; lag++) {+ float s = match(sg_cap, window + (lag - from), sg_capLength, NULL);+ if (s > *score) { *score = s; best = lag; }+ }+ match(sg_cap, window + (best - from), sg_capLength, gain);+ free(window);+ return best;+}++// Coarse search of file frames [from, to), then exact around the winner.+static uint64_t search(uint64_t from, uint64_t to, float *score, float *gain) {+ uint64_t lo = from / kCoarse, hi = MIN(to / kCoarse, sg_coarseCount);+ vDSP_Length capN = sg_capLength / kCoarse;+ if (hi <= lo || hi - lo <= capN) { *score = 0; return 0; }+ float *cap = malloc(capN * sizeof(float));+ float filter[kCoarse], one = 1.0f / kCoarse;+ vDSP_vfill(&one, filter, 1, kCoarse);+ vDSP_desamp(sg_cap, kCoarse, filter, cap, capN, kCoarse);+ const float *file = sg_coarse + lo;+ vDSP_Length fileN = hi - lo, lags = fileN - capN + 1;+ float *corr = malloc(lags * sizeof(float));+ vDSP_conv(file, 1, cap, 1, corr, 1, lags, capN);+ double running = 0;+ for (vDSP_Length i = 0; i < capN; i++) running += (double)file[i] * file[i];+ vDSP_Length coarse = 0;+ float best = -INFINITY;+ for (vDSP_Length i = 0; i < lags; i++) {+ float s = running > 1e-9 ? corr[i] / sqrtf((float)running) : 0;+ if (s > best) { best = s; coarse = i; }+ if (i + capN < fileN) running += (double)file[i + capN] * file[i + capN] - (double)file[i] * file[i];+ }+ free(cap); free(corr);+ int64_t center = (int64_t)(lo + coarse) * kCoarse;+ return refine(center - 2 * kCoarse, center + 2 * kCoarse, score, gain);+}++// Records `seconds` of Spotify's sound in one unbroken run; NO after the timeout.+static BOOL record(double seconds, NSString **error) {+ static float *storage;+ if (!storage) storage = malloc((size_t)(kAnchorSeconds * 96000) * sizeof(float));+ sg_cap = storage;+ atomic_store(&sg_capState, kIdle);+ sg_capLength = (uint32_t)(MIN(seconds, kAnchorSeconds) * MIN(sg_capRate > 0 ? sg_capRate : 44100, 96000));+ atomic_store(&sg_capFill, 0);+ atomic_store(&sg_capState, kArmed);+ for (int i = 0; i < (int)(seconds * 20) + 60 && atomic_load(&sg_capState) != kFull; i++) usleep(50000);+ if (atomic_load(&sg_capState) == kFull) return YES;+ atomic_store(&sg_capState, kIdle);+ *error = @"Spotify's sound did not run unbroken long enough";+ return NO;+}++#pragma mark - the writer++// Fills the ring from where the render thread reads until the generation changes or the file ends.+static void fill(NSString *path, double rate, uint64_t from, uint64_t generation) {+ Reader reader = {0};+ NSString *error;+ if (!openReader(&reader, path, rate, &error)) {+ SGLog(@"lossless: the writer could not open the file: %@", error);+ atomic_fetch_sub(&sg_writers, 1);+ return;+ }+ float *l = malloc(kChunk * sizeof(float)), *r = malloc(kChunk * sizeof(float));+ uint64_t end = from;+ BOOL seek = YES;+ while (atomic_load(&sg_generation) == generation) {+ uint64_t reading = MAX(atomic_load_explicit(&sg_readAt, memory_order_relaxed), atomic_load(&sg_ringStart));+ if (reading > end + (uint64_t)rate) {+ // Spotify plays ahead of what the ring holds: start again where it plays.+ end = reading + (uint64_t)(rate / 4);+ atomic_store(&sg_ringStart, end);+ atomic_store(&sg_ringEnd, end);+ seek = YES;+ }+ if (end + kChunk > reading + kRing - kChunk) { usleep(20000); continue; }+ UInt32 got = readAt(&reader, end, kChunk, l, r, seek);+ if (!got) break;+ seek = NO;+ uint64_t i = end % kRing;+ UInt32 first = (UInt32)MIN((uint64_t)got, kRing - i);+ memcpy(sg_ringL + i, l, first * sizeof(float));+ memcpy(sg_ringR + i, r, first * sizeof(float));+ memcpy(sg_ringL, l + first, (got - first) * sizeof(float));+ memcpy(sg_ringR, r + first, (got - first) * sizeof(float));+ end += got;+ // Frames the writer passed out of the ring's reach are gone.+ if (end > kRing && atomic_load(&sg_ringStart) < end - kRing) atomic_store(&sg_ringStart, end - kRing);+ atomic_store(&sg_ringEnd, end);+ }+ free(l); free(r);+ closeReader(&reader);+ atomic_fetch_sub(&sg_writers, 1);+}++#pragma mark - anchor, check, stop++static void stopPlaying(void) {+ atomic_fetch_add(&sg_generation, 1);+ quiesce();+ // A writer of the last anchor still inside its loop would write into the ring the next one resets.+ while (atomic_load(&sg_writers)) usleep(1000);+ atomic_store(&sg_anchorEpoch, 0);+}++static NSDictionary *anchor(NSString *path, double hint, NSString **error) {+ static dispatch_once_t once;+ dispatch_once(&once, ^{ SGPlayerSetSourceSelector(selector); });+ stopPlaying();+ if (!record(kAnchorSeconds, error)) return nil;+ double rate = sg_capRate;+ if (!load(path, rate, error)) return nil;+ if (sg_reader.frames <= sg_capLength) { *error = @"the file is shorter than the capture"; return nil; }++ CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();+ uint64_t from = 0, to = sg_reader.frames;+ if (hint >= 0) {+ from = (uint64_t)MAX(0, (hint - kHintWindow) * rate);+ to = (uint64_t)((hint + kHintWindow + kAnchorSeconds) * rate);+ if (from + sg_capLength >= sg_reader.frames) { *error = @"Spotify plays past the end of the file"; return nil; }+ }+ float score, gain;+ uint64_t lag = search(from, to, &score, &gain);+ double took = CFAbsoluteTimeGetCurrent() - start;+ SGLog(@"lossless: match %.4f at file %.3f s (hint %.1f s), gain %.3f, search %.2f s", score, lag / rate, hint, gain, took);+ if (score < kMinMatch) { *error = [NSString stringWithFormat:@"no match (%.3f)", score]; return nil; }+ if (sg_capEpoch != SGPlayerSourceEpoch()) { *error = @"the source changed while matching"; return nil; }++ uint32_t bits;+ memcpy(&bits, &gain, sizeof bits);+ atomic_store(&sg_gainBits, bits);+ sg_playEpoch = sg_capEpoch;+ sg_playSource = sg_capStart;+ sg_playAsset = lag;+ sg_playUnit = sg_capUnit;+ sg_playRate = rate;+ // The writer starts where Spotify plays by now, so the ring is not filled with frames already gone.+ uint64_t now = lag + (sg_capNext - sg_capStart);+ atomic_store(&sg_ringStart, now);+ atomic_store(&sg_ringEnd, now);+ atomic_store(&sg_readAt, now);+ uint64_t generation = atomic_load(&sg_generation);+ atomic_fetch_add(&sg_writers, 1);+ // Its own thread: it runs for the rest of the song, which would hold a shared queue's thread that long.+ NSThread *writer = [[NSThread alloc] initWithBlock:^{ fill(path, rate, now, generation); }];+ writer.qualityOfService = NSQualityOfServiceUserInitiated;+ writer.name = @"spotifyglass.lossless.writer";+ [writer start];+ atomic_store(&sg_anchorEpoch, sg_capEpoch);+ atomic_store(&sg_live, true);+ return @{@"rate" : @(rate), @"file_frame" : @(lag), @"file_seconds" : @(lag / rate), @"match" : @(score),+ @"gain" : @(gain), @"search_s" : @(took)};+}++static NSDictionary *check(NSString **error) {+ uint64_t epoch = atomic_load(&sg_anchorEpoch);+ if (!sg_coarse || !epoch) { *error = @"nothing anchored"; return nil; }+ NSMutableDictionary *result = [@{@"replaced_s" : @(atomic_load(&sg_replaced) / sg_reader.rate),+ @"epoch_current" : @(epoch == SGPlayerSourceEpoch())} mutableCopy];+ if (!record(kCheckSeconds, error)) return nil;+ if (sg_capUnit != sg_playUnit || sg_capEpoch != epoch) {+ result[@"note"] = @"the source changed since the anchor: Spotify plays its own sound again";+ return result;+ }+ int64_t expected = (int64_t)sg_playAsset + (int64_t)(sg_capStart - sg_playSource);+ if (expected + sg_capLength > sg_reader.frames) {+ result[@"note"] = @"past the end of the file";+ return result;+ }+ float score;+ uint64_t found = refine(expected - 2048, expected + 2048, &score, NULL);+ result[@"offset_frames"] = @((int64_t)found - expected);+ result[@"match"] = @(score);+ result[@"file_seconds"] = @(expected / sg_reader.rate);+ return result;+}++static dispatch_queue_t queue(void) {+ static dispatch_once_t once;+ dispatch_once(&once, ^{ sg_queue = dispatch_queue_create("spotifyglass.lossless", DISPATCH_QUEUE_SERIAL); });+ return sg_queue;+}++NSDictionary *SGLosslessAnchor(NSString *path, double hintSeconds, NSString **error) {+ __block NSDictionary *result;+ __block NSString *failure;+ dispatch_sync(queue(), ^{ NSString *e; result = anchor(path, hintSeconds, &e); failure = e; });+ *error = failure;+ return result;+}++NSDictionary *SGLosslessCheck(NSString **error) {+ __block NSDictionary *result;+ __block NSString *failure;+ dispatch_sync(queue(), ^{ NSString *e; result = check(&e); failure = e; });+ *error = failure;+ return result;+}++void SGLosslessStop(BOOL release) {+ atomic_fetch_add(&sg_generation, 1);+ atomic_store(&sg_live, false);+ atomic_store(&sg_anchorEpoch, 0);+ if (release) dispatch_async(queue(), ^{ drop(); });+}++BOOL SGLosslessPlaying(void) {+ uint64_t epoch = atomic_load(&sg_anchorEpoch);+ return epoch && epoch == SGPlayerSourceEpoch();+}diff --git a/tweak/Sources/Shared/Lossless/SGLosslessStore.h b/tweak/Sources/Shared/Lossless/SGLosslessStore.hnew file mode 100644index 00000000..a31f5df5--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGLosslessStore.h@@ -0,0 +1,16 @@+#import <Foundation/Foundation.h>++NS_ASSUME_NONNULL_BEGIN+// Documents/Lossless: FLAC and ALAC files, put there by the user or downloaded. A file goes with the Spotify track+// it was proven against (SGLosslessStoreRemember); until then a file is a candidate when its title tag (or its+// name) is the track's title and its length is within 2 s of the track's. Artists are not compared: a tag can+// name the composer where Spotify names the performer.+NSString *SGLosslessStoreDirectory(void);+// Main thread. The file for the track, or nil.+NSString *_Nullable SGLosslessStoreFile(NSString *uri, NSString *title, double duration);+// Main thread. Whether a file is there for a track whose length is not known yet (an upcoming one): by the+// index, else by title alone.+BOOL SGLosslessStoreHas(NSString *uri, NSString *title);+// Main thread. The file matched the track's sound: look it up by the track from now on.+void SGLosslessStoreRemember(NSString *uri, NSString *path);+NS_ASSUME_NONNULL_ENDdiff --git a/tweak/Sources/Shared/Lossless/SGLosslessStore.m b/tweak/Sources/Shared/Lossless/SGLosslessStore.mnew file mode 100644index 00000000..ef088638--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGLosslessStore.m@@ -0,0 +1,81 @@+#import "SGLosslessStore.h"+#import <AudioToolbox/AudioToolbox.h>++static NSString *const kIndex = @".index.plist"; // track URI -> file name++NSString *SGLosslessStoreDirectory(void) {+ NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;+ NSString *directory = [documents stringByAppendingPathComponent:@"Lossless"];+ [NSFileManager.defaultManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];+ return directory;+}++static NSString *indexPath(void) {+ return [SGLosslessStoreDirectory() stringByAppendingPathComponent:kIndex];+}++static NSString *folded(NSString *text) {+ return [[text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]+ stringByFoldingWithOptions:NSCaseInsensitiveSearch | NSWidthInsensitiveSearch locale:nil];+}++// A file's title tag and length, read once per file and kept by name and modification date.+static NSDictionary *tags(NSString *path) {+ static NSMutableDictionary<NSString *, NSDictionary *> *cache;+ if (!cache) cache = [NSMutableDictionary dictionary];+ NSDate *modified = [NSFileManager.defaultManager attributesOfItemAtPath:path error:NULL].fileModificationDate;+ NSString *key = [NSString stringWithFormat:@"%@|%f", path, modified.timeIntervalSince1970];+ if (cache[key]) return cache[key];+ NSMutableDictionary *found = [NSMutableDictionary dictionary];+ AudioFileID file;+ if (AudioFileOpenURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], kAudioFileReadPermission, 0, &file) == noErr) {+ CFDictionaryRef info = NULL;+ UInt32 size = sizeof info;+ if (AudioFileGetProperty(file, kAudioFilePropertyInfoDictionary, &size, &info) == noErr && info) {+ NSDictionary *read = CFBridgingRelease(info);+ if (read[@kAFInfoDictionary_Title]) found[@"title"] = read[@kAFInfoDictionary_Title];+ if (read[@kAFInfoDictionary_ApproximateDurationInSeconds]) found[@"duration"] = @([read[@kAFInfoDictionary_ApproximateDurationInSeconds] doubleValue]);+ }+ AudioFileClose(file);+ }+ cache[key] = found;+ return found;+}++static NSString *find(NSString *uri, NSString *title, double duration) {+ NSString *directory = SGLosslessStoreDirectory();+ NSDictionary *index = [NSDictionary dictionaryWithContentsOfFile:indexPath()];+ NSString *known = index[uri];+ if (known) {+ NSString *path = [directory stringByAppendingPathComponent:known];+ if ([NSFileManager.defaultManager fileExistsAtPath:path]) return path;+ }+ if (!title.length) return nil;+ NSString *wanted = folded(title);+ for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:NULL]) {+ NSString *extension = name.pathExtension.lowercaseString;+ if (![@[@"flac", @"m4a", @"caf"] containsObject:extension]) continue;+ NSString *path = [directory stringByAppendingPathComponent:name];+ NSDictionary *tag = tags(path);+ NSString *fileTitle = tag[@"title"];+ BOOL titled = fileTitle ? [folded(fileTitle) isEqualToString:wanted]+ : [folded(name.stringByDeletingPathExtension) containsString:wanted];+ if (titled && (duration < 0 || fabs([tag[@"duration"] doubleValue] - duration) <= 2)) return path;+ }+ return nil;+}++NSString *SGLosslessStoreFile(NSString *uri, NSString *title, double duration) {+ return duration > 0 ? find(uri, title, duration) : find(uri, @"", 0);+}++BOOL SGLosslessStoreHas(NSString *uri, NSString *title) {+ return find(uri, title, -1) != nil;+}++void SGLosslessStoreRemember(NSString *uri, NSString *path) {+ NSMutableDictionary *index = [NSMutableDictionary dictionaryWithContentsOfFile:indexPath()] ?: [NSMutableDictionary dictionary];+ if ([index[uri] isEqualToString:path.lastPathComponent]) return;+ index[uri] = path.lastPathComponent;+ [index writeToFile:indexPath() atomically:YES];+}diff --git a/tweak/Sources/Shared/Lossless/SGPCMReplacement.h b/tweak/Sources/Shared/Lossless/SGPCMReplacement.hnew file mode 100644index 00000000..7568090d--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGPCMReplacement.h@@ -0,0 +1,17 @@+#import "SGPreparedPCM.h"+#import "Shared/Player/SpeedPitch.h"++// Prototype only: no constructor enables this in Spotify. Call Enable once before using Install.+void SGPCMReplacementEnable(void);+// Off render. epoch must be current. sourceFrame and assetFrame describe the same next source frame,+// established by a playback owner, not by reading an audible position while Sing holds look-ahead.+// Assets are immutable and retained until all admitted readers have left. A publication is refused+// if retirement is backlogged. Initial cap is 32 MiB PCM per asset, at most three retained snapshots.+BOOL SGPCMReplacementInstall(SGPreparedPCM *asset, AudioUnit output, uint64_t epoch,+ uint64_t sourceFrame, uint64_t assetFrame);+// Off render. Invalidate the epoch and detach the source. An admitted callback may finish its block.+void SGPCMReplacementCancel(void);+// Render implementation exposed for deterministic tests; normally registered through Enable.+void SGPCMReplacementApply(AudioUnit output, uint64_t epoch, double sourceFrame,+ double sampleRate, UInt32 frames, AudioBufferList *data);+uint64_t SGPCMReplacementFrames(void);diff --git a/tweak/Sources/Shared/Lossless/SGPCMReplacement.m b/tweak/Sources/Shared/Lossless/SGPCMReplacement.mnew file mode 100644index 00000000..2fe92a87--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGPCMReplacement.m@@ -0,0 +1,89 @@+#import "SGPCMReplacement.h"+#import <stdatomic.h>+#import <pthread.h>+#include <math.h>++typedef struct Snapshot {+ CFTypeRef owner;+ const float *left, *right;+ AudioUnit output;+ double rate;+ uint64_t epoch, sourceFrame, assetFrame, length;+ struct Snapshot *next;+} Snapshot;++static _Atomic(Snapshot *) sg_active;+static atomic_uint sg_readers;+static atomic_uint_fast64_t sg_replaced;+static pthread_mutex_t sg_control = PTHREAD_MUTEX_INITIALIZER; // never acquired on render+static Snapshot *sg_retired;+static unsigned sg_retiredCount;+static BOOL sg_reclaimScheduled;++static void reclaim(void) {+ // Sequential consistency is required: a reader increments before loading active; a writer+ // exchanges active before checking readers. A future reader can only see the new snapshot.+ if (atomic_load(&sg_readers)) return;+ while (sg_retired) {+ Snapshot *old = sg_retired; sg_retired = old->next;+ CFRelease(old->owner); free(old); --sg_retiredCount;+ }+}+static void scheduleReclaim(void) {+ if (sg_reclaimScheduled || !sg_retired) return;+ sg_reclaimScheduled = YES;+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10*NSEC_PER_MSEC), dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{+ pthread_mutex_lock(&sg_control);+ sg_reclaimScheduled = NO;+ reclaim(); scheduleReclaim();+ pthread_mutex_unlock(&sg_control);+ });+}+static void retire(Snapshot *old) {+ if (old) { old->next = sg_retired; sg_retired = old; ++sg_retiredCount; }+ reclaim(); scheduleReclaim();+}+void SGPCMReplacementEnable(void) { SGPlayerSetSourceSelector(SGPCMReplacementApply); }+BOOL SGPCMReplacementInstall(SGPreparedPCM *asset, AudioUnit output, uint64_t epoch,+ uint64_t sourceFrame, uint64_t assetFrame) {+ if (!asset || !output || !epoch || assetFrame >= asset.frameCount || sourceFrame >= (1ULL<<53)) return NO;+ pthread_mutex_lock(&sg_control);+ reclaim();+ if (epoch != SGPlayerSourceEpoch() || sg_retiredCount >= 2) { pthread_mutex_unlock(&sg_control); return NO; }+ Snapshot *next = calloc(1, sizeof(*next));+ if (!next) { pthread_mutex_unlock(&sg_control); return NO; }+ next->owner = CFBridgingRetain(asset);+ next->left = asset.left; next->right = asset.right; next->length = asset.frameCount;+ next->rate = asset.sampleRate; next->output = output; next->epoch = epoch;+ next->sourceFrame = sourceFrame; next->assetFrame = assetFrame;+ retire(atomic_exchange(&sg_active, next));+ pthread_mutex_unlock(&sg_control);+ return YES;+}+void SGPCMReplacementCancel(void) {+ pthread_mutex_lock(&sg_control);+ SGPlayerInvalidateSource();+ retire(atomic_exchange(&sg_active, NULL));+ pthread_mutex_unlock(&sg_control);+}+uint64_t SGPCMReplacementFrames(void) { return atomic_load(&sg_replaced); }+void SGPCMReplacementApply(AudioUnit output, uint64_t epoch, double sourceFrame,+ double sampleRate, UInt32 frames, AudioBufferList *data) {+ atomic_fetch_add(&sg_readers, 1);+ Snapshot *s = atomic_load(&sg_active);+ if (!s || !frames || !data || data->mNumberBuffers != 2 || output != s->output || epoch != s->epoch+ || epoch != SGPlayerSourceEpoch() || sampleRate != s->rate || !isfinite(sourceFrame)+ || sourceFrame < s->sourceFrame || sourceFrame >= (double)(1ULL<<53) || floor(sourceFrame) != sourceFrame) goto done;+ uint64_t delta = (uint64_t)sourceFrame - s->sourceFrame;+ if (delta > s->length - s->assetFrame || frames > s->length - s->assetFrame - delta) goto done;+ for (UInt32 b = 0; b < 2; ++b) {+ if (!data->mBuffers[b].mData || data->mBuffers[b].mNumberChannels != 1+ || data->mBuffers[b].mDataByteSize / sizeof(float) < frames) goto done;+ }+ uint64_t at = s->assetFrame + delta;+ memcpy(data->mBuffers[0].mData, s->left+at, frames*sizeof(float));+ memcpy(data->mBuffers[1].mData, s->right+at, frames*sizeof(float));+ atomic_fetch_add(&sg_replaced, frames);+done:+ atomic_fetch_sub(&sg_readers, 1);+}diff --git a/tweak/Sources/Shared/Lossless/SGPreparedPCM.h b/tweak/Sources/Shared/Lossless/SGPreparedPCM.hnew file mode 100644index 00000000..a3f3a827--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGPreparedPCM.h@@ -0,0 +1,21 @@+#import <Foundation/Foundation.h>++NS_ASSUME_NONNULL_BEGIN+// Worker-owned native decode of a complete local lossless file. Not a progressive downloader.+@interface SGPCMDecodeCancellation : NSObject+- (void)cancel;+@property(nonatomic, readonly, getter=isCancelled) BOOL cancelled;+@end++@interface SGPreparedPCM : NSObject+@property(nonatomic, readonly) double sampleRate;+@property(nonatomic, readonly) uint64_t frameCount;+@property(nonatomic, readonly) const float *left;+@property(nonatomic, readonly) const float *right;+// Call off the render/main threads. Only stereo FLAC/ALAC, decoded to planar Float32. maxBytes is+// a PCM allocation budget (at most 32 MiB); encoded files are limited to 64 MiB. Cancellation is+// checked between native reads. The caller retains this object while its pointers are used.++ (nullable instancetype)prepareURL:(NSURL *)url sampleRate:(double)rate maxBytes:(NSUInteger)maxBytes+ cancellation:(SGPCMDecodeCancellation *)cancellation error:(NSError **)error;+@end+NS_ASSUME_NONNULL_ENDdiff --git a/tweak/Sources/Shared/Lossless/SGPreparedPCM.m b/tweak/Sources/Shared/Lossless/SGPreparedPCM.mnew file mode 100644index 00000000..9e76014a--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGPreparedPCM.m@@ -0,0 +1,92 @@+#import "SGPreparedPCM.h"+#import <AudioToolbox/AudioToolbox.h>+#import <stdatomic.h>+#include <math.h>++@implementation SGPCMDecodeCancellation {+ atomic_bool _cancelled;+}+- (void)cancel { atomic_store(&_cancelled, true); }+- (BOOL)isCancelled { return atomic_load(&_cancelled); }+@end++static void fail(NSError **error, NSInteger code, NSString *message) {+ if (error) *error = [NSError errorWithDomain:@"VitrineLosslessDecode" code:code+ userInfo:@{NSLocalizedDescriptionKey:message}];+}++@implementation SGPreparedPCM {+ float *_storage;+ uint64_t _capacity;+}++ (instancetype)prepareURL:(NSURL *)url sampleRate:(double)rate maxBytes:(NSUInteger)maxBytes+ cancellation:(SGPCMDecodeCancellation *)cancellation error:(NSError **)error {+ if (error) *error = nil;+ if (cancellation.cancelled) { fail(error, 1, @"Preparation cancelled."); return nil; }+ if (!url.isFileURL || !isfinite(rate) || rate < 8000 || rate > 192000 || maxBytes < 8192 || maxBytes > 32*1024*1024) {+ fail(error, 2, @"Unsupported file, sample rate or PCM budget."); return nil;+ }+ NSNumber *regular = nil, *fileSize = nil;+ [url getResourceValue:®ular forKey:NSURLIsRegularFileKey error:nil];+ [url getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];+ if (!regular.boolValue || !fileSize || fileSize.unsignedLongLongValue > 64*1024*1024) {+ fail(error, 2, @"Expected a complete local file within the encoded size limit."); return nil;+ }+ ExtAudioFileRef file = NULL;+ OSStatus status = ExtAudioFileOpenURL((__bridge CFURLRef)url, &file);+ if (status) { fail(error, status, @"The native decoder could not open the file."); return nil; }+ AudioStreamBasicDescription source = {0}; UInt32 size = sizeof(source);+ status = ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileDataFormat, &size, &source);+ SInt64 sourceFrames = 0; size = sizeof(sourceFrames);+ if (!status) status = ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileLengthFrames, &size, &sourceFrames);+ BOOL formatOK = (source.mFormatID == kAudioFormatFLAC || source.mFormatID == kAudioFormatAppleLossless)+ && source.mChannelsPerFrame == 2 && isfinite(source.mSampleRate) && source.mSampleRate >= 8000+ && source.mSampleRate <= 192000 && sourceFrames > 0;+ double expected = formatOK ? ceil(sourceFrames * rate / source.mSampleRate) : 0;+ uint64_t limit = maxBytes / (2 * sizeof(float));+ if (status || !formatOK || !isfinite(expected) || expected < 1 || expected > limit) {+ ExtAudioFileDispose(file);+ fail(error, status ?: 3, @"The file is not supported stereo lossless audio within the PCM budget."); return nil;+ }+ AudioStreamBasicDescription target = {rate, kAudioFormatLinearPCM,+ kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved, 4, 1, 4, 2, 32, 0};+ status = ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, sizeof(target), &target);+ SGPreparedPCM *result = [SGPreparedPCM new];+ result->_sampleRate = rate;+ result->_capacity = MIN(limit, (uint64_t)expected + 4096);+ result->_storage = status ? NULL : calloc((size_t)result->_capacity * 2, sizeof(float));+ if (!status && !result->_storage) status = kAudio_MemFullError;+ while (!status && !cancellation.cancelled) {+ uint64_t at = result->_frameCount;+ UInt32 frames = (UInt32)MIN(4096, result->_capacity - at);+ if (!frames) {+ // A file that exactly fills its budget still needs an EOF check, without allocating more.+ float left, right;+ struct { AudioBufferList list; AudioBuffer second; } tail = {{2, {{1,4,&left}}}, {1,4,&right}};+ UInt32 extra = 1;+ status = ExtAudioFileRead(file, &extra, &tail.list);+ if (!status && extra) status = kAudio_ParamError;+ break;+ }+ struct { AudioBufferList list; AudioBuffer second; } buffers = {+ {2, {{1, frames*4, result->_storage+at}}}, {1, frames*4, result->_storage+result->_capacity+at}};+ status = ExtAudioFileRead(file, &frames, &buffers.list);+ if (status || !frames) break;+ for (UInt32 i = 0; i < frames; ++i) {+ if (!isfinite(result->_storage[at+i]) || !isfinite(result->_storage[result->_capacity+at+i])) {+ status = kAudio_ParamError; break;+ }+ }+ result->_frameCount += frames;+ }+ ExtAudioFileDispose(file);+ if (cancellation.cancelled) { fail(error, 1, @"Preparation cancelled."); return nil; }+ if (status || fabs((double)result->_frameCount - expected) > 1) {+ fail(error, status ?: 4, @"The file did not decode completely within its declared length and budget."); return nil;+ }+ return result;+}+- (const float *)left { return _storage; }+- (const float *)right { return _storage + _capacity; }+- (void)dealloc { free(_storage); }+@enddiff --git a/tweak/Sources/Shared/Lossless/SGSourceEvents.x b/tweak/Sources/Shared/Lossless/SGSourceEvents.xnew file mode 100644index 00000000..487ea98c--- /dev/null+++ b/tweak/Sources/Shared/Lossless/SGSourceEvents.x@@ -0,0 +1,103 @@+// Application discontinuities invalidate the source selector's epoch, so a lossless anchor stops at once. Runs+// with lossless on, and in driver (FLEX) builds, whose anchor log records these events.+#import "Core/SGCore.h"+#import "SGLossless.h"+#import <AVFoundation/AVFoundation.h>+#import <mach/mach_time.h>+#import "Shared/Player/PlayerState.h"+#import "Shared/Player/SpeedPitch.h"+#import "Shared/Sing/Sing.h"+#import "SGAnchor.h"++static void event(NSString *kind, NSString *evidence) {+ SGPlayerInvalidateSource();+ uint64_t epoch = SGPlayerSourceEpoch(), host = mach_absolute_time();+ dispatch_block_t log = ^{ SGAnchorLog(kind, evidence, epoch, host); };+ if (NSThread.isMainThread) log();+ else dispatch_async(dispatch_get_main_queue(), log);+}++@interface SGSourceEvents : NSObject <SGPlayerStateObserver>+@end+@implementation SGSourceEvents {+ NSString *_track;+ BOOL _known, _paused, _playing;+ double _position, _duration, _at, _speed;+}+- (void)playerStateDidChange:(SPTPlayerState *)state {}+- (void)playerStateDidReport:(SPTPlayerState *)state {+ NSString *track = SGURIString(state.track.URI) ?: @"-";+ double now = NSProcessInfo.processInfo.systemUptime;+ double position = SGSingSourcePosition(state);+ double speed = state.playbackSpeed;+ if (!_known || ![track isEqualToString:_track]) {+ event(@"track", @"reported");+ } else {+ if (state.isPaused != _paused || state.isPlaying != _playing)+ event(state.isPaused || !state.isPlaying ? @"pause" : @"resume", @"reported");+ // Commands are observed separately. This also catches remote seeks and a same-URI wrap.+ // A wrap near the end with repeat enabled is evidence of a repeat, not a recording id.+ double expected = _position + (_playing && !_paused ? (now - _at) * _speed : 0);+ if (isfinite(position) && position >= 0 && _position >= 0 && fabs(position - expected) > 0.5) {+ BOOL repeat = state.options.repeatingTrack && _duration > 0+ && expected >= _duration - 2 && position < 2 && position < _position;+ event(repeat ? @"repeat" : @"position_jump", @"inferred");+ }+ }+ _known = YES;+ _track = track;+ _paused = state.isPaused;+ _playing = state.isPlaying;+ _position = position;+ _duration = state.duration;+ _at = now;+ _speed = speed;+}+@end++// These terminal command selectors are already evidenced and hooked by Shared/Sing/Sing.x.+// Shorter seeks and skips forward here, so a single command is counted once.+%hook SPTEsperantoPlayer+- (id)seekTo:(double)position relative:(long long)relative options:(id)options creatorTimestampPositionMs:(double)creator {+ event(@"seek", @"command");+ return %orig;+}+- (id)skipToNextTrackWithOptions:(id)options track:(id)track loggingParams:(id)params {+ event(@"skip", @"next_command");+ return %orig;+}+- (id)skipToPreviousTrackWithOptions:(id)options track:(id)track loggingParams:(id)params {+ event(@"skip", @"previous_command");+ return %orig;+}+- (id)stop {+ event(@"stop", @"command");+ return %orig;+}+%end++%ctor {+ #if !SG_DRIVER+ if (!SGHidden(SGKeyLossless)) return;+ #endif+ %init;+ dispatch_async(dispatch_get_main_queue(), ^{+ static SGSourceEvents *observer;+ observer = [SGSourceEvents new];+ SGAddPlayerStateObserver(observer);+ if (SGPlayerState()) [observer playerStateDidReport:SGPlayerState()];+ static BOOL sing;+ sing = SGSingOn();+ [NSNotificationCenter.defaultCenter addObserverForName:SGSingChangedNotification object:nil queue:nil usingBlock:^(NSNotification *note) {+ dispatch_block_t changed = ^{+ BOOL on = SGSingOn();+ if (on != sing) { sing = on; event(on ? @"sing_on" : @"sing_off", @"reported"); }+ };+ if (NSThread.isMainThread) changed();+ else dispatch_async(dispatch_get_main_queue(), changed);+ }];+ [NSNotificationCenter.defaultCenter addObserverForName:AVAudioSessionRouteChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {+ event(@"route", [NSString stringWithFormat:@"reason_%lu", (unsigned long)[note.userInfo[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue]]);+ }];+ });+}diff --git a/tweak/Sources/Shared/Player/PlayerState.h b/tweak/Sources/Shared/Player/PlayerState.hindex 4b6e0dcf..8ff74543 100644--- a/tweak/Sources/Shared/Player/PlayerState.h+++ b/tweak/Sources/Shared/Player/PlayerState.h@@ -17,6 +17,9 @@ NSString *SGURIString(id uri);@protocol SGPlayerStateObserver <NSObject>// Called when the track, the context, paused, playing, loading or shuffle changed, not for position.- (void)playerStateDidChange:(SPTPlayerState *)state;+@optional+// Every accepted report, including position-only updates. Called before the change callback.+- (void)playerStateDidReport:(SPTPlayerState *)state;@end// Observers are held weakly and need no removal.void SGAddPlayerStateObserver(id<SGPlayerStateObserver> observer);diff --git a/tweak/Sources/Shared/Player/PlayerState.x b/tweak/Sources/Shared/Player/PlayerState.xindex a9e8ad71..93415daa 100644--- a/tweak/Sources/Shared/Player/PlayerState.x+++ b/tweak/Sources/Shared/Player/PlayerState.x@@ -34,6 +34,9 @@ static NSString *keyOf(SPTPlayerState *state) {static void publish(SPTPlayerState *state) {sg_playerState = state;+ for (id<SGPlayerStateObserver> observer in sg_stateObservers.allObjects) {+ if ([observer respondsToSelector:@selector(playerStateDidReport:)]) [observer playerStateDidReport:state];+ }NSString *key = keyOf(state);if ([key isEqualToString:sg_stateKey]) return;sg_stateKey = key;diff --git a/tweak/Sources/Shared/Player/SpeedPitch.h b/tweak/Sources/Shared/Player/SpeedPitch.hindex 9a4f14d8..f6dbb9e1 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.h+++ b/tweak/Sources/Shared/Player/SpeedPitch.h@@ -74,6 +74,20 @@ typedef double (*SGPlayerHeldLead)(void);typedef double (*SGPlayerLeadOf)(SPTPlayerState *state);void SGPlayerSetLeadReaders(SGPlayerHeldLead held, SGPlayerLeadOf of);+// Experimental source selector, before the existing stage. Registered callback code must live for the+// process lifetime. Called after each successful, non-silent native mixer pull on the music output only.+// The native mixer is still drained exactly once. data is planar Float32 in the client format; replace+// a complete span or leave it untouched. No allocation, file I/O, Objective-C or waits here.+// sourceFrame is an output-local counter, not track position. The caller must prove a recording/frame+// anchor before enabling replacement. No production caller currently does so.+typedef void (*SGPlayerSourceSelector)(AudioUnit output, uint64_t epoch, double sourceFrame,+ double sampleRate, UInt32 frames, AudioBufferList *data);+void SGPlayerSetSourceSelector(SGPlayerSourceSelector selector);+uint64_t SGPlayerSourceEpoch(void);+// Any thread, including render. Invalidate on every seek/skip/recording occurrence/policy cancellation.+// Audio-output lifecycle changes also invalidate internally. An already admitted render may finish.+void SGPlayerInvalidateSource(void);+// The music's output: of the RemoteIO units Spotify runs at once (a chain per sample rate, and voice search's),// the one that carries every processor of its sound (speed and pitch, Sing's stage, the audio effects, Music// Haptics), NULL until Spotify connects or starts one. Any thread; the render thread compares it with a renderdiff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.xindex 18d3c14c..c65a9d2d 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.x+++ b/tweak/Sources/Shared/Player/SpeedPitch.x@@ -70,6 +70,7 @@#import "PlayerState.h"#import "SpeedPitch.h"#import "SGTimePitch.h"+#import "Shared/Lossless/SGAnchor.h"// The unit stays in this long after speed and pitch both came back to normal.static const double kOffAfter = 1.5;@@ -101,6 +102,9 @@ typedef struct {UInt32 sourceBus;atomic_bool taken;atomic_uint chunk;+ #if SG_DRIVER+ SGAnchorRing anchor;+ #endifFloat64 sourceTime; // its render thread only// Spotify's side (input scope, element 0), which the callback fills, and the hardware's (output scope),// which render notifies get.@@ -125,6 +129,15 @@ static _Atomic(SGTimePitch *) sg_pull, sg_inPlace;static atomic_bool sg_engaged;static pthread_mutex_t sg_buildLock = PTHREAD_MUTEX_INITIALIZER;static _Atomic(SGPlayerStage) sg_stage;+static _Atomic(SGPlayerSourceSelector) sg_sourceSelector;+static atomic_uint_fast64_t sg_sourceEpoch = 1;++uint64_t SGPlayerSourceEpoch(void) { return atomic_load(&sg_sourceEpoch); }+void SGPlayerInvalidateSource(void) { atomic_fetch_add(&sg_sourceEpoch, 1); }+void SGPlayerSetSourceSelector(SGPlayerSourceSelector selector) {+ SGPlayerInvalidateSource();+ atomic_store(&sg_sourceSelector, selector);+}static double sg_secondsPerTick;static OSStatus (*sg_setProperty)(AudioUnit, AudioUnitPropertyID, AudioUnitScope, AudioUnitElement, const void *, UInt32);@@ -204,8 +217,40 @@ static OSStatus pullSource(Output *output, AudioUnit source, const AudioTimeStamtime.mSampleTime = output->sourceTime;time.mFlags |= kAudioTimeStampSampleTimeValid;AudioUnitRenderActionFlags flags = 0;+ uint64_t epoch = SGPlayerSourceEpoch();+ double sourceFrame = output->sourceTime;+ #if SG_DRIVER+ uint64_t anchorHost = mach_absolute_time();+ #endifOSStatus status = AudioUnitRender(source, &flags, &time, output->sourceBus, count, target);output->sourceTime += count;+ // This counter advances through silent/error pulls, whereas the recording may not. Once a+ // native stall occurs, a future playback owner must establish a fresh recording/frame anchor.+ // Conservatively invalidate even natural silence; never resume an external cursor by guessing.+ BOOL observesSource = atomic_load(&sg_sourceSelector) != NULL;+ #if SG_DRIVER+ observesSource = YES;+ #endif+ if ((status != noErr || (flags & kAudioUnitRenderAction_OutputIsSilence))+ && observesSource && output == music()) SGPlayerInvalidateSource();+ #if SG_DRIVER+ if (output == music()) {+ double rate = rateOf(&output->client);+ // About ten samples per second at the current speed. Sing's refill may burst.+ double speed = MAX(0.25, loadFloat(&sg_speedBits));+ unsigned every = (unsigned)MAX(1, round(rate * speed / (10.0 * count)));+ SGAnchorRing *ring = &output->anchor;+ if (++ring->pulls >= every || ring->lastEpoch != epoch) {+ SGAnchorSample sample = {sourceFrame, rate, anchorHost, epoch,+ (uint64_t)(uintptr_t)atomic_load(&output->unit), count, status,+ (flags & kAudioUnitRenderAction_OutputIsSilence) != 0};+ if (SGAnchorPush(ring, sample)) {+ ring->pulls = 0;+ ring->lastEpoch = epoch;+ }+ }+ }+ #endifif (status != noErr) return status;// Asked to stop at silence: Spotify has none of its sound from here on yet, so the rest is not rendered.if ((flags & kAudioUnitRenderAction_OutputIsSilence) && sounding) {@@ -223,6 +268,20 @@ static OSStatus pullSource(Output *output, AudioUnit source, const AudioTimeStamdouble rate = rateOf(&output->client);if (rate > 0) atomic_fetch_add_explicit(&sg_playedMicros, (uint64_t)(count * 1e6 / rate), memory_order_relaxed);}+ SGPlayerSourceSelector selector = atomic_load(&sg_sourceSelector);+ if (selector && output == music()) {+ // Keep output ownership based on Spotify's original samples, not the replacement's sound.+ BOOL audible = NO;+ for (UInt32 b = 0; !audible && b < target->mNumberBuffers; ++b) {+ const float *samples = target->mBuffers[b].mData;+ for (UInt32 i = 0; samples && i < target->mBuffers[b].mDataByteSize / sizeof(float); ++i) {+ if (fabsf(samples[i]) > 1e-4f) { audible = YES; break; }+ }+ }+ if (audible) atomic_store_explicit(&output->loudAt, mach_absolute_time(), memory_order_relaxed);+ if (!(flags & kAudioUnitRenderAction_OutputIsSilence))+ selector(atomic_load(&output->unit), epoch, sourceFrame, rateOf(&output->client), count, target);+ }done += count;}return noErr;@@ -428,7 +487,8 @@ static OSStatus rendered(void *refCon, AudioUnitRenderActionFlags *flags, constUInt32 frames, AudioBufferList *data) {if (!(*flags & kAudioUnitRenderAction_PostRender) || bus != 0 || !data || !data->mNumberBuffers || !frames) return noErr;Output *output = refCon;- if (hasSound(&output->hardware, *flags, data)) atomic_store_explicit(&output->loudAt, mach_absolute_time(), memory_order_relaxed);+ BOOL sourceMetered = atomic_load(&sg_sourceSelector) && atomic_load(&output->taken) && output == music();+ if (!sourceMetered && hasSound(&output->hardware, *flags, data)) atomic_store_explicit(&output->loudAt, mach_absolute_time(), memory_order_relaxed);if (output != music()) return noErr;applyGain(&output->hardware, flags, frames, data);if (atomic_load(&output->taken) || !atomic_load(&sg_engaged)) return noErr;@@ -610,6 +670,7 @@ static void reselect(BOOL always, NSString *why) {SGLog(@"audio: the processors move from %@ to %@%@", describe(previous), describe(next), why ? [@", " stringByAppendingString:why] : @"");}if (next != previous || always) {+ SGPlayerInvalidateSource();tellWatchers();applyToFormat();}@@ -628,6 +689,7 @@ static void formatChanged(void *refCon, AudioUnit unit, AudioUnitPropertyID propif (scope != kAudioUnitScope_Input && scope != kAudioUnitScope_Output) return;// Off the thread setting it: settling may set the unit's connection, which is not done from inside its own listener.Output *output = refCon;+ if (output == music()) SGPlayerInvalidateSource();dispatch_async(sg_outputsQueue, ^{pthread_mutex_lock(&sg_outputsLock);if (atomic_load(&output->unit) == unit) {@@ -650,6 +712,7 @@ static void sourceChanged(AudioUnit unit) {Output *output = &sg_outputs[i];AudioUnit outputUnit = atomic_load(&output->unit);if (!outputUnit || atomic_load(&output->source) != unit) continue;+ if (output == music()) SGPlayerInvalidateSource();dispatch_async(sg_outputsQueue, ^{pthread_mutex_lock(&sg_outputsLock);AudioStreamBasicDescription client, hardware;@@ -667,11 +730,17 @@ static OSStatus setProperty(AudioUnit unit, AudioUnitPropertyID property, AudioUconst void *data, UInt32 size) {if (property != kAudioUnitProperty_MakeConnection || scope != kAudioUnitScope_Input || element != 0 || !data|| size < sizeof(AudioUnitConnection) || !isRemoteIO(unit)) {+ // Invalidate before applying a format change: Core Audio's listener may arrive later. Even+ // setting the same format conservatively invalidates a prepared source's negotiated contract.+ Output *current = music();+ if (property == kAudioUnitProperty_StreamFormat && current+ && (unit == atomic_load(¤t->unit) || unit == atomic_load(¤t->source))) SGPlayerInvalidateSource();OSStatus status = sg_setProperty(unit, property, scope, element, data, size);if (status == noErr && (property == kAudioUnitProperty_StreamFormat || property == kAudioUnitProperty_MaximumFramesPerSlice)) sourceChanged(unit);return status;}const AudioUnitConnection *connection = data;+ SGPlayerInvalidateSource();pthread_mutex_lock(&sg_outputsLock);Output *output = outputFor(unit);if (!output) {@@ -757,6 +826,7 @@ static OSStatus startOutput(AudioUnit unit) {static OSStatus (*sg_stopOutput)(AudioUnit unit);static OSStatus stopOutput(AudioUnit unit) {+ if (unit == SGPlayerMusicOutput()) SGPlayerInvalidateSource();OSStatus status = sg_stopOutput(unit);pthread_mutex_lock(&sg_outputsLock);Output *output = outputOf(unit);@@ -773,6 +843,7 @@ static OSStatus (*sg_dispose)(AudioComponentInstance unit);// Every output fed by the unit loses it, and the unit's own output is forgotten, before it goes; renders in// progress end first.static OSStatus disposeUnit(AudioComponentInstance unit) {+ SGPlayerInvalidateSource();pthread_mutex_lock(&sg_outputsLock);BOOL changed = NO;for (int i = 0; i < kMaxOutputs; i++) {@@ -1137,3 +1208,28 @@ BOOL SGPlayerWatchMusicOutput(SGPlayerOutputWatcher watcher) {%init;SGRequireClasses(@[@"SPTPlayerState"]);}++#if SG_DRIVER+bool SGPlayerAnchorRead(SGAnchorSample *latest, unsigned *dropped) {+ bool found = false;+ *dropped = 0;+ for (unsigned i = 0; i < kMaxOutputs; ++i) {+ SGAnchorSample sample;+ while (SGAnchorPop(&sg_outputs[i].anchor, &sample)) {+ if (!found || sample.host >= latest->host) *latest = sample;+ found = true;+ }+ *dropped += atomic_load_explicit(&sg_outputs[i].anchor.dropped, memory_order_relaxed);+ }+ return found;+}++double SGPlayerAnchorLatency(bool *engaged) {+ pthread_mutex_lock(&sg_buildLock);+ *engaged = atomic_load(&sg_engaged);+ SGTimePitch *unit = atomic_load(tapped() ? &sg_pull : &sg_inPlace);+ double latency = *engaged && unit ? SGTimePitchLatency(unit) : 0;+ pthread_mutex_unlock(&sg_buildLock);+ return latency;+}+#endifdiff --git a/tweak/Sources/Shared/Sing/Sing.h b/tweak/Sources/Shared/Sing/Sing.hindex dfc0c56d..8d2fbb32 100644--- a/tweak/Sources/Shared/Sing/Sing.h+++ b/tweak/Sources/Shared/Sing/Sing.h@@ -174,3 +174,10 @@ void SGSingRemoveOldModel(void);NSArray<NSString *> *SGSingComputeUnitNames(void);// Whether this OS can load the model (iOS 18). Memory is checked as the model loads (SGSingLoader.m).BOOL SGSingOSSupported(void);++// Spotify's position before Sing's lead correction, for discontinuity observation. Off render.+double SGSingSourcePosition(SPTPlayerState *state);+#if SG_DRIVER+// Whether the most recent stage call entered Sing's engine, rather than passing straight through.+BOOL SGSingAnchorStageActive(void);+#endifdiff --git a/tweak/Sources/Shared/Sing/Sing.x b/tweak/Sources/Shared/Sing/Sing.xindex 3bd0ed95..e89472b7 100644--- a/tweak/Sources/Shared/Sing/Sing.x+++ b/tweak/Sources/Shared/Sing/Sing.x@@ -145,8 +145,16 @@ static OSStatus pullChain(void *context, UInt32 frames, float *left, float *righreturn chain->pull(chain->context, frames, &buffers.list, sounding);}+#if SG_DRIVER+static atomic_bool sg_anchorStageActive;+BOOL SGSingAnchorStageActive(void) { return atomic_load(&sg_anchorStageActive); }+#endif+static OSStatus stage(UInt32 frames, AudioBufferList *data, SGPlayerPull pull, void *context) {SGSingEngine *engine = atomic_load_explicit(&sg_engine, memory_order_acquire);+ #if SG_DRIVER+ atomic_store(&sg_anchorStageActive, false);+ #endifif (!engine) return pull(context, frames, data, NULL);atomic_store_explicit(&sg_staged, true, memory_order_relaxed);atomic_store_explicit(&sg_rendered, true, memory_order_relaxed);@@ -158,6 +166,9 @@ static OSStatus stage(UInt32 frames, AudioBufferList *data, SGPlayerPull pull, vatomic_store_explicit(&sg_formatRefused, true, memory_order_relaxed);return pull(context, frames, data, NULL);}+ #if SG_DRIVER+ atomic_store(&sg_anchorStageActive, true);+ #endifChainPull chain = {pull, context};return SGSingEngineRender(engine, frames, data->mBuffers[0].mData, data->mBuffers[1].mData, pullChain, &chain);}@@ -767,6 +778,8 @@ static double rawPosition(SPTPlayerState *state) {return position;}+double SGSingSourcePosition(SPTPlayerState *state) { return rawPosition(state); }+double SGSingHeldLead(void) {return SGSingEngineLead(atomic_load_explicit(&sg_engine, memory_order_acquire));}