[Feature] Add Duration Match Algorithm

Added duration matching option to track matching algorithms
This commit is contained in:
Demizo 2022-09-01 21:53:48 -05:00
parent c3bf5119eb
commit a550d21b9a
3 changed files with 26 additions and 2 deletions

View File

@ -278,7 +278,11 @@ class Settings extends HookConsumerWidget {
value: SpotubeTrackMatchAlgorithm.popular,
),
DropdownMenuItem(
child: Text("YouTube's choice is my choice"),
child: Text("Match Song Duration"),
value: SpotubeTrackMatchAlgorithm.duration,
),
DropdownMenuItem(
child: Text("YouTube's Top choice"),
value: SpotubeTrackMatchAlgorithm.youtube,
),
],

View File

@ -10,6 +10,8 @@ enum SpotubeTrackMatchAlgorithm {
popular,
// selects the most popular one from the author of the track
authenticPopular,
// selects song that most closely matches the actual song's duration
duration,
}
class SpotubeTrack extends Track {

View File

@ -375,7 +375,25 @@ class Playback extends PersistedChangeNotifier {
} else {
VideoSearchList videos =
await raceMultiple(() => youtube.search.search(queryString));
if (matchAlgorithm != SpotubeTrackMatchAlgorithm.youtube) {
if (matchAlgorithm == SpotubeTrackMatchAlgorithm.duration) {
//Actual duration of desired song
int targetDuration = track.duration!.inSeconds;
//start with the first result
Video bestVideoMatch = videos[0];
int minDurationDifference =
(targetDuration - videos[0].duration!.inSeconds).abs();
//Check if any other results are closer to the actual song duration and prefer those
for (int i = 1; i < videos.length; i++) {
int durationDifference =
(targetDuration - videos[i].duration!.inSeconds).abs();
if (durationDifference < minDurationDifference) {
minDurationDifference = durationDifference;
bestVideoMatch = videos[i];
}
}
ytVideo = bestVideoMatch;
} else if (matchAlgorithm != SpotubeTrackMatchAlgorithm.youtube) {
List<Map> ratedRankedVideos = videos
.map((video) {
// the find should be lazy thus everything case insensitive