-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlautfmadmin_shuffle.php
644 lines (550 loc) · 18.7 KB
/
lautfmadmin_shuffle.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php
// Gewichtungen fuer Tags
class Weight {
const LOW_MAX = -3;
const LOW_MEDIUM = -2;
const LOW_MIN = -1;
const NORMAL = 0;
const HIGH_MIN = 1;
const HIGH_MEDIUM = 2;
const HIGH_MAX = 3;
}
// Strategie fuer Wortbeitraege
class WordDistribution {
const RANDOM = 0; // Beitraege shuffeln
const PROTECT = 1; // Beitraege auf urspruenglicher Position belassen
const LINK_NEXT = 2; // Beitraege an nachfolgenden Titel koppeln
const LINK_PREVIOUS = 3; // Beitraege an vorhergehenden Titel koppeln
}
class JingleOrder {
const PRESERVE = 0; // Originalreihenfolge beibehalten
const SHUFFLE_REPEAT = 1; // Zufaellige Reihenfolge mit Wiederholung nach letztem Jingle
const SHUFFLE = 2; // Zufaellige Reinehfolge, erneutest Shuffeln nach letztem Jingle
}
// Default- oder Playlisteinstellungen
class ShuffleSettings {
public $shuffleJingles = JingleOrder::PRESERVE;
public $jingleInterval = 0;
public $protectFirstJingle = 0;
public $offsetFirstJingle = -1; // -1 = zufaellig zwischen 0 und jingleInterval, sonst an vorgegebener Position
public $wordDistributionStrategy = WordDistribution::RANDOM;
public $maxTracksPerArtist = 0; // 0 = Abhaengig von Laenge
public $weights = array(); // Schluessel = Tagname, Wert = Weight
function copy() {
$c = new ShuffleSettings();
$c->shuffleJingles = $this->shuffleJingles;
$c->offsetFirstJingle = $this->offsetFirstJingle;
$c->protectFirstJingle = $this->protectFirstJingle;
$c->jingleInterval = $this->jingleInterval;
$c->wordDistributionStrategy = $this->wordDistributionStrategy;
$c->maxTracksPerArtist = $this->maxTracksPerArtist;
$c->weights = $this->weights;
return $c;
}
}
// Normalisiert einen Artist
// - Kleinbuchstaben
// - Abeschneiden nach definierten Trennern wie " feat"
class ArtistNormalizer {
public $separators = array(" feat");
function normalize($artist) {
$artist = strtolower($artist);
for($i = 0; $i < count($this->separators); $i++) {
$pos = strpos($artist, $this->separators[$i]);
if($pos > 0) {
return trim(substr($artist, 0, $pos));
}
}
return $artist;
}
}
// Interface fuer Trackfilterung
interface TrackFilter {
// Track-IDs die dieser Filter akzeptiert
public function getTrackIds($client, $stationName);
}
// Tracks einer Playlist
class PlaylistTrackFilter implements TrackFilter {
public $playlistName;
function __construct($playlistName) {
$this->playlistName = $playlistName;
}
public function getTrackIds($client, $stationName) {
$trackIds = array();
$playlist = $client->getPlaylistByName($stationName, $this->playlistName);
if(!is_null($playlist)) {
// Wir gehen direkt runter auf die Radioadmin-API - ist effizienter
$path = $client->getStationPath($stationName, "/playlists/".$playlist->id);
$raw = laut_get($client->origin, $client->token, $path);
$response = json_decode($raw);
for($i = 0; $i < count($response->{'entries'}); $i++) {
array_push($trackIds, $response->{'entries'}[$i]->{'track_id'});
}
}
return $trackIds;
}
}
// Filter: Gespielte Tracks
class PlayedTracksFilter implements TrackFilter {
public $days = 0; // Zahl der Tage fuer die die Trackstatistik abgerufen wird
public $minPlays = 1; // Mindestanzahl der Plays fuer einen Track
public $minHour = 0; // Startzeit (min)
public $maxHour = 23; // Startzeit (max)
function __construct($days, $minHour = 0, $maxHour = 23, $minPlays = 1) {
$this->days = $days;
$this->minHour = $minHour;
$this->maxHour = $maxHour;
$this->minPlays = $minPlays;
}
public function getTrackIds($client, $stationName) {
$trackIds = array();
$cache = TrackStatisticsCache::Instance();
$entries = $cache->getTrackStatistics($client, $stationName, $this->days);
$trackCounts = array();
for($i = 0; $i < count($entries); $i++) {
if($entries[$i]->id > 0) {
$hour = date("G", $entries[$i]->start);
if($hour >= $this->minHour && $hour <= $this->maxHour) {
if($this->minPlays < 2) {
array_push($trackIds, $entries[$i]->id);
}
else {
$trackCounts[$entries[$i]->id]++;
}
}
}
}
if($this->minPlays > 1) {
foreach ($trackCounts as $trackId => $count) {
if($count >= $this->minPlays) {
array_push($trackIds, $trackId);
}
}
}
return $trackIds;
}
}
class PlaylistShuffler {
public $defaultSettings;
public $artistNormalizer;
public $trackFilters = array(); // key = name, value = TrackFilter
private $client;
private $station;
private $playlistSettings = array();
private $tags = array();
// Konstructor: LautfmAdmin, Stationsname
function __construct($client, $station) {
$this->defaultSettings = new ShuffleSettings();
$this->artistNormalizer = new ArtistNormalizer();
$this->client = $client;
$this->station = $station;
}
// public
function registerPlaylistSettings($playlistName, $settings) {
$this->playlistSettings[$playlistName] = $settings;
}
function shuffle($playlist, $targetLength = 0) {
$settings = $this->defaultSettings;
if(array_key_exists($playlist->name, $this->playlistSettings)) {
$settings = $this->playlistSettings[$playlist->name];
}
$job = $this->initializeJob($playlist, $settings);
$targetLengthSec = $playlist->duration;
if($targetLength > 0 && $targetLength * 60 < $playlist->duration) {
$targetLengthMin = $targetLength * 60;
$targetLengthSec = $targetLengthMin * 60;
$this->preselectTracks($job, $settings, $targetLengthMin);
}
else {
$job->useAllTracks();
}
$numSegments = $job->getMaxArtistTracksToUse() * 2;
$segements = array();
for($i = 0; $i < $numSegments; $i++) {
$segments[$i] = new Segment();
}
for($i = 0; $i < count($job->artists); $i++) {
$numArtistTracks = $job->artists[$i]->getNumTracksToUse();
if($numArtistTracks == 0) {
continue;
}
$artistSegments = floor($numSegments / $numArtistTracks);
// find least filled segment that can act as first segment for this artist
$currentSegment = 0;
$minLength = $segments[0]->length;
for($s = 0; $s < $artistSegments; $s++) {
if($segments[$s]->length < $minLength) {
$currentSegment = $s;
$minLength = $segments[$s]->length;
}
}
// assign tracks of artist to segment
$tracksToUse = $job->artists[$i]->getTracksToUse();
for($t = 0; $t < count($tracksToUse); $t++) {
$segments[$currentSegment]->add($tracksToUse[$t]->track);
$currentSegment = $currentSegment + $artistSegments;
}
}
// build final playlist
$tracks = array();
for($s = 0; $s < count($segments); $s++) {
$segmentTracks = $segments[$s]->tracks;
$segmentTracks = shuffleArray($segmentTracks); // avoid having artists in same order within segments
for($t = 0; $t < count($segmentTracks); $t++) {
// TODO check for coupled tracks / successor coupling
array_push($tracks, $segmentTracks[$t]);
// TODO check for coupled tracks / predecessor coupling
}
}
$tracks = $this->insertJingles($job, $tracks, $settings, $targetLengthSec);
$tracks = $this->appendUnused($job, $tracks);
$tracks = $this->insertLinkedTracks($job, $tracks,$settings);
$tracks = $this->insertProtectedTracks($job, $tracks);
return $tracks;
}
// private
private function initializeJob($playlist, $settings) {
$artists = array();
$artist = null;
$job = new ShuffleJob();
$knownJingles = array();
for($i = 0; $i < count($playlist->tracks); $i++) {
$track = $playlist->tracks[$i];
if($track->type == "jingle") {
$key = "J".$track->id;
if($i == 0 && $settings->protectFirstJingle == 1) {
$job->startJingle = $track;
$knownJingles[$key] = 1;
}
else {
if(!array_key_exists($key, $knownJingles)) {
array_push($job->jingles, $track);
$knownJingles[$key] = 1;
}
}
}
else if($track->type == "moderation" && $settings->wordDistributionStrategy != WordDistribution::RANDOM) {
if($settings->wordDistributionStrategy == WordDistribution::PROTECT) {
$job->protectedTracks[$i] = $track;
}
else if($settings->wordDistributionStrategy == WordDistribution::LINK_NEXT && $i < count($playlist->tracks) - 1) {
$nextTrack = $playlist->tracks[$i + 1];
$job->linkedTracks[$nextTrack->id] = $track;
}
else if($settings->wordDistributionStrategy == WordDistribution::LINK_PREVIOUS && $i > 0) {
$prevTrack = $playlist->tracks[$i - 1];
$job->linkedTracks[$prevTrack->id] = $track;
}
}
else {
$artistName = $this->artistNormalizer->normalize($track->artist);
if(array_key_exists($artistName, $artists)) {
$artist = $artists[$artistName];
}
else {
$artist = new ShuffleArtist();
$artists[$artistName] = $artist;
array_push($job->artists, $artist);
}
$candidate = new ShuffleCandidate();
$candidate->track = $track;
$candidate->score = rand(100, 600);
array_push($artist->tracks, $candidate);
}
}
if(count($job->jingles) > 0 && $settings->shuffleJingles > 0) {
$job->jingles = shuffleArray($job->jingles);
}
$this->applyTagWeights($job, $settings);
// sort artist track lists
for($i = 0; $i < count($job->artists); $i++) {
usort($job->artists[$i]->tracks, "cmpScore");
$job->artists[$i]->score = $job->artists[$i]->tracks[0]->score;
}
usort($job->artists, "cmpScore");
return $job;
}
private function applyTagWeights($job, $settings) {
// initialize associative array with track weights
$weights = array();
foreach ($settings->weights as $tagName => $weight) {
$trackIds = $this->getTag($tagName);
for($i = 0; $i < count($trackIds); $i++) {
if(array_key_exists($trackIds[$i], $weights)) {
$old = $weights[$trackIds[$i]];
if(($weight > 0 && $old > 0 && $weight > $old) || ($weight < 0 && $old < 0 && $weight < $old)) {
$weights[$trackIds[$i]] = $weight;
}
else if(($old > 0 && $weight < 0) || ($weight > 0 && $old < 0)) {
$weights[$trackIds[$i]] = $weight + $old;
}
}
else {
$weights[$trackIds[$i]] = $weight;
}
}
}
// increase / decrease track scores
for($i = 0; $i < count($job->artists); $i++) {
for($j = 0; $j < count($job->artists[$i]->tracks); $j++) {
$candidate = $job->artists[$i]->tracks[$j];
if(array_key_exists($candidate->track->id, $weights)) {
$weight = $weights[$candidate->track->id];
if($weight > 0) {
$p = (4 - $weight) / 4;
$candidate->score = $candidate->score * $p;
}
else {
$weight = abs($weight);
$p = 1 + $weight / 4;
$candidate->score = $candidate->score * $p;
}
}
}
}
}
private function preselectTracks($job, $settings, $targetLengthMin) {
$maxTracksPerArtist = floor($targetLengthMin / 60);
if($settings->maxTracksPerArtist > 0 && $settings->maxTracksPerArtist < $maxTracksPerArtist) {
$maxTracksPerArtist = $settings->maxTracksPerArtist;
}
$length = 0;
$candidates = array();
for($i = 0; $i < count($job->artists); $i++) {
for($j = 0; $j < $maxTracksPerArtist && $j < count($job->artists[$i]->tracks); $j++) {
array_push($candidates, $job->artists[$i]->tracks[$j]);
}
}
usort($candidates, "cmpScore");
$i = 0;
while($i < count($candidates) && $length < $targetLengthMin * 60) {
$candidates[$i]->use = 1;
$length += $candidates[$i]->track->duration;
$i++;
}
}
private function insertProtectedTracks($job, $tracks) {
if(count($job->protectedTracks) == 0) {
return $tracks;
}
$newTracks = array();
for($i = 0; $i < count($tracks); $i++) {
$key = count($newTracks);
if(array_key_exists($key, $job->protectedTracks)) {
array_push($newTracks, $job->protectedTracks[$key]);
}
array_push($newTracks, $tracks[$i]);
}
return $newTracks;
}
private function insertLinkedTracks($job, $tracks, $settings) {
if(count($job->linkedTracks) == 0) {
return $tracks;
}
$newTracks = array();
for($i = 0; $i < count($tracks); $i++) {
$key = $tracks[$i]->id;
if($settings->wordDistributionStrategy == WordDistribution::LINK_NEXT && array_key_exists($key, $job->linkedTracks)) {
array_push($newTracks, $job->linkedTracks[$key]);
}
array_push($newTracks, $tracks[$i]);
if($settings->wordDistributionStrategy == WordDistribution::LINK_PREVIOUS && array_key_exists($key, $job->linkedTracks)) {
array_push($newTracks, $job->linkedTracks[$key]);
}
}
return $newTracks;
}
private function insertJingles($job, $tracks, $settings, $playlistLength) {
if(count($job->jingles) == 0) {
if(isset($job->startJingle)) {
array_unshift($tracks, $job->startJingle);
}
return $tracks;
}
$timeNextJingle = 0;
$jingleOffset = 0;
$jingleInterval = $settings->jingleInterval;
if($jingleInterval == 0) {
$numJingles = isset($job->startJingle) ? count($job->jingles) + 1 : count($job->jingles);
$jingleInterval = floor(($playlistLength / $numJingles) / 60);
}
$newTracks = array();
if(isset($job->startJingle)) {
array_push($newTracks, $job->startJingle);
$timeNextJingle = $jingleInterval;
$jingleOffset = $jingleInterval;
}
else {
$jingleOffset = $settings->offsetFirstJingle == -1 ? rand(0, $jingleInterval) : $settings->offsetFirstJingle;
$timeNextJingle = $jingleOffset;
}
$jingleIdx = 0;
$jingleCnt = 0;
$currentTimeSec = 0;
for($i = 0; $i < count($tracks); $i++) {
if($currentTimeSec / 60 >= $timeNextJingle) {
array_push($newTracks, $job->jingles[$jingleIdx]);
$usedJingles[$jingleIdx] = 1;
$currentTimeSec += $job->jingles[$jingleIdx]->duration;
$jingleIdx++;
if($jingleIdx == count($job->jingles)) {
$jingleIdx = 0;
if(count($job->jingles) > 1 && $settings->shuffleJingles == JingleOrder::SHUFFLE) {
// all jingles used, shuffle again
$job->jingles = shuffleArray($job->jingles);
}
}
$jingleCnt++;
$timeNextJingle = $jingleCnt * $jingleInterval + $jingleOffset;
}
array_push($newTracks, $tracks[$i]);
$currentTimeSec += $tracks[$i]->duration;
}
// echo "$jingleCnt ".count($job->jingles)."\n";
if($jingleCnt >= count($job->jingles)) {
// all jingles used - clear array
$job->jingles = array();
}
else {
// remove used jingles
array_splice($job->jingles, 0, $jingleCnt);
}
return $newTracks;
}
private function appendUnused($job, $tracks) {
// append unused tracks
$offsets = array();
for($i = 0; $i < count($job->artists); $i++) {
$offsets[$i] = 999;
for($j = 0; $j < count($job->artists[$i]->tracks) && $offsets[$i] == 999; $j++) {
if($job->artists[$i]->tracks[$j]->use == 0) {
$offsets[$i] = $j;
}
}
}
$added = 1;
while($added > 0) {
$added = 0;
for($i = 0; $i < count($job->artists); $i++) {
if($offsets[$i] < count($job->artists[$i]->tracks)) {
array_push($tracks, $job->artists[$i]->tracks[$offsets[$i]]->track);
$offsets[$i]++;
$added++;
}
}
}
// append unused jingles
for($i = 0; $i < count($job->jingles); $i++) {
array_push($tracks, $job->jingles[$i]);
}
return $tracks;
}
// Get track ids for tag or filter. Uses caching.
private function getTag($tagName) {
if(array_key_exists($tagName, $this->tags)) {
return $this->tags[$tagName];
}
$trackIds = NULL;
if(array_key_exists($tagName, $this->trackFilters)) {
$trackIds = $this->trackFilters[$tagName]->getTrackIds($this->client, $this->station);
}
else {
$trackIds = $this->client->getTag($this->station, $tagName);
}
$this->tags[$tagName] = $trackIds;
return $trackIds;
}
}
// Internal stuff
class ShuffleJob {
public $artists = array();
public $jingles = array();
public $startJingle;
public $protectedTracks = array();
public $linkedTracks = array();
function getMaxArtistTracksToUse() {
$max = 0;
for($i = 0; $i < count($this->artists); $i++) {
$artistMax = $this->artists[$i]->getNumTracksToUse();
if($artistMax > $max) $max = $artistMax;
}
return $max;
}
function useAllTracks() {
for($i = 0; $i < count($this->artists); $i++) {
for($j = 0; $j < count($this->artists[$i]->tracks); $j++) {
$this->artists[$i]->tracks[$j]->use = 1;
}
}
}
}
class Segment {
public $tracks = array(); // as array of Track
public $length = 0;
function add($track) {
array_push($this->tracks, $track);
$this->length += $track->duration;
}
}
class ShuffleArtist {
public $tracks = array(); // as array of ShuffleCandiate
public $score; // score of best track
private $numTracksToUse = -1;
private $tracksToUse = array();
function getTracksToUse() {
if(count($this->tracksToUse) == 0) {
for($i = 0; $i < count($this->tracks); $i++) {
if($this->tracks[$i]->use == 1) {
array_push($this->tracksToUse, $this->tracks[$i]);
}
}
}
return $this->tracksToUse;
}
function getNumTracksToUse() {
return count($this->getTracksToUse());
}
}
class ShuffleCandidate {
public $track;
public $score;
public $use = 0; // whether or not to use this track in the start section of the playlist
}
// Simple cache for track statistics
class TrackStatisticsCache {
private $entries = array();
private $daysRetrieved = array();
public static function Instance()
{
static $inst = null;
if ($inst === null) {
$inst = new TrackStatisticsCache();
}
return $inst;
}
private function __construct()
{
}
function getTrackStatistics($client, $station, $days) {
if(!$this->daysRetrieved[$station] || $days > $this->daysRetrieved[$station]) {
$this->entries[$station] = $client->getTrackStatistics($station, $days);
$this->daysRetrieved[$station] = $days;
}
return $this->entries[$station];
}
}
function cmpScore($a, $b) {
if($a->score == $b->score) return 0;
return $a->score < $b->score ? -1 : 1;
}
// for some reason shuffle does not work on arrays of objects - use this as replacement
function shuffleArray($array) {
$newArray = array();
$keys = range(0, count($array) - 1);
shuffle($keys);
for($i = 0; $i < count($keys); $i++) {
array_push($newArray, $array[$keys[$i]]);
}
return $newArray;
}
?>