Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Spotify's sound can be turned down by a gain of the mod's own
My-Name-Is-Jeffc53ee7c50f75
diff --git a/harness/speed/README.md b/harness/speed/README.mdindex e1453959..c3cb3e20 100644--- a/harness/speed/README.md+++ b/harness/speed/README.md@@ -33,3 +33,7 @@ the 48 kHz mixer drained 1.92x with both chains started (both outputs pulled it)44.1 kHz chain not at all, then silence once the 48 kHz one stopped.xcrun simctl launch --console-pty <udid> com.vojta.speedharness two++Last it sets the sleep timer's gain (`SGPlayerSetGain`) to 0.25 and back to 1, and each line reads the loudest+sample the speaker unit played over the step's last second, through a notify the harness adds after the mod's:+0.05 at 1, 0.0125 at 0.25.diff --git a/harness/speed/main.m b/harness/speed/main.mindex c0cc506c..3d8939dd 100644--- a/harness/speed/main.m+++ b/harness/speed/main.m@@ -15,6 +15,10 @@// sample rate, and checks that each decoder is drained at its own rate and only the music's output (the one// started last, or the one with sound) takes the speed, through a stop, a start, a silent decoder, a disposed// mixer and a disposed output. Each check prints PASS or FAIL.+//+// Last, the sleep timer's gain: 0.25, then 1 again. A notify of the harness's own, added after the mod's,+// reads the loudest sample the speaker unit played over the last second of each step: 0.05 at 1, 0.0125+// at 0.25.#import <UIKit/UIKit.h>#import <AudioToolbox/AudioToolbox.h>#import <AVFoundation/AVFoundation.h>@@ -24,9 +28,27 @@ double SGPlayerSpeed(void);void SGSetPlayerSpeed(double speed);void SGSetPlayerPitch(float semitones);BOOL SGPlayerSpeedAllowed(void);+void SGPlayerSetGain(float gain);static const double kRate = 44100;static atomic_uint_fast64_t sg_decoded;+// The loudest float sample the output played since the main thread last zeroed it, as its bits.+static atomic_uint sg_peakBits;++static OSStatus metered(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,+ UInt32 frames, AudioBufferList *data) {+ if (!(*flags & kAudioUnitRenderAction_PostRender) || bus != 0) return noErr;+ uint32_t bits = atomic_load(&sg_peakBits);+ float peak;+ memcpy(&peak, &bits, sizeof peak);+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) {+ const float *samples = data->mBuffers[b].mData;+ for (UInt32 i = 0; samples && i < data->mBuffers[b].mDataByteSize / sizeof(float); i++) peak = fmaxf(peak, fabsf(samples[i]));+ }+ memcpy(&bits, &peak, sizeof bits);+ atomic_store(&sg_peakBits, bits);+ return noErr;+}AudioUnit SGPlayerMusicOutput(void);@@ -137,6 +159,7 @@ static void check(OSStatus status, const char *what) {for (int i = 0; i < 3; i++) check(AudioUnitSetProperty(units[i], kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &slice, sizeof slice), "slice");for (int i = 0; i < 3; i++) check(AudioUnitInitialize(units[i]), "initialize");check(AudioOutputUnitStart(output), "start");+ check(AudioUnitAddRenderNotify(output, metered, NULL), "meter");_output = output;}@@ -203,9 +226,12 @@ static void expect(NSString *what, double wantA, double wantB) {NSTimeInterval now = CACurrentMediaTime();uint64_t decoded = atomic_load(&sg_decoded);double content = decoded / kRate;- NSLog(@"[harness] %-28@ decoder drained %.2fx over the last %.1f s; content %.2f s, state position %.2f s (off %+.0f ms), state speed %.2f",+ uint32_t bits = atomic_load(&sg_peakBits);+ float peak;+ memcpy(&peak, &bits, sizeof peak);+ NSLog(@"[harness] %-28@ decoder drained %.2fx over the last %.1f s; content %.2f s, state position %.2f s (off %+.0f ms), state speed %.2f, peak %.4f",what, (decoded - _lastDecoded) / kRate / (now - _lastAt), now - _lastAt, content, _state.position,- (_state.position - content) * 1000, [_state playbackSpeed]);+ (_state.position - content) * 1000, [_state playbackSpeed], peak);_lastDecoded = decoded;_lastAt = now;}@@ -239,6 +265,8 @@ static void expect(NSString *what, double wantA, double wantB) {@[@12, @"0.75x", @0.75, @3],@[@15, @"back to normal", @1, @0],@[@18, @"normal, unit out", @1, @0],+ @[@21, @"gain 0.25", @1, @0, @0.25],+ @[@24, @"gain back to 1", @1, @0, @1],];__block NSString *label = @"normal";for (NSArray *step in script) {@@ -247,12 +275,15 @@ static void expect(NSString *what, double wantA, double wantB) {label = step[1];SGSetPlayerSpeed([step[2] doubleValue]);SGSetPlayerPitch([step[3] floatValue]);+ if (step.count > 4) SGPlayerSetGain([step[4] floatValue]);}];+ // The peak is read over the step's last second, past the gain's half second ramp.+ [self after:[step[0] doubleValue] + 2 do:^{ atomic_store(&sg_peakBits, 0); }];// A report mid step, the way Spotify's player reports now and then.[self after:[step[0] doubleValue] + 1.5 do:^{ [self playerReports]; }];}if ([NSProcessInfo.processInfo.arguments containsObject:@"rate"]) [self after:7.5 do:^{ [self changeRate]; }];- [self after:21 do:^{+ [self after:27 do:^{[self report:label];exit(0);}];diff --git a/tweak/Sources/Shared/Player/SpeedPitch.h b/tweak/Sources/Shared/Player/SpeedPitch.hindex 434cd0fc..dba0a182 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.h+++ b/tweak/Sources/Shared/Player/SpeedPitch.h@@ -39,6 +39,11 @@ BOOL SGPlayerPitchFollowsSpeed(void);void SGSetPlayerPitchFollowsSpeed(BOOL follows);// Whether the output could be reached to change its pitch.BOOL SGPlayerPitchAvailable(void);+// The mod's own volume on Spotify's sound, 0 to 1, 1 when normal: the sleep timer's fade (SleepTimer.h).+// Any thread. The sound moves to it within half a second and never in a step. It scales what the speaker+// unit plays, in the output's own format, so a Connect device is left as it was, and it+// does nothing when Spotify's output was never reached (AudioOutputUnitStart not rebound). Not stored.+void SGPlayerSetGain(float gain);// The audio effects' reverb as the menu sets it, 0 to 100, 0 while it is off. Any amount turns the effects and// the reverb on with it; 0 turns the reverb off and leaves the rest of the effects as they were.float SGPlayerReverb(void);diff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.xindex 1ae8f223..8a889794 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.x+++ b/tweak/Sources/Shared/Player/SpeedPitch.x@@ -49,6 +49,9 @@//// Speed and pitch last until Spotify quits.//+// The music's render notify also scales each finished buffer by the gain SGPlayerSetGain asks for (the sleep+// timer's fade), in the chain or not.+//// Threading: the render callbacks and the notify run on each unit's render thread and touch only atomics,// their own output's record and the units. Spotify connects, starts, stops and disposes on its audio thread;// the record changes under sg_outputsLock, there and on sg_outputsQueue (format changes, the silence check).@@ -344,6 +347,41 @@ static BOOL hasSound(Format *format, AudioUnitRenderActionFlags flags, const Audreturn NO;}+#pragma mark - the render thread: the gain++// The gain asked for (main thread writes, render thread reads) and the one the sound is at (the music's render+// thread only). The sound moves toward the one asked for by at most the whole way in kGainSlew seconds, so a+// fade that starts late, a timer cancelled halfway or more time added never steps.+static const double kGainSlew = 0.5;+static atomic_uint sg_gainBits;+static float sg_gainNow = 1;++// Scales a finished buffer, in the hardware's format, ramping each frame from where the last buffer left off.+// Integers scale as they are, so a fixed point format needs no conversion.+static void applyGain(Format *hardware, AudioUnitRenderActionFlags *flags, UInt32 frames, AudioBufferList *data) {+ float target = loadFloat(&sg_gainBits);+ if (target == 1 && sg_gainNow == 1) return;+ UInt32 bytes = atomic_load_explicit(&hardware->bytes, memory_order_relaxed);+ double rate = rateOf(hardware);+ if ((bytes != 2 && bytes != 4) || rate <= 0) return;+ float from = sg_gainNow, most = (float)(frames / (kGainSlew * rate));+ float to = from + fmaxf(-most, fminf(target - from, most));+ sg_gainNow = to;+ if (*flags & kAudioUnitRenderAction_OutputIsSilence) return;+ BOOL isFloat = (atomic_load_explicit(&hardware->flags, memory_order_relaxed) & kAudioFormatFlagIsFloat) != 0;+ for (UInt32 b = 0; b < data->mNumberBuffers; b++) {+ AudioBuffer *buffer = &data->mBuffers[b];+ UInt32 channels = MAX(buffer->mNumberChannels, 1u), samples = buffer->mDataByteSize / bytes;+ if (!buffer->mData || samples < frames * channels) continue;+ for (UInt32 i = 0; i < frames * channels; i++) {+ float gain = from + (to - from) * (float)(i / channels) / (float)frames;+ if (bytes == 2) ((int16_t *)buffer->mData)[i] = (int16_t)(((int16_t *)buffer->mData)[i] * gain);+ else if (isFloat) ((float *)buffer->mData)[i] *= gain;+ else ((int32_t *)buffer->mData)[i] = (int32_t)(((int32_t *)buffer->mData)[i] * (double)gain);+ }+ }+}+// On every started output, `refCon` its Output: notes when it has sound, and shifts the pitch on the music's// when it is not fed by the callback.static OSStatus rendered(void *refCon, AudioUnitRenderActionFlags *flags, const AudioTimeStamp *timestamp, UInt32 bus,@@ -351,7 +389,9 @@ static OSStatus rendered(void *refCon, AudioUnitRenderActionFlags *flags, constif (!(*flags & kAudioUnitRenderAction_PostRender) || bus != 0 || !data || !data->mNumberBuffers || !frames) return noErr;Output *output = refCon;if (hasSound(&output->hardware, *flags, data)) atomic_store_explicit(&output->loudAt, mach_absolute_time(), memory_order_relaxed);- if (output != music() || atomic_load(&output->taken) || !atomic_load(&sg_engaged)) return noErr;+ if (output != music()) return noErr;+ applyGain(&output->hardware, flags, frames, data);+ if (atomic_load(&output->taken) || !atomic_load(&sg_engaged)) return noErr;atomic_store(&output->busy, true);SGTimePitch *unit = atomic_load(&sg_inPlace);if (atomic_load(&sg_engaged) && output == music() && unit && SGTimePitchSampleRate(unit) == rateOf(&output->hardware)) {@@ -851,6 +891,14 @@ BOOL SGPlayerPitchAvailable(void) {return sg_reachable || tapped();}+void SGPlayerSetGain(float gain) {+ if (!sg_reachable) {+ static int logged;+ if (logged++ < 1) SGLog(@"audio: Spotify's output was not reached, the gain cannot apply");+ }+ storeFloat(&sg_gainBits, fmaxf(0, fminf(gain, 1)));+}+AudioUnit SGPlayerMusicOutput(void) {Output *output = music();return output ? atomic_load(&output->unit) : NULL;@@ -913,6 +961,7 @@ BOOL SGPlayerWatchMusicOutput(SGPlayerOutputWatcher watcher) {%ctor {storeFloat(&sg_speedBits, 1);+ storeFloat(&sg_gainBits, 1);rebind();%init;SGRequireClasses(@[@"SPTPlayerState"]);