Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Pitch follows speed plays a song faster and higher together
My-Name-Is-Jeff5c6d4c9246a5
diff --git a/harness/pitch/README.md b/harness/pitch/README.mdindex 08e46dd4..564a8c84 100644--- a/harness/pitch/README.md+++ b/harness/pitch/README.md@@ -3,7 +3,9 @@`SGTimePitch.m` run on the Mac both ways the tweak runs it: in place over IO buffers of 1024, 4096and odd sizes (pitch alone), and pulling a source at rates from 0.5 to 2 (speed). Checks a 440 Hz sinecomes out at the pitch asked for and the source is consumed at the rate asked for, and reports the-unit's pulls, underruns, delay and cost. With a song path it also writes the song shifted up 3 and down 4+unit's pulls, underruns, delay and cost. Then one unit through the changes the menu makes while it is in+(a return to 1x, pitch set at 1x, Pitch follows speed switched), checking which ones change between+Varispeed and the stretch, the longest silence each leaves and the pitch after; a failed one exits 1. With a song path it also writes the song shifted up 3 and down 4semitones into `build/` to listen to../build.sh && build/pitch ../../../song.mp3diff --git a/harness/pitch/main.m b/harness/pitch/main.mindex 73d214a9..9ef3eeaa 100644--- a/harness/pitch/main.m+++ b/harness/pitch/main.m@@ -121,11 +121,13 @@ static OSStatus sineSource(void *context, UInt32 frames, AudioBufferList *data)return noErr;}-static void runPull(float rate, float semitones) {+static void runPull(float rate, float semitones, bool follows) {Sine sine = {0};SGTimePitch *unit = SGTimePitchCreate(kRate, 2, sineSource, &sine);+ SGTimePitchSetFollows(unit, follows);SGTimePitchSetRate(unit, rate);SGTimePitchSetSemitones(unit, semitones);+ SGTimePitchReset(unit);size_t total = (size_t)(kRate * 4);float *left = calloc(total, sizeof(float)), *right = calloc(total, sizeof(float));for (size_t done = 0; done < total; done += 1024) {@@ -133,22 +135,92 @@ static void runPull(float rate, float semitones) {if (SGTimePitchRender(unit, 1024, &buffers.list) != noErr) printf(" render failed\n");}double measured = frequency(left + (size_t)kRate, total - (size_t)kRate);- double expected = 440 * pow(2, semitones / 12);+ // Following speed, the pitch rises with the rate, the way a record played faster does.+ double expected = follows && (rate != 1 || semitones == 0) ? 440 * rate : 440 * pow(2, semitones / 12);double consumedRate = (double)SGTimePitchConsumed(unit) / total;- printf("pull rate %.2f %+3.0f st: %6.1f Hz (want %6.1f, %+.2f%%), consumed %.3fx, largest pull %u, failures %u\n", rate, semitones,+ printf("pull %-10s rate %.2f %+3.0f st: %6.1f Hz (want %6.1f, %+.2f%%), consumed %.3fx, largest pull %u, failures %u\n", follows ? "varispeed" : "stretch", rate, semitones,measured, expected, (measured / expected - 1) * 100, consumedRate, SGTimePitchLargestPull(unit), SGTimePitchFailures(unit));free(left);free(right);}+// The longest run of near silence, in frames, after the unit's first sound.+static size_t longestGap(const float *samples, size_t count) {+ size_t longest = 0, run = 0;+ for (size_t i = onset(samples, count); i < count; i++) {+ run = fabsf(samples[i]) < 0.01f ? run + 1 : 0;+ if (run > longest) longest = run;+ }+ return longest;+}++// One unit through a change of speed or pitch, the way the menu makes one: `before` for a second, then+// `after`, resetting only when SGTimePitchSwitchPending says (SpeedPitch.x's switchIfAsked). A sine has no+// silence longer than its zero crossings, about 2 frames, so a longer gap is the change heard.+typedef struct { float rate, semitones; bool follows; } Setting;+static bool runChange(const char *name, Setting before, Setting after, bool wantSwitch, size_t allowedGap) {+ Sine sine = {0};+ SGTimePitch *unit = SGTimePitchCreate(kRate, 2, sineSource, &sine);+ SGTimePitchSetFollows(unit, before.follows);+ SGTimePitchSetRate(unit, before.rate);+ SGTimePitchSetSemitones(unit, before.semitones);+ SGTimePitchReset(unit);+ size_t half = (size_t)kRate, total = 2 * half;+ float *left = calloc(total, sizeof(float)), *right = calloc(total, sizeof(float));+ bool switched = false, changed = false;+ for (size_t done = 0; done < total; done += 1024) {+ if (done >= half && !changed) {+ changed = true;+ SGTimePitchSetFollows(unit, after.follows);+ SGTimePitchSetRate(unit, after.rate);+ SGTimePitchSetSemitones(unit, after.semitones);+ switched = SGTimePitchSwitchPending(unit);+ if (switched) SGTimePitchReset(unit);+ }+ struct { AudioBufferList list; AudioBuffer second; } buffers = {{2, {{1, 4096, left + done}}}, {1, 4096, right + done}};+ SGTimePitchRender(unit, 1024, &buffers.list);+ }+ size_t gap = longestGap(left, total);+ bool resamples = after.follows && (after.rate != 1 || after.semitones == 0);+ double expected = resamples ? 440 * after.rate : 440 * pow(2, after.semitones / 12);+ double measured = frequency(left + half + half / 2, half / 2);+ bool ok = switched == wantSwitch && gap <= allowedGap && fabs(measured / expected - 1) < 0.005;+ printf("%s change %-44s: %s units, longest gap %4zu frames (%.1f ms), then %6.1f Hz (want %6.1f)\n", ok ? "ok " : "FAIL", name,+ switched ? "switched" : "kept ", gap, gap * 1000 / kRate, measured, expected);+ free(left);+ free(right);+ return ok;+}++// The changes SpeedPitch.x makes while the unit is in, Pitch follows speed being on unless said.+static bool runChanges(void) {+ bool ok = true;+ // A hold let go, Speed reset, a drag across 1: Varispeed all along, nothing lost.+ ok &= runChange("follows, 2x to 1x", (Setting){2, 0, true}, (Setting){1, 0, true}, false, 8);+ ok &= runChange("follows, 1x to 1.5x", (Setting){1, 0, true}, (Setting){1.5f, 0, true}, false, 8);+ // Pitch alone at 1x is the stretch's, reset on the way in: its delay once, never sound from before.+ ok &= runChange("follows, 1x, pitch 0 to +3", (Setting){1, 0, true}, (Setting){1, 3, true}, true, 4200);+ ok &= runChange("follows, 1.5x +3 to 1x +3", (Setting){1.5f, 3, true}, (Setting){1, 3, true}, true, 4200);+ ok &= runChange("follows switched off at 1.5x", (Setting){1.5f, 0, true}, (Setting){1.5f, 0, false}, true, 4200);+ ok &= runChange("follows switched on at 1.5x", (Setting){1.5f, 0, false}, (Setting){1.5f, 0, true}, true, 64);+ // Without following it stays the stretch.+ ok &= runChange("stretch, 1.5x to 1x", (Setting){1.5f, 0, false}, (Setting){1, 0, false}, false, 8);+ return ok;+}+int main(int argc, const char **argv) {@autoreleasepool {for (int pattern = 0; pattern < 3; pattern++) {for (NSNumber *semitones in @[@-12, @-5, @-1, @0.5, @3, @7, @12]) runSine(semitones.floatValue, pattern);}for (NSNumber *rate in @[@0.5, @0.75, @1, @1.25, @1.5, @2]) {- runPull(rate.floatValue, 0);- runPull(rate.floatValue, 3);+ runPull(rate.floatValue, 0, false);+ runPull(rate.floatValue, 3, false);+ runPull(rate.floatValue, 0, true);+ }+ if (!runChanges()) {+ printf("a change failed\n");+ return 1;}if (argc > 1) {NSString *song = @(argv[1]);diff --git a/tweak/Sources/Shared/Player/SGTimePitch.h b/tweak/Sources/Shared/Player/SGTimePitch.hindex abbcd976..fcf5b3ca 100644--- a/tweak/Sources/Shared/Player/SGTimePitch.h+++ b/tweak/Sources/Shared/Player/SGTimePitch.h@@ -32,11 +32,24 @@ SGTimePitch *SGTimePitchCreate(double sampleRate, UInt32 channels, SGTimePitchSodouble SGTimePitchSampleRate(const SGTimePitch *unit);UInt32 SGTimePitchChannels(const SGTimePitch *unit);-// Any thread, taking effect with the next buffer. Rate 1 is normal speed (0.25...4); semitones up or down.+// Any thread, taking effect with the next buffer (a change of unit waits for SGTimePitchReset, below). Rate+// 1 is normal speed (0.25...4); semitones up or down.void SGTimePitchSetRate(SGTimePitch *unit, float rate);void SGTimePitchSetSemitones(SGTimePitch *unit, float semitones);-// Forgets the sound held, so the next buffer starts over from silence. Only while nothing renders.+// Pull mode only: the speed done by resampling (Apple's Varispeed) instead of the time stretch, so a rate+// over 1 also plays higher, like a record, without the stretch's smearing. Any thread. While following,+// Varispeed renders at rate 1 too (passing the sound through), so a return to normal speed changes nothing;+// the time and pitch unit renders only pitch alone at rate 1, the semitones being ignored at any other rate.+void SGTimePitchSetFollows(SGTimePitch *unit, bool follows);++// Which of the two units renders changes only at SGTimePitchReset, so the render thread never hands over+// to a unit still holding sound from the last time it ran. True when the rate, the semitones or following+// set since then call for the other unit: the caller stops rendering, resets and starts again.+bool SGTimePitchSwitchPending(const SGTimePitch *unit);++// Forgets the sound held, so the next buffer starts over from silence, and picks the unit to render. Only+// while nothing renders.void SGTimePitchReset(SGTimePitch *unit);// Pull mode: `frames` frames of output into `data`, float non-interleaved buffers of the unit's channels.diff --git a/tweak/Sources/Shared/Player/SGTimePitch.m b/tweak/Sources/Shared/Player/SGTimePitch.mindex 045ebb8b..964cd92e 100644--- a/tweak/Sources/Shared/Player/SGTimePitch.m+++ b/tweak/Sources/Shared/Player/SGTimePitch.m@@ -8,6 +8,10 @@ enum { kFifoFrames = 16384 };struct SGTimePitch {AudioUnit unit;+ AudioUnit vari; // pull mode only: Varispeed, NULL in place+ atomic_bool follows;+ _Atomic float rate, semitones;+ atomic_bool resampling; // which unit renders; changed only by SGTimePitchResetdouble sampleRate;UInt32 channels;SGTimePitchSource source;@@ -85,6 +89,25 @@ SGTimePitch *SGTimePitchCreate(double sampleRate, UInt32 channels, SGTimePitchSoif (!status) status = AudioUnitSetProperty(unit->unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof callback);if (!status) status = AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, 1, 0);if (!status) status = AudioUnitInitialize(unit->unit);+ atomic_store(&unit->rate, 1);+ // Varispeed beside it, pulling the same source: a resampler, so speed and pitch move together.+ AudioComponentDescription varispeed = {+ .componentType = kAudioUnitType_FormatConverter,+ .componentSubType = kAudioUnitSubType_Varispeed,+ .componentManufacturer = kAudioUnitManufacturer_Apple,+ };+ AudioComponent variComponent = source ? AudioComponentFindNext(NULL, &varispeed) : NULL;+ if (!status && variComponent && AudioComponentInstanceNew(variComponent, &unit->vari) == noErr) {+ OSStatus vstatus = AudioUnitSetProperty(unit->vari, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, sizeof format);+ if (!vstatus) vstatus = AudioUnitSetProperty(unit->vari, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &format, sizeof format);+ if (!vstatus) vstatus = AudioUnitSetProperty(unit->vari, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFrames, sizeof maxFrames);+ if (!vstatus) vstatus = AudioUnitSetProperty(unit->vari, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, sizeof callback);+ if (!vstatus) vstatus = AudioUnitInitialize(unit->vari);+ if (vstatus) {+ AudioComponentInstanceDispose(unit->vari);+ unit->vari = NULL;+ }+ }if (status) {AudioComponentInstanceDispose(unit->unit);free(unit);@@ -113,23 +136,43 @@ UInt32 SGTimePitchChannels(const SGTimePitch *unit) {void SGTimePitchSetRate(SGTimePitch *unit, float rate) {AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, rate, 0);+ if (unit->vari) AudioUnitSetParameter(unit->vari, kVarispeedParam_PlaybackRate, kAudioUnitScope_Global, 0, rate, 0);+ atomic_store(&unit->rate, rate);+}++void SGTimePitchSetFollows(SGTimePitch *unit, bool follows) {+ atomic_store(&unit->follows, follows);}void SGTimePitchSetSemitones(SGTimePitch *unit, float semitones) {AudioUnitSetParameter(unit->unit, kNewTimePitchParam_Pitch, kAudioUnitScope_Global, 0, semitones * 100, 0);+ atomic_store(&unit->semitones, semitones);+}++// Varispeed while following, through normal speed too, so coming back to 1x never changes units; the time+// and pitch unit for a stretch, and for pitch alone at normal speed.+static bool wantsVarispeed(const SGTimePitch *unit) {+ return unit->vari && atomic_load(&unit->follows)+ && (atomic_load(&unit->rate) != 1 || atomic_load(&unit->semitones) == 0);+}++bool SGTimePitchSwitchPending(const SGTimePitch *unit) {+ return wantsVarispeed(unit) != atomic_load(&unit->resampling);}void SGTimePitchReset(SGTimePitch *unit) {AudioUnitReset(unit->unit, kAudioUnitScope_Global, 0);+ if (unit->vari) AudioUnitReset(unit->vari, kAudioUnitScope_Global, 0);unit->written = unit->read = 0;unit->sampleTime = 0;+ atomic_store(&unit->resampling, wantsVarispeed(unit));}OSStatus SGTimePitchRender(SGTimePitch *unit, UInt32 frames, AudioBufferList *data) {if (!frames || frames > kSGTimePitchMaxFrames) return kAudioUnitErr_TooManyFramesToProcess;AudioTimeStamp timestamp = {.mSampleTime = unit->sampleTime, .mFlags = kAudioTimeStampSampleTimeValid};AudioUnitRenderActionFlags flags = 0;- OSStatus status = AudioUnitRender(unit->unit, &flags, ×tamp, 0, frames, data);+ OSStatus status = AudioUnitRender(atomic_load(&unit->resampling) ? unit->vari : unit->unit, &flags, ×tamp, 0, frames, data);unit->sampleTime += frames;if (status != noErr) atomic_fetch_add_explicit(&unit->failures, 1, memory_order_relaxed);return status;diff --git a/tweak/Sources/Shared/Player/SpeedPitch.h b/tweak/Sources/Shared/Player/SpeedPitch.hindex 384c4a65..7b09a6fd 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.h+++ b/tweak/Sources/Shared/Player/SpeedPitch.h@@ -23,5 +23,10 @@ void SGSetPlayerSpeed(double speed);// Semitones Spotify's output is moved by, 0 when it is not.float SGPlayerPitch(void);void SGSetPlayerPitch(float semitones);+// Speed and pitch move together, like a record, by resampling rather than the time stretch. On until+// switched off, from the switch under the two sliders.+#define SGKeyPitchFollowsSpeed @"spotifyglass.speed.pitchFollows"+BOOL SGPlayerPitchFollowsSpeed(void);+void SGSetPlayerPitchFollowsSpeed(BOOL follows);// Whether the output could be reached to change its pitch.BOOL SGPlayerPitchAvailable(void);diff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.xindex f9757731..bd06082b 100644--- a/tweak/Sources/Shared/Player/SpeedPitch.x+++ b/tweak/Sources/Shared/Player/SpeedPitch.x@@ -351,8 +351,10 @@ static SGTimePitch *unitForFormat(void) {disengage();unit = SGTimePitchCreate(rate, channels, pull ? pullForUnit : NULL, NULL);if (unit) {+ SGTimePitchSetFollows(unit, SGPlayerPitchFollowsSpeed());SGTimePitchSetRate(unit, pull ? sg_speed : 1);SGTimePitchSetSemitones(unit, sg_semitones);+ SGTimePitchReset(unit);}atomic_store(slot, unit);SGLog(@"redesign speed: %@ %@ for %.0f Hz, %u channels", unit ? @"a unit" : @"no unit", pull ? @"in the chain" : @"in place", rate, (unsigned)channels);@@ -370,6 +372,16 @@ static void report(void) {SGTimePitchConsumed(unit) / SGTimePitchSampleRate(unit));}+// Hands the render thread to the unit's other half, reset, when the speed, the pitch or Pitch follows speed+// now call for it. That costs the unit's delay once (93 ms going to the stretch), the same as putting it in,+// where rendering the other half as it was left would play what it held from last time.+static void switchIfAsked(SGTimePitch *unit) {+ if (!atomic_load(&sg_engaged) || !SGTimePitchSwitchPending(unit)) return;+ disengage();+ SGTimePitchReset(unit);+ atomic_store(&sg_engaged, true);+}+// Puts the unit in or takes it out for the current speed and pitch.static void apply(void) {BOOL normal = sg_speed == 1 && sg_semitones == 0;@@ -395,6 +407,7 @@ static void apply(void) {SGTimePitchReset(unit);atomic_store(&sg_engaged, true);}+ switchIfAsked(unit);}#pragma mark - the menu's calls@@ -424,6 +437,17 @@ void SGSetPlayerPitch(float semitones) {apply();}+BOOL SGPlayerPitchFollowsSpeed(void) {+ return SGEnabled(SGKeyPitchFollowsSpeed);+}++void SGSetPlayerPitchFollowsSpeed(BOOL follows) {+ SGSetEnabled(SGKeyPitchFollowsSpeed, follows);+ SGTimePitch *unit = atomic_load(&sg_pull);+ if (unit) SGTimePitchSetFollows(unit, follows);+ apply();+}+BOOL SGPlayerPitchAvailable(void) {return sg_startOutput != NULL || tapped();}diff --git a/tweak/Sources/Shared/Player/SpeedPitchMenu.x b/tweak/Sources/Shared/Player/SpeedPitchMenu.xindex b601c8ae..3c8a4534 100644--- a/tweak/Sources/Shared/Player/SpeedPitchMenu.x+++ b/tweak/Sources/Shared/Player/SpeedPitchMenu.x@@ -29,7 +29,7 @@// A menu this soon after the more button's tap is the player's.static const NSTimeInterval kMenuAfterTap = 3;-static const CGFloat kRowHeight = 56, kSliderBlockHeight = 72, kPanelBottom = 12;+static const CGFloat kRowHeight = 56, kSliderBlockHeight = 72, kSwitchRowHeight = 44, kPanelBottom = 12;// The block's own measures and type, so it stands on Spotify's sheet under either look rather than on// the redesign's Kit: the sheet's side margin, the gap everything else is a multiple of, and a spring// that settles without overshooting.@@ -96,6 +96,8 @@ static char kBlockKey, kDecidedKey, kWatchedKey, kShownAtKey, kRowsInKey;UILabel *_speedName, *_pitchName;UIButton *_speedValue, *_pitchValue;UISlider *_speed, *_pitch;+ UILabel *_followsName;+ UISwitch *_follows;float _shownSpeed, _shownPitch;NSTimeInterval _speedSentAt;BOOL _speedPending;@@ -217,14 +219,19 @@ static void placeTick(UISlider *slider) {_speed.accessibilityLabel = @"Speed";_pitch = [self slider:-kMaxPitch max:kMaxPitch normal:0 minImage:@"arrow.down" maxImage:@"arrow.up"];_pitch.accessibilityLabel = @"Pitch";- for (UIView *view in @[_speedName, _speedValue, _speed, _pitchName, _pitchValue, _pitch]) [_panel addSubview:view];+ _followsName = makeLabel(nameFont, primary());+ _followsName.text = @"Pitch follows speed";+ _follows = [UISwitch new];+ _follows.accessibilityLabel = @"Pitch follows speed";+ [_follows addTarget:self action:@selector(followsChanged) forControlEvents:UIControlEventValueChanged];+ for (UIView *view in @[_speedName, _speedValue, _speed, _pitchName, _pitchValue, _pitch, _followsName, _follows]) [_panel addSubview:view];[self refresh];return self;}+ (CGFloat)heightOpen:(BOOL)open {- return kRowHeight + (open ? 2 * kSliderBlockHeight + kPanelBottom : 0);+ return kRowHeight + (open ? 2 * kSliderBlockHeight + kSwitchRowHeight + kPanelBottom : 0);}- (void)layoutSubviews {@@ -239,7 +246,7 @@ static void placeTick(UISlider *slider) {CGFloat summaryX = CGRectGetMaxX(_title.frame) + kGrid;_summary.frame = CGRectMake(summaryX, 0, MAX(0, CGRectGetMinX(_chevron.frame) - kGrid - summaryX), kRowHeight);- _panel.frame = CGRectMake(0, kRowHeight, width, 2 * kSliderBlockHeight + kPanelBottom);+ _panel.frame = CGRectMake(0, kRowHeight, width, 2 * kSliderBlockHeight + kSwitchRowHeight + kPanelBottom);CGFloat y = 0;for (NSArray<UIView *> *line in @[@[_speedName, _speedValue, _speed], @[_pitchName, _pitchValue, _pitch]]) {line[0].frame = CGRectMake(side, y + 4, width / 2 - side, 24);@@ -249,6 +256,9 @@ static void placeTick(UISlider *slider) {placeTick((UISlider *)line[2]);y += kSliderBlockHeight;}+ CGSize toggle = [_follows sizeThatFits:CGSizeZero];+ _follows.frame = CGRectMake(width - side - toggle.width, y + (kSwitchRowHeight - toggle.height) / 2, toggle.width, toggle.height);+ _followsName.frame = CGRectMake(side, y, CGRectGetMinX(_follows.frame) - side - kGrid, kSwitchRowHeight);}#pragma mark state@@ -277,7 +287,7 @@ static NSString *pitchText(float pitch) {_speed.enabled = speedAllowed;_pitch.enabled = pitchAvailable;_speed.alpha = speedAllowed ? 1 : 0.4;- _pitch.alpha = pitchAvailable ? 1 : 0.4;+ _follows.on = SGPlayerPitchFollowsSpeed();[self showValues];}@@ -289,6 +299,10 @@ static NSString *pitchText(float pitch) {[_speedValue layoutIfNeeded];[_pitchValue layoutIfNeeded];}];+ // While pitch follows a speed that is not normal, the speed sets the pitch, so the slider stands aside.+ BOOL pitchFree = pitchAvailable && !(_follows.on && _shownSpeed != 1);+ _pitch.enabled = pitchFree;+ _pitch.alpha = pitchFree ? 1 : 0.4;_speedValue.enabled = speedAllowed && _shownSpeed != 1;_pitchValue.enabled = pitchAvailable && _shownPitch != 0;_speed.accessibilityValue = speedAllowed ? speedText(_shownSpeed) : @"Unavailable";@@ -380,6 +394,12 @@ static NSString *pitchText(float pitch) {}}+- (void)followsChanged {+ SGSetPlayerPitchFollowsSpeed(_follows.on);+ SGPlayFeedback(SGFeedbackDetent);+ [self refresh];+}+- (void)resetSpeed {_shownSpeed = 1;[self sendSpeed];