PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/flash/examples/js/step-by-section.js

http://echo-nest-remix.googlecode.com/
JavaScript | 63 lines | 50 code | 11 blank | 2 comment | 12 complexity | fe820ba16faf032156c5b774f60bb92b MD5 | raw file
 1// see step-by-section.py,
 2// Originally by Adam Lindsay, 2009-03-10.
 3
 4function remix(analysis) {
 5    
 6    function areBeatNumber(beat) {
 7        return function(x) {
 8            return x.localContext()[0] == beat;
 9        };
10    }
11
12    var choices = 4;
13
14    var meter = analysis.timeSignature.value;
15    var fadeIn = analysis.endOfFadeIn;
16    var fadeOut = analysis.startOfFadeOut;
17    var sections = analysis.sections.that(overlapRange(fadeIn, fadeOut));
18    var outchunks = [];
19
20    for (var i = 0; i < sections.length; i++) {
21        console.log(i);
22        var section = sections[i];
23        var beats = analysis.beats.that(areContainedBy(section));
24        var segments = analysis.segments.that(overlap(section));
25        var numBars = section.children().length;
26
27        if (beats.length < meter) {
28            continue;
29        }
30
31        var b = [];
32        var segstarts = [];
33        for (var m = 0; m < meter; m++) {
34            b.push(beats.that(areBeatNumber(m)))
35            segstarts.push(segments.that(overlapStartsOf(b[m])))
36        }
37
38        if (b.length == 0) {
39            continue;
40        }
41        else if (b[0].length == 0) {
42            continue;
43        }
44
45        var now = b[0][0];
46
47        for (var x = 0; x < numBars * meter; x++) {
48            var beat = x % meter;
49            var nextBeat = (x + 1) % meter
50            var nowEndSegment = segments.that(containPoint(now.end))[0]
51            var nextCandidates = segstarts[nextBeat].orderedBy(timbreDistanceFrom(nowEndSegment))
52            if (nextCandidates.length == 0) {
53                continue;
54            }
55            var nextChoice = nextCandidates[Math.floor(Math.random() * (Math.min(choices, nextCandidates.length)))];
56            var next = b[nextBeat].that(startDuring(nextChoice))[0];
57            outchunks.push(now);
58            now = next
59        }
60    }
61
62    return outchunks;
63}