PageRenderTime 43ms CodeModel.GetById 34ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/flash/src/js/sorting.js

http://echo-nest-remix.googlecode.com/
JavaScript | 90 lines | 30 code | 9 blank | 51 comment | 1 complexity | 6a8e35a8c33af032da3a583043bcf99a MD5 | raw file
 1/**
 2* Sorting key functions.
 3*
 4* All of the functions in this module can be used as a sorting key for
 5* `AudioQuantumList.orderedBy`, as in::
 6*
 7*     analysis.segments.orderedBy(duration)
 8*
 9* Some of the functions in this module return *another* function that takes
10* one argument, an `AudioQuantum`, and returns a value (typically a `float`)
11* that can then be used as a sorting value.
12*
13* By convention, all of these functions are named to be noun phrases that
14* follow `sortedBy`, as seen above.
15*/
16
17var sorting = {
18    /**
19    * Returns the `AudioQuantum`\'s `confidence` as a sorting value.
20    */
21    confidence: function(x) {
22        return x.confidence;
23    },
24
25    /**
26    * Returns the `AudioQuantum`\'s `duration` as a sorting value.
27    */
28    duration: function(x) {
29        return x.duration;
30    },
31
32    /**
33    * Returns a function that returns the value of `timbre`\[*index*]
34    * of its input `AudioQuantum`. Sorts by the values of the *index*-th
35    * value in the timbre vector.
36    */
37    timbreValue: function(index) {
38        return function(x) {return x.timbre[index];};
39    },
40
41    /**
42    * Returns a function that returns the value of `pitch`\[*index*]
43    * of its input `AudioQuantum`. Sorts by the values of the *index*-th
44    * value in the pitch vector.
45    */
46    pitchValue: function(index) {
47        return function(x) {return x.pitches[index];};
48    },
49
50    /**
51    * Returns a function that returns the sum of the squared differences
52    * between the `pitch` vector of its input `AudioQuantum` and the `pitch`
53    * vector of the reference parameter *seg*. Sorts by the pitch distance
54    * from the reference `AudioSegment`.
55    */
56    pitchDistanceFrom: function(seg) {
57        return function(x) {return sorting._sumDiffSquared(seg.pitches, x.pitches);};
58    },
59
60    /**
61    * Returns a function that returns the sum of the squared differences
62    * between the `pitch` vector of its input `AudioQuantum` and the `pitch`
63    * vector of the reference parameter *seg*. Sorts by the pitch distance
64    * from the reference `AudioSegment`.
65    */
66    timbreDistanceFrom: function(seg) {
67        return function(x) {return sorting._sumDiffSquared(seg.timbre, x.timbre);};
68    },
69
70    /**
71    * Returns the sum of the twelve pitch vectors' elements. This is a very
72    * fast way of judging the relative noisiness of a segment.
73    */
74    noisiness: function(x) {
75        return x.pitches.sum();
76    },
77
78    /* local helper functions: */
79
80    /**
81    * Local helper function. The square of the difference between a and b.
82    */
83    _sumDiffSquared: function(a, b) {
84        var result = 0;
85        for (var i = 0; i < a.length; i++) {
86            result += Math.pow(a[i] - b[i], 2);
87        }
88        return result;
89    }
90};