aboutsummaryrefslogtreecommitdiff
path: root/app/js/services.js
blob: f40188df7e41568331f6f8af1f40156e345ed5af (plain) (blame)
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
'use strict';

/* Services */

app.service('pathService', function() {
    var self = {
        dist: function(point1, point2) {
            var dx = point1.x - point2.x;
            var dy = point1.y - point2.y;
            return Math.sqrt(dx * dx + dy * dy);
        },
        computePlaylist: function(songList, constrainList, duration) {
            //compute distances from the constrain path
            
            var songListCopy = songList.slice(0);
            var maxDistOverall = Number.MIN_VALUE;
            angular.forEach(songListCopy, function(song){
                song.selected = false;
                song.minDist = Number.MAX_VALUE;
                for(var i=0; i<constrainList.length;i++){
                    var point = constrainList[i];
                    var d = self.dist(song, point);
                    if(d < song.minDist){
                        song.minDist = d;
                        song.closest = point;
                        song.closestIndex = i;
                    }
                }
                if(song.minDist > maxDistOverall){
                    maxDistOverall = song.minDist;
                }
            });
            //select the songs
            var selection = [];
            var tuning = .5;
            angular.forEach(songListCopy, function(song){
                song.rand = (tuning + tuning*Math.random())*song.minDist;
            });
            songListCopy.sort(function(a,b){return a.rand-b.rand;});
            //take from the sorted list util we reach the desired duration
            while(duration > 0 && songListCopy.length > 0){
                var removed = songListCopy.splice(0, 1);
                selection.push(removed[0]);
                duration -= 10;
                removed[0].selected = true;
            }
            
            selection.sort(function(a,b){ return a.closestIndex-b.closestIndex;});

            //clean points
            angular.forEach(songList, function(song) {
                delete song.minDist;
                delete song.closest;
                delete song.closestIndex;
                delete song.rand;
            });

            return selection;
        }
    };
    return self;
});