Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: smooth the karaoke word sweep and lift
Vojtěch Škopekdba0bcc20971
diff --git a/tweak/Sources/Features/Karaoke/KaraokeView.m b/tweak/Sources/Features/Karaoke/KaraokeView.mindex a01347b8..5da64711 100644--- a/tweak/Sources/Features/Karaoke/KaraokeView.m+++ b/tweak/Sources/Features/Karaoke/KaraokeView.m@@ -2,11 +2,14 @@#import "Karaoke.h"static const CGFloat kFontSize = 30, kMargin = 24, kLineGap = 24, kRowTighten = 2;-static const CGFloat kDimAlpha = 0.3, kFillEdge = 18, kLift = 2.5;+static const CGFloat kDimAlpha = 0.3, kFillEdge = 22, kLift = 2.5, kDimScale = 0.97;static const CGFloat kAnchor = 0.28; // where the sung line rests, as a share of the heightstatic const CGFloat kEdgeFade = 0.1; // the lines fade out over this share at the top and bottomstatic const CGFloat kBlurPerLine = 1.4, kMaxBlur = 6;static const NSTimeInterval kBrowseHold = 3; // after scrolling by hand, how long until it follows the song again+static const double kFloatMinMs = 700, kFloatLeadMs = 80; // a short word still floats up this slowly+static const double kClockSnapMs = 250, kClockPull = 0.08;+static NSString *const kBlurPath = @"filters.gaussianBlur.inputRadius";@interface CAFilter : NSObject+ (instancetype)filterWithType:(NSString *)type;@@ -26,11 +29,15 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect f@interface SGKaraokeWordView : UIView@property (nonatomic, readonly) SGKaraokeWord *word;@property (nonatomic, readonly) UILabel *lit;-@property (nonatomic) CGFloat progress;+@property (nonatomic) CGFloat offset; // where the word starts along its line, rows laid end to end+- (void)fillTo:(CGFloat)cursor;+- (void)floatAt:(double)ms;+- (void)settle;@end@implementation SGKaraokeWordView {CAGradientLayer *_fill;+ CGFloat _filled, _lift;}- (instancetype)initWithWord:(SGKaraokeWord *)word font:(UIFont *)font {@@ -49,21 +56,37 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect f_fill.startPoint = CGPointMake(0, 0.5);_fill.endPoint = CGPointMake(1, 0.5);_lit.layer.mask = _fill;- _progress = -1;- self.progress = 0;+ _filled = NAN;+ [self fillTo:-CGFLOAT_MAX];return self;}-// The mask is as wide as the word plus its edge and starts fully left of it, so 0 lights nothing.-- (void)setProgress:(CGFloat)progress {- if (progress == _progress) return;- _progress = progress;- CGFloat width = self.bounds.size.width + kFillEdge, height = self.bounds.size.height;+// The cursor is in line units and the feathered edge is centred on it, so the edge runs on through+// the space into the next word instead of starting over at each one.+- (void)fillTo:(CGFloat)cursor {+ CGFloat width = self.bounds.size.width, height = self.bounds.size.height;+ CGFloat local = MAX(-kFillEdge / 2, MIN(width + kFillEdge / 2, cursor - _offset));+ if (local == _filled) return;+ _filled = local;[CATransaction begin];[CATransaction setDisableActions:YES];- _fill.frame = CGRectMake(progress * width - width, -height / 2, width, height * 2);+ _fill.frame = CGRectMake(local - kFillEdge / 2 - width, -height / 2, width + kFillEdge, height * 2);[CATransaction commit];- self.transform = CGAffineTransformMakeTranslation(0, -kLift * MIN(1, progress * 2));+}++// Rises like a critically damped spring let go as the word starts: no jolt, a long soft landing.+// x = 5 at the end of the word is 96 % of the way up.+- (void)floatAt:(double)ms {+ double x = MAX(0, ms - _word.start + kFloatLeadMs) / MAX(_word.end - _word.start, kFloatMinMs) * 5;+ CGFloat lift = kLift * (1 - (1 + x) * exp(-x));+ if (lift == _lift) return;+ _lift = lift;+ self.transform = CGAffineTransformMakeTranslation(0, -lift);+}++- (void)settle {+ _lift = 0;+ self.transform = CGAffineTransformIdentity;}@end@@ -74,12 +97,18 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect f@property (nonatomic, readonly) SGKaraokeLine *line;@property (nonatomic) BOOL active;@property (nonatomic) CGFloat blur;-- (void)showTime:(NSInteger)ms;+- (void)showTime:(double)ms;@end+typedef struct {+ double ms, x, slope;+} SGSweepKnot;+@implementation SGKaraokeLineView {NSArray<SGKaraokeWordView *> *_words;NSUInteger _generation;+ SGSweepKnot *_knots;+ NSUInteger _knotCount;}- (instancetype)initWithLine:(SGKaraokeLine *)line width:(CGFloat)width font:(UIFont *)font {@@ -87,7 +116,7 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect fif (!self) return nil;_line = line;CGFloat space = ceil([@" " sizeWithAttributes:@{NSFontAttributeName: font}].width);- CGFloat row = ceil(font.lineHeight) - kRowTighten, x = 0, y = 0;+ CGFloat row = ceil(font.lineHeight) - kRowTighten, x = 0, y = 0, offset = 0;NSMutableArray<SGKaraokeWordView *> *words = [NSMutableArray array];for (SGKaraokeWord *word in line.words) {SGKaraokeWordView *view = [[SGKaraokeWordView alloc] initWithWord:word font:font];@@ -97,22 +126,86 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect fy += row;}view.center = CGPointMake(x + size.width / 2, y + size.height / 2);+ view.offset = offset;x += size.width + space;+ offset += size.width + space;[self addSubview:view];[words addObject:view];}_words = words;self.frame = CGRectMake(0, 0, width, y + ceil(font.lineHeight));+ // Scales toward its left edge, where the text is aligned.+ self.layer.anchorPoint = CGPointMake(0, 0.5);+ [self buildSweep];CAFilter *blur = [NSClassFromString(@"CAFilter") filterWithType:@"gaussianBlur"];if (blur) self.layer.filters = @[blur];return self;}+- (void)dealloc {+ free(_knots);+}++static double secant(SGSweepKnot *knots, NSUInteger i) {+ return (knots[i + 1].x - knots[i].x) / (knots[i + 1].ms - knots[i].ms);+}++// The fill cursor reaches each word's left edge as the word starts and clears the last word as it+// ends, on a monotone cubic through those points, so the pace changes between words without a kink.+- (void)buildSweep {+ _knots = calloc(_words.count + 1, sizeof(SGSweepKnot));+ if (!_words.count) return;+ for (NSUInteger i = 0; i <= _words.count; i++) {+ SGKaraokeWordView *word = _words[MIN(i, _words.count - 1)];+ double ms = i < _words.count ? word.word.start : word.word.end;+ double x = i == 0 ? -kFillEdge / 2 : i < _words.count ? word.offset : word.offset + word.bounds.size.width + kFillEdge / 2;+ if (_knotCount && ms <= _knots[_knotCount - 1].ms) {+ _knots[_knotCount - 1].x = x;+ continue;+ }+ _knots[_knotCount++] = (SGSweepKnot){ms, x, 0};+ }+ // Fritsch-Carlson slopes: capped so the cursor never runs backwards or overshoots a word.+ for (NSUInteger k = 0; k < _knotCount; k++) {+ BOOL first = k == 0, last = k + 1 == _knotCount;+ if (first && last) break;+ if (first || last) {+ _knots[k].slope = secant(_knots, first ? 0 : k - 1);+ continue;+ }+ double before = secant(_knots, k - 1), after = secant(_knots, k);+ double h0 = _knots[k].ms - _knots[k - 1].ms, h1 = _knots[k + 1].ms - _knots[k].ms;+ _knots[k].slope = MIN(MIN(2 * before, 2 * after), (before * h1 + after * h0) / (h0 + h1));+ }+}++- (CGFloat)cursorAt:(double)ms {+ if (!_knotCount) return -CGFLOAT_MAX;+ if (ms <= _knots[0].ms) return _knots[0].x;+ if (_knotCount == 1 || ms >= _knots[_knotCount - 1].ms) return _knots[_knotCount - 1].x;+ NSUInteger k = 0;+ while (ms >= _knots[k + 1].ms) k++;+ SGSweepKnot a = _knots[k], b = _knots[k + 1];+ double h = b.ms - a.ms, u = (ms - a.ms) / h, u2 = u * u, u3 = u2 * u;+ return (2 * u3 - 3 * u2 + 1) * a.x + (u3 - 2 * u2 + u) * h * a.slope+ + (3 * u2 - 2 * u3) * b.x + (u3 - u2) * h * b.slope;+}++// Eases from wherever the blur is on screen, so a line coming into focus sharpens instead of snapping.- (void)setBlur:(CGFloat)blur {if (blur == _blur) return;+ id shown = [self.layer.presentationLayer valueForKeyPath:kBlurPath];+ CGFloat from = [shown isKindOfClass:NSNumber.class] ? [shown doubleValue] : _blur;_blur = blur;- if (self.layer.filters) [self.layer setValue:@(blur) forKeyPath:@"filters.gaussianBlur.inputRadius"];+ if (!self.layer.filters) return;+ [self.layer setValue:@(blur) forKeyPath:kBlurPath];+ CABasicAnimation *ease = [CABasicAnimation animationWithKeyPath:kBlurPath];+ ease.fromValue = @(from);+ ease.toValue = @(blur);+ ease.duration = 0.6;+ ease.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];+ [self.layer addAnimation:ease forKey:@"blur"];}- (void)setActive:(BOOL)active {@@ -121,32 +214,37 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect fNSUInteger generation = ++_generation;if (active) {for (SGKaraokeWordView *word in _words) {+ [word.layer removeAllAnimations];[word.lit.layer removeAllAnimations];word.lit.alpha = 1;- word.progress = 0;+ [word fillTo:-CGFLOAT_MAX];+ [word settle];}return;}- // A sung line fades back to dim rather than dropping its fill at once.- [UIView animateWithDuration:0.4 animations:^{- for (SGKaraokeWordView *word in self->_words) {- word.lit.alpha = 0;- word.transform = CGAffineTransformIdentity;- }+ // A sung line fades back to dim rather than dropping its fill at once, and its words sink back+ // on a spring slow enough to still be seen doing it.+ [UIView animateWithDuration:0.9 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0+ options:UIViewAnimationOptionAllowUserInteraction+ animations:^{+ for (SGKaraokeWordView *word in self->_words) [word settle];+ } completion:nil];+ [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{+ for (SGKaraokeWordView *word in self->_words) word.lit.alpha = 0;} completion:^(BOOL finished) {if (generation != self->_generation) return;for (SGKaraokeWordView *word in self->_words) {- word.progress = 0;+ [word fillTo:-CGFLOAT_MAX];word.lit.alpha = 1;}}];}-- (void)showTime:(NSInteger)ms {+- (void)showTime:(double)ms {+ CGFloat cursor = [self cursorAt:ms];for (SGKaraokeWordView *word in _words) {- NSInteger start = word.word.start, end = word.word.end;- CGFloat progress = end > start ? (CGFloat)(ms - start) / (end - start) : (ms >= end ? 1 : 0);- word.progress = MAX(0, MIN(1, progress));+ [word fillTo:cursor];+ [word floatAt:ms];}}@@ -168,6 +266,9 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect fCGFloat _builtWidth;BOOL _showing;CAGradientLayer *_fade;+ double _clock;+ NSInteger _reported;+ CFTimeInterval _clockTime;}- (instancetype)initWithFrame:(CGRect)frame {@@ -240,6 +341,7 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect freturn;}_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];+ _link.preferredFrameRateRange = CAFrameRateRangeMake(80, 120, 120);[_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];}@@ -290,17 +392,24 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect fNSInteger distance = (NSInteger)i - _active;CGFloat y = height * kAnchor + tops[i].doubleValue - focusTop;CGRect frame = CGRectMake(kMargin, y, view.bounds.size.width, view.bounds.size.height);+ CGPoint center = CGPointMake(kMargin, CGRectGetMidY(frame));+ CGFloat scale = distance == 0 ? 1 : kDimScale;+ CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);view.blur = distance == 0 ? 0 : MIN(kMaxBlur, labs(distance) * kBlurPerLine);BOOL near = CGRectIntersectsRect(CGRectInset(self.bounds, 0, -height / 2), frame)|| CGRectIntersectsRect(CGRectInset(self.bounds, 0, -height / 2), view.frame);if (!animated || !near) {- view.frame = frame;+ view.center = center;+ view.transform = transform;continue;}NSTimeInterval delay = distance > 0 ? MIN(0.3, distance * 0.04) : 0;[UIView animateWithDuration:0.7 delay:delay usingSpringWithDamping:0.86 initialSpringVelocity:0options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction- animations:^{ view.frame = frame; } completion:nil];+ animations:^{+ view.center = center;+ view.transform = transform;+ } completion:nil];}// Room to scroll until the first line or the last one reaches the anchor.CGFloat lastTop = tops.lastObject.doubleValue;@@ -322,6 +431,22 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect f}}+// The player's position run on by the frame times the display will show and eased toward each new+// reading, so the sweep follows neither the callback's jitter nor the small jumps of the core's+// corrections. A seek or a new track is too far off to ease and is taken at once.+- (double)clockMs {+ NSInteger raw = SGKaraokePositionMs();+ CFTimeInterval shown = _link.targetTimestamp;+ BOOL running = raw != _reported; // a paused player reports the same position every frame+ double reported = raw + (running ? (shown - CACurrentMediaTime()) * 1000 : 0);+ double predicted = _clock + (running ? (shown - _clockTime) * 1000 : 0);+ double error = reported - predicted;+ _clock = raw < 0 || fabs(error) > kClockSnapMs ? reported : predicted + error * kClockPull;+ _reported = raw;+ _clockTime = shown;+ return _clock;+}+- (void)tick {NSString *track = SGKaraokePlayingTrack();if (!(track == _track || [track isEqualToString:_track])) {@@ -339,7 +464,8 @@ static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect f[self setShowing:_lines != nil];if (!_lineViews) return;- NSInteger now = SGKaraokePositionMs(), active = -1;+ double now = [self clockMs];+ NSInteger active = -1;for (NSUInteger i = 0; i < _lines.count && _lines[i].start <= now; i++) active = i;if (active != _active) {if (_active >= 0 && _active < (NSInteger)_lineViews.count) _lineViews[_active].active = NO;