Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: add Apple Music style word-by-word lyrics
Vojtěch Škopek2fba1df7e703
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex f5e59394..801f7b30 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -43,6 +43,8 @@ app:Hub JSON (AdHubs.x) and the feeds (Feeds.m), Premium pop-ups dropped (AdPopups.x), and the responsesrewritten on the way in (AdNetwork.x, Premium.m over the protobuf walker in Protobuf.m)ArtistBlock/ tracks by blocked artists skipped as they start (ArtistSkip.x), the list and the Blocked artists page under Player+ Karaoke/ Apple Music style lyrics on the full screen page: lines read from color-lyrics and the player's clock (KaraokeSource.x),+ words timed by estimate inside Spotify's line times (KaraokeTiming.m), drawn by KaraokeView.m over the page (KaraokePage.x)Onboarding/ the welcome tour over Home on the first launch (Onboarding.x, the pages in Tour.m), offered again from the Mod pageAbout/ the update check and the Mod page: the build, its updates, the links and the resetdiff --git a/tweak/Sources/Features/Karaoke/Karaoke.h b/tweak/Sources/Features/Karaoke/Karaoke.hnew file mode 100644index 00000000..3728c166--- /dev/null+++ b/tweak/Sources/Features/Karaoke/Karaoke.h@@ -0,0 +1,32 @@+// Apple Music style lyrics on the full screen lyrics page: the line being sung lights up word by+// word, the rest dim and blur with distance. Spotify only times whole lines, so the words inside a+// line are timed by an estimate (KaraokeTiming.m). The lines are read from the color-lyrics response+// as it arrives and the position from the player's state (KaraokeSource.x); a track without synced+// lyrics keeps Spotify's own page.+#import <UIKit/UIKit.h>++#define SGKeyKaraokeLyrics @"spotifyglass.karaokeLyrics"++// Times in milliseconds from the start of the track.+@interface SGKaraokeWord : NSObject+@property (nonatomic, copy) NSString *text;+@property (nonatomic) NSInteger start, end;+@end++@interface SGKaraokeLine : NSObject+@property (nonatomic, copy) NSArray<SGKaraokeWord *> *words;+@property (nonatomic) NSInteger start, end;+@end++// A color-lyrics body, protobuf or JSON, as timed lines; nil unless Spotify synced it by line.+NSArray<SGKaraokeLine *> *SGKaraokeLinesFromBody(NSData *body);++NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID); // nil until the lyrics came+NSString *SGKaraokePlayingTrack(void); // the base62 id, nil before the player reported+NSInteger SGKaraokePositionMs(void); // negative when unknown+void SGKaraokeSeek(NSInteger ms);++@interface SGKaraokeView : UIView+// Hides Spotify's own lyrics next to this view while it has lyrics to show, and brings them back when not.+- (void)syncSiblings;+@enddiff --git a/tweak/Sources/Features/Karaoke/KaraokePage.x b/tweak/Sources/Features/Karaoke/KaraokePage.xnew file mode 100644index 00000000..e362a855--- /dev/null+++ b/tweak/Sources/Features/Karaoke/KaraokePage.x@@ -0,0 +1,31 @@+// The karaoke view goes over the lyrics part of the full screen lyrics page (trees/lyrics.txt:+// Lyrics_FullscreenElementPageImpl.LyricsView, between the header and the controls), so Spotify's+// header, close button and controls stay working. It is laid over rather than swapped in: Spotify's+// list keeps running underneath and comes back the moment a track has no synced lyrics.+#import "Core/SGCore.h"+#import "Karaoke.h"++static char kKaraokeKey;++%hook _TtC32Lyrics_FullscreenElementPageImpl10LyricsView+- (void)layoutSubviews {+ %orig;+ UIView *host = (UIView *)self;+ SGKaraokeView *view = objc_getAssociatedObject(host, &kKaraokeKey);+ if (!view) {+ SGLog(@"karaoke: page opened, playing %@", SGKaraokePlayingTrack());+ view = [[SGKaraokeView alloc] initWithFrame:host.bounds];+ objc_setAssociatedObject(host, &kKaraokeKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ }+ if (view.superview == host) [host bringSubviewToFront:view];+ else [host addSubview:view];+ view.frame = host.bounds;+ [view syncSiblings];+}+%end++%ctor {+ if (!SGFlag(SGKeyKaraokeLyrics, NO)) return;+ %init;+ SGRequireClasses(@[@"_TtC32Lyrics_FullscreenElementPageImpl10LyricsView"]);+}diff --git a/tweak/Sources/Features/Karaoke/KaraokeSource.x b/tweak/Sources/Features/Karaoke/KaraokeSource.xnew file mode 100644index 00000000..502fccfc--- /dev/null+++ b/tweak/Sources/Features/Karaoke/KaraokeSource.x@@ -0,0 +1,114 @@+// Where the karaoke page gets its lines and its clock. The color-lyrics body is copied as it passes+// the same URLSession delegates AdBlock/AdNetwork.x reads, untouched, and kept per track, since the+// page may open long after the request finished. The clock is SPTEsperantoPlayer's state, asked for on+// every frame: the player is caught the first time the app asks it, and its position runs on by itself.+#import "Core/SGCore.h"+#import "Karaoke.h"+#import "Headers/SPTPlayer.h"++static const NSUInteger kKeptTracks = 40;++static NSMutableDictionary<NSString *, NSArray<SGKaraokeLine *> *> *sg_lyrics;+static __weak id sg_player;+static char kBodyKey;++static NSString *trackInURL(NSURL *url) {+ NSString *path = url.path;+ NSRange marker = [path rangeOfString:@"/color-lyrics/v2/track/"];+ if (marker.location == NSNotFound) return nil;+ NSString *track = [[path substringFromIndex:NSMaxRange(marker)] componentsSeparatedByString:@"/"].firstObject;+ return track.length ? track : nil;+}++static void received(NSURLSessionTask *task, NSData *data) {+ if (!trackInURL(task.currentRequest.URL)) return;+ NSMutableData *body = objc_getAssociatedObject(task, &kBodyKey);+ if (!body) objc_setAssociatedObject(task, &kBodyKey, (body = [NSMutableData data]), OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ [body appendData:data];+}++static void completed(NSURLSessionTask *task, NSError *error) {+ NSMutableData *body = objc_getAssociatedObject(task, &kBodyKey);+ if (!body) {+ NSString *path = task.currentRequest.URL.path;+ if ([path.lowercaseString containsString:@"lyrics"]) SGLog(@"karaoke: lyrics request not read: %@ (error %@)", path, error);+ return;+ }+ objc_setAssociatedObject(task, &kBodyKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ NSString *track = trackInURL(task.currentRequest.URL);+ if (error || !track) return;+ NSArray<SGKaraokeLine *> *lines = SGKaraokeLinesFromBody(body);+ SGLog(@"karaoke: lyrics for %@, %lu bytes, %lu synced lines", track, (unsigned long)body.length, (unsigned long)lines.count);+ if (!lines) return;+ dispatch_async(dispatch_get_main_queue(), ^{+ if (sg_lyrics.count >= kKeptTracks) [sg_lyrics removeAllObjects];+ sg_lyrics[track] = lines;+ });+}++NSArray<SGKaraokeLine *> *SGKaraokeLinesForTrack(NSString *trackID) {+ return trackID ? sg_lyrics[trackID] : nil;+}++static SPTPlayerState *playerState(void) {+ id player = sg_player;+ return [player respondsToSelector:@selector(state)] ? [(id<SPTPlayer>)player state] : nil;+}++NSString *SGKaraokePlayingTrack(void) {+ id uri = playerState().track.URI;+ NSString *text = [uri isKindOfClass:NSURL.class] ? ((NSURL *)uri).absoluteString : [uri description];+ return [text hasPrefix:@"spotify:track:"] ? [text substringFromIndex:@"spotify:track:".length] : nil;+}++NSInteger SGKaraokePositionMs(void) {+ SPTPlayerState *state = playerState();+ if (!state) return -1;+ return (NSInteger)((state.isPaused ? state.positionAsOfTimestamp : state.position) * 1000);+}++void SGKaraokeSeek(NSInteger ms) {+ id player = sg_player;+ if (![player respondsToSelector:@selector(seekTo:)]) return;+ [(id<SPTPlayer>)player seekTo:ms / 1000.0];+}++%hook SPTEsperantoPlayer+- (id)state {+ if (!sg_player) sg_player = self;+ return %orig;+}+%end++%hook SPTDataLoaderService+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data {+ received(task, data);+ %orig;+}+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {+ completed(task, error);+ %orig;+}+%end++%hook _TtC26Connectivity_HttpClientKit20HttpClientURLSession+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task didReceiveData:(NSData *)data {+ received(task, data);+ %orig;+}+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {+ completed(task, error);+ %orig;+}+%end++%ctor {+ if (!SGFlag(SGKeyKaraokeLyrics, NO)) return;+ sg_lyrics = [NSMutableDictionary dictionary];+ %init;+ SGLog(@"karaoke: on");+ SGRequireClasses(@[+ @"SPTEsperantoPlayer", @"SPTPlayerState",+ @"SPTDataLoaderService", @"_TtC26Connectivity_HttpClientKit20HttpClientURLSession",+ ]);+}diff --git a/tweak/Sources/Features/Karaoke/KaraokeTiming.m b/tweak/Sources/Features/Karaoke/KaraokeTiming.mnew file mode 100644index 00000000..9d601a78--- /dev/null+++ b/tweak/Sources/Features/Karaoke/KaraokeTiming.m@@ -0,0 +1,116 @@+#import "Karaoke.h"+#import "Features/AdBlock/Protobuf.h"++@implementation SGKaraokeWord+@end++@implementation SGKaraokeLine+@end++// A line is sung at about this pace and never takes longer than the gap to the next one. A slow+// line gets at least 60 % of its gap, but not more than 1.8x the estimate, so a long instrumental+// after it does not stretch its last word across the break.+static const NSInteger kLineBaseMs = 350, kMsPerLetter = 75;+static const double kMinGapShare = 0.6, kMaxStretch = 1.8;+// Added to every word's letters, for the breath between words and so a one-letter word still shows.+static const NSUInteger kWordWeight = 2;++static NSUInteger lettersIn(NSString *text) {+ NSUInteger count = 0;+ NSCharacterSet *letters = NSCharacterSet.alphanumericCharacterSet;+ for (NSUInteger i = 0; i < text.length; i++) {+ if ([letters characterIsMember:[text characterAtIndex:i]]) count++;+ }+ return count;+}++static BOOL isBreak(NSString *text) {+ NSString *trimmed = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];+ return trimmed.length == 0 || [trimmed isEqualToString:@"♪"];+}++// gap is the time to the next line, 0 for the last one.+static SGKaraokeLine *timedLine(NSString *text, NSInteger start, NSInteger gap) {+ NSMutableArray<NSString *> *tokens = [NSMutableArray array];+ for (NSString *token in [text componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]) {+ if (token.length) [tokens addObject:token];+ }+ NSUInteger weight = 0, letters = 0;+ for (NSString *token in tokens) {+ NSUInteger count = lettersIn(token);+ letters += count;+ weight += count + kWordWeight;+ }+ NSInteger estimate = kLineBaseMs + (NSInteger)letters * kMsPerLetter;+ NSInteger sung = estimate;+ if (gap > 0) {+ sung = MAX((NSInteger)(gap * kMinGapShare), estimate);+ sung = MIN(sung, (NSInteger)(estimate * kMaxStretch));+ sung = MIN(sung, gap);+ }++ NSMutableArray<SGKaraokeWord *> *words = [NSMutableArray array];+ double at = start;+ for (NSString *token in tokens) {+ SGKaraokeWord *word = [SGKaraokeWord new];+ word.text = token;+ word.start = (NSInteger)at;+ at += (double)sung * (lettersIn(token) + kWordWeight) / weight;+ word.end = (NSInteger)at;+ [words addObject:word];+ }+ SGKaraokeLine *line = [SGKaraokeLine new];+ line.words = words;+ line.start = start;+ line.end = start + sung;+ return line;+}++static NSArray<SGKaraokeLine *> *timedLines(NSArray<NSNumber *> *starts, NSArray<NSString *> *texts) {+ NSMutableArray<SGKaraokeLine *> *lines = [NSMutableArray array];+ for (NSUInteger i = 0; i < texts.count; i++) {+ if (isBreak(texts[i])) continue;+ NSInteger start = starts[i].integerValue;+ NSInteger gap = i + 1 < starts.count ? starts[i + 1].integerValue - start : 0;+ [lines addObject:timedLine(texts[i], start, MAX(gap, 0))];+ }+ return lines.count ? lines : nil;+}++// Lyrics { 1 data: { 1 time_synchronized, 2 repeated line: { 1 offset_ms, 2 content } }, 2 colors }+static NSArray<SGKaraokeLine *> *fromProtobuf(NSData *body) {+ SGPBField *data = SGPBFirst(SGPBParse(body), 1);+ if (data.wire != 2) return nil;+ NSArray<SGPBField *> *fields = SGPBParse(data.payload);+ if (!SGPBFirst(fields, 1).varint) return nil;+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];+ NSMutableArray<NSString *> *texts = [NSMutableArray array];+ for (SGPBField *field in fields) {+ if (field.number != 2 || field.wire != 2) continue;+ NSArray<SGPBField *> *line = SGPBParse(field.payload);+ [starts addObject:@((int32_t)SGPBFirst(line, 1).varint)];+ [texts addObject:SGPBText(SGPBFirst(line, 2)) ?: @""];+ }+ return timedLines(starts, texts);+}++// { "lyrics": { "syncType": "LINE_SYNCED", "lines": [ { "startTimeMs": "1234", "words": "..." } ] } }+static NSArray<SGKaraokeLine *> *fromJSON(NSData *body) {+ NSDictionary *root = [NSJSONSerialization JSONObjectWithData:body options:0 error:nil];+ NSDictionary *lyrics = [root isKindOfClass:NSDictionary.class] ? root[@"lyrics"] : nil;+ if (![lyrics isKindOfClass:NSDictionary.class] || ![lyrics[@"syncType"] isEqual:@"LINE_SYNCED"]) return nil;+ NSMutableArray<NSNumber *> *starts = [NSMutableArray array];+ NSMutableArray<NSString *> *texts = [NSMutableArray array];+ for (NSDictionary *line in lyrics[@"lines"]) {+ if (![line isKindOfClass:NSDictionary.class]) continue;+ id words = line[@"words"];+ [starts addObject:@([line[@"startTimeMs"] integerValue])];+ [texts addObject:[words isKindOfClass:NSString.class] ? words : @""];+ }+ return timedLines(starts, texts);+}++NSArray<SGKaraokeLine *> *SGKaraokeLinesFromBody(NSData *body) {+ if (!body.length) return nil;+ return ((const uint8_t *)body.bytes)[0] == '{' ? fromJSON(body) : fromProtobuf(body);+}diff --git a/tweak/Sources/Features/Karaoke/KaraokeView.m b/tweak/Sources/Features/Karaoke/KaraokeView.mnew file mode 100644index 00000000..b56e9f86--- /dev/null+++ b/tweak/Sources/Features/Karaoke/KaraokeView.m@@ -0,0 +1,300 @@+#import "Core/SGCore.h"+#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 kAnchor = 0.28; // where the sung line rests, as a share of the height+static const CGFloat kEdgeFade = 0.1; // the lines fade out over this share at the top and bottom+static const CGFloat kBlurPerLine = 1.4, kMaxBlur = 6;++@interface CAFilter : NSObject++ (instancetype)filterWithType:(NSString *)type;+@end++static UILabel *wordLabel(NSString *text, UIFont *font, UIColor *color, CGRect frame) {+ UILabel *label = [[UILabel alloc] initWithFrame:frame];+ label.text = text;+ label.font = font;+ label.textColor = color;+ return label;+}++#pragma mark - a word++// The word twice: dim underneath, white on top behind a mask whose feathered edge slides across it.+@interface SGKaraokeWordView : UIView+@property (nonatomic, readonly) SGKaraokeWord *word;+@property (nonatomic, readonly) UILabel *lit;+@property (nonatomic) CGFloat progress;+@end++@implementation SGKaraokeWordView {+ CAGradientLayer *_fill;+}++- (instancetype)initWithWord:(SGKaraokeWord *)word font:(UIFont *)font {+ CGSize size = [word.text sizeWithAttributes:@{NSFontAttributeName: font}];+ self = [super initWithFrame:CGRectMake(0, 0, ceil(size.width), ceil(font.lineHeight))];+ if (!self) return nil;+ _word = word;+ [self addSubview:wordLabel(word.text, font, [UIColor colorWithWhite:1 alpha:kDimAlpha], self.bounds)];+ _lit = wordLabel(word.text, font, UIColor.whiteColor, self.bounds);+ [self addSubview:_lit];++ CGFloat width = self.bounds.size.width + kFillEdge;+ _fill = [CAGradientLayer layer];+ _fill.colors = @[(id)UIColor.whiteColor.CGColor, (id)UIColor.whiteColor.CGColor, (id)UIColor.clearColor.CGColor];+ _fill.locations = @[@0, @((width - kFillEdge) / width), @1];+ _fill.startPoint = CGPointMake(0, 0.5);+ _fill.endPoint = CGPointMake(1, 0.5);+ _lit.layer.mask = _fill;+ _progress = -1;+ self.progress = 0;+ 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;+ [CATransaction begin];+ [CATransaction setDisableActions:YES];+ _fill.frame = CGRectMake(progress * width - width, -height / 2, width, height * 2);+ [CATransaction commit];+ self.transform = CGAffineTransformMakeTranslation(0, -kLift * MIN(1, progress * 2));+}++@end++#pragma mark - a line++@interface SGKaraokeLineView : UIView+@property (nonatomic, readonly) SGKaraokeLine *line;+@property (nonatomic) BOOL active;+@property (nonatomic) CGFloat blur;+- (void)showTime:(NSInteger)ms;+@end++@implementation SGKaraokeLineView {+ NSArray<SGKaraokeWordView *> *_words;+ NSUInteger _generation;+}++- (instancetype)initWithLine:(SGKaraokeLine *)line width:(CGFloat)width font:(UIFont *)font {+ self = [super initWithFrame:CGRectZero];+ if (!self) return nil;+ _line = line;+ CGFloat space = ceil([@" " sizeWithAttributes:@{NSFontAttributeName: font}].width);+ CGFloat row = ceil(font.lineHeight) - kRowTighten, x = 0, y = 0;+ NSMutableArray<SGKaraokeWordView *> *words = [NSMutableArray array];+ for (SGKaraokeWord *word in line.words) {+ SGKaraokeWordView *view = [[SGKaraokeWordView alloc] initWithWord:word font:font];+ CGSize size = view.bounds.size;+ if (x > 0 && x + size.width > width) {+ x = 0;+ y += row;+ }+ view.center = CGPointMake(x + size.width / 2, y + size.height / 2);+ x += size.width + space;+ [self addSubview:view];+ [words addObject:view];+ }+ _words = words;+ self.frame = CGRectMake(0, 0, width, y + ceil(font.lineHeight));++ CAFilter *blur = [NSClassFromString(@"CAFilter") filterWithType:@"gaussianBlur"];+ if (blur) self.layer.filters = @[blur];+ return self;+}++- (void)setBlur:(CGFloat)blur {+ if (blur == _blur) return;+ _blur = blur;+ if (self.layer.filters) [self.layer setValue:@(blur) forKeyPath:@"filters.gaussianBlur.inputRadius"];+}++- (void)setActive:(BOOL)active {+ if (active == _active) return;+ _active = active;+ NSUInteger generation = ++_generation;+ if (active) {+ for (SGKaraokeWordView *word in _words) {+ [word.lit.layer removeAllAnimations];+ word.lit.alpha = 1;+ word.progress = 0;+ }+ 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;+ }+ } completion:^(BOOL finished) {+ if (generation != self->_generation) return;+ for (SGKaraokeWordView *word in self->_words) {+ word.progress = 0;+ word.lit.alpha = 1;+ }+ }];+}++- (void)showTime:(NSInteger)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));+ }+}++@end++#pragma mark - the page++@implementation SGKaraokeView {+ CADisplayLink *_link;+ NSString *_track;+ NSArray<SGKaraokeLine *> *_lines;+ NSArray<SGKaraokeLineView *> *_lineViews;+ NSInteger _active;+ CGFloat _builtWidth;+ BOOL _showing;+ CAGradientLayer *_fade;+}++- (instancetype)initWithFrame:(CGRect)frame {+ self = [super initWithFrame:frame];+ if (!self) return nil;+ self.hidden = YES;+ _active = -1;+ _fade = [CAGradientLayer layer];+ _fade.colors = @[(id)UIColor.clearColor.CGColor, (id)UIColor.whiteColor.CGColor,+ (id)UIColor.whiteColor.CGColor, (id)UIColor.clearColor.CGColor];+ _fade.locations = @[@0, @(kEdgeFade), @(1 - kEdgeFade), @1];+ self.layer.mask = _fade;+ [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]];+ return self;+}++- (void)tapped:(UITapGestureRecognizer *)tap {+ CGPoint point = [tap locationInView:self];+ for (SGKaraokeLineView *view in _lineViews) {+ if (!CGRectContainsPoint(CGRectInset(view.frame, -kMargin, -kLineGap / 2), point)) continue;+ SGKaraokeSeek(view.line.start);+ return;+ }+}++- (void)didMoveToWindow {+ [super didMoveToWindow];+ [_link invalidate];+ _link = nil;+ if (!self.window) return;+ _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];+ [_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];+}++- (void)layoutSubviews {+ [super layoutSubviews];+ [CATransaction begin];+ [CATransaction setDisableActions:YES];+ _fade.frame = self.bounds;+ [CATransaction commit];+ if (_lines && self.bounds.size.width != _builtWidth) [self rebuild];+}++- (void)rebuild {+ for (UIView *view in _lineViews) [view removeFromSuperview];+ _lineViews = nil;+ _active = -1;+ _builtWidth = self.bounds.size.width;+ CGFloat width = _builtWidth - 2 * kMargin;+ if (width <= 0) return;+ UIFont *font = [UIFont systemFontOfSize:kFontSize weight:UIFontWeightBold];+ NSMutableArray<SGKaraokeLineView *> *views = [NSMutableArray array];+ for (SGKaraokeLine *line in _lines) {+ SGKaraokeLineView *view = [[SGKaraokeLineView alloc] initWithLine:line width:width font:font];+ [self addSubview:view];+ [views addObject:view];+ }+ _lineViews = views;+ [self placeLinesAnimated:NO];+}++// The sung line rests at the anchor and the rest stack around it; lines below it follow a beat+// later, the further down the later, as Apple Music's do.+- (void)placeLinesAnimated:(BOOL)animated {+ NSInteger focus = MAX(_active, 0);+ CGFloat height = self.bounds.size.height, top = 0, focusTop = 0;+ NSMutableArray<NSNumber *> *tops = [NSMutableArray array];+ for (NSUInteger i = 0; i < _lineViews.count; i++) {+ if ((NSInteger)i == focus) focusTop = top;+ [tops addObject:@(top)];+ top += _lineViews[i].bounds.size.height + kLineGap;+ }+ for (NSUInteger i = 0; i < _lineViews.count; i++) {+ SGKaraokeLineView *view = _lineViews[i];+ NSInteger 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);+ 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;+ continue;+ }+ NSTimeInterval delay = distance > 0 ? MIN(0.3, distance * 0.04) : 0;+ [UIView animateWithDuration:0.7 delay:delay usingSpringWithDamping:0.86 initialSpringVelocity:0+ options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction+ animations:^{ view.frame = frame; } completion:nil];+ }+}++- (void)syncSiblings {+ for (UIView *sibling in self.superview.subviews) {+ if (sibling != self && _showing) sibling.alpha = 0;+ }+}++- (void)setShowing:(BOOL)showing {+ if (showing == _showing) return;+ _showing = showing;+ self.hidden = !showing;+ for (UIView *sibling in self.superview.subviews) {+ if (sibling != self) sibling.alpha = showing ? 0 : 1;+ }+}++- (void)tick {+ NSString *track = SGKaraokePlayingTrack();+ if (!(track == _track || [track isEqualToString:_track])) {+ SGLog(@"karaoke: page shows track %@, lyrics %@", track, SGKaraokeLinesForTrack(track) ? @"captured" : @"not captured yet");+ _track = track;+ _lines = nil;+ _builtWidth = 0;+ for (UIView *view in _lineViews) [view removeFromSuperview];+ _lineViews = nil;+ }+ if (!_lines && track && (_lines = SGKaraokeLinesForTrack(track))) {+ SGLog(@"karaoke: showing %lu lines of %@", (unsigned long)_lines.count, track);+ [self setNeedsLayout];+ }+ [self setShowing:_lines != nil];+ if (!_lineViews) return;++ NSInteger now = SGKaraokePositionMs(), 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;+ if (active >= 0) _lineViews[active].active = YES;+ BOOL jump = labs(active - _active) > 2; // a seek, not the song moving on+ _active = active;+ [self placeLinesAnimated:!jump];+ }+ if (active >= 0) [_lineViews[active] showTime:now];+}++@enddiff --git a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.mindex 8dba0bc2..b13d71a8 100644--- a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m+++ b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m@@ -4,9 +4,13 @@#import "Features/Flags/Flags.h"#import "Features/Gestures/Gestures.h"#import "Features/ArtistBlock/ArtistBlock.h"+#import "Features/Karaoke/Karaoke.h"static UIViewController *lyricsPage(void) {return [[SGModPage alloc] initWithTitle:@"Lyrics" intro:SGRestartNote sections:@[+ SGSection(nil, @[+ SGOptionRow(@"Apple Music style", @"Word by word on the full screen page; timing inside a line is estimated", SGKeyKaraokeLyrics),+ ]),SGSection(@"Spotify's flags", @[SGFlagRow(@"Translations in the player", @"ios-feature-lyrics.enable_lyrics_multilanguage_npv"),SGFlagRow(@"Translations full screen", @"ios-feature-lyrics.enable_lyrics_multilanguage_fullscreen"),diff --git a/tweak/Sources/Headers/SPTPlayer.h b/tweak/Sources/Headers/SPTPlayer.hindex c0024f53..537d5477 100644--- a/tweak/Sources/Headers/SPTPlayer.h+++ b/tweak/Sources/Headers/SPTPlayer.h@@ -13,9 +13,14 @@@interface SPTPlayerState : NSObject@property (nonatomic, readonly) SPTPlayerTrack *track;+@property (nonatomic, readonly) BOOL isPaused;+// Seconds. position runs on from positionAsOfTimestamp by the time elapsed since the state was made.+@property (nonatomic, readonly) double position;+@property (nonatomic, readonly) double positionAsOfTimestamp;@end@protocol SPTPlayer <NSObject>@property (nonatomic, readonly) SPTPlayerState *state;- (void)skipToNextTrack;+- (void)seekTo:(double)seconds;@end