-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylist-remix-engine.js
More file actions
123 lines (99 loc) · 2.09 KB
/
Copy pathplaylist-remix-engine.js
File metadata and controls
123 lines (99 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const playlists = [
[
{
trackId: "trk101",
artist: "Velvet Comet",
title: "Crimson Afterglow",
votes: 5,
bpm: 122
},
{
trackId: "trk102",
artist: "Neon Harbor",
title: "Static Horizon",
votes: 2,
bpm: 108
},
{
trackId: "trk103",
artist: "Lunar Arcade",
title: "Midnight Frequency",
votes: 4,
bpm: 128
}
],
[
{
trackId: "trk201",
artist: "Solar Echo",
title: "Glass Skyline",
votes: 3,
bpm: 115
},
{
trackId: "trk202",
artist: "Velvet Comet",
title: "Satellite Hearts",
votes: 6,
bpm: 124
}
]
];
function flattenPlaylists(playlists) {
if (!Array.isArray(playlists)) {
return [];
}
const result = [];
playlists.forEach((playlist, playlistIndex) => {
playlist.forEach((track, trackIndex) => {
result.push({
...track,
source: [playlistIndex, trackIndex]
});
});
});
return result;
}
function scoreTracks(tracks) {
return tracks.map(track => ({
...track,
score: track.votes * 10 - Math.abs(track.bpm - 120)
}));
}
function dedupeTracks(tracks) {
const seen = new Set();
return tracks.filter(track => {
if (seen.has(track.trackId)) {
return false;
}
seen.add(track.trackId);
return true;
});
}
function enforceArtistQuota(tracks, maxOccurrences) {
const artistCount = new Map();
return tracks.filter(track => {
const count = artistCount.get(track.artist) || 0;
if (count >= maxOccurrences) {
return false;
}
artistCount.set(track.artist, count + 1);
return true;
});
}
function buildSchedule(tracks) {
return tracks.map((track, index) => ({
slot: index + 1,
trackId: track.trackId
}));
}
function remixPlaylist(playlists, maxOccurrences) {
const flattened = flattenPlaylists(playlists);
const scored = scoreTracks(flattened);
const deduped = dedupeTracks(scored);
const quotaEnforced = enforceArtistQuota(
deduped,
maxOccurrences
);
return buildSchedule(quotaEnforced);
}