diff --git a/.github/workflows/harmony-fast-dsp.yml b/.github/workflows/harmony-fast-dsp.yml new file mode 100644 index 000000000..cf4324086 --- /dev/null +++ b/.github/workflows/harmony-fast-dsp.yml @@ -0,0 +1,39 @@ +name: Harmony native DSP fast verification +on: + pull_request: + types: [opened, reopened, synchronize] + paths: + - 'engine/builtin_fx/Harmony*.h' + - 'tests/harmony*.cpp' + - 'CMakeLists.txt' + - '.github/workflows/harmony-fast-dsp.yml' +permissions: + contents: read +concurrency: + group: harmony-fast-${{ github.event.pull_request.number }} + cancel-in-progress: true +jobs: + harmony-cpp: + name: Native DSP compile and PCM regression (Linux, no JUCE) + runs-on: ubuntu-latest + timeout-minutes: 7 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: Build and run synchronous vocal fixture + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_pitch_synchronous_test.cpp -o /tmp/arson-harmony-psola + /tmp/arson-harmony-psola + - name: Compile and run complete engine dry, pitch, stereo and graph-independent PCM fixtures + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_dsp_test.cpp -o /tmp/arson-harmony-engine + /tmp/arson-harmony-engine + - name: Verify stage separation, mono and exact PCM + run: | + g++ -std=c++20 -O2 -Wall -Wextra tests/harmony_stage_spatial_test.cpp -o /tmp/arson-harmony-spatial + /tmp/arson-harmony-spatial + - name: Compile and exercise native offline listening A/B renderer + run: | + g++ -std=c++20 -O2 -Wall -Wextra tools/harmony_offline_ab.cpp -o /tmp/arson-harmony-ab + /tmp/arson-harmony-ab --self-test diff --git a/CMakeLists.txt b/CMakeLists.txt index 622f606aa..62e5f4879 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,9 +115,22 @@ if(BUILD_TESTING) add_executable(ARMixFxStructuralHandoffTests tests/live_fx_structural_handoff_test.cpp) target_compile_features(ARMixFxStructuralHandoffTests PRIVATE cxx_std_20) add_test(NAME R0BStructuralFxAckAndControlThreadRetirement COMMAND ARMixFxStructuralHandoffTests) + add_executable(ARMixHarmonyOfflineAb tools/harmony_offline_ab.cpp) + target_compile_features(ARMixHarmonyOfflineAb PRIVATE cxx_std_20) + add_test(NAME NativeHarmonyOfflineDryAndABRenderHarness COMMAND ARMixHarmonyOfflineAb --self-test) + add_executable(ARMixHarmonyPitchSyncTests tests/harmony_pitch_synchronous_test.cpp) + target_compile_features(ARMixHarmonyPitchSyncTests PRIVATE cxx_std_20) + add_test(NAME NativeHarmonyPitchSynchronousFormantCandidateAndBlockParity COMMAND ARMixHarmonyPitchSyncTests) + add_executable(ARMixHarmonySpatialTests tests/harmony_stage_spatial_test.cpp) + target_compile_features(ARMixHarmonySpatialTests PRIVATE cxx_std_20) + add_test(NAME NativeHarmonyBackingStageSpatialDepthAndMonoPDC COMMAND ARMixHarmonySpatialTests) add_executable(ARMixHarmonyDspTests tests/harmony_dsp_test.cpp) target_compile_features(ARMixHarmonyDspTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyMultivoicePitchAndRealtimeFinitePCM COMMAND ARMixHarmonyDspTests) + # Non-gating synthetic engine-only CPU profile (not ASIO certification). + add_executable(ARMixHarmonyCpuProfileTests tests/harmony_cpu_profile_test.cpp) + target_compile_features(ARMixHarmonyCpuProfileTests PRIVATE cxx_std_20) + add_test(NAME NativeHarmonySyntheticCallbackCpuProfile COMMAND ARMixHarmonyCpuProfileTests) add_executable(ARMixHarmonyMusicGuideTests tests/harmony_music_key_test.cpp) target_compile_features(ARMixHarmonyMusicGuideTests PRIVATE cxx_std_20) add_test(NAME NativeHarmonyCrossChannelMusicKeyAndManualFallback COMMAND ARMixHarmonyMusicGuideTests) @@ -129,7 +142,7 @@ if(BUILD_TESTING) add_test(NAME ApprovedNativeHarmonyEditorAckAndSingleGui COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_harmony_assembly_test.py") add_executable(ARMixMixerRoutingTests tests/mixer_routing_plan_test.cpp) target_compile_features(ARMixMixerRoutingTests PRIVATE cxx_std_20) - add_dependencies(ARMixMixerRoutingTests ARMixHarmonyDspTests ARMixHarmonyMusicGuideTests ARMixHarmonyChannelPcmTests) + add_dependencies(ARMixMixerRoutingTests ARMixHarmonyDspTests ARMixHarmonyMusicGuideTests ARMixHarmonyChannelPcmTests ARMixHarmonyCpuProfileTests) add_test(NAME TenStripStaticRoutingAndMaster COMMAND ARMixMixerRoutingTests) # M3D atomic native Session: the accepted Console roster cannot advance # without a successful epoch/revision-gated PCM publication. diff --git a/docs/ARD_ARSON_HARMONY_DEPTH_ENGINE_20261109.md b/docs/ARD_ARSON_HARMONY_DEPTH_ENGINE_20261109.md new file mode 100644 index 000000000..6b1d7fe0c --- /dev/null +++ b/docs/ARD_ARSON_HARMONY_DEPTH_ENGINE_20261109.md @@ -0,0 +1,133 @@ +# ARD — ArSon Harmony FX Backing-Only Spatial Depth Engine (v1.0) +**Date:** 2026-11-09 · **Status:** Proposed architecture with staged implementation checks +**Scope:** Native built-in Harmony on PR #487; companion [PRD](PRD_ARSON_HARMONY_NATURAL_DEPTH_20261109.md) and [Natural Ensemble](ARSON_HARMONY_NATURAL_ENSEMBLE_ARCHITECTURE_20261109.md). + +## Evidence-led acoustic model +Human auditory distance relies on **direct/reverberant energy ratio**, overall source level, spectral balance and interaural cues. Increasing reverberant ratio commonly signals greater distance; absolute physical distance cannot be inferred from a simple Space knob. First-arriving direct sound determines much of localization (precedence); a small number of obvious repeats can produce slap/comb effects rather than a coherent room. More high-frequency absorption is a mixing cue, but **air absorption over just a few metres does not justify extreme LPF**. For a live singer, masking and consonant smearing are unacceptable. + +- Bronkhorst & Houtgast, *Nature* 1999, distance versus DRR: https://www.nature.com/articles/17374 +- Zahorik review, distance, intensity, DRR, spectral and interaural cues: https://pmc.ncbi.nlm.nih.gov/articles/PMC4744263/ +- Litovsky et al. review of the precedence effect: https://pmc.ncbi.nlm.nih.gov/articles/PMC4310855/ +- JASA 2025 frontal distance study (DRR or spectral centroid best single cue): https://pubmed.ncbi.nlm.nih.gov/40928551/ +- Sound on Sound depth mixing (level/EQ/diffuse reverb versus clarity): https://www.soundonsound.com/techniques/creating-sense-depth-your-mix +- iZotope mix depth: https://www.izotope.com/community/blog/what-is-mix-depth-how-to-create-front-back-space +- FDN theory / realtime late room: https://artificial-audio.github.io/portfolio/fdn/ + +## Before: audited exact C++ issue +`engine/builtin_fx/HarmonyDsp.h::process` mixes lead with +`(1.f - wet_*0.65f*voicedGain_)` despite the lead being dry; at Wet=.45 / voicedGain=1 this is about -3 dB lead. This is **gain duck**, not proof of phase cancellation. Current `HarmonyEnsembleSpace.h` adds one early opposite-side tap/voice but has **no diffuse late field**. At most it generates scattered echoes and pan; it does not control DRR as an acoustic scene. + +## One-source, one-room block graph +```text +Mono mic PCM + accepted ChannelId / native graph + | + +--> phase-safe analysis / voice targets --> pitch shifters (2-5) + | | + | +--> independent singer onset / timbre / pitch modulation + | | + | +--> short direct backing L/R (Space-dependent attenuation) + | +--> per-singer fixed-tap filtered early reflections + | +--> ONE shared small stereo diffuse late field [FDN] + | (backing send ONLY; fixed delay, stable feedback) + | + +--> exact delayed native L/R lead --> optional explicitly enabled Pitch Tune + | | + +-----------------------------> sum w backing L/R + -> explicit Gain -> existing PCM bus +``` +Only existing `Settings::space`, wet, color and voices. No new scene schema, plugin routing, hidden monitor step, GUI Lab, or second ownership. `Space=0` disables audible room, not the valid PCM path. Diffuse field is never fed with lead. + +## Time/frequency strategy +- **Front:** lead unchanged by wet, stereo and sample latency untouched. Pitch Tune explicitly replaces/corrects when user enables it; never implied by Wet. +- **Direct backup:** keep existing individually phase-offset 5–17 ms arrival, deterministic cent modulation and prepared pan L/C/R. At increasing Space, reduce direct backing gain mildly (candidate 0dB to -4dB) so backing remains intelligible but moves behind. +- **Backstage spectral contour:** one sample-rate-normalized low-pass in each voice with a smooth dry/filtered crossfade, evaluated on backing only. Candidate corner around 3–6 kHz and partial dry retention; do not physically claim air propagation at 3 m. +- **Early field:** use existing per-voice 12–20 ms fixed taps, with mild high-frequency damping and side distribution; no modulation of the delay tap by Space (avoids Doppler/comb chirp). +- **Late field:** one shared bounded 4-branch stereo feedback delay network (FDN), fixed integer delay lines with different non-harmonic lengths and a normalized Hadamard feedback transform. Feedback damping and per-line gain computed in non-RT `prepare(rate)` to target about 0.45–0.7 s decay. Conservative energy normalization, filtered output, no runaway or metallic tonal build-up. First prototype is room **ambience**, not cathedral/hall reverb. +- **Control:** `space` interpolates smoothly across width, direct gain, backing brightness, early/late send and return. `wet` scales backing energy only; disabled or Wet=0 must not leak old tail. All coefficients finite and clamped. No audio-owner reallocation or blocking. + +## Proposed bounded C++ data ownership +- `HarmonyEnsembleSpace.h`: per-voice onset, identity modulation and 4096-sample early reflection rings; optional per-voice damping state. Precompute sample-rate coefficients on CONTROL `prepare(rate)`; callback only adds/multiplies. +- `HarmonyDepthRoom.h`: 4 shared, 8192-sample float delay rings (128 KiB); 4 damping states; 4 feedback gains; 1 monotonic write index. Delays up to 41 ms at 192k remain in bounds. A 4x4 orthonormal Hadamard transformation (scale 0.5) ensures controlled feedback energy. No `new`, FFT, `pow`, `sin`, `log`, device calls or synchronization in `processSample`. +- `HarmonyDsp.h`: keep lead path independent of `wet`; accumulate backing direct and early/late sends by voice, shared stereo room returns only in wet branch. Use existing 5-ms control smoothing and fixed instance state. +- Single publisher and callback: preserve bridge ACK/readback/graph lease, `ChannelHarmonyProcessor`, SPSC packets and `latencySamples()`. Declared PDC remains 1536 samples @48k until evidence warrants an independent P0 latency redesign. + +## Risks, failure behavior and test design +| Risk | Defensive behavior / evidence | +|---|---| +| Room self-oscillation / metallic flutter | Precomputed feedback magnitude <1 and damping, orthogonal matrix; impulse decay < target floor by ~2 seconds; 32/44.1/48/96/192k CTest and extended silence | +| Mono cancellation / comb | Positive-polarity forward dry, no artificial anti-phase width; L+R output energy with 2/3/5 voices >0 and lead parity; evaluate ER frequency response with real voice | +| Space automation chirping | Interpolate gains/filter coefficients, **not** delay lengths; test 0->1->0 at arbitrary 64-sample boundaries, output finite without transient spike | +| Lead duck | Unit check dry equals original delayed PCM at wet=0,45,100 and multiple Space; actual lead equation cannot reference Wet | +| Reverb masks lyrics | Limit room gain, high-pass/LP spectrally soften only wet room; compare consonant intelligibility, DRR as measured against current impl | +| Real singer still robot | Keep depth independent; this architecture doesn't solve formant/grain synthesis; P0 resynthesis follows separate proof | +| Device restart / session | `prepare(rate)` resets room/filters and restarts deterministic state, no stale room tail from previous device; maintain GUI native ACK identity | + +## No shortcut quality declaration +Do not equate "FDN compiled" with “feels like three people metres away.” First require baseline/updated dry and backing stems rendered at level parity, mono/stereo blind A/B for perceived front-back ranking, listening through nearfields/headphones/mono laptop speaker, and actual ASIO 48/96 kHz 64/128/256 buffers with full mixer FX rack and CPU p95/p99/XRUN data. The desired response is **distant but distinct, not washed out**. Exact room dimensions and HRTF positioning are not implemented and must not be shown as facts in GUI. + +## 2026-11-09 implementation checkpoint D0–D1 (unqualified acoustically) + +D0 implemented: `HarmonyDsp::leadPath` no longer references Wet; Pitch Tune still explicitly blends corrected lead. Direct backing attenuation is separate from dry lead. D1 implemented: `EnsembleSpace::shadeBacking` adds a rate-correct gentle LP shading to **backing only**; `BackingDepthRoom` adds a 4-branch fixed-delay, precomputed 0.55 s nominal FDN with frequency damping and non-anti-phase stereo outputs. Space smooths original pan, backing attenuation, early-field gain, late send and return. No new controls/schema or device calls; no extra plugin PDC. + +Added CTest gates for 32/44.1/48/96/192 kHz impulse/tail feedback stability, reset/silence/mono compatibility, backing HF-shading boundedness and explicit pitch-tune lead preservation. This is an acoustically *unqualified* room candidate until real listening A/B and physical ASIO load. + +## 2026-11-09 D2 — backing-stage acoustic depth candidate + +Field feedback: the pitch-synchronous singer is now acceptably natural but backing appears too close to the original singer. Keep original lead and pitch resynthesis unchanged. New `HarmonyStageSpatial.h` introduces a fixed 0.22ms far-ear precedence cue, a **second per-voice early reflection** at 27–38ms (first remains 12–20ms), and stronger direct/reverberant separation. Existing ONE shared backing-only 4x4 FDN remains unchanged. + +With Space `s` in [0,1], direct gain is `1-0.62s`, early send `0.49s`, late return `1.08s`. Positive-polarity stereo image; no exact HRTF or claimed physical metres, no extra audio latency or device, no delay-time modulation, no lead processing changes. An offline-only switch retains old spatial routing to compare against the current pitch engine. The one-knob GUI remains unchanged. + +Test `NativeHarmonyBackingStageSpatialDepthAndMonoPDC` validates 32–192k, monotonic early/direct energy, three/five singer first-arrival and mono energy, 64/257 callback parity, declared 1536-sample PDC and Wet0 lead invariance. Aural user validation and physical ASIO xrun still required. + +## 2026-11-09 P1 spatial refinement: frequency-dependent far-ear cue + +After the stage placement milestone, the fixed 0.22ms far-ear precedence cue used the *same full-band vocal* for near and far ears, so lateral placement could still resemble ordinary pan/doubling rather than a believable backing position. The stage now precomputes one gentle 4.1kHz one-pole for the **contralateral-only** short-delay ring. The near-ear direct path, lead PCM, source pitch-synchronous resynthesis, fixed sample PDC, per-singer reflection timing, shared room, and existing Space control are unchanged. No speculative physical 3D/HRTF distance claims. + +At Space 0 the far-ear blend is zero; at higher Space, far ear combines a proportion of the original with a short delayed, spectrally softened replica. Fixed delay and preprepared coefficient prevent audio-control chirps. This is a small independent cue, not new unbounded spectral processing. Synthetic tests compare 350Hz / 8kHz far-to-near energy at 48/96/192k, preserve full-band Space0 parity, plus existing per-singer reflection/DRR, dry polarity, mono and callback-block parity. Real singer/headphones/mono A/B still gate acceptance. + +### 2026-11-09 center backing separation + +The fixed short far-ear shadow cue now combines with a modest, **center-only backing direct** recess. With three singers the middle backing singer can otherwise overlap the dry center-lead image. At Space 1 its center direct level is 0.83 of its original while its reflected and one-room-send components remain unchanged. The effect tapers with physical stage direction and Space (fully neutral at Space 0), preserves side singer direct energy and never changes dry lead, pitch/formant engine or published PDC. Impulse regressions validate actual center direct/room ratio, no full muting or polarity tricks. Subjective quality is still an ear-test. + + +## 2026-11-09 D3 — conservatively focused musical backing room + +`HarmonyRoomFocus.h` is a shared 170Hz high-pass on the BACKING ROOM SEND +only, and a 12ms attack / 240ms release lead-envelope follower that +modulates only the late room return (max 22% attenuation at Space=1). +Its input is the original latency-aligned dry lead, not a second channel. +During sustained lead, late diffusion is gently eased back; between +phrases the late field blooms again. Direct singers, all first reflections, +lead dry PCM and pitch/formant candidate are unaffected. No FFT, lookahead, +extra delay/PDC, GUI knob, saved schema change or realtime allocation. + +Native regression measures the 75/600Hz frequency discrimination at +32/44.1/48/96/192k, bounded attack/release, Space0 unity and restart +clearing. Offline A/B adds `10_room_focus_off_space_060.wav` and +`11_room_focus_off_backing_only.wav` with the SAME stage and pitch +to isolate the room change. Synthetic tests are not a real singer or +physical ASIO qualification. + + +## 2026-11-09 D4 field fix — harmony wet presence and knob optical spacing + +A recorded owner listening session found pitch quality noticeably human, +but backing was too thin even with 4 voices, 100% Wet and Space 60%. +`HarmonyDsp` previously applied `0.65*Wet/sqrt(voices)`, followed by +the existing Space direct attenuation. The new **control-thread** voice +law `Wet*(0.77+0.38*Wet)/sqrt(voices)` raises wet backing roughly ++3.2 dB at Wet45% and +4.95 dB at Wet100%, without ducking the original +dry lead or moving spatial placements. Wet0 is exactly zero; 2–5 voices +retain sqrt-count normalization. This is not a master limiter, and no +physical hardware headroom or singing quality is implied by the formula. +An offline-only before-process() switch produces the prior presence law +for matched-A/B WAV `12_presence_old_space_060.wav` and +`13_presence_old_backing_only.wav`. Synthetic self-test asserts backing +stem RMS increases in the expected bounded range and dry lead parity. + +The screenshot showed COLOR/SPACE/WET/GAIN overlapping the glowing ring: +the pseudo ring extends 11px below the 68/59px knob but the old label +was separated by only a 1px gap. Add a compact 12px top margin and 11px +line height to the caption; preserve all editor dimensions, dial size, +pointer and parameter meanings. Chromium geometry checks real caption +clearance and value noncollision across existing test viewports. diff --git a/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md b/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md index 2a205e6fb..9ade422a9 100644 --- a/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md +++ b/docs/ARSON_HARMONY_ENGINE_AUDIT_20261108.md @@ -49,3 +49,67 @@ A 1536-sample lookback at 48 kHz is **32 ms DSP delay**, before device input + o - Rubber Band v4 technical docs expose formant options and exact latency API, but licensing, ASIO callback suitability and measured overhead must be independently assessed; do not add by assumption. **Status:** engineering candidate only; PR stays Draft pending exact-HEAD qualification and physical vocal FAT. + +## 2026-11-09 follow-up — pitch CPU and measurable callback budget (post-merge) + +- Roll the shifted window's energy from one candidate lag to the next. Keep the existing dot-product, confidence threshold, quantizer and FX/PDC/GUI contracts. Each adjacent lag changes by one outgoing + one incoming squared sample instead of recomputing 160 squares. Only floating-point rounding may differ. +- Extend deterministic C++ PCM regressions to a multi-harmonic A2 sample with 64/128/257-frame chunks. Existing E2/C6, dry-latency alignment, automation and music-guide tests remain in force. +- Add `NativeHarmonySyntheticCallbackCpuProfile` to CTest. Synthetic engine-only p50/p95/p99/max data covers 48 kHz (64/128/256 samples), 96 kHz (128 samples), with 2/5 voices. Timing is recorded only outside engine->process(), but Windows runner contention makes absolute timing unsuitable for a CI hard gate. +- No quantitative speedup claim without controlled before/after on one machine. No ASIO driver, JUCE graph, real-world multichannel CPU, formant quality or ear monitoring is measured by this synthetic profile. Physical singer FAT remains release blocking. + +## 2026-11-09 P0 bounded realtime engine milestone + +**Release scope**: Stable 48-kHz 1536-sample (32ms) DSP lookback and exact native PDC remain unchanged until recorded singing/listening establishes a safe lower-latency algorithm. No experimental formant shifter or hidden direct monitor path is enabled merely to advertise latency gains. + +- Move pitch autocorrelation off one large sample spike: snapshot a fixed decimated window every ~6.7ms, process at most 32 candidate lags per sample-scheduled slice, finish before the next analysis window. All snapshots and per-instance state are fixed-size, and graph/audio ownership is unchanged. This reduces the worst-case single-sample analysis work even at ASIO 64. +- Keep the sliding lagged energy improvement from previous #485, preserving confidence thresholds, song key readback, five-voice controls and existing tests. +- Recompute voiced gate and ratio smoothing coefficients on `prepare(rate)` only so physical time constants remain the same across 32–192kHz device reopen; 48kHz response stays calibrated to the earlier engine. +- Enforce deterministic output for identical stereo singing PCM across 64/127/256 sample callback partitioning and instrument the maximum completed pitch-lag work per sample. Retain all prior E2–C6, cross-channel Music Key and dry 192k latency tests. +- The synthetic engine-only CPU profile is diagnostic, not a hardware underrun oracle. Next **non-automatable** P0 gate: independent actual Windows ASIO interface, human singing + recorded dry/wet FFT/formant comparison, 48/96k with 64/128/256 periods and 2–5 voices for 30 minutes; callback p95/p99/max, driver XRUNs and measured loopback round trip must be documented. +- Formant-preserving pitch synchronous synthesis (TD-PSOLA / equivalent) remains a separate replaceable shifter milestone: it must be verified against consonants, onset/off pitch, male/female vowels and CPU/latency tradeoffs before becoming audible default. Never claim today's granular shifter preserves formants. + +Project status: P0 **realtime boundedness implementation** awaiting exact-HEAD Windows CTest and singer FAT; not a final professional-quality or latency qualification. + +## 2026-11-09 P1 — stereo phase-resilient Harmony analysis + +Both `HarmonyDsp` vocal analysis/grain source and the cross-ChannelId `MusicKeyDetector` used `0.5*(L+R)`. Valid `L=-R` PCM from an extreme stereo widener or polarity-inverting source disappeared completely from both. The read-only music source remained audible elsewhere but Harmony incorrectly fell back to Manual. + +`StereoPhaseSafeFold.h` now provides a **per-instance, callback-owned, constant-work** fold with precomputed 20ms polarity evidence and 15ms smoothed recovery, strict -0.78 engage / -0.55 release hysteresis, silence release, and stereo analysis-only polarity repair. Ordinary coherent stereo keeps the existing sum. Neither dry stereo, audio routing, FX rack, SPSC schema, 2–5 voices, output device, declared latency nor GUI changes. No heap, locks, timers or per-sample trigonometry. + +PCM regressions: anti-phase singer A3 and nonzero five-voice harmony at 48/96k; anti-phase music C major at 48/96k, silence/Manual fallback; real native other-ChannelId antiphase A-minor guide with read-only source samples, plus preexisting in-phase, rekey, block parity, PDC and state migration. This only prevents destructive analysis cancellation, it is *not* formant preservation or ASIO/ear latency certification. Physical singer FAT remains open. + +## 2026-11-09 P0/P1/P2 tone and CPU milestone — exact speaker-independent tests + +**P0 boundedness**: Preserve exactly the native two-grain shifter's 1536 samples of 48k lookback and its 32-lag-per-slice pitch work. Do not promise latency reduction, physical ASIO stability or true formant preservation without human singing and measured loopback; this commit instead ensures that deterministic pitch ratios and timbral control stay safe across devices. + +**P1 tone parity**: Extract `RateStableColor` (257 precomputed floats/instance). The original 48k one-pole coefficient `0.07 + color*0.77` is retained within float precision; at 32, 44.1, 96, 192k convert that same attenuation to its equivalent physical time. The smoothing envelope remains separate. Lookup/interpolation occurs only once per active processing sample and is shared across all active harmonies plus lead correction. Control-thread `prepare()` alone uses `std::pow`. Tests compare 10ms decay consistency across all five device rates and seven Color settings. + +**P2 realtime hot path**: Five extra `pow(2, semitones/12)` calls per pitch detection hop were replaced with a 49-entry immutable, off-callback prepared interval ratio table and one reference ratio exponent. Precomputed semitone intervals are combined by multiplication, with output clamp retained. Exhaustive deterministic test covers 12 keys, both scales, 71 degrees, five detunes and all five voice intervals (approximately 42,600 ratios). Maximum deviation from the former formula is below `1e-6` absolute in tests. No new state packets, allocations, locks, threads, routed audio, FX slot changes or GUI code. + +**Release gate**: This is source-level algorithmic hardening; the CI synthetic CPU profile is diagnostic only. Additional real voice/female/male formant comparisons and 64/128/256 ASIO buffer loopback/driver XRUN evidence remain mandatory. + +## 2026-11-09 P0 bounded-pitch work and hostile PCM follow-up + +**Actual source-level issue:** 32 candidate lags × 160 dot-product taps were still evaluated in a single callback sample, causing an avoidable periodic CPU spike even when the average synthetic CPU looked acceptable. Reducing the per-sample slice width without shortening cadence would cause silent abandoned pitch windows. This checkpoint changes BOTH: `maxPitchLagsPerSlice=8` and `pitchSliceEvery_=detectEvery_/32`. Total candidate-lag dot products and 48k grain/PCM/PDC are unchanged. Every snapshot finishes with headroom before the next analysis hop on all 32–192k supported sample rates, with separate completed/abandoned window counters for test-only diagnostics. + +**P0 hostile PCM fail-closed:** Native input guards now clip finite >4.0f amplitude before the internal dry/grain histories (the engine already clamps all output to +/-4.0f); NaN and infinity still become silence. `StereoPhaseSafeFold` returns the bounded fold it actually uses for correlation, instead of computing an unbounded sum after clipping evidence. This prevents a host supplying finite near-FLT_MAX PCM from poisoning Music Guide or vocal analysis with infinity. Ordinary valid PCM [-4,+4], channel polarity, dry PDC, Music Track read-only routing and GUI are untouched. + +**Qualification:** 5 sample rates (32/44.1/48/96/192k), 3 callback partitions (64/127/256 frames), deterministic A3 voiced note, complete correlation windows/no abandoned windows and <=8 candidate lags per slice; real 2/5-voice hostile IEEE PCM and direct near-FLT_MAX stereo guide. The existing 48k blockwise PCM parity, low bass/high soprano, 48/192k stereo dry and 1536-sample PDC regression remain active. Hardware ASIO and spectral/formant gate remain unverified; no claim of a completed P0 low-latency/formant engine. + +## 2026-11-09 P0 Harmony native-key and singer telemetry UX + +The field failure had two independent operator problems: (1) stale modeless-editor +control lease on graph/device change was resolved by accepted-graph auto-rebind, +and (2) native pitch `observedNote` was in the authoritative C++ snapshot but +never visible to the operator in Manual Key, which only polled Music mode. +The editor now uses the EXISTING 12Hz console tick for <=2Hz readback in +both modes, showing `TRACKING A3`/ `WAITING VOICE`/ `DSP SYNCING` +from the true PCM-owner `observedNote`, never inferred from the UI key. +Key gestures outline the requested letter immediately but the accepted +key/LCD still changes ONLY after the native DSP ACK and matching readback. +The status reports `KEY C#m - AWAITING DSP ACK` while pending. One +small footer string and a one-pixel indicator; no graph routing, FX +parameter schema, audio callback work or polling timer added. Real Chromium +test additionally checks manual-mode liveness, immediate gesture indicator +and its disappearance after accepted readback. Vocal formant/spatial realism +remains unproven and separate from this GUI feedback milestone. diff --git a/docs/ARSON_HARMONY_NATURAL_ENSEMBLE_ARCHITECTURE_20261109.md b/docs/ARSON_HARMONY_NATURAL_ENSEMBLE_ARCHITECTURE_20261109.md new file mode 100644 index 000000000..aa64cb791 --- /dev/null +++ b/docs/ARSON_HARMONY_NATURAL_ENSEMBLE_ARCHITECTURE_20261109.md @@ -0,0 +1,95 @@ +# ArSon Harmony — Natural Vocal Ensemble DSP Architecture & Evidence +2026-11-09 | source branch PR #487 | Design audit, **NOT** acoustic certification + +## Product acceptance + +A user singing one live monophonic lead must hear **one intelligible lead in front and independently perceived backing singers**, e.g. three voices behind, moderately left, near-center and moderately right. The BACKINGS must vary in timing, intonation, timbre and room position without creating robotic timbre, metallic grain edges, clicks, doubling of consonant transients, excess latency or ASIO XRUN. Key buttons should respond immediately with *pending* feedback, then turn *accepted* only on true native DSP ACK/readback. GUI should display an audio-owned tracking note, not pretend that pressing Key triggers sound or proves PCM processing. + +## Source-code diagnosis (before redesign) + +Current `engine/builtin_fx/HarmonyDsp.h` uses one phase-safe mono analysis/grain source for all harmony voices, two overlapping 16ms grains per shifter, identical initial grain phase for all voices, note-locked fixed diatonic offsets `{+2,+4,-2,+6,-4}` scale degrees, the **same one-pole Color** coefficient for all voices, equal-power stereo pan, and shared smoothing. `Space` only multiplies pan; it supplies no distance, room or early reflections. There are no differentiated vocal-formant paths, per-voice onset microtiming, natural vibrato or voice-leading across chords. The current declared DSP PDC at 48k remains **1536 samples / 32ms**. `Voice count=3` represents three wet harmony voices in addition to dry lead, not three separately modeled people. + +This is functional prototype pitch-shifting, **not** a formant-stable choir model. Improving pan or reverb alone cannot repair consonant smearing, pitch-mark phase discontinuity, or shifted formants. `Color` is *brightness*, not voice-tract formant preservation. + +## Research cross-check (public engineering references) + +- Antares Harmony Engine: independent voice timbre/vibrato/panning, humanize and choir controls; scale, chord and arranged notes; https://www.antarestech.com/products/creative-vocal-effects/harmony-engine +- Antares walkthrough: per-voice pan/throat length, vibrato onset/rate, timing and pitch variation; https://www.antarestech.com/blog/tutorial-introduction-to-harmony-engine +- Eventide H949: intelligent de-glitched splicing, micropitch and randomized delay to reduce lockstep duplication; https://www.eventideaudio.com/plug-ins/h949-harmonizer/ +- Eventide Quadravox: independent diatonic voice intervals and pitch transitions; https://cdn.eventideaudio.com/manuals/h90/1.1.2/content/algorithms/pitch.html +- PSOLA singing paper: pitch marks and 2-period Hann window, formant-aware spectral envelope processing, low-cost resynthesis but pitch-mark error is audible; https://www.mdpi.com/2076-3417/6/11/368 +- AES PSOLA + transient preservation: pitched segmentation needs robust marks and explicit consonant/transient bypass/reinjection; https://aes.org/publications/elibrary-page/?id=15624 +- Real-time phase vocoder spectral envelope: whitening/reconstruction is an alternative but incurs spectral window work and transient tradeoffs; https://www.isca-archive.org/interspeech_2017/lenarczyk17_interspeech.pdf +- Rubber Band documentation: `OptionFormantPreserved` and `OptionPitchHighConsistency` for dynamically varying vocal pitch. LiveShifter documents **>=50ms delay** depending on settings; not a free drop-in replacement for a low-latency live ASIO path; https://breakfastquay.com/rubberband/integration.html and https://breakfastquay.com/rubberband/code-doc/classRubberBand_1_1RubberBandLiveShifter.html. License GPLv2+ or commercial, review distribution terms before any integration: https://breakfastquay.com/rubberband/license.html +- Backing-vocal spatial placement / front-back depth: complementary pan and differences in timbre, early reflections, reverb/delay; natural doubles need independent microtiming; mono-check to avoid comb filtering: https://www.izotope.com/community/blog/tips-for-mixing-vocal-harmonies and https://www.izotope.com/community/blog/what-is-mix-depth-how-to-create-front-back-space and https://www.izotope.com/community/blog/10-tips-for-better-mixes-through-panning + +## Committed engineering decision, staged — no rushed wholesale DSP swap + +**P0: native GUI provenance, truth, response.** Keep one audio-owned ChannelId/InstanceId, native ACK and readback. Preserve editor across device/graph revision using safe auto-rebind. `observedNote` (quantized tracked MIDI note, *not exact raw singing F0*) now drives a small `TRACKING A3` / `WAITING VOICE` indicator on existing console ticks at <=2Hz, including Manual Key. Key gesture outlined as *queued* immediately while the actual LCD highlight is still the last accepted DSP value. No new polling timer, speculative PCM meters, Lab modes, FX permission gates or GUI shell redesign. + +**P0: make pitch/resynthesis natural, then measure.** Retain the proven prototype behind a native A/B flag during experimentation. Candidate quality path: confidence-gated fundamental detection -> robust glottal-epoch / pitch-mark tracking (using existing pitch estimator as an initial predictor; verify each mark by correlation) -> low-cost overlap-add on two-period tapered periods, scheduled at target F0. Preserve spectral envelope via pitch-synchronous segment handling; if audible vowel/formant drift persists at large shifts, benchmark pre/post spectral-envelope or LPC correction off the shared analysis window. Shared normalized/unvoiced consonant and transient path should not create three repeated sibilant attacks. When pitch confidence falls, crossfade safely to neutral or limited fallback rather than random target harmonies. Do not invent claims that TD-PSOLA alone is perfect at large intervals. Maintain the same single native PCM owner, immutable prepared controls, bounded history and predictable callback instruction budget. + +**P1: three distinct backing singers; ensemble spatializer.** Model each voice independently AFTER pitch generation: slight deterministic, slowly evolving detune rather than random-per-sample jitter; own phrase-onset delay / vibrato rate+depth/offset; mild unique spectral contour and intentional register. Keep leading dry vocal prominent. Initial *hypotheses to measure/listen*, not shipped presets: pan L=-0.35 to -0.45, inner=-0.05 to +0.05, R=+0.35 to +0.45; differences in onset 10–25ms; low-amplitude individual 3–8-cent pitch drift (slow and correlated within a phrase), modest sustain-only vibrato ~4–6 Hz, no hard random reset at graph boundaries. Per-voice room-send and early reflection mix with small filtered reflected taps ~20–50ms, not five independent convolution reverbs. Reverberant-to-direct ratio and high-frequency loss convey distance; simply cranking global width does not make a voice sound behind the lead. Pan+room must remain mono-compatible with no unexpected polarity cancellation; avoid phase-widening shortcuts. + +**P2: musical arrangement reliability.** Key+scale alone does NOT tell the current chord; fixed diatonic thirds/fifths can harmonize out of chord when music changes. Source-channel Music Guide must stay an independent read-only reference with confidence hysteresis/fallback; explore chord estimation / optional MIDI note control and smooth voice-leading only AFTER reliable native PCM keys and natural pitch shifting. No silent auto-chord hallucination. `Space` remains a single easy control that intelligently combines width/depth with carefully tuned bounds; power and Wet remain intuitive. + +**Low-latency ownership rule.** The source singer's ASIO monitor must not be slowed by experimental spatialization. Any PDC change needs native graph/host correctness and same-time dry/wet alignment verification. The present 1536-sample lookback is a measured *code delay*, not a benchmark of total ASIO round trip. Incremental tuning cannot be declared 'zero latency'. No locks/malloc/blocking on callback, no unbounded FFT work on sample path, no plugin worker feeding the realtime thread with a blocking rendezvous. + +## Measured acceptance gates before release promotion + +1. Exact Windows native compiled tests at 32/44.1/48/96/192 kHz and callback periods 64/128/256: deterministic output across buffer partitionings; bounded per-sample/per-block costs, no NaN/inf, clipping, lost ACK, stale FX lease or phase-cancelled mono. +2. Native visual/transport tests: tap Key while voice is live -> immediate pending outline/status; only accepted key after audio ACK and matching readback; live pitch tracking readback while Manual; Graph revision, device restart, FX move/recreate, stale ACK rejected; renderer never strands user on REOPEN. +3. Voice quality: licensed/consented male/female/child-free adult recordings, vowels A/E/I/O/U, breath/voiced/unvoiced consonants and held/fast syllables; measure F0 trajectory error, F1/F2/F3 shift, transient repetition, onset jitter, sibilance and phrase coherence against dry and reference algorithms. +4. Spatial A/B with mono fold-down: blind listen for **three distinguishable backup singers behind the lead**, not combed/flanged copies. Check L/C/R placements, loudness-normalized mono/stereo, low/normal/high background levels, output on mono speakers and headphones. +5. Actual ASIO physical FAT at 48/96 kHz, 64/128/256 frames: CPU time distribution p95/p99, deadline margin and XRUNs with 2/3/5 voices and multiple mixer plugins, hot graph/device rebind. These results cannot be inferred from ordinary GitHub Actions. + +**Non-claim:** The present PR does not yet implement or certify natural formants, an acoustic spatial engine, or a shorter engine PDC. Changing those requires measured code, CTest, render comparisons, audio A/B and physical ASIO proof. + +## 2026-11-09 P1 implemented — bounded singer individuation and early room + +New `engine/builtin_fx/HarmonyEnsembleSpace.h` defines five deterministic +backing singer identities, allocated as fixed-size fields and prepared on the +control thread. Each has its own arrival offset (5–17ms), phase and slight +detune (~1–3 cents) with onset-ramped, subtle 4.5–5.35Hz vibrato, and a +mild unique timbre coefficient. Up to five fixed 4096-sample reflection rings +supply one integer-delay, opposite-side quiet early reflection (12–20ms). +The same existing `Space` control now governs moderate pan width and +room amount, without adding another GUI mode or `.smix` schema field. + +No `sin`, `pow`, heap allocation, locks, worker or buffered audio queue +runs inside callback; each singer's LFO uses control-thread sine LUT. All +reflection histories advance when backing voices are inactive so stale FX +cannot reappear. No native graph, VST3, ASIO callback owner, slider/preset, +transport or routing changes. Plugin PDC remains 1536 samples @48k +(32ms). Vocal backing output is intentionally slightly later than the +lead, rather than delaying the lead itself: this is *musical onset and +depth*, NOT an additional reported whole-plugin latency. + +Acceptance includes fixed-tap reflection impulse at 32–192k, isolation +between singers, bounded detune/vibrato and mono-compatible 3-voice +stereo field at 48/96k. Existing PCM block-partitioning, host bypass, +anti-phase dry and realtime CPU diagnostic still apply. This is a P1 +ensemble-spatial realism increment, **not a formant-preserving +resynthesis fix**. The original two-grain pitch shifter can still sound +robotic on real recorded consonants and wide intervals. It requires a +separate pitch-synchronous/formant-aware algorithm and A/B acoustic gate. + +## 2026-11-09 P0 native pitch-synchronous candidate (requires physical singer qualification) + +Audit found the audible 16ms fixed-grain playback still changes vowel phase at unrelated seam boundaries. This commit adds a **causal, bounded pitch-synchronous window candidate** to the same ChannelId `HarmonyDsp`. It uses source-F0 pitch periods and local positive source landmarks to read waveform windows at **unity PCM speed**, while note ratio controls the spacing of synthesis centers; formants are no longer intentionally frequency-scaled by speeding the source playback. The original grain shifter is retained as the fallback for missing landmarks/low correlation/unvoiced phonemes; transitions ramp over 12ms. This is an approximation, not a proven glottal-closure PSOLA or completed formant-safe voice model. + +- One 16,384-sample mono history already owned by Harmony; no new audio route, device, host, PDC, user control or UI. Per-voice candidate stores at most 8 active source windows, fixed-size. Peak searches are bounded to <1 source period and never read future input. +- New original-harmonic source CTest checks up/down ratios, a low male period, high-rate registers and **exact 64/257 callback-partition PCM parity** at 48/96/192k. Existing native lead preservation and phase-safe music tests still apply. +- **Crucial nonclaim:** harmonic synthetic tests do not establish a singer's natural vowel formants or consonant intelligibility. Positive peaks are not reliable epochs for all breathy, growled or multi-pitch vocals. Do not mark PR production-ready until real recorded-vocal spectral F1/F2/F3, fricative/phrase boundaries, loudness-matched blind A/B and worst-case ASIO xrun/latency testing. If an audible regression occurs, fall back to the previous grain implementation before release. + +## 2026-11-09 Native vocal A/B workstation milestone + +The same `HarmonyDsp::Engine` now exposes an **offline reference-only** switch to re-render the previous grain synthesis against the pitch-synchronous candidate; normal live devices always default to the candidate and no new GUI mode/schema/control packet is added. The separate `ARMixHarmonyOfflineAb` C++ CLI takes a licensed/consented **adult** voice excerpt in mono/stereo 16/24-bit PCM or 32-bit float RIFF WAV, 32–192 kHz, <=60 seconds: + +```sh +ARMixHarmonyOfflineAb dry-vocal.wav output-directory +``` + +Outputs `01_lead_reference.wav`, `02_legacy_grain_mix.wav`, `03_new_space_060.wav`, `04_new_backing_only.wav`, `05_new_mono_fold.wav`, `06_new_space_000.wav`, `07_new_space_100.wav`, and `metrics.json`. All cases are reset from identical input and Manual C major / three voices / Wet 45%, Pitch Tune OFF, Gain 0dB. Wet0 lead is compared sample-exact across candidates; backing stem is mathematical difference from that lead and rejects hard-clipped output. The tool runs offline only; no filesystem IO in the audio callback. Compile/run `--self-test` as a quick synthetic source integrity check. + +**Unmet acoustic gates:** actual consented singer recordings and blind A/B, F1/F2/F3/vowel envelope preservation, noisy consonants, source expression, physical Windows ASIO loopback and CPU/xrun timings. Do NOT use the synthetic self-test or unnormalized WAV comparison to claim a professional natural-sounding result. diff --git a/docs/PRD_ARSON_HARMONY_NATURAL_DEPTH_20261109.md b/docs/PRD_ARSON_HARMONY_NATURAL_DEPTH_20261109.md new file mode 100644 index 000000000..4b3a00397 --- /dev/null +++ b/docs/PRD_ARSON_HARMONY_NATURAL_DEPTH_20261109.md @@ -0,0 +1,49 @@ +# PRD — ArSon Harmony FX: Natural Backing Vocal Depth (v1.0) +**Date:** 2026-11-09 · **Status:** Binding product target, not a claim of acoustic quality achieved +**Owner scope:** ArSonMix native built-in Harmony on draft PR #487. Preserve the approved compact Harmony editor, same .smix schema, native PCM ownership, and auto-rebinding DSP/GUI. +**Related:** [Natural Ensemble architecture](ARSON_HARMONY_NATURAL_ENSEMBLE_ARCHITECTURE_20261109.md) · [Harmony engine audit](ARSON_HARMONY_ENGINE_AUDIT_20261108.md) · [Depth engine ARD](ARD_ARSON_HARMONY_DEPTH_ENGINE_20261109.md) + +## User outcome / job to be done +One live singer should remain **clearly in front and unmistakably themselves**. Enabling 3 harmony backing singers should sound like **three people standing farther back**—mildly left, inner-center and mildly right in a coherent shared acoustic space. Harmony must not replace, cancel, phase-comb or automatically duck the original singer. When harmony is muted/off/at Wet 0, the same dry vocal must be preserved exactly except declared latency/PDC, explicitly selected pitch correction and global Gain. + +Users currently report a robotic/noise-like stack with little natural back/front separation. This is a P0 sound-quality defect, not solved by more panning or simply adding vibrato or wet reverb. The original 2-grain pitch-shifter is **still unqualified on singer recordings**; adding depth is a separate measurable, staged milestone. + +## Native one-knob user workflow +- The single existing **Space** knob remains the primary distance/width control; no Room Lab, advanced switches, extra dialog, permission gate or mandatory mixer routing. Wet controls backing strength; Color controls timbral brightness; Gain remains explicit output gain. +- At **Space 0**: compact, center-dominant dry backing (little/no backing reflection), lead still forward. +- At **Space 0.6 (factory default)**: audible but unobtrusive backing group a few steps behind, with left/near-center/right separation. Depth comes from lower backing direct level relative to reflected/diffuse sound, smooth upper timbre, modest onset offset and coherent short room, **not arbitrary meters or fake 3D localization**. +- At **Space 1**: noticeably deeper backing; no excessively long tails, discrete echo, metallic ring, mono disappearing vocals or extra lead level loss. Space automation must not sweep variable delay taps. +- **Wet 0 or rack bypass**: backing and its room must not leak into the monitoring output. Switching the rack on/off must follow native state, no GUI-only fake effect. +- **Music / Manual Key**: existing authoritative audio-owner ACK/readback, observed note and auto-rebind remain intact. Changing Space while holding a note must not produce zipper/click. + +## Audio precedence and separation requirements +1. **Lead invariant (P0):** Without user-enabled Pitch Tune, the sample-aligned lead path is independent of Wet/Space and retains channel polarity. Only the explicitly selected output Gain can scale both. Do not reuse a wet/dry crossfade that subtracts lead energy. +2. **Backing-only depth (P1):** all direct attenuation, high-frequency shading, early reflections and late diffusion apply to backing voices only. No additional effect may enter the lead path inadvertently. +3. **Direct-to-reverberant ratio (DRR):** deeper Space decreases backing DRR relative to Space near zero. This is a measurable signal-energy relationship, NOT a fixed literal physical distance. More reflection alone is not a substitute for quieter direct backing; no exaggerated reverb masking lyrics. +4. **Realistic front/back:** differentiated backing timbre without muffling consonants into noise; controlled stereo spread, mild independent phrases and natural pitch resynthesis. Existing 5–17 ms singer onset offsets and 12–20 ms early reflections need additional diffuse room energy to avoid hearing a single metallic/slap echo. +5. **Same room:** three singers share one subtle stereo diffuse field (with per-voice source position), not three unrelated giant reverb boxes. Late field is correlated acoustically but should not collapse to identical phase L=−R. +6. **Mono/speaker sanity:** mono fold of all wet voices stays audible and finite, no polarity inversion of dry signal, no excessive comb-filtering from source duplication. Test direct monitor + host monitored route externally: two simultaneous lead paths can still cause comb filtering outside Harmony, which DSP alone cannot cure. +7. **RT budget / portable:** keep current single JUCE PCM callback, bounded fixed memory and deterministic per-sample work. No fft/heap/mutex/host calls in realtime, no forced new global audio device, no new plugin PDC unless graph PDC tests pass. + +## Objective acceptance gates (all required) +| Gate | Test and evidence | Release requirement | +|---|---|---| +| Lead preservation | Wet=0/45/100, Space=0/60/100, no Pitch Tune, native PCM difference against dry-latency baseline | Sample-exact or <=1e-6 numerical lead-path delta after removing backing; no silent wet lead duck | +| Backstage vs foreground | Record natural male/female adult singing phrases; loudness-matched blind A/B between Space 0 / 0.6 / 1 | Listener reliably ranks backing behind lead and three voices separate; report results, not invented 3m coordinates | +| DRR / spectral balance | Synthetic impulse + vowel, separate backing-only STEM before final sum, calculate direct/reflected RMS in defined windows, compare bands | DRR decreases with Space; no extreme high-frequency loss (sibilant intelligibility); log curve | +| Room behavior | 32–192 kHz transient and silence; inspect 0.3–1.5 s tails and decay | No audible flutter echo, metallic ping, runaway feedback, NaN, noise-on-silence or tail on Wet 0 | +| Mono compatibility | L+R fold-down, L-R, in-phase and anti-phase stereo; 2/3/5 voices, 64/128/256 frames | Lead unchanged, backing energy remains audible with no complete cancellation | +| Realtime | Exact Windows MSVC/CTest plus CPU profile and real ASIO 48/96k × 64/128/256 frames with FX rack open/close and device graph restart | No extra graph latency, XRUNs or callback p95/p99 regressions; report hardware measurements | +| Natural vocal quality (separate P0) | Consented vowel/consonant, breathy, low-bass, female-soprano, slides and vibrato recordings | Formant and intelligibility evidence; cannot call the ensemble natural before this gate | + +## Milestones / explicit nonclaims +**D0:** Update PRD/ARD, isolate and remove wet-dependent lead duck, add mono/dry assertions. +**D1:** Prepared-sample-rate backing spectral shading + source-specific early reflections; one small bounded *backing-only* stereo diffuse late field; Space maps direct and reflected energy, not just width. +**D2:** Calibrate room decay/DRR and blind-listening A/B; profile worst-case Windows ASIO and return the simplest model that passes. +**D3 (separate P0 quality):** formant-preserving voiced/unvoiced resynthesis and expressive musical voice leading. D1 must not be marketed as completion of D3. + +**Release rule:** CI passing proves code correctness; perceived 3-person background distance and human timbre need real recordings/listener evidence. Keep PR Draft until justified. + +## 2026-11-09 D0/D1 working candidate (pending CI and ear-test) + +Updated `HarmonyDsp.h` removes the silent Wet-controlled lead duck, preserves the already selected Pitch Tune semantics and builds an independent backing-only distance chain. The existing Space control now adjusts direct-versus-reflected ratio, gentle high-frequency contour and one shared late stereo ambience field in addition to moderate pan. This addresses the *architecture* behind depth—not a verified acoustic outcome. D2 listener ranking/DRR level matching and D3 formant-aware synthesis remain open; preserve Draft status. diff --git a/engine/builtin_fx/HarmonyDepthRoom.h b/engine/builtin_fx/HarmonyDepthRoom.h new file mode 100644 index 000000000..83854d3f0 --- /dev/null +++ b/engine/builtin_fx/HarmonyDepthRoom.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +#include +#include + +namespace armix::harmony { +// ONE deterministic backing-only stereo ambience field, not 5 reverbs. +// A 4x4 orthonormal Hadamard feedback stage distributes early line energy. +// Loop gains and pole coefficients are prepared off the realtime callback; +// the total energy gain is strictly < 1 before frequency damping. +class BackingDepthRoom final { +public: + static constexpr std::size_t branches=4; + static constexpr std::size_t capacity=8192; + struct Output {float left=0.f,right=0.f;}; + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + constexpr std::array delayMs{{23.f,29.f,34.f,41.f}}; + for(std::size_t i=0;i(std::lround( + static_cast(delayMs[i])*.001*rate)); + if(delay_[i]<2||delay_[i]>=capacity)return false; + // Nominal room RT60 ~0.55 s at low frequencies, shorter at + // high frequencies through the one-pole damping filter. + feedback_[i]=static_cast(std::pow( + 10.,-3.*static_cast(delay_[i])/(rate*.55))); + if(!std::isfinite(feedback_[i])||feedback_[i]>=1.f)return false; + lines_[i].fill(0.f);damped_[i]=0.f; + } + dampingAlpha_=static_cast( + 1.-std::exp(-2.*3.141592653589793*3200./rate)); + cursor_=0; + return dampingAlpha_>0.f&&dampingAlpha_<1.f; + } + Output process(float inputL,float inputR) noexcept { + std::array d{}; + for(std::size_t i=0;i,branches> lines_{}; + std::array delay_{}; + std::array feedback_{},damped_{}; + float dampingAlpha_=0.f; + std::size_t cursor_=0; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyDsp.h b/engine/builtin_fx/HarmonyDsp.h index 56d245e84..e8f76587d 100644 --- a/engine/builtin_fx/HarmonyDsp.h +++ b/engine/builtin_fx/HarmonyDsp.h @@ -5,6 +5,13 @@ #include #include #include +#include "HarmonyStereoFold.h" +#include "HarmonyPreparedTone.h" +#include "HarmonyEnsembleSpace.h" +#include "HarmonyDepthRoom.h" +#include "HarmonyRoomFocus.h" +#include "HarmonyStageSpatial.h" +#include "HarmonyPitchSynchronous.h" // Deterministic, allocation-free vocal harmony DSP. No JUCE or GUI dependency. // Grain shifting is a functioning prototype, not formant-accurate synthesis. @@ -61,6 +68,7 @@ inline float smoothGrainBlend(float phase) noexcept { class Engine final { public: static constexpr int maxVoices=5; + static constexpr int maxPitchLagsPerSlice=8; // fewer burst MACs per sample // Maximum 192 kHz grain is 3072 frames: 2*grain latency + 3*grain // history = 15360, so 16384 frames retain the full rate/ratio budget. // Halves the three large audio rings per instance without RT allocation. @@ -72,6 +80,10 @@ class Engine final { latency_=2*grain_; decimation_=std::max(1,static_cast(std::round(rate/12000.))); detectEvery_=std::max(128,static_cast(rate/150.)); + // 32 fixed time slices per analysis hop, but only 8 lags/slice. + // Even 32k input completes every supported 65-1200 Hz candidate + // before the next frozen PCM window. Distributes RT work on ASIO 64. + pitchSliceEvery_=std::max(1,detectEvery_/32); // Support bass through soprano fundamentals; keep Nyquist/finiteness guards. // Earlier 850 Hz cap failed to track high soprano notes (e.g. C6). lowLag_=std::max(8,static_cast(rate/(decimation_*1200.))); @@ -79,16 +91,39 @@ class Engine final { if(lowLag_>=highLag_||latency_+3*grain_>=static_cast(capacity))return false; left_.fill(0.f);right_.fill(0.f);mono_.fill(0.f);decimated_.fill(0.f); filtered_.fill(0.f);phase_.fill(0.f);ratio_.fill(1.f);targetRatio_.fill(1.f); + // Independent grain clocks avoid five identical backing-singer seams. + // Lead correction keeps its neutral original phase. + for(int v=0;v(v)]=.157f*static_cast(v); // Low-pass before downsampling: raw 12 kHz decimation aliases vocal sibilants // into the periodicity detector and destabilizes the live note readback. decimationLowpass_=0.f; + if(!vocalFold_.prepare(rate)||!toneColor_.prepare(rate)|| + !ensemble_.prepare(rate)||!depthRoom_.prepare(rate)|| + !stage_.prepare(rate)||!roomFocus_.prepare(rate))return false; + pitchIntervals_.prepare(); + pitchSync_.reset(); + pitchSyncBlend_.fill(0.f);pitchSyncFiltered_.fill(0.f); + sourcePeriodSamples_=0.f;sourceConfidence_=0.f;sampleClock_=0; + pitchSyncReferenceEnabled_=true; + stageDepthReferenceEnabled_=true; + roomFocusReferenceEnabled_=true; + presenceReferenceEnabled_=true; + pitchSyncAlpha_=static_cast(1.-std::exp(-1./(rate*.012))); const float cutoff=1800.f; decimationAlpha_=static_cast(1.-std::exp(-2.*3.141592653589793*cutoff/rate)); write_=0;downWrite_=0;downSeen_=0;downCounter_=0; - detectCounter_=0;noteHold_=0;currentNote_=-1;candidateNote_=-1; + detectCounter_=0;pitchSliceCounter_=0;pitchNextLag_=0; + pitchActive_=false;pitchBest_=-1.f;pitchBestLag_=0; + peakPitchLagWork_=0;completedPitchAnalyses_=0;abandonedPitchAnalyses_=0; + noteHold_=0;currentNote_=-1;candidateNote_=-1; guideKey_=-1;guideScale_=-1; voiced_=false;voicedGain_=0.f;settings_=Settings{}; - smoothingAlpha_=static_cast(1.-std::exp(-1./(rate*0.005))); // 5 ms continuous control ramp + smoothingAlpha_=static_cast(1.-std::exp(-1./(rate*0.005))); // 5 ms control ramp + // Preserve the established 48k envelope while keeping durations + // constant at 32-192k. Exponents computed off the callback. + voicedGateAlpha_=static_cast(1.-std::pow(1.-0.002,48000./rate)); + ratioAlpha_=static_cast(1.-std::pow(1.-0.00045,48000./rate)); processed_=false;settings_=Settings{}; return setSettings(settings_); } @@ -99,15 +134,28 @@ class Engine final { float gain=1.f; float mixL[maxVoices]{},mixR[maxVoices]{}; }; - static bool prepareSettings(const Settings& next,PreparedSettings& out) noexcept { + // Wet-only musical presence, prepared OFF the audio callback. + // 4-voice full Wet rises ~5 dB while dry lead and stage remain unchanged. + static float normalizedBackingWetAmplitude(float wet,int voices, + bool enhanced=true) noexcept { + if(!std::isfinite(wet)||wet<=0.f||voices<2||voices>maxVoices)return 0.f; + const float w=std::clamp(wet,0.f,1.f); + const float presence=enhanced?(.77f+.38f*w):.65f; + return w*presence/std::sqrt(static_cast(voices)); + } + static bool prepareSettings(const Settings& next,PreparedSettings& out, + bool enhancedPresence=true) noexcept { if(!valid(next))return false; PreparedSettings prepared; prepared.settings=next; prepared.gain=std::pow(10.f,next.gainDb/20.f); constexpr float pi=3.14159265358979323846f; - const float amplitude=next.wet*0.65f/std::sqrt(static_cast(next.voices)); + const float amplitude=normalizedBackingWetAmplitude( + next.wet,next.voices,enhancedPresence); for(int v=0;v(v)/(next.voices-1)-1.f); + // Width is moderate even at the center-friendly default Space. + const float pan=.72f*next.space* + (2.f*static_cast(v)/(next.voices-1)-1.f); const float angle=(pan+1.f)*pi*0.25f; prepared.mixL[v]=amplitude*std::cos(angle); prepared.mixR[v]=amplitude*std::sin(angle); @@ -129,6 +177,7 @@ class Engine final { } settings_=next.settings; targetWet_=next.settings.wet;targetColor_=next.settings.color; + targetSpace_=next.settings.space; targetGain_=next.gain; targetCorrection_=next.settings.correction?1.f:0.f; for(int v=0;v(v)]=next.mixR[v]; } if(!processed_){ - wet_=targetWet_;color_=targetColor_;gain_=targetGain_; + wet_=targetWet_;color_=targetColor_;space_=targetSpace_; + gain_=targetGain_; correctionMix_=targetCorrection_; mixL_=targetMixL_;mixR_=targetMixR_; } @@ -146,7 +196,8 @@ class Engine final { // FX mailbox invokes applyPreparedSettings(), never this trig/pow path. bool setSettings(const Settings& next) noexcept { PreparedSettings prepared; - return prepareSettings(next,prepared)&&applyPreparedSettings(prepared); + return prepareSettings(next,prepared,presenceReferenceEnabled_)&& + applyPreparedSettings(prepared); } // Called only on the existing audio callback from a separately selected // source channel. Invalid/ambiguous music always restores Manual Key. @@ -167,8 +218,38 @@ class Engine final { } const Settings& settings() const noexcept{return settings_;} int latencySamples() const noexcept{return latency_;} + // Offline A/B harness only: retain the exact legacy granular reference + // without introducing a user-facing mode or changing serialized FX state. + // Production instances always default to the pitch-synchronous candidate. + void setPitchSynchronousCandidateForOfflineReference(bool enabled) noexcept { + pitchSyncReferenceEnabled_=enabled; + } + // Offline A/B only. Default live audio always uses the new stage; no + // ordinary GUI control, device transaction or saved setting is added. + void setStageDepthForOfflineReference(bool enabled) noexcept { + stageDepthReferenceEnabled_=enabled; + } + void setRoomFocusForOfflineReference(bool enabled) noexcept { + roomFocusReferenceEnabled_=enabled; + } + // Offline-only before process(): same native pitch, stage, room and wet, + // but old vs new normalized voice gain, for controlled vocal A/B. + bool setBackingPresenceForOfflineReference(bool enabled) noexcept { + if(processed_)return false; + presenceReferenceEnabled_=enabled; + return setSettings(settings_); + } int observedMidiNote() const noexcept{return currentNote_;} bool voiced() const noexcept{return voiced_;} + int peakPitchLagWork() const noexcept{return peakPitchLagWork_;} + // Audio-owner telemetry; standalone CTest reads after process() returns. + int completedPitchAnalyses() const noexcept{return completedPitchAnalyses_;} + int abandonedPitchAnalyses() const noexcept{return abandonedPitchAnalyses_;} + // Wet changes are NEVER a hidden sidechain duck on the real lead. Only + // explicitly enabled Pitch Tune may substitute corrected lead samples. + static float leadPath(float original,float corrected,float correctionMix) noexcept { + return original*(1.f-correctionMix)+corrected*correctionMix; + } void process(float* l,float* r,int frames) noexcept { if(!l||!r||frames<=0||rate_<=0.)return; processed_=true; @@ -178,6 +259,7 @@ class Engine final { const float a=smoothingAlpha_; wet_+=a*(targetWet_-wet_); color_+=a*(targetColor_-color_); + space_+=a*(targetSpace_-space_); gain_+=a*(targetGain_-gain_); correctionMix_+=a*(targetCorrection_-correctionMix_); for(int v=0;v=decimation_){ @@ -196,27 +281,85 @@ class Engine final { downWrite_=(downWrite_+1)%decimated_.size(); ++downSeen_; } - if(++detectCounter_>=detectEvery_){detectCounter_=0;detectPitch();} + if(++detectCounter_>=detectEvery_){ + detectCounter_=0;beginPitchAnalysis(); + } + // Correlation search is spread across the existing sample + // callback: never evaluate all 175+ candidate lags at once. + // Every 64-sample ASIO period sees a bounded number of slices. + if(pitchActive_ && ++pitchSliceCounter_>=pitchSliceEvery_){ + pitchSliceCounter_=0;continuePitchAnalysis(); + } const float dryL=tap(left_,static_cast(latency_)); const float dryR=tap(right_,static_cast(latency_)); - voicedGain_+=((voiced_?1.f:0.f)-voicedGain_)*0.002f; + voicedGain_+=((voiced_?1.f:0.f)-voicedGain_)*voicedGateAlpha_; + // Compute the device-rate-correct filter coefficient once per + // active sample, not once for each of up to six shifters. + const float filterCoef=voicedGain_>0.00001f? + toneColor_.coefficient(color_):0.f; float lead=0.f; if(correctionMix_>0.00001f&&voicedGain_>0.00001f) - lead=shifted(maxVoices,color_); + lead=shifted(maxVoices,filterCoef); const float leadBlend=correctionMix_*voicedGain_; - float outL=(dryL*(1.f-leadBlend)+lead*leadBlend)*(1.f-wet_*0.65f*voicedGain_); - float outR=(dryR*(1.f-leadBlend)+lead*leadBlend)*(1.f-wet_*0.65f*voicedGain_); - if(voicedGain_>0.00001f){ - for(int v=0;v(v); - if(std::abs(mixL_[ix])+std::abs(mixR_[ix])<1.e-5f)continue; - const float y=shifted(v,color_)*voicedGain_; - outL+=y*mixL_[ix];outR+=y*mixR_[ix]; + float outL=leadPath(dryL,lead,leadBlend); + float outR=leadPath(dryR,lead,leadBlend); + // Direct sound and reflections from backing singers only. + // Preserve the exact previous spatial gains in offline reference. + const auto stageGain=BackingStageSpatial::gains(space_); + const float backingDirect=stageDepthReferenceEnabled_? + stageGain.direct:1.f-.38f*space_; + const float earlyReturn=stageDepthReferenceEnabled_? + stageGain.early:.28f*space_; + float roomSendL=0.f,roomSendR=0.f; + for(int v=0;v(v); + const bool audible=voicedGain_>0.00001f&& + std::abs(mixL_[ix])+std::abs(mixR_[ix])>=1.e-5f; + float voice=0.f; + if(audible){ + voice=shifted(v,ensemble_.timbreCoefficient(v,filterCoef), + ensemble_.arrivalSamples(v), + ensemble_.pitchRatio(v))*voicedGain_; + } + voice=ensemble_.shadeBacking(v,voice,space_); + const float reflection=ensemble_.exchangeReflection(v,voice); + const float second=ensemble_.secondReflection(v); + const float directL=voice*mixL_[ix],directR=voice*mixR_[ix]; + const auto placed=stage_.place(v,voice,reflection,second, + mixL_[ix],mixR_[ix],space_); + if(stageDepthReferenceEnabled_){ + outL+=placed.directL*backingDirect+placed.earlyL*earlyReturn; + outR+=placed.directR*backingDirect+placed.earlyR*earlyReturn; + roomSendL+=placed.roomL;roomSendR+=placed.roomR; + }else{ + outL+=directL*backingDirect+ + reflection*mixR_[ix]*earlyReturn; + outR+=directR*backingDirect+ + reflection*mixL_[ix]*earlyReturn; + roomSendL+=directL;roomSendR+=directR; } } + // One shared backing-only diffuse field. Both its input and + // audible return ramp with Space. Wet=0 gates its old tail on + // a smooth <=5 ms envelope while the room ring still decays. + // Room-only low-cut and musical tail bloom; lead remains untouched. + const float sendL=roomSendL*space_,sendR=roomSendR*space_; + const auto cleaned=roomFocus_.filterSend(sendL,sendR); + const auto room=depthRoom_.process( + roomFocusReferenceEnabled_?cleaned.left:sendL, + roomFocusReferenceEnabled_?cleaned.right:sendR); + const float roomGate=std::min(1.f,wet_*4.f); + const float lateReturn=(stageDepthReferenceEnabled_? + stageGain.late:.85f*space_)*roomGate; + const float focus=roomFocus_.returnGain(dryL,dryR,space_); + const float roomReturn=lateReturn*(roomFocusReferenceEnabled_?focus:1.f); + outL+=room.left*roomReturn;outR+=room.right*roomReturn; l[i]=std::clamp(outL*gain_,-4.f,4.f); r[i]=std::clamp(outR*gain_,-4.f,4.f); + ensemble_.advance(voiced_); + stage_.advance(); write_=(write_+1)%capacity; + ++sampleClock_; } } private: @@ -228,59 +371,109 @@ class Engine final { const auto a1=(a0+capacity-1)%capacity; return a[a0]*(1.f-f)+a[a1]*f; } - float shifted(int voice,float color) noexcept { + float shifted(int voice,float filterCoef,float arrival=0.f, + float fineRatio=1.f) noexcept { float& currentRatio=ratio_[static_cast(voice)]; - currentRatio+=(targetRatio_[static_cast(voice)]-currentRatio)*0.00045f; + currentRatio+=(targetRatio_[static_cast(voice)]-currentRatio)*ratioAlpha_; + const float effectiveRatio=std::clamp(currentRatio*fineRatio,.49f,2.01f); const float p=phase_[static_cast(voice)]; const float pB=p<0.5f?p+0.5f:p-0.5f; - const float delayA=2.f*grain_+(1.f-currentRatio)*p*grain_; - const float delayB=2.f*grain_+(1.f-currentRatio)*pB*grain_; + const float delayA=2.f*grain_+arrival+(1.f-effectiveRatio)*p*grain_; + const float delayB=2.f*grain_+arrival+(1.f-effectiveRatio)*pB*grain_; // Cubic smoothstep over the complementary two-grain windows. // Unlike the triangular seam, both endpoints have zero slope: // reduced grain-edge modulation without sin/cos in audio callback. const float w=smoothGrainBlend(p); float y=w*tap(mono_,delayA)+(1.f-w)*tap(mono_,delayB); - const float coef=0.07f+color*0.77f; - float& f=filtered_[static_cast(voice)]; - f+=coef*(y-f);y=f; - phase_[static_cast(voice)]+=1.f/static_cast(grain_); - if(phase_[static_cast(voice)]>=1.f) - phase_[static_cast(voice)]-=1.f; + const auto ix=static_cast(voice); + float& f=filtered_[ix]; + f+=filterCoef*(y-f); + // Voiced, periodic sources can use causal pitch-synchronous + // overlap-add. The exact source period is measured, not inferred + // from the harmony target. Uncertain/nonperiodic sounds retain the + // previous validated granular shifter with a bounded 12ms crossfade. + const bool confident=pitchSyncReferenceEnabled_&&voiced_&&sourceConfidence_>.82f&& + sourcePeriodSamples_>=8.f; + PitchSynchronousVoice::Output sync{}; + if(confident) + sync=pitchSync_.process(voice,sampleClock_,write_,mono_, + sourcePeriodSamples_,effectiveRatio, + static_cast(latency_)+arrival); + float& colorState=pitchSyncFiltered_[ix]; + colorState+=filterCoef*(sync.sample-colorState); + float& blend=pitchSyncBlend_[ix]; + blend+=pitchSyncAlpha_*((confident&&sync.ready?1.f:0.f)-blend); + y=f*(1.f-blend)+colorState*blend; + phase_[ix]+=1.f/static_cast(grain_); + if(phase_[ix]>=1.f)phase_[ix]-=1.f; return y; } - void detectPitch() noexcept { + // Prepare one immutable, bounded pitch-analysis window every analysis hop. + // The subsequent 8-lag correlation slices cannot race PCM history writes: + // they only read the frozen fixed-size pitchWindow_. + void beginPitchAnalysis() noexcept { + if(pitchActive_)++abandonedPitchAnalyses_; + pitchActive_=false; if(downSeen_(highLag_+170))return; constexpr int span=160; - // Gather a contiguous fixed-size pitch window ONCE per analysis hop. - // Previously every candidate/lag re-indexed circular history in the - // inner O(lags * span) loop: unpredictable CPU peaks on tiny ASIO buffers. - std::array samples{}; auto ix=(downWrite_+decimated_.size()-1)%decimated_.size(); for(int j=0;j(j)]=decimated_[ix]; + pitchWindow_[static_cast(j)]=decimated_[ix]; ix=ix==0?decimated_.size()-1:ix-1; } - double energy=0.; + pitchEnergy_=0.; for(int j=0;j(j)]; - energy+=x*x; + const double x=pitchWindow_[static_cast(j)]; + pitchEnergy_+=x*x; } - if(energy/static_cast(span)<1.e-6){ - voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1;return; + if(pitchEnergy_/static_cast(span)<1.e-6){ + sourceConfidence_=0.f;voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1; + return; } - float best=-1.f;int bestLag=0; - std::array correlations{}; - for(int lag=lowLag_;lag<=highLag_;++lag){ - double xy=0.,yy=0.; + pitchCorrelations_.fill(0.f); + pitchLagEnergy_=0.; + for(int j=0;j(j+lowLag_)]; + pitchLagEnergy_+=y*y; + } + pitchBest_=-1.f;pitchBestLag_=0;pitchNextLag_=lowLag_; + pitchSliceCounter_=0; + pitchActive_=true; + } + void continuePitchAnalysis() noexcept { + if(!pitchActive_)return; + constexpr int span=160; + const int endLag=std::min(highLag_,pitchNextLag_+maxPitchLagsPerSlice-1); + const int work=endLag-pitchNextLag_+1; + peakPitchLagWork_=std::max(peakPitchLagWork_,work); + for(int lag=pitchNextLag_;lag<=endLag;++lag){ + if(lag>lowLag_){ + const double leaving=pitchWindow_[static_cast(lag-1)]; + const double entering=pitchWindow_[static_cast(span+lag-1)]; + pitchLagEnergy_+=entering*entering-leaving*leaving; + } + double xy=0.; for(int j=0;j(j)]; - const double y=samples[static_cast(j+lag)]; - xy+=x*y;yy+=y*y; + const double x=pitchWindow_[static_cast(j)]; + const double y=pitchWindow_[static_cast(j+lag)]; + xy+=x*y; } - const float correlation=static_cast(xy/std::sqrt(energy*yy+1.e-14)); - correlations[static_cast(lag)]=correlation; - if(correlation>best){best=correlation;bestLag=lag;} + const float correlation=static_cast( + xy/std::sqrt(pitchEnergy_*std::max(0.,pitchLagEnergy_)+1.e-14)); + pitchCorrelations_[static_cast(lag)]=correlation; + if(correlation>pitchBest_){pitchBest_=correlation;pitchBestLag_=lag;} + } + pitchNextLag_=endLag+1; + if(pitchNextLag_>highLag_){ + pitchActive_=false; + ++completedPitchAnalyses_; + finishPitchAnalysis(); } + } + void finishPitchAnalysis() noexcept { + const auto& correlations=pitchCorrelations_; + float best=pitchBest_; + int bestLag=pitchBestLag_; // Prefer shortest strong period rather than an octave-multiple peak. if(best>0.77f){ for(int lag=lowLag_;lag<=highLag_;++lag){ @@ -290,7 +483,7 @@ class Engine final { } } if(bestLag==0||best<=0.77f){ - voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1;return; + sourceConfidence_=0.f;voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1;return; } // Three-point peak interpolation reduces integer-lag tuning steps, // especially at the high end of the supported singing range. @@ -306,7 +499,7 @@ class Engine final { const float hz=static_cast(rate_)/(decimation_*lag); const float midi=69.f+12.f*std::log2(hz/440.f); if(!std::isfinite(midi)||midi<31.f||midi>96.f){ - voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1;return; + sourceConfidence_=0.f;voiced_=false;noteHold_=0;candidateNote_=-1;currentNote_=-1;return; } const int activeKey=effectiveKey(),activeScale=effectiveScale(); int degree=nearestDegree(midi,activeKey,activeScale); @@ -323,29 +516,65 @@ class Engine final { // do not generate provisional harmony on a single noisy transient. if(noteHold_<2)return; currentNote_=note;voiced_=true; + const float newPeriod=static_cast(rate_/hz); + if(sourcePeriodSamples_>0.f&& + std::abs(newPeriod-sourcePeriodSamples_)>.25f*sourcePeriodSamples_){ + pitchSync_.reset();pitchSyncBlend_.fill(0.f); + pitchSyncFiltered_.fill(0.f); + } + sourcePeriodSamples_=newPeriod; + sourceConfidence_=std::clamp(best,0.f,1.f); // Fifth harmony voice is a lower sixth in key; keep each voice // independent and normalize its level against the total count. + // One fractional-pitch exponent per analysis hop; each harmony + // interval is an integer number of semitones in the chosen scale. + // Five previous pow() calls move to the CONTROL prepared LUT. + const float baseRatio=std::pow(2.f,(note-midi)/12.f); constexpr std::array intervals{{2,4,-2,6,-4}}; for(int v=0;v(v)], activeKey,activeScale); targetRatio_[static_cast(v)]=std::clamp( - std::pow(2.f,(target-midi)/12.f),0.50f,2.0f); + baseRatio*pitchIntervals_.ratio(target-note),0.50f,2.0f); } - targetRatio_[maxVoices]=std::clamp(std::pow(2.f,(note-midi)/12.f),0.75f,1.34f); + targetRatio_[maxVoices]=std::clamp(baseRatio,0.75f,1.34f); } double rate_=0.; int grain_=0,latency_=0,decimation_=4,detectEvery_=320; int lowLag_=12,highLag_=144;std::size_t write_=0,downWrite_=0,downSeen_=0; int downCounter_=0,detectCounter_=0; + int pitchSliceEvery_=1,pitchSliceCounter_=0,pitchNextLag_=0; + int pitchBestLag_=0,peakPitchLagWork_=0; + int completedPitchAnalyses_=0,abandonedPitchAnalyses_=0; + bool pitchActive_=false; + float pitchBest_=-1.f; + double pitchEnergy_=0.,pitchLagEnergy_=0.; + // Fixed PCM snapshots; no allocation or variable work on the callback. + std::array pitchWindow_{}; + std::array pitchCorrelations_{}; int currentNote_=-1,candidateNote_=-1,noteHold_=0; int guideKey_=-1,guideScale_=-1; bool voiced_=false;float voicedGain_=0.f; bool processed_=false; - float smoothingAlpha_=1.f; + float smoothingAlpha_=1.f,voicedGateAlpha_=0.002f,ratioAlpha_=0.00045f; float wet_=0.f,targetWet_=0.f,color_=0.f,targetColor_=0.f; + float sourcePeriodSamples_=0.f,sourceConfidence_=0.f,pitchSyncAlpha_=0.f; + std::uint64_t sampleClock_=0; + bool pitchSyncReferenceEnabled_=true,stageDepthReferenceEnabled_=true; + bool roomFocusReferenceEnabled_=true; + bool presenceReferenceEnabled_=true; + std::array pitchSyncBlend_{},pitchSyncFiltered_{}; + float space_=0.f,targetSpace_=0.f; float gain_=1.f,targetGain_=1.f,correctionMix_=0.f,targetCorrection_=0.f; std::array mixL_{},mixR_{},targetMixL_{},targetMixR_{}; float decimationLowpass_=0.f,decimationAlpha_=0.f; + StereoPhaseSafeFold vocalFold_{}; + RateStableColor toneColor_{}; + PreparedPitchIntervals pitchIntervals_{}; + EnsembleSpace ensemble_{}; + BackingDepthRoom depthRoom_{}; + BackingStageSpatial stage_{}; + BackingRoomFocus roomFocus_{}; + PitchSynchronousVoice pitchSync_{}; Settings settings_{}; std::array left_{},right_{},mono_{}; std::array decimated_{}; diff --git a/engine/builtin_fx/HarmonyEnsembleSpace.h b/engine/builtin_fx/HarmonyEnsembleSpace.h new file mode 100644 index 000000000..ad91b2cb8 --- /dev/null +++ b/engine/builtin_fx/HarmonyEnsembleSpace.h @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +#include +#include + +// Deterministic, bounded, single-audio-owner backing-singer individualization. +// Prepared on non-realtime thread. No random generator, heap, mutex, worker +// or transcendental functions in the PCM callback. +namespace armix::harmony { +class EnsembleSpace final { +public: + static constexpr int voices=5; + static constexpr std::size_t reflectionCapacity=8192; + static constexpr std::size_t sineCount=1024; + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + constexpr float tau=6.2831853071795864769f; + constexpr std::array arrivalMs{{5.f,9.f,12.f,15.f,17.f}}; + constexpr std::array reflectionMs{{12.f,15.f,17.f,19.f,20.f}}; + constexpr std::array secondMs{{27.f,31.f,34.f,36.f,38.f}}; + constexpr std::array cents{{-2.8f,1.4f,3.2f,-1.7f,2.3f}}; + constexpr std::array vibratoHz{{4.7f,5.2f,4.9f,5.35f,4.55f}}; + constexpr std::array vibratoCents{{2.4f,3.0f,2.8f,2.6f,3.3f}}; + constexpr std::array timbre{{.93f,.86f,.96f,.89f,.83f}}; + for(std::size_t i=0;i(i)/sineCount); + for(int v=0;v(v); + offset_[ix]=static_cast(rate*arrivalMs[ix]*.001); + reflectionDelay_[ix]=static_cast(std::lround(rate*reflectionMs[ix]*.001)); + secondDelay_[ix]=static_cast(std::lround(rate*secondMs[ix]*.001)); + if(reflectionDelay_[ix]<2|| + reflectionDelay_[ix]>=static_cast(reflectionCapacity)|| + secondDelay_[ix]<=reflectionDelay_[ix]|| + secondDelay_[ix]>=static_cast(reflectionCapacity))return false; + ratio_[ix]=std::pow(2.f,cents[ix]/1200.f); + vibratoRatio_[ix]=vibratoCents[ix]*(.69314718056f/1200.f); + increment_[ix]=vibratoHz[ix]/static_cast(rate); + phase_[ix]=static_cast(ix)*.197f; + tone_[ix]=timbre[ix]; + reflections_[ix].fill(0.f); + } + // A sample-rate-stable, shallow air/room HF shading of backing + // singers only. Bypassed exactly when Space=0; lead is untouched. + shadingAlpha_=static_cast( + 1.-std::exp(-2.*3.141592653589793*4400./rate)); + distanceLowpass_.fill(0.f); + ageStart_=static_cast(rate*.065); + ageRamp_=static_cast(1./(rate*.165)); + age_=0;cursor_=0; + return true; + } + float shadeBacking(int v,float sample,float space) noexcept { + const auto i=static_cast(v); + distanceLowpass_[i]+=shadingAlpha_*(sample-distanceLowpass_[i]); + const float blend=.52f*std::clamp(space,0.f,1.f); + return sample*(1.f-blend)+distanceLowpass_[i]*blend; + } + float arrivalSamples(int v) const noexcept { + return offset_[static_cast(v)]; + } + int reflectionSamples(int v) const noexcept { + return reflectionDelay_[static_cast(v)]; + } + float timbreCoefficient(int v,float colorCoefficient) const noexcept { + return colorCoefficient*tone_[static_cast(v)]; + } + // All 5 LFOs have independent, deterministic phase and frequency. + // Sustain vibrato appears gradually; no per-block reset or random jitter. + float pitchRatio(int v) noexcept { + const auto i=static_cast(v); + const float position=phase_[i]*static_cast(sineCount); + const auto index=static_cast(position); + const auto following=(index+1)&(sineCount-1); + const float fraction=position-static_cast(index); + const float wave=sine_[index]+(sine_[following]-sine_[index])*fraction; + phase_[i]+=increment_[i]; + if(phase_[i]>=1.f)phase_[i]-=1.f; + const float mature=std::clamp((age_-ageStart_)*ageRamp_,0.f,1.f); + return ratio_[i]*(1.f+wave*vibratoRatio_[i]*mature); + } + // Exactly one filtered early reflection from each independent singer. + // Fixed integer tap delay: changing Space can never sweep a delay line + // or create a pitch chirp. 4096 samples covers 20ms even at 192k. + float exchangeReflection(int v,float sample) noexcept { + const auto i=static_cast(v); + reflections_[i][cursor_]=sample; + const auto delayed=(cursor_+reflectionCapacity- + static_cast(reflectionDelay_[i]))&(reflectionCapacity-1); + return reflections_[i][delayed]; + } + // A distinct later early-reflection arrival from the very same bounded + // singer ring. Call after exchangeReflection() writes the current sample. + float secondReflection(int singer) const noexcept { + const auto i=static_cast(singer); + const auto at=(cursor_+reflectionCapacity- + static_cast(secondDelay_[i]))&(reflectionCapacity-1); + return reflections_[i][at]; + } + int secondReflectionSamples(int singer) const noexcept { + return secondDelay_[static_cast(singer)]; + } + void advance(bool voiced) noexcept { + cursor_=(cursor_+1)&(reflectionCapacity-1); + if(!voiced)age_=0; + else if(age_<100000000)++age_; + } +private: + std::array sine_{}; + std::array,voices> reflections_{}; + std::array offset_{},ratio_{},vibratoRatio_{}; + std::array increment_{},phase_{},tone_{},distanceLowpass_{}; + float shadingAlpha_=0.f; + std::array reflectionDelay_{},secondDelay_{}; + std::size_t cursor_=0; + int age_=0,ageStart_=0; + float ageRamp_=0.f; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyMusicKey.h b/engine/builtin_fx/HarmonyMusicKey.h index 3743cb79f..a633fa0f2 100644 --- a/engine/builtin_fx/HarmonyMusicKey.h +++ b/engine/builtin_fx/HarmonyMusicKey.h @@ -4,6 +4,7 @@ #include #include #include +#include "HarmonyStereoFold.h" // Audio-thread-only, bounded chroma guide from a DIFFERENT ChannelId's real // pre-fader PCM. Not a chord recognizer and never an audible sidechain. @@ -21,6 +22,7 @@ class MusicKeyDetector final { bool prepare(double rate) noexcept { if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; decimation_=std::max(1,static_cast(std::round(rate/6000.))); + if(!stereoFold_.prepare(rate))return false; const double fs=rate/decimation_; sampleAlpha_=static_cast(1.-std::exp(-1./(fs*.18))); lowpassAlpha_=static_cast(1.-std::exp(-2.*3.141592653589793*1500./rate)); @@ -35,6 +37,7 @@ class MusicKeyDetector final { reset();return true; } void reset() noexcept { + stereoFold_.reset(); inC_.fill(0.f);inS_.fill(0.f); oscC_.fill(1.f);oscS_.fill(0.f);hist_.fill(0.f); downCount_=0;periodCount_=0;normalizeCount_=0; @@ -47,7 +50,7 @@ class MusicKeyDetector final { for(int n=0;n rotorC_{},rotorS_{},weight_{}; std::array oscC_{},oscS_{},inC_{},inS_{}; std::array hist_{}; diff --git a/engine/builtin_fx/HarmonyPitchSynchronous.h b/engine/builtin_fx/HarmonyPitchSynchronous.h new file mode 100644 index 000000000..9395ea1ca --- /dev/null +++ b/engine/builtin_fx/HarmonyPitchSynchronous.h @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +#include +#include +#include + +namespace armix::harmony { +// Experimental causal pitch-synchronous source-window candidate. +// Vowel waveforms are read at unity sample speed; pitch changes arise by +// re-spacing aligned source windows, rather than resampling their formants. +// Positive waveform peaks are approximate epochs, NOT proven glottal closures. +// This must pass actual singer A/B before acoustic release qualification. +// All state is fixed; no allocation, locks, FFT or IO in process(). +template +class PitchSynchronousVoice final { + static_assert(Capacity >= 4096 && (Capacity & (Capacity-1)) == 0); + static_assert(Voices > 0 && Voices <= 8); +public: + struct Output { float sample=0.f; bool ready=false; }; + void reset() noexcept { states_={}; } + void resetVoice(int v) noexcept { + if(v>=0&&v(v)]={}; + } + // time/writeIndex refer to the input sample just written to the ring. + // fixedDelay retains the host's published latency; a singer-specific + // arrival delay can be added without moving the original dry PCM. + Output process(int v,std::uint64_t time,std::size_t writeIndex, + const std::array& input, + float sourcePeriod,float ratio,float fixedDelay) noexcept { + if(v<0||v>=Voices||!std::isfinite(sourcePeriod)|| + !std::isfinite(ratio)||!std::isfinite(fixedDelay)|| + sourcePeriod<8.f||sourcePeriod>3200.f|| + ratio<.49f||ratio>2.01f||fixedDelay(v)]; + const double period=static_cast(sourcePeriod); + const double spacing=period/static_cast(ratio); + const double width=std::max(period,spacing); + if(!state.initialized) { + state.initialized=true; + state.nextCenter=static_cast(time)+width; + } + // Advance at most two grains per sample, even on hostile period jumps. + // Normal supported periods place >=4 samples between target marks. + for(int scheduled=0;scheduled<2 && + static_cast(time)+.0001>=state.nextCenter-width;++scheduled) { + const double desired=state.nextCenter-static_cast(fixedDelay); + const auto peak=findPositiveEpoch(input,time,writeIndex,desired,period); + if(peak>=0) { + auto& grain=state.grains[state.nextSlot]; + grain={state.nextCenter,static_cast(peak),width,true}; + state.nextSlot=(state.nextSlot+1)%state.grains.size(); + } + state.nextCenter+=spacing; + } + if(state.nextCenter+width(time)) + state.nextCenter=static_cast(time)+width; + float sum=0.f,weight=0.f; + for(auto& grain:state.grains) { + if(!grain.active)continue; + const double offset=static_cast(time)-grain.center; + const double u=offset/grain.width; + if(u>=1.){grain.active=false;continue;} + if(u<=-1.)continue; + // Raised compact zero-slope window, avoiding per-sample trig. + const float w=static_cast((1.-u*u)*(1.-u*u)); + const float sample=readAt(input,time,writeIndex,grain.source+offset); + sum+=sample*w;weight+=w; + } + if(weight<.05f)return {}; + return {sum/weight,true}; + } +private: + struct Grain {double center=0.,source=0.,width=1.;bool active=false;}; + struct State { + std::array grains{}; + double nextCenter=0.; + std::size_t nextSlot=0; + bool initialized=false; + }; + // Bound the peak search to <1 source cycle, never scanning beyond the + // already received microphone input. Unreliable speech uses fallback. + static std::int64_t findPositiveEpoch(const std::array& input, + std::uint64_t time,std::size_t writeIndex, + double desired,double period) noexcept { + const auto half=static_cast(std::ceil(period*.46)); + const auto center=static_cast(std::llround(desired)); + const auto oldest=static_cast( + time>=Capacity-3?time-(Capacity-3):0); + const auto newest=static_cast(time>1?time-1:0); + const auto start=std::max(oldest,center-half); + const auto end=std::min(newest,center+half); + if(end( + static_cast(time)-t); + const float value=input[(writeIndex+Capacity-age)&(Capacity-1)]; + if(value>best){best=value;peak=t;} + } + return peak; + } + static float readAt(const std::array& input, + std::uint64_t time,std::size_t writeIndex,double position) noexcept { + if(time<2||position<0.||position>static_cast(time-1)|| + position( + time>=Capacity-3?time-(Capacity-3):0))return 0.f; + const double age=static_cast(time)-position; + const auto whole=static_cast(age); + const float fractional=static_cast( + age-static_cast(whole)); + const auto index=(writeIndex+Capacity-whole)&(Capacity-1); + return input[index]*(1.f-fractional)+ + input[(index+Capacity-1)&(Capacity-1)]*fractional; + } + std::array states_{}; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyPreparedTone.h b/engine/builtin_fx/HarmonyPreparedTone.h new file mode 100644 index 000000000..ee1d78cd0 --- /dev/null +++ b/engine/builtin_fx/HarmonyPreparedTone.h @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +#include +#include + +// Control-thread prepared Harmony timbre filter and equal-temperament +// interval ratios. No transcendental function or allocation on AUDIO. +namespace armix::harmony { +class RateStableColor final { +public: + static constexpr std::size_t segments=256; + // 48k is the previously approved Color response; all other rates + // preserve the same one-pole physical-time attenuation. + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + for(std::size_t i=0;i<=segments;++i){ + const double color=static_cast(i)/segments; + const double previous=.07+.77*color; + coefficients_[i]=static_cast( + 1.-std::pow(1.-previous,48000./rate)); + } + return true; + } + float coefficient(float color) const noexcept { + const float position=std::clamp(color,0.f,1.f)*segments; + const auto index=static_cast(position); + const auto next=std::min(index+1,segments); + const float fraction=position-static_cast(index); + const float current=coefficients_[index]; + return current+(coefficients_[next]-current)*fraction; + } +private: + std::array coefficients_{}; +}; +class PreparedPitchIntervals final { +public: + static constexpr int maximumSemitones=24; + void prepare() noexcept { + for(int delta=-maximumSemitones;delta<=maximumSemitones;++delta) + ratios_[static_cast(delta+maximumSemitones)]= + std::pow(2.f,static_cast(delta)/12.f); + } + // Caller guarantees scale-degree intervals remain inside +/-24. + // Clamping makes an unexpected new music scale safe rather than OOB. + float ratio(int semitones) const noexcept { + const int index=std::clamp(semitones,-maximumSemitones,maximumSemitones); + return ratios_[static_cast(index+maximumSemitones)]; + } +private: + std::array ratios_{}; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyRoomFocus.h b/engine/builtin_fx/HarmonyRoomFocus.h new file mode 100644 index 000000000..90bb9e3fe --- /dev/null +++ b/engine/builtin_fx/HarmonyRoomFocus.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +namespace armix::harmony { +// Backing-ROOM ONLY, no lead/direct FX; prepared off realtime. +class BackingRoomFocus final { +public: + struct Stereo {float left=0.f,right=0.f;}; + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + constexpr double pi=3.14159265358979323846; + hp_=static_cast(1.-std::exp(-2.*pi*170./rate)); + attack_=static_cast(1.-std::exp(-1./(rate*.012))); + release_=static_cast(1.-std::exp(-1./(rate*.240))); + lowL_=lowR_=env_=0.f; + return hp_>0.f&&hp_<1.f&&attack_>0.f&&release_>0.f; + } + Stereo filterSend(float l,float r) noexcept { + lowL_+=hp_*(l-lowL_);lowR_+=hp_*(r-lowR_); + if(std::abs(lowL_)<1.e-12f)lowL_=0.f; + if(std::abs(lowR_)<1.e-12f)lowR_=0.f; + return {l-lowL_,r-lowR_}; + } + float returnGain(float dryL,float dryR,float space) noexcept { + const float level=std::min(4.f,std::max(std::abs(dryL),std::abs(dryR))); + env_+=(level>env_?attack_:release_)*(level-env_); + if(env_<1.e-10f)env_=0.f; + return 1.f-.22f*std::clamp(space,0.f,1.f)*(env_/(env_+.10f)); + } + float leadEnvelope()const noexcept{return env_;} +private: + float hp_=0.f,attack_=0.f,release_=0.f,lowL_=0.f,lowR_=0.f,env_=0.f; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyStageSpatial.h b/engine/builtin_fx/HarmonyStageSpatial.h new file mode 100644 index 000000000..d032d40ab --- /dev/null +++ b/engine/builtin_fx/HarmonyStageSpatial.h @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include +#include +#include +namespace armix::harmony { +// A fixed-clock backing-only virtual stage, not individualized HRTF rendering. +// Per-voice far-ear precedence timing and two physically separated early taps +// supplement the existing ONE shared room. No modulated delay or polarity flips. +class BackingStageSpatial final { +public: + static constexpr int voices=5; + static constexpr std::size_t earCapacity=128; + struct Gains {float direct=1.f,early=0.f,late=0.f;}; + struct Output {float directL=0, directR=0, earlyL=0, earlyR=0, roomL=0, roomR=0;}; + static Gains gains(float space) noexcept { + const float s=std::clamp(space,0.f,1.f); + return {1.f-.62f*s,.49f*s,1.08f*s}; + } + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + const auto delay=static_cast(std::lround(rate*.00022)); + if(delay<2||delay>=earCapacity)return false; + farEarDelay_=delay; + // Head-shadow proxy: the contralateral signal is gently softened + // before a fixed precedence delay. Prepared once per device rate. + // Not a measured HRTF and never applied to the original dry lead. + farEarLowpassAlpha_=static_cast( + 1.-std::exp(-2.*3.141592653589793*4100./rate)); + if(!(farEarLowpassAlpha_>0.f&&farEarLowpassAlpha_<1.f))return false; + farEarLowpassState_.fill(0.f); + for(auto& ring:farEarHistory_)ring.fill(0.f); + cursor_=0; + return true; + } + Output place(int singer,float source,float firstReflection, + float secondReflection,float mixL,float mixR, + float space) noexcept { + // Engine calls with validated singer indices [0,4]. + const auto i=static_cast(singer); + auto& ring=farEarHistory_[i]; + float& filtered=farEarLowpassState_[i]; + filtered+=farEarLowpassAlpha_*(source-filtered); + // Store filtered signal for the far ear only. The near ear keeps the + // original pitch-synchronous PCM unchanged. Reuse the old fixed ring: + // no extra audio buffer, allocation or reported latency. + ring[cursor_]=filtered; + const float delayed=ring[(cursor_+earCapacity-farEarDelay_)&(earCapacity-1)]; + const float sum=std::abs(mixL)+std::abs(mixR); + const float direction=sum>1.e-9f?(mixR-mixL)/sum:0.f; + // Only the quieter, contralateral ear carries the tiny fixed lag. + // Space crossfades the level, NOT the delay (no Doppler sweep). + const float cross=.68f*std::clamp(space,0.f,1.f)* + std::clamp(2.15f*std::abs(direction),0.f,1.f); + const float far=source*(1.f-cross)+delayed*cross; + const float left=(direction>0.f?far:source)*mixL; + const float right=(direction<0.f?far:source)*mixR; + // Center backing voice otherwise coincides with the center lead. + // Recess its *direct* component slightly but retain early and + // shared-room sends: a modest DRR cue, not a volume duck on lead. + // At Space 0 the original direct output is bit-for-bit unchanged. + const float centerAmount=1.f-std::clamp( + 1.7f*std::abs(direction),0.f,1.f); + const float centerRecess=1.f-.17f* + std::clamp(space,0.f,1.f)*centerAmount; + // Early sound spreads in both directions with positive polarity: + // first reflection is contralateral, second modestly ipsilateral. + const float earlyLeft=.68f*firstReflection*mixR+ + .32f*secondReflection*(.67f*mixL+.33f*mixR); + const float earlyRight=.68f*firstReflection*mixL+ + .32f*secondReflection*(.33f*mixL+.67f*mixR); + return {left*centerRecess,right*centerRecess, + earlyLeft,earlyRight,left,right}; + } + void advance() noexcept {cursor_=(cursor_+1)&(earCapacity-1);} + std::size_t farEarDelaySamples()const noexcept{return farEarDelay_;} +private: + std::array,voices> farEarHistory_{}; + std::array farEarLowpassState_{}; + float farEarLowpassAlpha_=0.f; + std::size_t cursor_=0,farEarDelay_=0; +}; +} // namespace armix::harmony diff --git a/engine/builtin_fx/HarmonyStereoFold.h b/engine/builtin_fx/HarmonyStereoFold.h new file mode 100644 index 000000000..8d6f4f854 --- /dev/null +++ b/engine/builtin_fx/HarmonyStereoFold.h @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#pragma once +#include +#include + +// Callback-owned analysis ONLY, not an audible stereo output mixer. +// Recover music/vocal guide PCM when opposite-polarity stereo cancels L+R. +// Normal correlated stereo remains the exact legacy sum; near-opposition +// needs two-threshold hysteresis and a smoothed phase-aware sign fold. +// Constant-work, fixed-state, lock/heap-free, zero added sample latency. +namespace armix::harmony { +class StereoPhaseSafeFold final { +public: + bool prepare(double rate) noexcept { + if(!std::isfinite(rate)||rate<32000.||rate>192000.)return false; + evidenceAlpha_=static_cast(1.-std::exp(-1./(rate*.020))); + blendAlpha_=static_cast(1.-std::exp(-1./(rate*.015))); + reset(); + return true; + } + void reset() noexcept { + energyL_=0.f;energyR_=0.f;cross_=0.f;amount_=0.f; + antiCorrelated_=false; + } + float process(float left,float right) noexcept { + // Both callers sanitize nonfinite source PCM. Limit only correlation + // evidence amplitude, never change a valid dry stereo source. + const float l=std::clamp(left,-4.f,4.f); + const float r=std::clamp(right,-4.f,4.f); + energyL_+=evidenceAlpha_*(l*l-energyL_); + energyR_+=evidenceAlpha_*(r*r-energyR_); + cross_+=evidenceAlpha_*(l*r-cross_); + const float power=.5f*(energyL_+energyR_); + if(power<1.e-8f)antiCorrelated_=false; + else if(antiCorrelated_){ + if(cross_>-.55f*power)antiCorrelated_=false; + }else if(cross_<-.78f*power)antiCorrelated_=true; + amount_+=blendAlpha_*((antiCorrelated_?1.f:0.f)-amount_); + // amount=0 -> original 0.5*(L+R) for ordinary audio. + // amount=1 -> recovered 0.5*(L-R). + // Use bounded evidence samples for the return as well: finite + // but huge external float PCM would otherwise overflow L+R before + // pitch/Music Guide filtering. + return .5f*(l+r)-amount_*r; + } + float rescueAmount() const noexcept{return amount_;} +private: + float evidenceAlpha_=0.f,blendAlpha_=0.f; + float energyL_=0.f,energyR_=0.f,cross_=0.f,amount_=0.f; + bool antiCorrelated_=false; +}; +} // namespace armix::harmony diff --git a/site/preview-v2/assets/mixer-fx-rack.css b/site/preview-v2/assets/mixer-fx-rack.css index b5f8376f9..74cf294a6 100644 --- a/site/preview-v2/assets/mixer-fx-rack.css +++ b/site/preview-v2/assets/mixer-fx-rack.css @@ -518,7 +518,7 @@ .mx-harm-sliders{ display:grid;grid-template-columns:repeat(2,minmax(105px,1fr)); width:min(100%,390px);align-items:center;justify-items:center; - gap:5px 17px;padding:8px 12px + gap:14px 17px;padding:3px 12px } .mx-harm-slider{ --dial-turn:0deg;display:flex;flex-direction:column;align-items:center; @@ -566,7 +566,12 @@ transform-origin:50% 27px;transform:rotate(var(--dial-turn)); pointer-events:none } -.mx-harm-dial-label{font-size:9px;letter-spacing:.85px;color:#405963;font-weight:600} +/* Outer illuminated ring extends 11px beyond the knob's actual face. + Leave real space below the halo without growing the mixer FX editor. */ +.mx-harm-dial-label{ + margin-top:12px;font-size:9px;line-height:11px; + letter-spacing:.85px;color:#405963;font-weight:600 +} .mx-harm-slider output{ font:500 10px/1.2 Inter,'Segoe UI',sans-serif; font-variant-numeric:tabular-nums;color:#253f49 @@ -710,11 +715,13 @@ .mx-harm-foot>span:first-child{ min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap } +.mx-harm-foot #mxHTracking{flex:0 0 auto;white-space:nowrap;color:#80c6c3} +.mx-harm-note.queued:not(.active){outline:2px solid #69d6b4;outline-offset:1px} @media(max-width:740px){ .mx-harm-body{grid-template-columns:126px minmax(0,1fr) 206px;min-height:224px} .mx-harm-left{padding:14px 9px 10px} .mx-harm-signature strong{font-size:15px} - .mx-harm-sliders{grid-template-columns:repeat(2,minmax(78px,1fr));gap:4px;padding:5px} + .mx-harm-sliders{grid-template-columns:repeat(2,minmax(78px,1fr));gap:10px 4px;padding:2px 5px} .mx-harm-dial-face{width:59px;height:59px} .mx-harm-dial-pointer{height:19px;transform-origin:50% 23px} .mx-harm-slider{height:101px} diff --git a/site/preview-v2/assets/mixer-native-harmony.js b/site/preview-v2/assets/mixer-native-harmony.js index 9800f90d4..c7d4d11f6 100644 --- a/site/preview-v2/assets/mixer-native-harmony.js +++ b/site/preview-v2/assets/mixer-native-harmony.js @@ -60,7 +60,7 @@ root.innerHTML=` MANUAL -
PREVIEW · NO AUDIO
+
PREVIEW · NO AUDIOVOICE WAITING
`; // Cached editor nodes and change-only text: no DOM traversal/paint churn on // native readback ticks while the user sings. @@ -106,7 +106,7 @@ const backend=()=>window.__JUCE__?.backend; const state=()=>window.__ARMIX_NATIVE_M2__?.state?.()?.current; const demos=new Map(); let current=null,settings=fresh(),pending=null,latest=null,seq=0,readbackRequest=""; -let readbackSentAt=0; +let readbackSentAt=0,verified=false,heldPatch=null; let mode="DEMO",readbackLatency=0,observedNote=-1,preset="trio"; let detectedKey=-1,detectedScale=-1,detectedConfidence=0,lastGuideRead=0; let effectiveKey=-1,effectiveScale=-1,sourceSignature="",availableSources=[]; @@ -118,15 +118,75 @@ const sliders=sliderDefs.map(([key])=>({ key,input:root.querySelector('[data-field="'+key+'"]'), output:root.querySelector('[data-out="'+key+'"]') })); -const context=()=>{ +// The native accepted graph is the only authority, including after reopen. +// Track exact insert identity through a slot move; allow a successor at the +// former slot only on the SAME stable ChannelId. Never retarget another strip. +const acceptedLocation=()=>{ const n=state(); if(!current||!native()||!n?.admitted)return null; - const channel=n.graph?.channels?.find(ch=>ch.id===current.channelId); - const insert=channel?.inserts?.[current.slot]; - if(insert?.id!==current.instanceId||insert?.catalogId!==ID)return null; - return {channelId:current.channelId,deviceEpoch:n.device.epoch, - graphRevision:n.graph.revision,slot:current.slot,instanceId:current.instanceId}; + const id=current.channelId||window.__ARMIX_NATIVE_M2__?.nativeIdFor?.(current.track); + const channel=n.graph?.channels?.find(ch=>ch.id===id),inserts=channel?.inserts; + if(!Array.isArray(inserts))return null; + let slot=current.instanceId? + inserts.findIndex(x=>x?.id===current.instanceId&&x.catalogId===ID):-1; + if(slot<0&&inserts[current.slot]?.catalogId===ID&&inserts[current.slot]?.id) + slot=current.slot; + if(slot<0)return null; + const insert=inserts[slot]; + if(!insert?.id||insert.catalogId!==ID)return null; + return {channelId:id,deviceEpoch:n.device.epoch, + graphRevision:n.graph.revision,slot,instanceId:insert.id}; +}; +const context=()=>{ + const c=acceptedLocation(); + if(!c||!current||c.channelId!==current.channelId|| + c.instanceId!==current.instanceId||c.slot!==current.slot|| + c.deviceEpoch!==current.boundEpoch|| + c.graphRevision!==current.boundRevision)return null; + return c; }; +// Carry only actually edited fields; never push stale full plugin presets +// into a freshly created instance after a device/graph transition. +function holdUnfinishedEdit(){ + const desired=latest||pending?.settings; + if(!desired)return; + const diff={}; + for(const key of Object.keys(settings)) + if(desired[key]!==settings[key])diff[key]=desired[key]; + heldPatch={...diff,...(heldPatch||{})}; +} +function reconcileBinding(){ + if(!native()||!current)return false; + const c=acceptedLocation(); + if(!c){ + if(verified||pending||readbackRequest){ + holdUnfinishedEdit();verified=false;pending=null;latest=null; + readbackRequest="";effectiveKey=-1;effectiveScale=-1; + hint("HARMONY RECONNECTING · WAITING FOR ACCEPTED FX");paint(); + } + return false; + } + const changed=current.channelId!==c.channelId|| + current.instanceId!==c.instanceId||current.slot!==c.slot|| + current.boundEpoch!==c.deviceEpoch|| + current.boundRevision!==c.graphRevision; + if(changed){ + holdUnfinishedEdit(); + current.channelId=c.channelId;current.instanceId=c.instanceId; + current.slot=c.slot;current.boundEpoch=c.deviceEpoch; + current.boundRevision=c.graphRevision; + pending=null;latest=null;verified=false; + readbackRequest="";readbackSentAt=0; + effectiveKey=-1;effectiveScale=-1; + detectedKey=-1;detectedScale=-1;detectedConfidence=0; + showText(el("mxHChannel"),current.track.name+" · FX "+(c.slot+1)); + hint("HARMONY CONNECTED · READING DSP"); + refreshGuideSources();paint();snapshot(true); + }else if(!verified&&(!readbackRequest||Date.now()-readbackSentAt>2500)){ + snapshot(true); + } + return true; +} const hint=t=>showText(el("mxHStatus"),t); const valid=s=>s&&Number.isInteger(s.key)&&s.key>=0&&s.key<12&& Number.isInteger(s.scale)&&s.scale>=0&&s.scale<=1&& @@ -176,7 +236,8 @@ function syncPower(){ power.setAttribute("aria-label",power.title); } function paint(){ - const v=latest||pending?.settings||settings; + const v=latest||pending?.settings|| + (heldPatch?{...settings,...heldPatch}:settings); const auto=v.guideMode===1; // Native LCD never guesses from music confidence or pending UI edits. // One atomic native readback is the actual effective DSP key+scale pair. @@ -186,6 +247,13 @@ function paint(){ const reliable=displayKey>=0&&displayKey<12&&displayScale>=0&&displayScale<=1; const inGraph=availableSources.some(ch=>ch.id===v.guideChannelId); const detected=auto&&detectedKey>=0&&detectedScale>=0; + // Gesture feedback is visibly immediate, but the active key still + // reflects ONLY the last native PCM ACK + authoritative readback. + const draft=latest||pending?.settings|| + (heldPatch?{...settings,...heldPatch}:null); + const queuedKey=nativeView&&draft&& + (draft.key!==settings.key||draft.scale!==settings.scale) + ?draft.key:-1; for(const b of guideButtons){ const active=Number(b.dataset.guideMode)===v.guideMode; b.classList.toggle("active",active); @@ -195,8 +263,11 @@ function paint(){ const source=el("mxHGuideSource"); const chosen=inGraph?v.guideChannelId:"0"; if(source.value!==chosen)source.value=chosen; - for(const b of noteButtons)b.classList.toggle("active", - reliable&&Number(b.dataset.key)===displayKey); + for(const b of noteButtons){ + b.classList.toggle("active", + reliable&&Number(b.dataset.key)===displayKey); + b.classList.toggle("queued",Number(b.dataset.key)===queuedKey); + } for(const b of voiceButtons){ const active=Number(b.dataset.voices)===v.voices; b.classList.toggle("active",active); @@ -231,6 +302,12 @@ function paint(){ showText(output,key==="gainDb"?v[key].toFixed(1)+" dB":Math.round(value)+"%"); } syncPower(); + const detectedNote=observedNote>=0 && observedNote<=127? + keys[observedNote%12]+String(Math.floor(observedNote/12)-1):""; + const tracking=nativeView? + !verified?"DSP SYNCING":detectedNote?"TRACKING "+detectedNote:"WAITING VOICE": + "DEMO"; + showText(el("mxHTracking"),tracking); showText(el("mxHLatency"),mode==="NATIVE"?readbackLatency+" samples":"DEMO"); } // One in-flight readback, a bounded retry on the EXISTING 12 Hz console @@ -246,24 +323,31 @@ function snapshot(force=false){ } function send(next){ const c=context(); - if(!c){hint("NATIVE INSTANCE CHANGED · REOPEN FX");return;} + if(!c||!verified){hint("HARMONY RECONNECTING · EDIT HELD");return;} if(pending){latest=next;hint("DSP APPLYING · LATEST EDIT HELD");paint();return;} const id="harm-"+String(Date.now()*1000+(seq++%1000)); pending={id,settings:next,ack:false,context:c,startedAt:Date.now()}; backend().emitEvent("arMixAction",{action:"nativeHarmonySetSettings", requestId:id,...c,settings:next}); - hint("DSP QUEUED · AWAITING AUDIO ACK");paint(); + hint(next.key!==settings.key||next.scale!==settings.scale? + "KEY "+keys[next.key]+(next.scale?"m":"")+" · AWAITING DSP ACK": + "DSP QUEUED · AWAITING AUDIO ACK");paint(); } function edit(patch){ - const next={...(latest||pending?.settings||settings),...patch}; + const next={...(latest||pending?.settings|| + (heldPatch?{...settings,...heldPatch}:settings)),...patch}; if(!valid(next))return; preset="custom"; if(!native()){ settings=next; demos.set(current?.track?.id+":"+current?.slot,{...settings}); hint("BROWSER PREVIEW · VISUAL ONLY · NO AUDIO"); - }else if(mode==="NATIVE")send(next); - else hint("NATIVE HARMONY NOT ACCEPTED"); + }else if(mode==="NATIVE"){ + if(!reconcileBinding()||!verified){ + heldPatch={...(heldPatch||{}),...patch}; + hint("HARMONY RECONNECTING · EDIT SAVED"); + }else send(next); + }else hint("NATIVE HARMONY NOT ACCEPTED"); paint(); } noteButtons.forEach(b=>b.addEventListener("click",()=> @@ -296,7 +380,12 @@ el("mxHPreset").addEventListener("change",e=>{ const next={...(latest||pending?.settings||settings),...change}; if(!native()){settings=next;demos.set(current?.track?.id+":"+current?.slot,{...next}); hint("BROWSER PREVIEW · VISUAL ONLY · NO AUDIO"); - }else if(mode==="NATIVE")send(next); + }else if(mode==="NATIVE"){ + if(!reconcileBinding()||!verified){ + heldPatch={...(heldPatch||{}),...change}; + hint("HARMONY RECONNECTING · PRESET SAVED"); + }else send(next); + } }paint(); }); el("mxHPower").addEventListener("click",()=>{ @@ -331,21 +420,26 @@ window.ArSonMixNativeHarmony=Object.freeze({ id:ID, insert(target){return Boolean(window.__ARMIX_NATIVE_M2__?.insertPlugin?.(ID,target));}, open(track,slot){ - pending=null;latest=null;seq++;observedNote=-1;readbackLatency=0; + pending=null;latest=null;heldPatch=null;verified=false; + seq++;observedNote=-1;readbackLatency=0; readbackRequest="";readbackSentAt=0; detectedKey=-1;detectedScale=-1;detectedConfidence=0;lastGuideRead=0; effectiveKey=-1;effectiveScale=-1;sourceSignature="";availableSources=[]; const n=state(),id=window.__ARMIX_NATIVE_M2__?.nativeIdFor?.(track); const ch=n?.graph?.channels?.find(x=>x.id===id),insert=ch?.inserts?.[slot]; - current={track,slot,channelId:null,instanceId:null}; - if(native()&&n?.admitted&&insert?.catalogId===ID&&insert.id){ - current.channelId=id;current.instanceId=insert.id;mode="NATIVE"; - settings=fresh();hint("NATIVE · READING ACCEPTED DSP");snapshot(); - }else if(!native()){ + current={track,slot,channelId:id,instanceId:null, + boundEpoch:null,boundRevision:null}; + showText(el("mxHChannel"),track.name+" · FX "+(slot+1)); + if(native()){ + mode="NATIVE";settings=fresh(); + hint("HARMONY · LOCATING ACCEPTED DSP"); + if(n?.admitted&&insert?.catalogId===ID&&insert.id) + current.instanceId=insert.id; + reconcileBinding(); + }else{ mode="DEMO";settings={...(demos.get(track.id+":"+slot)||fresh())}; hint("BROWSER PREVIEW · VISUAL ONLY · NO AUDIO"); - }else{mode="UNAVAILABLE";settings=fresh();hint("NATIVE HARMONY SLOT NOT ACCEPTED");} - el("mxHChannel").textContent=track.name+" · FX "+(slot+1); + } root.hidden=false;refreshGuideSources();paint(); }, close(){root.hidden=true;} @@ -380,7 +474,7 @@ backend()?.addEventListener?.("arMixBuiltinHarmonySnapshot",snap=>{ if(pending.ack)hint("DSP APPLIED · WAITING READBACK"); return; } - settings={...snap.settings}; + settings={...snap.settings};verified=true; effectiveKey=snap.effectiveKey;effectiveScale=snap.effectiveScale; detectedKey=Number.isInteger(snap.guideDetectedKey)?snap.guideDetectedKey:-1; detectedScale=Number.isInteger(snap.guideDetectedScale)?snap.guideDetectedScale:-1; @@ -388,18 +482,28 @@ backend()?.addEventListener?.("arMixBuiltinHarmonySnapshot",snap=>{ observedNote=Number.isInteger(snap.observedNote)?snap.observedNote:-1; readbackLatency=Number.isInteger(snap.latencySamples)?snap.latencySamples:0; pending=null; + refreshGuideSources(); if(latest){const next=latest;latest=null;send(next);} - else hint(settings.guideMode===1?"NATIVE · LIVE MUSIC GUIDE":"NATIVE DSP ACCEPTED · "+c.instanceId); - refreshGuideSources();paint(); + else if(heldPatch){ + const patch=heldPatch;heldPatch=null; + const next={...settings,...patch}; + if(next.guideMode===1&& + !availableSources.some(ch=>ch.id===next.guideChannelId)){ + next.guideMode=0;next.guideChannelId="0"; + } + if(valid(next))send(next); + else hint("HARMONY RESTORED · INVALID EDIT DISCARDED"); + }else hint(settings.guideMode===1?"NATIVE · LIVE MUSIC GUIDE":"NATIVE DSP ACCEPTED · "+c.instanceId); + paint(); }); backend()?.addEventListener?.("arMixConsoleSnapshot",()=>{ - if(root.hidden||!native()||!current?.instanceId)return; + if(root.hidden||!native()||!current)return; queueMicrotask(()=>{ - if(!context()){ - pending=null;latest=null;effectiveKey=-1;effectiveScale=-1; - readbackRequest=""; - hint("DEVICE / GRAPH CHANGED · REOPEN ACCEPTED FX");paint();return; + if(!reconcileBinding()){ + hint("HARMONY RECONNECTING · WAITING FOR ACCEPTED FX"); + return; } + if(!verified)return; syncPower(); const now=Date.now(); if(pending){ @@ -413,7 +517,10 @@ backend()?.addEventListener?.("arMixConsoleSnapshot",()=>{ } return; } - if(settings.guideMode===1&&now-lastGuideRead>750){ + // Manual mode also needs real singer activity. The accepted native + // audio-owner observedNote already exists; use this 12Hz console tick + // at <=2Hz readback without allocating a new polling timer. + if(now-lastGuideRead>500){ lastGuideRead=now;snapshot(); } }); diff --git a/tests/approved_native_harmony_smoke.py b/tests/approved_native_harmony_smoke.py index 32aeb3249..3d50d7014 100644 --- a/tests/approved_native_harmony_smoke.py +++ b/tests/approved_native_harmony_smoke.py @@ -25,15 +25,15 @@ def last_action(page,kind): return actions[-1] def readback(page,state,values,key,scale,detected=(-1,-1),applied=None, - request=None): + request=None,instance=INSTANCE,observed=-1): action=request or last_action(page,"nativeHarmonySnapshot") packet={ "requestId":action["requestId"], - "channelId":SOURCE,"instanceId":INSTANCE,"status":"ACCEPTED", + "channelId":SOURCE,"instanceId":instance,"status":"ACCEPTED", "deviceEpoch":state["device"]["epoch"], "graphRevision":state["graph"]["revision"], "schemaVersion":2,"sampleRate":48000,"latencySamples":1536, - "settings":copy.deepcopy(values),"observedNote":-1, + "settings":copy.deepcopy(values),"observedNote":observed, "guideDetectedKey":detected[0],"guideDetectedScale":detected[1], "guideConfidence":0.75 if detected[0]>=0 else 0.0, "effectiveKey":key,"effectiveScale":scale, @@ -104,7 +104,8 @@ def main(): # Native LCD must not fabricate manual/detected effective key # until the exact instance-scoped DSP snapshot arrives. assert page.locator("#mxHKeyReadout").inner_text()=="—" - readback(page,state,DEFAULT,0,0) + readback(page,state,DEFAULT,0,0,observed=57) + assert page.locator("#mxHTracking").inner_text()=="TRACKING A3" assert page.locator("#mxHKeyReadout").inner_text()=="C" assert page.locator("#mxHGuideStatus").inner_text()=="MANUAL" assert page.locator("#mxHSourcePicker").is_hidden() @@ -158,9 +159,14 @@ def main(): assert manual["settings"]["guideMode"]==0 assert manual["settings"]["guideChannelId"]=="0" assert page.locator("#mxHKeyReadout").inner_text()=="Am" + assert "AWAITING DSP ACK" in page.locator("#mxHStatus").inner_text() + assert "queued" in (page.locator( + '.mx-harm-note[data-key="7"]').get_attribute("class") or "") locked={**DEFAULT,"key":7} apply_edit(page,state,locked,7,0) assert page.locator("#mxHKeyReadout").inner_text()=="G" + assert "queued" not in (page.locator( + '.mx-harm-note[data-key="7"]').get_attribute("class") or "") assert page.locator("#mxHGuideStatus").inner_text()=="MANUAL" assert page.locator("#mxHSourcePicker").is_hidden() # G is a literal circle, subtly larger than unselected notes. @@ -296,7 +302,161 @@ def face_metrics(locator): readback(page,state,selected,9,1,detected=(9,1), request=old_poll) assert page.locator("#mxHKeyReadout").inner_text()=="G" + # Manual Key also observes audio-owner tracking on existing + # console ticks. Never infer singing from a UI button state. + # The preceding bypass-readback tests may have left ONE + # outstanding (throttled) snapshot request. Complete it as + # native JUCE would, rather than asserting a second request + # within the in-flight 2500ms readback fence. + latest_snap=last_action(page,"nativeHarmonySnapshot") + readback(page,state,values,7,0,request=latest_snap) + before=len(page.evaluate( + "()=>window.__actions.filter(x=>x.action==='nativeHarmonySnapshot')")) + # Native host produces ~12 console ticks per second, not one + # artificially selected 530ms tick after an arbitrary prior + # music readback. Exercise the real tick cadence for 2s. + # A single tick may legitimately be coalesced or queued. + after=before + for _ in range(24): + emit(page,"arMixConsoleSnapshot",state) + after=len(page.evaluate( + "()=>window.__actions.filter(x=>x.action==='nativeHarmonySnapshot')")) + if after>before:break + page.wait_for_timeout(85) + assert after>before,("no Manual Key native tracking poll", + page.locator("#mxHStatus").inner_text(), + page.locator("#mxHTracking").inner_text()) + monitor=last_action(page,"nativeHarmonySnapshot") + readback(page,state,changed,7,0,request=monitor,observed=60) + assert page.locator("#mxHTracking").inner_text()=="TRACKING C4" + # Reproduce the field screenshot: singer DSP continues playing + # while a native device/graph revision changes under an open + # Harmony editor. It MUST auto-rebind; no user reopen/wizard. + if width==1366: + accepted={**changed,"voices":5} + def push_graph(): + state["mixerSession"]["channels"]=copy.deepcopy( + state["graph"]["channels"]) + emit(page,"arMixConsoleSnapshot",state) + def bump(): + graph=state["graph"] + graph["revision"]=str(int(graph["revision"])+1) + def await_snap(instance,slot): + page.wait_for_function("""arg=>window.__actions.some(x=> + x.action==='nativeHarmonySnapshot' && + x.graphRevision===arg.revision && + x.instanceId===arg.instance && x.slot===arg.slot)""", + arg={"revision":state["graph"]["revision"], + "instance":instance,"slot":slot}) + result=last_action(page,"nativeHarmonySnapshot") + assert result["instanceId"]==instance,result + assert result["slot"]==slot,result + bump();push_graph() + await_snap(INSTANCE,0) + assert "REOPEN" not in page.locator("#mxHStatus").inner_text() + readback(page,state,accepted,7,0) + page.locator('[data-voices="4"]').click() + write=last_action(page,"nativeHarmonySetSettings") + assert write["graphRevision"]==state["graph"]["revision"] + accepted={**accepted,"voices":4} + apply_edit(page,state,accepted,7,0) + + # Recreated *same-channel* FX instance; stale ACKs cannot + # commit controls or paint a previous DSP identity. + successor="harmony-native-recreated" + bump() + state["graph"]["channels"][0]["inserts"][0]["id"]=successor + push_graph() + await_snap(successor,0) + emit(page,"arMixBuiltinHarmonyAck", + {"requestId":write["requestId"],"status":"APPLIED"}) + readback(page,state,DEFAULT,0,0,instance=successor) + assert page.locator("#mxHKeyReadout").inner_text()=="C" + page.locator('[data-key="9"]').click() + new_write=last_action(page,"nativeHarmonySetSettings") + assert new_write["instanceId"]==successor + assert new_write["channelId"]==SOURCE + assert new_write["settings"]["key"]==9 + emit(page,"arMixBuiltinHarmonyQueue", + {"requestId":new_write["requestId"],"status":"QUEUED"}) + emit(page,"arMixBuiltinHarmonyAck", + {"requestId":new_write["requestId"],"status":"APPLIED"}) + accepted={**DEFAULT,"key":9} + readback(page,state,accepted,9,0, + applied=new_write["requestId"],instance=successor) + assert page.locator("#mxHKeyReadout").inner_text()=="A" + + # Exact same insert identity follows an FX reorder, and + # subsequent edits use NEW accepted slot (not old index). + bump() + other={"id":"eq-accepted-other","catalogId": + "armix.builtin.channel-eq.v1","name":"EQ", + "bypassed":False} + harmony=copy.deepcopy(state["graph"]["channels"][0]["inserts"][0]) + state["graph"]["channels"][0]["inserts"]=[other,harmony] + push_graph() + await_snap(successor,1) + readback(page,state,accepted,9,0,instance=successor) + page.locator('[data-voices="5"]').click() + moving=last_action(page,"nativeHarmonySetSettings") + assert moving["slot"]==1 and moving["instanceId"]==successor + emit(page,"arMixBuiltinHarmonyQueue", + {"requestId":moving["requestId"],"status":"QUEUED"}) + emit(page,"arMixBuiltinHarmonyAck", + {"requestId":moving["requestId"],"status":"APPLIED"}) + accepted={**accepted,"voices":5} + readback(page,state,accepted,9,0, + applied=moving["requestId"],instance=successor) + + # A temporarily unpublished FX must NOT accept stale + # gestures. Hold the latest edit and apply only after + # a fresh accepted instance-scoped DSP readback. + bump();state["graph"]["channels"][0]["inserts"]=[other] + push_graph() + page.wait_for_function("""()=>document.querySelector( + '#mxHStatus').textContent.includes('RECONNECTING')""") + before=len(page.evaluate("""()=>window.__actions.filter( + x=>x.action==='nativeHarmonySetSettings')""")) + page.locator('[data-voices="3"]').click() + after=len(page.evaluate("""()=>window.__actions.filter( + x=>x.action==='nativeHarmonySetSettings')""")) + assert before==after,"stale FX received an edit" + bump();state["graph"]["channels"][0]["inserts"]=[other,harmony] + push_graph();await_snap(successor,1) + readback(page,state,accepted,9,0,instance=successor) + resumed=last_action(page,"nativeHarmonySetSettings") + assert resumed["slot"]==1 and resumed["instanceId"]==successor + assert resumed["settings"]["voices"]==3,resumed + emit(page,"arMixBuiltinHarmonyQueue", + {"requestId":resumed["requestId"],"status":"QUEUED"}) + emit(page,"arMixBuiltinHarmonyAck", + {"requestId":resumed["requestId"],"status":"APPLIED"}) + accepted={**accepted,"voices":3} + readback(page,state,accepted,9,0, + applied=resumed["requestId"],instance=successor) + assert page.locator('#mxHStatus').inner_text().startswith("NATIVE") + assert "FX 2" in page.locator("#mxHChannel").inner_text() assert not errors,errors + # The knob's lit pseudo-ring extends 11px past its face. + # Test real layout, not only CSS text, at every viewport. + dial_geometry=page.evaluate("""()=>[...document.querySelectorAll( + '#mxNativeHarmony .mx-harm-slider')].map(s=>{ + const f=s.querySelector('.mx-harm-dial-face').getBoundingClientRect(); + const c=s.querySelector('.mx-harm-dial-label').getBoundingClientRect(); + const o=s.querySelector('output').getBoundingClientRect(); + return {gap:c.top-f.bottom,labelBottom:c.bottom,outputTop:o.top, + faceTop:f.top,outputBottom:o.bottom}; + })""") + assert len(dial_geometry)==4,dial_geometry + for geom in dial_geometry: + assert geom["gap"]>=12.0,("caption overlaps halo",width,geom) + assert geom["outputTop"]>=geom["labelBottom"]-0.5,( + "caption/value collision",width,geom) + # Check spacing between rows as well as each knob. + # The next luminous ring reaches 11px above its face. + for upper,lower in zip(dial_geometry[:2],dial_geometry[2:]): + assert upper["outputBottom"]+2<=lower["faceTop"]-11,( + "next-row halo collides with upper value",width,upper,lower) rect=editor.bounding_box() assert rect and rect["width"]<=width+1,(width,rect) assert rect["x"]>=0 and rect["x"]+rect["width"]<=width+1,rect diff --git a/tests/harmony_cpu_profile_test.cpp b/tests/harmony_cpu_profile_test.cpp new file mode 100644 index 000000000..deadc8bcd --- /dev/null +++ b/tests/harmony_cpu_profile_test.cpp @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Non-gating, reproducible synthetic ENGINE-only callback CPU profile. +// Does not measure JUCE graph scheduling, ASIO driver or singer ear latency. +#include "../engine/builtin_fx/HarmonyDsp.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +using Clock=std::chrono::steady_clock; +constexpr int warmup=256; +constexpr int measured=512; +void profile(double rate,int block,int voices){ + auto engine=std::make_unique(); + if(!engine->prepare(rate)){std::cerr<<"Harmony prepare failed\n";std::exit(2);} + armix::harmony::Settings settings; + settings.voices=voices;settings.wet=0.55f;settings.correction=true; + if(!engine->setSettings(settings)){std::cerr<<"Harmony settings rejected\n";std::exit(2);} + std::array l{},r{}; + std::array elapsed{}; + double phase=0.; + const double step=6.283185307179586*220./rate; + for(int blockIndex=0;blockIndex(0.16*std::sin(phase)+ + 0.04*std::sin(2.*phase)); + l[static_cast(i)]=x; + r[static_cast(i)]=x; + phase+=step; + if(phase>=6.283185307179586)phase-=6.283185307179586; + } + const auto started=Clock::now(); + engine->process(l.data(),r.data(),block); + const auto finished=Clock::now(); + if(blockIndex>=warmup) + elapsed[static_cast(blockIndex-warmup)]= + std::chrono::duration(finished-started).count(); + for(int i=0;i(i)])|| + !std::isfinite(r[static_cast(i)])){ + std::cerr<<"Nonfinite Harmony PCM in profile\n";std::exit(2); + } + } + const double deadlineUs=1.e6*static_cast(block)/rate; + const auto misses=std::count_if(elapsed.begin(),elapsed.end(), + [deadlineUs](double elapsedUs){return elapsedUs>deadlineUs;}); + std::sort(elapsed.begin(),elapsed.end()); + std::cout< #include #include +#include using namespace armix::harmony; static void need(bool value,const char* why){if(!value){std::cerr<(); + need(pitched->prepare(48000.),"harmonic tracking prepare failed"); + constexpr int samples=24000; + std::vector hl(samples),hr(samples); + for(int i=0;i(i)/48000.; + hl[i]=hr[i]=static_cast( + .17*std::sin(6.283185307179586*110.*t)+ + .04*std::sin(6.283185307179586*220.*t)+ + .02*std::sin(6.283185307179586*330.*t)); + } + for(int i=0;iprocess(hl.data()+i,hr.data()+i,std::min(block,samples-i)); + need(pitched->voiced()&&pitched->observedMidiNote()==45, + "rolling pitch energy altered singer-like A2 classification"); + } + // P0: alternate ASIO block sizes must produce identical output PCM. + // Sliced analysis operates per sample, never depends on callback framing. + std::vector referenceL,referenceR; + for(int block:{64,127,256}){ + auto bounded=std::make_unique(); + need(bounded->prepare(48000.),"P0 bounded engine prepare failed"); + Settings rich;rich.voices=5;rich.wet=.7f;rich.correction=true; + need(bounded->setSettings(rich),"P0 quality setup failed"); + constexpr int frames=48000; + std::vector left(frames),right(frames); + for(int i=0;i(.18*std::sin(phase)+.06*std::sin(2*phase)); + right[i]=static_cast(.16*std::sin(phase)+.04*std::sin(3*phase)); + } + for(int i=0;iprocess(left.data()+i,right.data()+i,std::min(block,frames-i)); + need(bounded->peakPitchLagWork()>0&& + bounded->peakPitchLagWork()<=Engine::maxPitchLagsPerSlice, + "P0 pitch analysis overran per-sample lag budget"); + need(bounded->completedPitchAnalyses()>100&& + bounded->abandonedPitchAnalyses()==0, + "P0 slices failed to finish before the next analysis hop"); + need(bounded->voiced()&&bounded->observedMidiNote()==57, + "P0 bounded singer tone detection failed"); + if(referenceL.empty()){ + referenceL=left;referenceR=right; + }else for(int i=0;i(); + need(opposite->prepare(rate),"antiphase singer prepare failed"); + Settings wet;wet.voices=5;wet.wet=.85f;wet.correction=true; + need(opposite->setSettings(wet),"antiphase singer settings failed"); + const int count=static_cast(rate*.65); + std::vector left(static_cast(count)); + std::vector right(static_cast(count)); + for(int i=0;i( + .19*std::sin(6.283185307179586*220.*i/rate)+ + .05*std::sin(6.283185307179586*440.*i/rate)); + left[static_cast(i)]=x; + right[static_cast(i)]=-x; + } + for(int i=0;iprocess(left.data()+i,right.data()+i,std::min(257,count-i)); + need(opposite->voiced()&&opposite->observedMidiNote()==57, + "phase-opposed singer lost stable A3 pitch"); + double harmonized=0.; + for(int i=count/2;i(i)]; + const float r=right[static_cast(i)]; + need(std::isfinite(l)&&std::isfinite(r),"phase-safe PCM not finite"); + const double recovered=.5*(static_cast(l)+r); + harmonized+=recovered*recovered; + } + need(harmonized>0.005,"opposed stereo produced no harmonized audio"); + } + + // P1: the approved 48 kHz Color law must be preserved. At every other + // supported sample rate, equal physical time must imply equal one-pole + // attenuation; no pow/log calls can occur inside the audio callback. + for(double rate:{32000.,44100.,48000.,96000.,192000.}){ + RateStableColor table; + need(table.prepare(rate),"color filter prepare rejected supported rate"); + for(float color:{0.f,.08f,.25f,.5f,.75f,.91f,1.f}){ + const float actual=table.coefficient(color); + const float legacy=.07f+.77f*color; + need(std::isfinite(actual)&&actual>0.f&&actual<1.f, + "rate-adjusted Color coefficient invalid"); + if(rate==48000.) + need(std::abs(actual-legacy)<1.e-6f, + "48 kHz Color legacy response changed"); + // 125 microseconds is six samples at 48k. A 10ms measurement + // would hide every wrong Color coefficient: these filters all + // decay to ~zero well before 10ms. + const double oldDecay=std::pow(1.-static_cast(legacy),6.); + const double newDecay=std::pow(1.-static_cast(actual), + rate*.000125); + need(std::abs(oldDecay-newDecay)<1.e-4, + "Color attenuation changes at identical physical time"); + } + } + // P2: compare every supported key/scale/degree against the old per-voice + // pow(2,(targetMidi-observedMidi)/12) reference before accepting a LUT. + PreparedPitchIntervals intervals; + intervals.prepare(); + for(int key=0;key<12;++key) + for(int scale=0;scale<2;++scale) + for(int degree=-5;degree<=65;++degree){ + const int note=noteAt(degree,key,scale); + constexpr std::array offsets{{2,4,-2,6,-4}}; + for(float detune:{-.45f,-.17f,0.f,.23f,.49f}){ + const float midi=static_cast(note)+detune; + const float base=std::pow(2.f,(note-midi)/12.f); + for(const int offset:offsets){ + const int target=noteAt(degree+offset,key,scale); + need(std::abs(target-note)<=PreparedPitchIntervals::maximumSemitones, + "harmonic interval outside precomputed LUT"); + const float old=std::clamp(std::pow(2.f,(target-midi)/12.f), + 0.5f,2.f); + const float next=std::clamp(base*intervals.ratio(target-note), + 0.5f,2.f); + need(std::abs(old-next)<1.e-6f, + "precomputed harmony interval changed output pitch ratio"); + } + } + } + // P0: rescued phase-opposed ANALYSIS must never modify DRY output. + // The player, direct hardware monitor and graph still own stereo polarity. + for(double rate:{48000.,192000.}){ + auto dry=std::make_unique(); + need(dry->prepare(rate),"dry antiphase sample-rate prepare failed"); + Settings silentRack;silentRack.wet=0.f;silentRack.correction=false; + need(dry->setSettings(silentRack),"dry antiphase packet failed"); + const int latency=dry->latencySamples(); + const int count=latency*3+217; + std::vector original(static_cast(count)); + std::vector left(static_cast(count)); + std::vector right(static_cast(count)); + for(int i=0;i( + .21*std::sin(6.283185307179586*140.*i/rate)+ + .06*std::sin(6.283185307179586*280.*i/rate)); + original[static_cast(i)]=x; + left[static_cast(i)]=x; + right[static_cast(i)]=-x; + } + for(int i=0;iprocess(left.data()+i,right.data()+i,std::min(127,count-i)); + for(int i=0;i(i-latency)]; + need(std::abs(left[static_cast(i)]-expected)<1.e-6f&& + std::abs(right[static_cast(i)]+expected)<1.e-6f, + "P0 polarity rescue corrupted dry stereo or exact PDC"); + } + } + + // P0: all supported device rates and irregular ASIO-shaped periods must + // complete the full frozen correlation search WITHOUT dropped windows. + // This prevents a naive 32->8-lag reduction from silently truncating E2-C6. + for(const double rate:{32000.,44100.,48000.,96000.,192000.}){ + for(int block:{64,127,256}){ + auto sliced=std::make_unique(); + need(sliced->prepare(rate),"P0 multirate slice prepare failed"); + const int samples=static_cast(rate*.30); + std::vector l(static_cast(samples)); + std::vector r(static_cast(samples)); + for(int i=0;i( + .16*std::sin(6.283185307179586*220.*i/rate)+ + .04*std::sin(6.283185307179586*440.*i/rate)); + l[static_cast(i)]=value; + r[static_cast(i)]=value; + } + for(int i=0;iprocess(l.data()+i,r.data()+i,std::min(block,samples-i)); + need(sliced->peakPitchLagWork()<=8&& + sliced->peakPitchLagWork()>0, + "P0 worst-case lag slice exceeded eight"); + need(sliced->completedPitchAnalyses()>=20 && + sliced->abandonedPitchAnalyses()==0, + "P0 correlation hop overwritten on supported device"); + need(sliced->voiced() && sliced->observedMidiNote()==57, + "P0 sliced note lost in a supported rate/block configuration"); + } + } + // P0 defensive PCM: no allocation/crash/NaN from extreme finite, + // infinity or NaN, including during the silence->singer transition. + for(int voiceCount:{2,5}){ + auto overflow=std::make_unique(); + need(overflow->prepare(48000.),"P0 extreme PCM prepare failed"); + Settings full;full.wet=1.f;full.voices=voiceCount;full.correction=true; + need(overflow->setSettings(full),"P0 extreme PCM settings rejected"); + constexpr int frames=8192; + std::vector l(frames),r(frames); + for(int i=0;i::max(): + j==1?-std::numeric_limits::max(): + j==2?std::numeric_limits::infinity(): + j==3?-std::numeric_limits::infinity(): + j==4?std::numeric_limits::quiet_NaN(): + j==5?4.5f:j==6?-4.5f:0.f; + l[static_cast(i)]=raw; + r[static_cast(i)]=-raw; + } + for(int i=0;iprocess(l.data()+i,r.data()+i,std::min(64,frames-i)); + for(int i=0;i(i)]) && + std::isfinite(r[static_cast(i)]) && + std::abs(l[static_cast(i)])<=4.f && + std::abs(r[static_cast(i)])<=4.f, + "P0 extreme PCM yielded invalid or out-of-bounds output"); + } + // Direct analysis-only stereo fold must never produce NaN/inf even if a + // connected host provides a finite float close to FLT_MAX. + StereoPhaseSafeFold guard; + need(guard.prepare(48000.),"P0 stereo analysis fold prepare failed"); + for(int i=0;i<4096;++i){ + const float huge=std::numeric_limits::max(); + const float y=guard.process(huge,huge); + need(std::isfinite(y)&&std::abs(y)<=4.f, + "P0 guide PCM overflowed in stereo analysis fold"); + } + + // P1 natural ensemble: each singer owns a distinct stable arrival time, + // vibrato phase/rate and early-reflection history. No heap or random PCM + // work is introduced, and reflections must not bleed between singers. + for(double rate:{32000.,48000.,96000.,192000.}){ + auto ensemble=std::make_unique(); + need(ensemble->prepare(rate),"ensemble prepare rejected supported rate"); + need(ensemble->arrivalSamples(0)>0.f&& + ensemble->arrivalSamples(2)>ensemble->arrivalSamples(1), + "independent singer onset offsets not prepared"); + need(ensemble->reflectionSamples(4)( + EnsembleSpace::reflectionCapacity), + "highest-rate reflection would read outside fixed history"); + const int delay=ensemble->reflectionSamples(0); + for(int i=0;i<=delay+5;++i){ + for(int v=0;vexchangeReflection(v,input); + const float expected=(i==delay&&v==0)?1.f:0.f; + need(std::abs(out-expected)<1.e-6f, + "ensemble reflection delay or channel isolation broken"); + } + ensemble->advance(true); + } + float minRatio=2.f,maxRatio=0.f; + const int sustain=static_cast(rate*.60); + for(int i=0;ipitchRatio(0); + const float b=ensemble->pitchRatio(1); + need(std::isfinite(a)&&std::isfinite(b)&&a>.996f&&a<1.004f&& + b>.996f&&b<1.004f, + "ensemble vibrato/detune exceeds transparent musical bounds"); + minRatio=std::min(minRatio,a);maxRatio=std::max(maxRatio,a); + ensemble->advance(true); + } + need(maxRatio-minRatio>0.0004f, + "ensemble vocalist lacks independent sustain modulation"); + ensemble->advance(false); + } + // At Space=0 mono input stays mono with no reflected crossfeed. At + // Space=1, three distinct backing singers must form true stereo PCM, + // while the singer's dry path keeps its declared delay and center. + for(double rate:{48000.,96000.}){ + constexpr int samples=48000; + std::vector zeroL(samples),zeroR(samples); + std::vector wideL(samples),wideR(samples); + auto center=std::make_unique(); + auto wide=std::make_unique(); + need(center->prepare(rate)&&wide->prepare(rate), + "ensemble space engine prepare failed"); + Settings narrow; narrow.voices=3;narrow.wet=.6f;narrow.space=0.f; + Settings spread=narrow;spread.space=1.f; + need(center->setSettings(narrow)&&wide->setSettings(spread), + "ensemble controls not accepted"); + for(int i=0;i(.16*std::sin( + 6.283185307179586*220.*i/rate)+.05*std::sin( + 6.283185307179586*440.*i/rate)); + zeroL[static_cast(i)]=zeroR[static_cast(i)]=x; + wideL[static_cast(i)]=wideR[static_cast(i)]=x; + } + for(int i=0;iprocess(zeroL.data()+i,zeroR.data()+i,len); + wide->process(wideL.data()+i,wideR.data()+i,len); + } + double centerDifference=0.,spreadDifference=0.,wideEnergy=0.,foldEnergy=0.; + for(int i=samples/2;i(i)]- + zeroR[static_cast(i)]); + spreadDifference+=std::abs(wideL[static_cast(i)]- + wideR[static_cast(i)]); + const double x=.5*(wideL[static_cast(i)]+ + wideR[static_cast(i)]); + wideEnergy+=wideL[static_cast(i)]* + wideL[static_cast(i)]+ + wideR[static_cast(i)]* + wideR[static_cast(i)]; + foldEnergy+=x*x; + } + need(centerDifference<.0001, + "Space zero leaked phase-widened stereo or unbalanced rooms"); + need(spreadDifference>0.5&&wideEnergy>1.&&foldEnergy>.1, + "ensemble room has no stereo depth or loses mono compatibility"); + } + + // Field feedback: full Wet, 4 voices, Space 60% still sounded thin. + // Presence curve must lift BACKING only, with sqrt voice count normalization + // and exact Wet0 silence; all scalar math is prepared on control thread. + for(int voices=2;voices<=Engine::maxVoices;++voices){ + float previous=0.f; + for(float wet:{0.f,.15f,.45f,.60f,.85f,1.f}){ + const float modern=Engine::normalizedBackingWetAmplitude(wet,voices); + const float legacy=Engine::normalizedBackingWetAmplitude(wet,voices,false); + need(std::isfinite(modern)&&modern>=previous&& + modern<=1.151f/std::sqrt(static_cast(voices)), + "Harmony wet presence nonmonotone or unbounded"); + if(wet==0.f)need(modern==0.f&&legacy==0.f,"Wet0 leaked harmony"); + if(wet==1.f)need(modern>legacy*1.73f&&modernprior*1.73f,"prepared Harmony pan lost presence or normalization"); + } + } + // P0 lead preservation: backing Wet must never change the lead amplitude. + // The *only* intentional lead replacement is Pitch Tune's explicit blend. + for(float dry:{-.9f,-.3f,0.f,.27f,.9f}){ + need(Engine::leadPath(dry,.5f,0.f)==dry, + "wet-independent original lead unexpectedly ducked"); + need(std::abs(Engine::leadPath(dry,.5f,.5f)-(.5f*dry+.25f))<1.e-6f, + "explicit Pitch Tune blend changed"); + } + + // P1 diffuse shared BACKING room: no immediate output, fixed delay at + // all accepted rates, decaying rather than metallic or unstable feedback. + for(double rate:{32000.,44100.,48000.,96000.,192000.}){ + auto room=std::make_unique(); + need(room->prepare(rate),"backing room rejected supported sample rate"); + for(std::size_t v=0;vdelaySamples(v)>0&& + room->delaySamples(v)feedbackGain(v)>0.f&&room->feedbackGain(v)<1.f, + "backing FDN missing bounded delays or stable feedback"); + const int total=static_cast(rate*1.8); + double first=0.,late=0.,last=0.,mono=0.; + for(int i=0;iprocess(i==0?1.f:0.f,i==0?1.f:0.f); + need(std::isfinite(o.left)&&std::isfinite(o.right)&& + std::abs(o.left)<2.f&&std::abs(o.right)<2.f, + "backing room blew up or returned non-finite PCM"); + const double energy=o.left*o.left+o.right*o.right; + if(i(rate*.02)) + need(energy<1.e-18,"diffuse field leaked before its first tap"); + if(i(rate*.11))first+=energy; + else if(i(rate*.35))late+=energy; + if(i>static_cast(rate*1.5))last+=energy; + mono+=(.5*(o.left+o.right))*(.5*(o.left+o.right)); + } + need(first>1.e-5&&late>1.e-9&&last1.e-5,"room anti-phase cancelled in mono"); + need(room->prepare(rate),"device restart failed to reset room"); + for(int i=0;i<512;++i){ + const auto o=room->process(0.f,0.f); + need(o.left==0.f&&o.right==0.f, + "room restart retained stale output from previous session"); + } + } + // P1 stereo depth changes must remain audible in mono, but lead should + // stay transparent regardless of Space and Wet when no Pitch Tune. + for(float depth:{0.f,.5f,1.f}){ + EnsembleSpace singer; + need(singer.prepare(48000.),"backing HF shader prepare failed"); + double inputEnergy=0.,outputEnergy=0.; + for(int i=0;i<3000;++i){ + const float pulse=(i%7)<3?1.f:-1.f; + const float shaded=singer.shadeBacking(0,pulse,depth); + inputEnergy+=pulse*pulse; + outputEnergy+=shaded*shaded; + singer.advance(true); + } + need(outputEnergy>0.&&outputEnergy<=inputEnergy*1.01, + "backing distance contour amplified or removed singer"); + if(depth==0.f)need(std::abs(outputEnergy-inputEnergy)<1.e-6, + "Space zero failed to preserve direct backing"); + } + // Declared 48k wet/dry sample PDC must remain 2*16ms even after slicing. + need(e->latencySamples()==1536,"P0 changed live session PDC without proof"); std::cout<<"Native Harmony DSP: deterministic PCM, 2-5 voices, octave/scale, stereo, silence and 32-192k PASS\n"; } diff --git a/tests/harmony_music_bank_test.cpp b/tests/harmony_music_bank_test.cpp index a8ac2ebdb..ba4adbc72 100644 --- a/tests/harmony_music_bank_test.cpp +++ b/tests/harmony_music_bank_test.cpp @@ -73,17 +73,20 @@ int main(){ assert(live->effectiveKeyCode()==14); // music absent: manual D minor juce::MemoryBlock saved; live->getStateInformation(saved); - ChannelHarmonyProcessor restored; + // Realtime processor owns fixed DSP and reflection rings; production + // allocates FX instances on heap. Two stack-local processors overflow + // the default Windows 1 MiB CTest stack after stage-reflection growth. + auto restored=std::make_unique(); // Production NativeMixerSessionFxRestore prepares first, then restores. // Source must be armed at the actual DSP input, not just in accepted UI. - restored.prepareToPlay(48000.,128); - assert(restored.isReady()); - assert(restored.restoreUnpublishedState(saved.getData(), + restored->prepareToPlay(48000.,128); + assert(restored->isReady()); + assert(restored->restoreUnpublishedState(saved.getData(), static_cast(saved.getSize()))); - assert(restored.acceptedSettings().guideMode==1); - assert(restored.acceptedSettings().guideChannelId==music.value); - assert(restored.musicSourceChannel()==music.value); - assert(restored.observedMusicKey()==-1); // no stale old-device estimate + assert(restored->acceptedSettings().guideMode==1); + assert(restored->acceptedSettings().guideChannelId==music.value); + assert(restored->musicSourceChannel()==music.value); + assert(restored->observedMusicKey()==-1); // no stale old-device estimate // Version-1 state still loads as Manual Key with no source binding. juce::MemoryBlock old; { @@ -97,14 +100,14 @@ int main(){ writer.writeFloat(.5f);writer.writeFloat(0.f); } assert(old.getSize()>0); - ChannelHarmonyProcessor migrated; - migrated.prepareToPlay(48000.,128); - assert(migrated.isReady()); - assert(migrated.restoreUnpublishedState(old.getData(), + auto migrated=std::make_unique(); + migrated->prepareToPlay(48000.,128); + assert(migrated->isReady()); + assert(migrated->restoreUnpublishedState(old.getData(), static_cast(old.getSize()))); - assert(migrated.acceptedSettings().guideMode==0); - assert(migrated.acceptedSettings().guideChannelId==0); - assert(migrated.musicSourceChannel()==0); + assert(migrated->acceptedSettings().guideMode==0); + assert(migrated->acceptedSettings().guideChannelId==0); + assert(migrated->musicSourceChannel()==0); // Music-source retarget must not bleed the old channel's guide into the // new source. After switching to Manual the other-channel PCM must never // change the selected manual key. A third row owns no FX and no audible WIRE. @@ -146,7 +149,11 @@ int main(){ std::sin(6.28318530718*261.625565*t)+ std::sin(6.28318530718*329.627557*t))); } + // Real anti-phase other-channel PCM remains unmodified by the guide. + for(int j=0;j<128;++j)r[j]=-l[j]; + const float originalL=l[17],originalR=r[17]; bank2(musicB,l,r,128); + assert(l[17]==originalL&&r[17]==originalR); } assert(live->observedMusicKey()==9&&live->observedMusicScale()==1); assert(live->effectiveKeyCode()==21); // detected A minor diff --git a/tests/harmony_music_key_test.cpp b/tests/harmony_music_key_test.cpp index 03b65317e..6196e9fb5 100644 --- a/tests/harmony_music_key_test.cpp +++ b/tests/harmony_music_key_test.cpp @@ -46,6 +46,30 @@ int main(){ need(estimate.valid()&&estimate.key==9&&estimate.scale==1, "A-minor music track did not identify natural minor"); am.reset();need(!am.estimate().valid(),"channel retarget retained stale key"); + // Extreme polarity-inverted stereo music still contains C-major PCM. + MusicKeyDetector opposed; + need(opposed.prepare(rate),"opposed music prepare failed"); + const int frames=static_cast(rate*3.5); + std::vector invL(static_cast(frames)); + std::vector invR(static_cast(frames)); + for(int i=0;i(i)/rate; + const float v=static_cast(.09*( + std::sin(6.283185307179586*261.625565*t)+ + std::sin(6.283185307179586*329.627557*t)+ + std::sin(6.283185307179586*391.995436*t))); + invL[static_cast(i)]=v; + invR[static_cast(i)]=-v; + } + for(int i=0;i +#include +#include +#include +#include +#include +using armix::harmony::PitchSynchronousVoice; +using Shifter=PitchSynchronousVoice<16384,6>; +static void need(bool yes,const char* reason){ + if(!yes){std::cerr< render(double rate,double f0,double ratio,int block){ + auto p=Shifter{}; + std::array ring{}; + const int n=static_cast(rate*.65); + std::vector out(static_cast(n)); + for(int pos=0;pos(t)&16383]=static_cast( + .19*std::sin(phase)+.052*std::sin(2*phase+.31)+ + .029*std::sin(3*phase+.17)); + const auto sample=p.process(0,t,static_cast(t)&16383,ring, + static_cast(rate/f0),static_cast(ratio), + static_cast(rate*.032)); + out[static_cast(t)]=sample.sample; + need(std::isfinite(sample.sample)&&std::abs(sample.sample)<2.f, + "synchronous output nonfinite/unbounded"); + } + return out; +} +static double measuredHz(const std::vector& signal,double rate,double wanted){ + const int begin=static_cast(rate*.27); + const int length=static_cast(rate*.23); + const int low=static_cast(rate/(wanted*1.05)); + const int high=static_cast(rate/(wanted*.95)); + double best=-100.;int lag=0; + for(int d=low;d<=high;++d){ + double numerator=0,energy=0; + for(int t=begin;t(t)]* + signal[static_cast(t-d)]; + energy+=signal[static_cast(t)]* + signal[static_cast(t)]; + } + if(numerator/(energy+1.e-20)>best){ + best=numerator/(energy+1.e-20);lag=d; + } + } + need(best>.6&&lag>0,"synchronous source lost periodicity"); + return rate/lag; +} +int main(){ + // Actual source-period pitch shifting, not just a sine-wave finite test. + // Three original harmonics approximate one voiced source; FFT/formant and + // actual adult singer evidence are still necessary before quality claims. + for(const auto& v:std::array,5>{{ + {{48000.,220.,1.5}},{{48000.,220.,.667}}, + {{48000.,82.40689,1.2}},{{96000.,440.,.75}}, + {{192000.,880.,1.4}} + }}){ + const auto a=render(v[0],v[1],v[2],64); + const auto b=render(v[0],v[1],v[2],257); + need(a.size()==b.size(),"different source length"); + for(std::size_t i=0;i silence{}; + need(!bad.process(0,0,0,silence,0.f,1.f,1536.f).ready, + "invalid F0 accepted"); + need(!bad.process(0,100,100,silence,3200.f,1.f,1536.f).ready, + "insufficient host latency accepted"); + std::cout<<"bounded source windows and block parity passed\n"; +} diff --git a/tests/harmony_stage_spatial_test.cpp b/tests/harmony_stage_spatial_test.cpp new file mode 100644 index 000000000..a1935f73e --- /dev/null +++ b/tests/harmony_stage_spatial_test.cpp @@ -0,0 +1,169 @@ +// SPDX-License-Identifier: AGPL-3.0-only +#include "../engine/builtin_fx/HarmonyStageSpatial.h" +#include "../engine/builtin_fx/HarmonyRoomFocus.h" +#include "../engine/builtin_fx/HarmonyEnsembleSpace.h" +#include "../engine/builtin_fx/HarmonyDsp.h" +#include +#include +#include +#include +#include +#include +using namespace armix::harmony; +static void need(bool b,const char* reason) { + if(!b){std::cerr<(); + auto stage=std::make_unique(); + need(e->prepare(rate)&&stage->prepare(rate),"stage prepare failed"); + need(stage->farEarDelaySamples()>=2,"missing far-ear cue"); + need(e->secondReflectionSamples(singer)>e->reflectionSamples(singer), + "secondary reflection not after first"); + const float pan=.72f*space*(2.f*singer/(voices-1)-1.f); + const float angle=(pan+1.f)*3.14159265358979323846f*.25f; + const float ml=std::cos(angle),mr=std::sin(angle); + const auto g=BackingStageSpatial::gains(space); + SceneEnergy x{}; + for(int i=0;i(rate*.16);++i){ + const float input=i==0?.7f:0.f; + const float a=e->exchangeReflection(singer,input); + const float b=e->secondReflection(singer); + const auto p=stage->place(singer,input,a,b,ml,mr,space); + const double dl=p.directL*g.direct,dr=p.directR*g.direct; + const double rl=p.earlyL*g.early,rr=p.earlyR*g.early; + x.direct+=dl*dl+dr*dr;x.early+=rl*rl+rr*rr; + x.mono+=.25*(dl+dr+rl+rr)*(dl+dr+rl+rr); + x.stereo+=(dl+rl-dr-rr)*(dl+rl-dr-rr); + need(std::isfinite(dl)&&std::isfinite(dr)&& + std::isfinite(rl)&&std::isfinite(rr),"nonfinite stage PCM"); + e->advance(true);stage->advance(); + } + need(x.direct>.001&&x.mono>.0001,"backing collapsed in mono"); + if(space==0.f)need(x.early==0.,"Space 0 leaked reflection"); + else need(x.early>1.e-5,"room reflection inaudible"); + return x; +} +static std::vector render(int block,float space,bool wet){ + auto e=std::make_unique(); + need(e->prepare(48000.),"native engine prepare"); + Settings s;s.voices=3;s.wet=wet?.65f:0.f;s.space=space; + need(e->setSettings(s),"engine settings invalid"); + constexpr int n=24000; + std::vector l(n),r(n); + for(int i=0;i(.18*std::sin(p)+.045*std::sin(2*p)); + r[i]=static_cast(.12*std::sin(p)+.07*std::sin(3*p)); + } + for(int i=0;iprocess(l.data()+i,r.data()+i,std::min(block,n-i)); + for(float value:l)need(std::isfinite(value),"render nonfinite"); + need(e->latencySamples()==1536,"native PDC changed"); + return l; +} + +// Separate synthetic frequency-band check for the modest head-shadow cue. +// Compare far/near energy at 350 Hz and 8 kHz while keeping pan and Space +// constant. Not a binaural localization or human HRTF listening test. +static double farNearRatio(double rate,double frequency,float space){ + BackingStageSpatial stage; + need(stage.prepare(rate),"head shadow rate prepare failed"); + const int frames=static_cast(rate*.09); + double nearEnergy=0.,farEnergy=0.; + for(int t=0;t(.4*std::sin( + 6.283185307179586*frequency*t/rate)); + const auto p=stage.place(0,input,0.f,0.f,.28f,.96f,space); + if(t>static_cast(rate*.04)){ + // Compare against direct ear gains to remove normal panning. + nearEnergy+=(p.directR/.96f)*(p.directR/.96f); + farEnergy+=(p.directL/.28f)*(p.directL/.28f); + } + stage.advance(); + } + need(nearEnergy>1.e-5&&farEnergy>0.,"head shadow null output"); + return farEnergy/nearEnergy; +} +// Synthetic room clarity and phrase-envelope proof, not real singer listening. +static double focusEnergy(double rate,double hz){ + BackingRoomFocus f;need(f.prepare(rate),"room focus rate"); + const int n=static_cast(rate*.16);double sum=0.; + for(int i=0;i(.3*std::sin( + 6.283185307179586*hz*i/rate)); + const auto s=f.filterSend(x,x); + need(std::isfinite(s.left)&&std::isfinite(s.right),"room finite"); + if(i>n/2)sum+=s.left*s.left; + } + return sum/(n/2); +} +static void focusTest(double rate){ + BackingRoomFocus f;need(f.prepare(rate),"room init"); + need(focusEnergy(rate,75.)(rate*.35);++i){ + const float g=f.returnGain(.40f,.40f,1.f); + need(g>.78f&&g<=1.f&&std::abs(g-last)<.003f, + "room level jump or overduck");last=g; + } + need(last<.85f,"room activity follower inactive"); + need(f.returnGain(.4f,.4f,0.f)==1.f,"Space0 room not neutral"); + for(int i=0;i(rate*1.5);++i) + last=f.returnGain(0.f,0.f,1.f); + need(last>.995f&&last<=1.f,"room tail did not bloom in gaps"); + need(f.prepare(rate)&&f.leadEnvelope()==0.f,"room restart stale envelope"); + need(f.filterSend(0.f,0.f).left==0.f&& + f.returnGain(0.f,0.f,1.f)==1.f,"room restart stale sample"); +} +int main(){ + for(double rate:{32000.,44100.,48000.,96000.,192000.}){ + focusTest(rate); + double previous=-1.; + for(float space:{.25f,.6f,1.f}){ + const auto left=scene(rate,space,0,3); + const auto relative=left.early/left.direct; + need(relative>previous,"Space no longer lowers direct/reflected energy ratio"); + previous=relative; + need(left.stereo>1.e-5,"left image collapsed to mono"); + const auto center=scene(rate,space,1,3); + const auto right=scene(rate,space,2,3); + need(center.mono>.0001&&right.mono>.0001,"other voices lost in mono"); + } + scene(rate,0.f,0,3); + scene(rate,.8f,4,5); + } + // A center harmony voice competes directly with the lead phantom center. + // Center-only direct recession is modest; its room send remains intact. + for(double rate:{48000.,96000.,192000.}){ + BackingStageSpatial x; + need(x.prepare(rate),"center recession prepare failed"); + auto zero=x.place(1,.5f,0.f,0.f,.5f,.5f,0.f); + x.advance(); + auto deep=x.place(1,.5f,0.f,0.f,.5f,.5f,1.f); + need(std::abs(zero.directL-.25f)<1.e-6f&& + std::abs(zero.roomL-.25f)<1.e-6f, + "Space0 center direct changed"); + need(deep.directLdeep.roomL*.80f, + "center backing direct did not move behind lead"); + need(std::abs(deep.roomL-.25f)<1.e-6f, + "center stage lost shared-room input"); + const double low=farNearRatio(rate,350.,1.f); + const double high=farNearRatio(rate,8000.,1.f); + need(high.08,"far ear over-attenuated or disappeared"); + need(std::abs(farNearRatio(rate,350.,0.f)-1.)<.015, + "Space zero changed neutral stereo signal"); + need(std::abs(farNearRatio(rate,8000.,0.f)-1.)<.015, + "Space zero changed high-frequency signal"); + } + for(float space:{0.f,.6f,1.f}) + need(render(64,space,true)==render(257,space,true), + "callback partition affected spatial audio"); + need(render(64,0.f,false)==render(257,1.f,false), + "Space changed true lead at Wet0"); + std::cout<<"Harmony stage depth, reflections, mono, PDC and parity PASS\n"; +} diff --git a/tests/native_harmony_assembly_test.py b/tests/native_harmony_assembly_test.py index e262f1103..1a00c7e2f 100644 --- a/tests/native_harmony_assembly_test.py +++ b/tests/native_harmony_assembly_test.py @@ -90,6 +90,11 @@ def test_quiet_manual_music_mode_and_selector(self): self.assertIn('edit({guideMode:0,guideChannelId:"0"})',self.ui) self.assertIn('const status=!auto?"MANUAL":!inGraph?"SAFE":detected?"AUTO":"LISTEN";',self.ui) self.assertIn("showText(el(\"mxHKeyReadout\"),",self.ui) + self.assertIn('id="mxHTracking" aria-live="off"',self.ui) + self.assertIn('showText(el("mxHTracking"),tracking)',self.ui) + self.assertIn('if(now-lastGuideRead>500)',self.ui) + self.assertIn('b.classList.toggle("queued"',self.ui) + self.assertIn('.mx-harm-note.queued:not(.active)',self.css) self.assertIn('role="status" aria-live="polite"',self.ui) self.assertIn(".mx-harm-mode button.active",self.css) self.assertIn(".mx-harm-source-picker[hidden]{display:none}",self.css) @@ -154,7 +159,12 @@ def test_reference_inspired_wide_harmony_and_exact_shared_fx_chrome(self): def test_no_fabricated_native_pcm(self): self.assertIn("BROWSER PREVIEW · VISUAL ONLY · NO AUDIO",self.ui) self.assertIn("if(!native())",self.ui) - self.assertIn("if(native()&&n?.admitted",self.ui) + # Accepted ChannelId/instance binding is re-established on the + # current graph, not permanently pinned to the editor's open tick. + self.assertIn("if(!current||!native()||!n?.admitted)return null;",self.ui) + self.assertIn("if(n?.admitted&&insert?.catalogId===ID&&insert.id)",self.ui) + self.assertIn("function reconcileBinding(){",self.ui) + self.assertNotIn("REOPEN ACCEPTED FX",self.ui) self.assertIn("Native Harmony host not ready · audio unchanged",self.html) with self.assertRaises(ValueError): apply_native_peq(self.html) diff --git a/tools/harmony_offline_ab.cpp b/tools/harmony_offline_ab.cpp new file mode 100644 index 000000000..de612a792 --- /dev/null +++ b/tools/harmony_offline_ab.cpp @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Offline native Harmony A/B: genuine adult vocal WAVs, no live callback IO. +// Synthesized self-test is only a build and integrity gate, not an ear-test. +#include "../engine/builtin_fx/HarmonyDsp.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using armix::harmony::Engine; +using armix::harmony::Settings; +namespace fs=std::filesystem; +struct Wave {unsigned rate=0;std::vector left,right;}; +static std::uint16_t u16(const unsigned char* p){return std::uint16_t(p[0])|(std::uint16_t(p[1])<<8);} +static std::uint32_t u32(const unsigned char* p){return std::uint32_t(p[0])|(std::uint32_t(p[1])<<8)|(std::uint32_t(p[2])<<16)|(std::uint32_t(p[3])<<24);} +static void put16(std::ostream& out,std::uint16_t v){out.put(char(v&255));out.put(char((v>>8)&255));} +static void put32(std::ostream& out,std::uint32_t v){for(int k=0;k<4;++k)out.put(char((v>>(8*k))&255));} +static Wave readWave(const fs::path& file){ + if(!fs::exists(file)||fs::file_size(file)>220000000)throw std::runtime_error("WAV missing/too large"); + std::ifstream stream(file,std::ios::binary); + if(!stream)throw std::runtime_error("WAV open failed"); + const std::vector bytes((std::istreambuf_iterator(stream)),{}); + if(bytes.size()<44||std::memcmp(bytes.data(),"RIFF",4)!=0|| + std::memcmp(bytes.data()+8,"WAVE",4)!=0) + throw std::runtime_error("expected RIFF/WAVE"); + std::uint16_t fmt=0,ch=0,bits=0,align=0;std::uint32_t hz=0; + std::size_t offset=12,dataStart=0,dataBytes=0; + while(offset+8<=bytes.size()){ + const auto size=static_cast(u32(bytes.data()+offset+4)); + const auto start=offset+8; + if(size>bytes.size()-start)throw std::runtime_error("truncated WAV chunk"); + if(std::memcmp(bytes.data()+offset,"fmt ",4)==0){ + if(size<16)throw std::runtime_error("WAV fmt too short"); + const auto* p=bytes.data()+start; + fmt=u16(p);ch=u16(p+2);hz=u32(p+4);align=u16(p+12);bits=u16(p+14); + }else if(std::memcmp(bytes.data()+offset,"data",4)==0){ + dataStart=start;dataBytes=size; + } + offset=start+size+(size&1); + } + if(hz<32000||hz>192000||(ch!=1&&ch!=2)|| + !((fmt==1&&(bits==16||bits==24))||(fmt==3&&bits==32))|| + align!=ch*(bits/8)||!dataBytes||dataBytes%align) + throw std::runtime_error("use 32-192kHz mono/stereo PCM16/24 or IEEE float32 WAV"); + const auto frames=dataBytes/align; + if(frames>static_cast(hz)*60)throw std::runtime_error("use <=60s original adult-vocal excerpt"); + Wave out;out.rate=hz;out.left.resize(frames);out.right.resize(frames); + const auto* data=bytes.data()+dataStart; + for(std::size_t i=0;ifloat{ + const auto* p=data+i*align+static_cast(c)*(bits/8); + if(fmt==3){ + const auto raw=u32(p);float x=0;std::memcpy(&x,&raw,sizeof(x)); + return std::isfinite(x)?std::clamp(x,-4.f,4.f):0.f; + } + if(bits==16)return static_cast(std::int16_t(u16(p)))/32768.f; + std::int32_t value=std::int32_t(p[0])|(std::int32_t(p[1])<<8)| + (std::int32_t(p[2])<<16); + if(value&0x800000)value|=~0xffffff; + return static_cast(value)/8388608.f; + }; + out.left[i]=sample(0); + out.right[i]=sample(ch==2?1:0); + } + return out; +} +static void writeWave(const fs::path& path,const Wave& wave){ + if(wave.left.size()!=wave.right.size()||wave.left.size()>0x1fffffff) + throw std::runtime_error("WAV output too large"); + std::ofstream out(path,std::ios::binary|std::ios::trunc); + if(!out)throw std::runtime_error("WAV output open failed"); + const std::uint32_t n=static_cast(wave.left.size()*8); + out.write("RIFF",4);put32(out,n+36);out.write("WAVEfmt ",8); + put32(out,16);put16(out,3);put16(out,2);put32(out,wave.rate); + put32(out,wave.rate*8);put16(out,8);put16(out,32); + out.write("data",4);put32(out,n); + for(std::size_t i=0;i(); + if(!e->prepare(input.rate)||!e->setSettings(settings)) + throw std::runtime_error("native Harmony configuration invalid"); + e->setPitchSynchronousCandidateForOfflineReference(synchronous); + e->setStageDepthForOfflineReference(enhancedDepth); + e->setRoomFocusForOfflineReference(focus); + if(!e->setBackingPresenceForOfflineReference(presence)) + throw std::runtime_error("Harmony presence offline reference rejected"); + Wave out=input; + for(std::size_t pos=0;pos(block)) + e->process(out.left.data()+pos,out.right.data()+pos, + static_cast(std::min(static_cast(block),out.left.size()-pos))); + return out; +} +static double rms(const Wave& x){ + if(x.left.empty())return 0.;double acc=0.; + for(std::size_t i=0;i=3.999f||std::abs(modern.right[i])>=3.999f) + throw std::runtime_error("mixed output clipped: lower recording level for valid stem subtraction"); + const Wave backing=subtract(modern,dry); + const Wave previousDepth=process(input,settings,true,127,false); + const Wave previousRoomFocus=process(input,settings,true,127,true,false); + const Wave previousPresence=process(input,settings,true,127,true,true,false); + writeWave(folder/"01_lead_reference.wav",dry); + writeWave(folder/"02_legacy_grain_mix.wav",old); + writeWave(folder/"03_new_space_060.wav",modern); + writeWave(folder/"04_new_backing_only.wav",backing); + writeWave(folder/"05_new_mono_fold.wav",fold(modern)); + writeWave(folder/"08_previous_depth_space_060.wav",previousDepth); + writeWave(folder/"09_previous_backing_only.wav",subtract(previousDepth,dry)); + writeWave(folder/"10_room_focus_off_space_060.wav",previousRoomFocus); + writeWave(folder/"11_room_focus_off_backing_only.wav",subtract(previousRoomFocus,dry)); + writeWave(folder/"12_presence_old_space_060.wav",previousPresence); + writeWave(folder/"13_presence_old_backing_only.wav",subtract(previousPresence,dry)); + settings.space=0.f;writeWave(folder/"06_new_space_000.wav",process(input,settings,true)); + settings.space=1.f;writeWave(folder/"07_new_space_100.wav",process(input,settings,true)); + generateReport(folder,input,dry,old,modern,backing,previousDepth,previousPresence); +} +int main(int argc,char** argv){ + try{ + if(argc==2&&std::string(argv[1])=="--self-test"){ + Wave v;v.rate=48000;v.left.resize(18000);v.right.resize(18000); + for(int i=0;i<18000;++i){ + const double ph=6.283185307179586*220.*i/48000.; + v.left[i]=v.right[i]=static_cast( + .13*std::sin(ph)+.06*std::sin(2*ph)+.03*std::sin(3*ph)); + } + Settings s;s.wet=.45f; + auto before=process(v,s,false); + auto after=process(v,s,true); + if(!(rms(before)>0.01&&rms(after)>0.01))return 2; + s.wet=1.f;s.space=.6f;s.voices=4; + const auto baseline=process(v,s,true,127,true,true,false); + const auto boosted=process(v,s,true,127,true,true,true); + auto drySettings=s;drySettings.wet=0.f; + const auto dry=process(v,drySettings,true); + const double beforeRms=rms(subtract(baseline,dry)); + const double afterRms=rms(subtract(boosted,dry)); + if(!(beforeRms>.0001&&afterRms>beforeRms*1.50&& + afterRms