Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Spatial voice has Distance, Room, Back in front and Instruments width
My-Name-Is-Jefff2330a0a2d2b
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex d5115269..e1bfa702 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -312,9 +312,11 @@ Shared:thermal state Serious up Karaoke is held and lets the model go,unless Ignore heat warnings is on, and runs again at Fair. Spatialvoice holds the separated vocals in front as the head turns: HeadGestures' motion gives the yaw off a- front that follows the head over 20 s, and the render thread pans the vocals' middle at equal power,+ front that follows the head (Back in front, 5 s unless set), and the render thread pans the vocals' middle at equal power,narrowed, with the far ear up to 0.65 ms late and low-passed; it stands down for iOS's own spatial- audio. The Spatial voice page's preview (SGSpatialPreview.m) turns a disc of dots under the listener by+ audio. Its Sound section sets Distance (the voice down by the square root of the distance), Room+ (a wet-only AUReverb2 fed the voice's middle, emptied by a seek) and Instruments width (the rest's side+ scaled); off, all three leave the song exactly as it was. The Spatial voice page's preview (SGSpatialPreview.m) turns a disc of dots under the listener bythe same front (SGSpatialVoiceAngle, SGSingEngine.h), on Core Animation alone and only while the pageshows. The mic is on the redesign's lyrics (Redesigned/Lyrics/SGRSingButton.m). Tested on the Macagainst harness/sing/ (the lead, its cap, a slow decoder, a track boundary, spatial voice and its front without the model: `build/sing spatial`; thediff --git a/harness/sing/build-sim.sh b/harness/sing/build-sim.shindex b0d5c5bf..c3820ace 100755--- a/harness/sing/build-sim.sh+++ b/harness/sing/build-sim.sh@@ -17,7 +17,7 @@ SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)xcrun -sdk iphonesimulator clang -target arm64-apple-ios18.0-simulator -fobjc-arc -g -O1 -isysroot "$SDK" -Wno-deprecated-declarations \-I"$SRC" -I"$SING" -I"$SRC/Shared/Player" \sim/main.m ../scene.m "$OUT/gen/SpeedPitch.m" "$OUT/gen/Sing.m" "$SRC/Shared/Player/SGTimePitch.m" \- "$SING/SGSingModel.m" "$SING/SGSingLoader.m" "$SING/SGSingSeparator.m" "$SING/SGSingEngine.m" \+ "$SING/SGSingModel.m" "$SING/SGSingLoader.m" "$SING/SGSingSeparator.m" "$SING/SGSingEngine.m" "$SRC/Shared/AudioEffects/SGDSPReverb.m" \"$SRC/Core/SGRebind.m" "$SRC/Core/SGLog.m" "$SRC/Core/SGPrefs.m" "$SRC/Core/SGUIMode.m" "$SRC/Core/SGFlagForce.m" \-framework UIKit -framework QuartzCore -framework MediaPlayer -framework AudioToolbox -framework AVFoundation -framework Accelerate -framework CoreML -framework Foundation \-o "$OUT/SingHarness.app/SingHarness"diff --git a/harness/sing/build.sh b/harness/sing/build.shindex d5fef260..51ae12d9 100755--- a/harness/sing/build.sh+++ b/harness/sing/build.sh@@ -11,5 +11,6 @@ OUT=build/sing${SANITIZE:+-$SANITIZE}mkdir -p buildxcrun clang -fobjc-arc -O2 -g -Wall -Werror -target arm64-apple-macos15.0 ${SANITIZE:+-fsanitize=$SANITIZE -O1 -fno-omit-frame-pointer} \-I shim -I ../../tweak/Sources main.m "$SING"/SGSingLoader.m "$SING"/SGSingSeparator.m "$SING"/SGSingEngine.m \+ ../../tweak/Sources/Shared/AudioEffects/SGDSPReverb.m \-framework Foundation -framework AudioToolbox -framework Accelerate -framework CoreML -o "$OUT"echo "built $OUT"diff --git a/harness/sing/main.m b/harness/sing/main.mindex d9b776ff..8803fbd7 100644--- a/harness/sing/main.m+++ b/harness/sing/main.m@@ -1077,27 +1077,47 @@ static void checkSpatial(void) {step, toneStep, loudest);writeWAV(@"spatial.wav", out);SGSingEngineDestroy(engine);++ // Distance: at 4 m, straight ahead and with no room, the voice is at the square root of 1/4 of its level, -6.02 dB.+ engine = SGSingEngineCreate();+ SGSingEngineSetSeparator(engine, [[SGWholeSeparator alloc] initWithModel:nil]);+ SGSingEngineSetLevel(engine, 1);+ SGSingEngineSetSpatial(engine, true, 4, 0, 1);+ SGSingEngineSetOn(engine, true);+ source = (Source){tone, 0};+ Audio far = play(engine, tone, &source, 1024, 8, ^(double played) {});+ size_t from = 5 * kSGSingRate, to = 7 * kSGSingRate;+ double farDB = 10 * log10(energy(far.left, from, to) / energy(tone.left, from, to));+ CHECK(fabs(farDB + 6.02) < 0.2, "at 4 m the voice is %.2f dB (the square root of 1/4: -6.02)", farDB);+ // Room, where AUReverb2 is (not on the Mac): the room adds to the far voice.+ SGSingEngineSetSpatial(engine, true, 4, 1, 1);+ source = (Source){tone, 0};+ Audio roomy = play(engine, tone, &source, 1024, 8, ^(double played) {});+ double roomDB = 10 * log10(energy(roomy.left, from, to) / energy(tone.left, from, to));+ printf("note: with Room at 100%% the voice at 4 m is %.2f dB (%s)\n", roomDB,+ roomDB > farDB + 0.5 ? "the room is heard" : "no room here: AUReverb2 is missing or silent");+ SGSingEngineDestroy(engine);}// The front the voice is held off (SGSpatialVoiceAngle), fed at 25 Hz as AirPods send their motion.static void checkFront(void) {SGSpatialFront front = {0};- double angle = SGSpatialVoiceAngle(&front, 0.3, 10);+ double angle = SGSpatialVoiceAngle(&front, 0.3, 10, 20);CHECK(angle == 0, "the first motion is the front: the voice straight ahead (%.3f)", angle);// The head turned 60 degrees left (yaw grows) at once, then held there.- double turned = SGSpatialVoiceAngle(&front, 0.3 + M_PI / 3, 10.04), held = turned;+ double turned = SGSpatialVoiceAngle(&front, 0.3 + M_PI / 3, 10.04, 20), held = turned;double time = 10.04;- for (; time < 30.04 - 1e-9; time += 0.04) held = SGSpatialVoiceAngle(&front, 0.3 + M_PI / 3, time + 0.04);- CHECK(fabs(turned - M_PI / 3) < 0.01 && fabs(held - M_PI / 3 * exp(-1)) < 0.01,- "a head turned 60 degrees left has the voice %.1f degrees right, and 20 s on %.1f (the front's 1/e: %.1f)",- turned * 180 / M_PI, held * 180 / M_PI, 60 * exp(-1));+ for (; time < 30.04 - 1e-9; time += 0.04) held = SGSpatialVoiceAngle(&front, 0.3 + M_PI / 3, time + 0.04, 20);+ CHECK(fabs(turned - M_PI / 3) < 0.01 && fabs(held - M_PI / 3 * exp(-3)) < 0.01,+ "a head turned 60 degrees left has the voice %.1f degrees right, and 20 s on %.1f (5%% of the turn left: %.1f)",+ turned * 180 / M_PI, held * 180 / M_PI, 60 * exp(-3));// Across the back: yaw wraps from +179 to -179 degrees, which is 2 degrees further left, not 358 right.SGSpatialFront back = {0};- SGSpatialVoiceAngle(&back, M_PI - 0.01, 0);- double wrapped = SGSpatialVoiceAngle(&back, -M_PI + 0.01, 0.04);+ SGSpatialVoiceAngle(&back, M_PI - 0.01, 0, 20);+ double wrapped = SGSpatialVoiceAngle(&back, -M_PI + 0.01, 0.04, 20);CHECK(fabs(wrapped - 0.02) < 0.001, "across +-180 degrees the voice moves %.2f degrees", wrapped * 180 / M_PI);// Headphones out for 2 s and in again: the front starts over where the head points.- double again = SGSpatialVoiceAngle(&front, -1, time + 2);+ double again = SGSpatialVoiceAngle(&front, -1, time + 2, 20);CHECK(again == 0, "after a gap in the motion the voice is ahead again (%.3f)", again);}diff --git a/plist/liquid-glass.plist b/plist/liquid-glass.plistindex d4f98fb0..7d784cdb 100644--- a/plist/liquid-glass.plist+++ b/plist/liquid-glass.plist@@ -19,6 +19,6 @@<key>NSLocalNetworkUsageDescription</key><string>Find nearby speakers and devices for Spotify Connect and Cast.</string><key>NSMotionUsageDescription</key>- <string>AirPods gestures read how your head moves, so a double nod can like a song and a shake can skip it.</string>+ <string>AirPods gestures and Spatial voice read how your head moves, so a double nod can like a song, a shake can skip it, and Karaoke's voice can stay in front of you as you turn.</string></dict></plist>diff --git a/tweak/Sources/Shared/AudioEffects/SGDSPEffects.h b/tweak/Sources/Shared/AudioEffects/SGDSPEffects.hindex 5e0dd188..d34f80d7 100644--- a/tweak/Sources/Shared/AudioEffects/SGDSPEffects.h+++ b/tweak/Sources/Shared/AudioEffects/SGDSPEffects.h@@ -66,6 +66,10 @@ SGDSPReverb *SGDSPReverbCreate(double rate, int preset, float amount);void SGDSPReverbSet(SGDSPReverb *reverb, int preset, float amount);void SGDSPReverbRun(void *reverb, float *left, float *right, uint32_t frames);void SGDSPReverbFree(void *reverb);+// Wet only, for a send: what comes out is the room alone, none of what went in.+void SGDSPReverbSetSend(SGDSPReverb *reverb, int preset);+// Empties the room's tail. Off the render thread, or on it between two runs.+void SGDSPReverbReset(SGDSPReverb *reverb);// A script in JSFX's shape: a desc: line and sliderN: defaults, then @init, @slider, @block and @sample// sections, spl0 and spl1 the samples, srate the rate. NULL and why, with the file's line, when it doesdiff --git a/tweak/Sources/Shared/AudioEffects/SGDSPReverb.m b/tweak/Sources/Shared/AudioEffects/SGDSPReverb.mindex a43483ff..efd67aae 100644--- a/tweak/Sources/Shared/AudioEffects/SGDSPReverb.m+++ b/tweak/Sources/Shared/AudioEffects/SGDSPReverb.m@@ -75,9 +75,8 @@ SGDSPReverb *SGDSPReverbCreate(double rate, int preset, float amount) {return reverb;}-void SGDSPReverbSet(SGDSPReverb *reverb, int preset, float amount) {+static void setPreset(SGDSPReverb *reverb, int preset, float mix) {const Preset *p = &kPresets[preset < 0 ? 0 : preset >= SGDSPReverbPresetCount ? SGDSPReverbPresetCount - 1 : preset];- float mix = fminf(100, p->mix * fmaxf(0, amount) / 50);const struct { AudioUnitParameterID id; float value; } values[] = {{kReverb2Param_DryWetMix, mix}, {kReverb2Param_Gain, 0}, {kReverb2Param_MinDelayTime, p->minDelay},{kReverb2Param_MaxDelayTime, p->maxDelay}, {kReverb2Param_DecayTimeAt0Hz, p->decay},@@ -88,6 +87,19 @@ void SGDSPReverbSet(SGDSPReverb *reverb, int preset, float amount) {}}+void SGDSPReverbSet(SGDSPReverb *reverb, int preset, float amount) {+ int index = preset < 0 ? 0 : preset >= SGDSPReverbPresetCount ? SGDSPReverbPresetCount - 1 : preset;+ setPreset(reverb, index, fminf(100, kPresets[index].mix * fmaxf(0, amount) / 50));+}++void SGDSPReverbSetSend(SGDSPReverb *reverb, int preset) {+ setPreset(reverb, preset, 100);+}++void SGDSPReverbReset(SGDSPReverb *reverb) {+ AudioUnitReset(reverb->unit, kAudioUnitScope_Global, 0);+}+void SGDSPReverbRun(void *state, float *left, float *right, uint32_t frames) {SGDSPReverb *reverb = state;memcpy(reverb->input[0], left, frames * sizeof(float));diff --git a/tweak/Sources/Shared/Sing/SGSingEngine.h b/tweak/Sources/Shared/Sing/SGSingEngine.hindex 3d0510c1..68e93230 100644--- a/tweak/Sources/Shared/Sing/SGSingEngine.h+++ b/tweak/Sources/Shared/Sing/SGSingEngine.h@@ -57,9 +57,13 @@ void SGSingEngineSetLevel(SGSingEngine *engine, float level);// behind sounds as the front does): 0, where it starts, leaves the vocals exactly as the song has them. The// render glides to a new angle over a few tens of milliseconds, so it can be set as often as the head moves.void SGSingEngineSetVoiceAngle(SGSingEngine *engine, float radians);+// Spatial voice's Distance (meters, 1 to 6.5), Room (0 to 1) and Instruments width (0 to 2, 1 as the song has them),+// which apply while `on`: farther, the voice is quieter and more of it is the room; wider, the rest of the song spreads+// further to each side. Off, or at 1 m, no room and width 1, the song is as the angle alone leaves it. Any thread.+void SGSingEngineSetSpatial(SGSingEngine *engine, bool on, float meters, float room, float width);// Where spatial voice holds the voice, for Sing.x and the Spatial voice page's preview alike: off a front that-// follows where the head points over 20 s, so the voice drifts back ahead of a head that stays turned and the+// follows where the head points, 95% of the way there `frontSeconds` after it turned (Back in front, 20 s unless set), so the voice drifts back ahead of a head that stays turned and the// attitude's own drift never carries it off. A gap in the motion over 1 s (headphones out and in again), or a// front cleared to zero, starts it over where the head points.typedef struct {@@ -68,14 +72,15 @@ typedef struct {} SGSpatialFront;// The voice's angle off the head in radians to its right, from a head motion's yaw and timestamp (seconds).-static inline double SGSpatialVoiceAngle(SGSpatialFront *f, double yaw, double time) {+static inline double SGSpatialVoiceAngle(SGSpatialFront *f, double yaw, double time, double frontSeconds) {// CoreMotion's yaw turns counterclockwise seen from above, so it grows as the head turns left, and the voice// held ahead is then off to the head's right. Not yet heard on AirPods; flip it here.const double yawToRight = 1;- const double frontSeconds = 20, motionGap = 1;+ const double motionGap = 1;double since = time - f->last;if (!f->hasFront || since < 0 || since > motionGap) f->front = yaw;- else f->front = remainder(f->front + remainder(yaw - f->front, 2 * M_PI) * (1 - exp(-since / frontSeconds)), 2 * M_PI);+ // Three time constants: e^-3 of the turn, 5%, is left after frontSeconds.+ else f->front = remainder(f->front + remainder(yaw - f->front, 2 * M_PI) * (1 - exp(-3 * since / frontSeconds)), 2 * M_PI);f->hasFront = true;f->last = time;return yawToRight * remainder(yaw - f->front, 2 * M_PI);diff --git a/tweak/Sources/Shared/Sing/SGSingEngine.m b/tweak/Sources/Shared/Sing/SGSingEngine.mindex dbc45d59..aed667c0 100644--- a/tweak/Sources/Shared/Sing/SGSingEngine.m+++ b/tweak/Sources/Shared/Sing/SGSingEngine.m@@ -3,6 +3,7 @@#import <stdatomic.h>#import "SGSingEngine.h"#import "SGSingSeparator.h"+#import "Shared/AudioEffects/SGDSPEffects.h"enum {kRing = kSGSingRate * 10, // what the rings hold: the longest lead and a window, with room to spare@@ -38,6 +39,13 @@ static const float kVoiceWidth = 0.5f;static const float kVoiceDelay = 0.00065f * kSGSingRate;static const float kVoiceShadow = 0.35f;static const float kVoiceGlideSeconds = 0.05f;+// Distance and Room, with Spatial voice on: the voice is at its own level at kNearest and quieter farther off, by the+// square root of the distance (gentler than a free field's 1/d, so the far end still sings); the room's share grows from+// half of Room's at the nearest to all of it at the farthest. The room is AUReverb2's medium room, wet only, fed the+// voice's middle so its tail stays put as the head turns, and let ring kRoomTailSeconds after the voice stops reaching it.+static const float kNearest = 1, kFarthest = 6.5f;+static const int kRoomPreset = 2;+static const float kRoomTailSeconds = 4;struct SGSingEngine {float *left, *right, *vocalsLeft, *vocalsRight; // rings, a frame at position p at p % kRing@@ -61,6 +69,13 @@ struct SGSingEngine {struct { double when; uint64_t ahead; } asked[kAsked];unsigned askedNext;atomic_uint levelBits, targetLead, voiceAngleBits;+ atomic_bool spatial; // Spatial voice on: Distance and Room apply+ atomic_uint distanceBits, roomBits; // meters, and 0 to 1+ atomic_uint widthBits; // the instruments' stereo width, 1 as the song has it+ SGDSPReverb *room; // NULL where AUReverb2 is missing (the Mac): no room+ float *send[2]; // the room's input and output, kSGDSPEffectMaxFrames each+ float directGain, sendGain, width; // the render's, gliding to what Distance, Room and width ask for+ uint64_t roomTail; // frames the room still rings after the voice stops reaching itatomic_uint leadCap; // frames the lead may hold at most, separating or not (SGSingEngineSetLeadCap)// The render thread's own.@@ -106,6 +121,9 @@ static void storeFloat(atomic_uint *slot, float value) {// Starts the rings over where the mixer is now.static void startOver(SGSingEngine *engine) {+ // The room's tail is the old place's: it does not ring on over the new one.+ if (engine->room) SGDSPReverbReset(engine->room);+ engine->roomTail = 0;uint64_t written = atomic_load_explicit(&engine->written, memory_order_relaxed);atomic_fetch_add(&engine->dropped, written - atomic_load_explicit(&engine->played, memory_order_relaxed));atomic_store(&engine->played, written);@@ -251,6 +269,16 @@ OSStatus SGSingEngineRender(SGSingEngine *engine, UInt32 frames, float *left, flfloat vocalsTo, otherTo;gainsFor(loadFloat(&engine->levelBits), &vocalsTo, &otherTo);float vocalsStep = (vocalsTo - engine->vocalsGain) / frames, otherStep = (otherTo - engine->otherGain) / frames;+ float directTo = 1, sendTo = 0, widthTo = 1;+ if (atomic_load_explicit(&engine->spatial, memory_order_relaxed)) {+ widthTo = loadFloat(&engine->widthBits);+ float distance = fmaxf(kNearest, loadFloat(&engine->distanceBits)), room = loadFloat(&engine->roomBits);+ directTo = sqrtf(kNearest / distance);+ sendTo = engine->room ? room * (0.5f + 0.5f * (distance - kNearest) / (kFarthest - kNearest)) : 0;+ }+ float directStep = (directTo - engine->directGain) / frames, sendStep = (sendTo - engine->sendGain) / frames;+ float widthStep = (widthTo - engine->width) / frames;+ bool sending = engine->room && (sendTo > 0 || engine->sendGain > 0 || engine->roomTail > 0);float fade = 1.0f / (kFadeSeconds * kSGSingRate), amountTo = on && mixing ? 1 : 0;// The voice's angle glides the short way round toward the one set, and lands on it; within the buffer@@ -269,6 +297,10 @@ OSStatus SGSingEngineRender(SGSingEngine *engine, UInt32 frames, float *left, flfloat x = engine->left[index], y = engine->right[index];engine->vocalsGain += vocalsStep;engine->otherGain += otherStep;+ engine->directGain += directStep;+ engine->sendGain += sendStep;+ engine->width += widthStep;+ float voiceSend = 0;if (position < ready) {engine->amount += fmaxf(-fade, fminf(amountTo - engine->amount, fade));float a = engine->amount, otherGain = engine->otherGain - 1;@@ -286,15 +318,35 @@ OSStatus SGSingEngineRender(SGSingEngine *engine, UInt32 frames, float *left, fl} else {engine->shadowLeft = engine->shadowRight = (v + w) * 0.5f;}- left[i] = fmaxf(-1, fminf(x + a * (engine->vocalsGain * placedLeft - v + otherGain * (x - v)), 1));- right[i] = fmaxf(-1, fminf(y + a * (engine->vocalsGain * placedRight - w + otherGain * (y - w)), 1));+ float voice = engine->vocalsGain * engine->directGain;+ voiceSend = a * engine->vocalsGain * engine->sendGain * (v + w) * 0.5f;+ // Instruments width: the rest's side (half its left less its right) scaled, its middle kept.+ float side = (engine->width - 1) * ((x - v) - (y - w)) * 0.5f * engine->otherGain;+ left[i] = fmaxf(-1, fminf(x + a * (voice * placedLeft - v + otherGain * (x - v) + side), 1));+ right[i] = fmaxf(-1, fminf(y + a * (voice * placedRight - w + otherGain * (y - w) - side), 1));} else {engine->amount = 0;left[i] = x;right[i] = y;dry += working;}+ if (sending) engine->send[0][i % kSGDSPEffectMaxFrames] = voiceSend;+ // The room's next stretch, once its input is full or the buffer ends: rung through and added.+ if (sending && ((i + 1) % kSGDSPEffectMaxFrames == 0 || i + 1 == frames)) {+ UInt32 count = i % kSGDSPEffectMaxFrames + 1, first = i + 1 - count;+ memcpy(engine->send[1], engine->send[0], count * sizeof(float));+ SGDSPReverbRun(engine->room, engine->send[0], engine->send[1], count);+ for (UInt32 j = 0; j < count; j++) {+ left[first + j] = fmaxf(-1, fminf(left[first + j] + engine->send[0][j], 1));+ right[first + j] = fmaxf(-1, fminf(right[first + j] + engine->send[1][j], 1));+ }+ }}+ if (sending) engine->roomTail = sendTo > 0 || engine->sendGain > 1e-4f ? (uint64_t)(kRoomTailSeconds * kSGSingRate)+ : engine->roomTail > frames ? engine->roomTail - frames : 0;+ engine->directGain = directTo;+ engine->sendGain = sendTo;+ engine->width = widthTo;engine->vocalsGain = vocalsTo;engine->otherGain = otherTo;played += frames;@@ -439,8 +491,20 @@ SGSingEngine *SGSingEngineCreate(void) {engine->window[c] = calloc(kSGSingWindowFrames, sizeof(float));engine->vocals[c] = calloc(kSGSingWindowFrames, sizeof(float));}- engine->vocalsGain = engine->otherGain = 1;+ engine->vocalsGain = engine->otherGain = engine->directGain = engine->width = 1;+ storeFloat(&engine->widthBits, 1);storeFloat(&engine->levelBits, 1);+ storeFloat(&engine->distanceBits, kNearest);+ engine->room = SGDSPReverbCreate(kSGSingRate, kRoomPreset, 0);+ if (engine->room) {+ SGDSPReverbSetSend(engine->room, kRoomPreset);+ engine->send[0] = calloc(kSGDSPEffectMaxFrames, sizeof(float));+ engine->send[1] = calloc(kSGDSPEffectMaxFrames, sizeof(float));+ if (!engine->send[0] || !engine->send[1]) {+ SGDSPReverbFree(engine->room);+ engine->room = NULL;+ }+ }updateTarget(engine, 1);atomic_store(&engine->leadCap, UINT_MAX);atomic_store(&engine->alive, true);@@ -468,6 +532,9 @@ void SGSingEngineDestroy(SGSingEngine *engine) {free(engine->right);free(engine->vocalsLeft);free(engine->vocalsRight);+ SGDSPReverbFree(engine->room);+ free(engine->send[0]);+ free(engine->send[1]);free(engine);}@@ -511,6 +578,13 @@ void SGSingEngineSetLevel(SGSingEngine *engine, float level) {storeFloat(&engine->levelBits, level);}+void SGSingEngineSetSpatial(SGSingEngine *engine, bool on, float meters, float room, float width) {+ storeFloat(&engine->widthBits, isfinite(width) ? fminf(2, fmaxf(0, width)) : 1);+ storeFloat(&engine->distanceBits, isfinite(meters) ? fminf(kFarthest, fmaxf(kNearest, meters)) : kNearest);+ storeFloat(&engine->roomBits, isfinite(room) ? fminf(1, fmaxf(0, room)) : 0);+ atomic_store(&engine->spatial, on);+}+void SGSingEngineSetVoiceAngle(SGSingEngine *engine, float radians) {storeFloat(&engine->voiceAngleBits, isfinite(radians) ? remainderf(radians, 2 * (float)M_PI) : 0);}diff --git a/tweak/Sources/Shared/Sing/SGSpatialPreview.m b/tweak/Sources/Shared/Sing/SGSpatialPreview.mindex 1f7da042..5e340780 100644--- a/tweak/Sources/Shared/Sing/SGSpatialPreview.m+++ b/tweak/Sources/Shared/Sing/SGSpatialPreview.m@@ -346,7 +346,7 @@ static CAGradientLayer *floorLight(CGPoint at, CGFloat size, UIColor *color, CGF// as the song has played, and the two agree once the head has been still a while.__block SGSpatialFront front = {0};SGHeadMotionListen(kListener, ^(CMDeviceMotion *motion) {- double angle = motion ? SGSpatialVoiceAngle(&front, motion.attitude.yaw, motion.timestamp) : NAN;+ double angle = motion ? SGSpatialVoiceAngle(&front, motion.attitude.yaw, motion.timestamp, SGSingSpatialFront()) : NAN;if (!motion) front.hasFront = false;NSTimeInterval at = motion.timestamp;dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf followed:angle at:at]; });diff --git a/tweak/Sources/Shared/Sing/Sing.h b/tweak/Sources/Shared/Sing/Sing.hindex 5134a251..16d676ae 100644--- a/tweak/Sources/Shared/Sing/Sing.h+++ b/tweak/Sources/Shared/Sing/Sing.h@@ -49,6 +49,13 @@ extern NSNotificationName const SGSingButtonDidChangeNotification;// Spatial voice: through headphones that track the head, the vocals stay in front as it turns; off until// switched on, and at once.#define SGKeySingSpatial @"spotifyglass.sing.spatial"+// Spatial voice's sound: how far ahead the voice is (meters, 1.5 until set), how much of it is the room (percent, 25+// until set), how long a head that stays turned takes to bring it back in front (seconds, 5 until set), and how wide+// the instruments spread (percent, 110 until set).+#define SGKeySingSpatialDistance @"spotifyglass.sing.spatial.distance"+#define SGKeySingSpatialRoom @"spotifyglass.sing.spatial.room"+#define SGKeySingSpatialFront @"spotifyglass.sing.spatial.front"+#define SGKeySingSpatialWidth @"spotifyglass.sing.spatial.width"// Posted on the main thread whenever what Sing is doing, or its model's download, changes.extern NSString *const SGSingChangedNotification;@@ -98,6 +105,13 @@ BOOL SGSingSpatialAvailable(void);BOOL SGSingSpatial(void);// Stored, the motion's permission asked for when it goes on, and applied at once.void SGSetSingSpatial(BOOL on);+// Distance, Room, Back in front and Instruments width: read as stored, and stored and applied at once. Any thread for+// the getters.+double SGSingSpatialDistance(void);+double SGSingSpatialRoom(void);+double SGSingSpatialFront(void);+double SGSingSpatialWidth(void);+void SGSetSingSpatialSound(double meters, double roomPercent, double frontSeconds, double widthPercent);// The top of the Spatial voice page: listener and voice on a field of dots seen from behind the head, which// follows the head through HeadGestures' motion while it is on screen and the motion is allowed, and idlesdiff --git a/tweak/Sources/Shared/Sing/Sing.x b/tweak/Sources/Shared/Sing/Sing.xindex b2fa134b..0422b3db 100644--- a/tweak/Sources/Shared/Sing/Sing.x+++ b/tweak/Sources/Shared/Sing/Sing.x@@ -50,8 +50,8 @@// comes back; Spotify decides whether it resumes.//// Spatial voice listens to Shared/HeadGestures' motion (the app's one CMHeadphoneMotionManager) while Sing is-// on and Spotify plays, and hands the engine the head's yaw off a front that follows where the head points over-// 20 s, so the voice drifts back ahead of a head that stays turned and the attitude's own drift never carries+// on and Spotify plays, and hands the engine the head's yaw off a front that follows where the head points (Back in front,+// 5 s unless set), so the voice drifts back ahead of a head that stays turned and the attitude's own drift never carries// it off. With no motion (no such headphones, Motion & Fitness not allowed) the voice stays ahead. iOS's own// spatial audio on the route already holds the whole song in place, so spatial voice stands down for it.//@@ -322,6 +322,8 @@ static void prepareAheadSoon(void) {static BOOL sg_spatialListening;// On HeadGestures' motion queue only.static SGSpatialFront sg_front;+// Back in front, read on the motion queue: stored on the main thread.+static _Atomic double sg_frontSeconds = 5;static void headMoved(CMDeviceMotion *motion) {SGSingEngine *engine = atomic_load_explicit(&sg_engine, memory_order_acquire);@@ -330,7 +332,7 @@ static void headMoved(CMDeviceMotion *motion) {if (engine) SGSingEngineSetVoiceAngle(engine, 0);return;}- double angle = SGSpatialVoiceAngle(&sg_front, motion.attitude.yaw, motion.timestamp);+ double angle = SGSpatialVoiceAngle(&sg_front, motion.attitude.yaw, motion.timestamp, sg_frontSeconds);if (engine) SGSingEngineSetVoiceAngle(engine, (float)angle);}@@ -362,10 +364,48 @@ BOOL SGSingSpatial(void) {return SGHidden(SGKeySingSpatial);}+static double storedNumber(NSString *key, double fallback, double low, double high) {+ id value = [NSUserDefaults.standardUserDefaults objectForKey:key];+ double number = [value isKindOfClass:NSNumber.class] ? [value doubleValue] : fallback;+ return isfinite(number) ? fmin(high, fmax(low, number)) : fallback;+}++double SGSingSpatialDistance(void) {+ return storedNumber(SGKeySingSpatialDistance, 1.5, 1, 6.5);+}++double SGSingSpatialRoom(void) {+ return storedNumber(SGKeySingSpatialRoom, 25, 0, 100);+}++double SGSingSpatialFront(void) {+ return storedNumber(SGKeySingSpatialFront, 5, 5, 60);+}++double SGSingSpatialWidth(void) {+ return storedNumber(SGKeySingSpatialWidth, 110, 0, 200);+}++// The engine's Distance and Room, on while Spatial voice is.+static void applySpatialSound(SGSingEngine *engine) {+ if (engine) SGSingEngineSetSpatial(engine, SGSingSpatial(), SGSingSpatialDistance(), SGSingSpatialRoom() / 100, SGSingSpatialWidth() / 100);+}++void SGSetSingSpatialSound(double meters, double roomPercent, double frontSeconds, double widthPercent) {+ NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;+ [defaults setDouble:meters forKey:SGKeySingSpatialDistance];+ [defaults setDouble:roomPercent forKey:SGKeySingSpatialRoom];+ [defaults setDouble:frontSeconds forKey:SGKeySingSpatialFront];+ [defaults setDouble:widthPercent forKey:SGKeySingSpatialWidth];+ sg_frontSeconds = SGSingSpatialFront();+ applySpatialSound(atomic_load(&sg_engine));+}+static void updateRest(void);void SGSetSingSpatial(BOOL on) {SGSetEnabled(SGKeySingSpatial, on);+ applySpatialSound(atomic_load(&sg_engine));// Asked while the Sing page is in front, rather than with the next song.if (on) SGHeadMotionAskPermission();updateSpatial();@@ -400,6 +440,7 @@ static void apply(void) {return;}SGSingEngineSetLevel(engine, SGSingLevel());+ applySpatialSound(engine);atomic_store_explicit(&sg_engine, engine, memory_order_release);}BOOL held = sg_hot && !SGHidden(SGKeySingIgnoreHeat);@@ -1166,6 +1207,7 @@ static void readHeat(void) {sg_outputReachable = SGPlayerWatchMusicOutput(musicOutputChanged);if (!sg_outputReachable) SGLog(@"sing: Spotify's output cannot be watched, Sing cannot read its format");SGPlayerSetStage(stage);+ sg_frontSeconds = SGSingSpatialFront();%init;SGRequireClasses(@[@"SPTPlayerState", @"SPTEsperantoPlayer"]);// Runs on is Automatic or CPU only: GPU, Neural Engine and GPU and Neural Engine (2-4) are Automatic now, as is everydiff --git a/tweak/Sources/Shared/Sing/SingSettings.m b/tweak/Sources/Shared/Sing/SingSettings.mindex c5dc3d9a..1372312d 100644--- a/tweak/Sources/Shared/Sing/SingSettings.m+++ b/tweak/Sources/Shared/Sing/SingSettings.m@@ -149,6 +149,22 @@ UIViewController *SGSpatialVoiceSettingsPage(void) {@"you; stay turned, and it comes round in front again. It follows your head through headphones that "@"track it: AirPods Pro, AirPods 3 or later, AirPods Max, and asks for Motion & Fitness the first time. "@"While iOS's own Spatialize Stereo is on, which already holds the whole song in place, the voice is left to it."),+ SGNotedSection(@"Sound", @[+ SGSliderRow(@"Distance", nil, 1, 6.5, 0.5, ^double { return SGSingSpatialDistance(); }, ^(double value) {+ SGSetSingSpatialSound(value, SGSingSpatialRoom(), SGSingSpatialFront(), SGSingSpatialWidth());+ }, ^NSString *(double value) { return [NSString stringWithFormat:@"%.1f m", value]; }),+ SGSliderRow(@"Room", nil, 0, 100, 5, ^double { return SGSingSpatialRoom(); }, ^(double value) {+ SGSetSingSpatialSound(SGSingSpatialDistance(), value, SGSingSpatialFront(), SGSingSpatialWidth());+ }, ^NSString *(double value) { return [NSString stringWithFormat:@"%.0f%%", value]; }),+ SGSliderRow(@"Back in front", nil, 5, 60, 1, ^double { return SGSingSpatialFront(); }, ^(double value) {+ SGSetSingSpatialSound(SGSingSpatialDistance(), SGSingSpatialRoom(), value, SGSingSpatialWidth());+ }, ^NSString *(double value) { return [NSString stringWithFormat:@"After %.0f s", value]; }),+ SGSliderRow(@"Instruments width", nil, 0, 200, 5, ^double { return SGSingSpatialWidth(); }, ^(double value) {+ SGSetSingSpatialSound(SGSingSpatialDistance(), SGSingSpatialRoom(), SGSingSpatialFront(), value);+ }, ^NSString *(double value) { return [NSString stringWithFormat:@"%.0f%%", value]; }),+ ], @"How far ahead the voice sings, and how much of the room you hear around it: farther, the voice is quieter and "+ @"more of it is the room. Back in front is how long a head that stays turned takes to bring the voice ahead again. "+ @"Instruments width spreads the rest of the song wider than the recording, or narrows it below 100%."),] footer:nil];page = made;return made;