PageRenderTime 264ms CodeModel.GetById 121ms app.highlight 11ms RepoModel.GetById 130ms app.codeStats 0ms

/flash/src/js/selection.js

http://echo-nest-remix.googlecode.com/
JavaScript | 229 lines | 120 code | 19 blank | 90 comment | 25 complexity | 74fb9e70516eab572a4ec22b703974a9 MD5 | raw file
  1/**
  2* Selection filters.
  3*
  4* The functions in this module each return *another* function that takes
  5* one argument, an `AudioQuantum`, and returns an `AudioQuantum` or `false`.
  6*
  7* By convention, all of these functions are named to be verb phrases that
  8* agree with a plural noun in a restrictive clause introduced by `that`,
  9* as in::
 10*
 11*     analysis.segments.that(fallOnThe(1))
 12*/
 13var selection = {
 14    /**
 15    * Returns a function that tests if its input `AudioQuantum` lies
 16    * between the *start* and *end* parameters.
 17    */
 18    areContainedByRange: function(start, end) {
 19        return function(x) {
 20            return x.start >= start && x.end <= end;
 21        };
 22    },
 23
 24    /**
 25    * Returns a function that tests if its input `AudioQuantum` lies
 26    * within the interval of the parameter *aq* `AudioQuantum`,
 27    */
 28    areContainedBy: function(aq) {
 29        return function(x) {
 30            return x.start >= aq.start && x.end <= aq.end;
 31        };
 32    },
 33
 34    /**
 35    * Returns a function that tests if its input `AudioQuantum` overlaps
 36    * in any way the interval between the parameters *start* and *end*.
 37    */
 38    overlapRange: function(start, end) {
 39        return function(x) {
 40            return x.end > start && x.start < end;
 41        };
 42    },
 43
 44    /**
 45    * Returns a function that tests if its input `AudioQuantum` overlaps
 46    * in any way the parameter *aq* `AudioQuantum`.
 47    */
 48    overlap: function(aq) {
 49        return function(x) {
 50            return x.end > aq.start && x.start < aq.end;
 51        };
 52    },
 53
 54
 55    /**
 56    * Returns a function that tests if its input `AudioQuantum`\'s `end`
 57    * lies in the interval between the parameters *start* and *end*.
 58    */
 59    endDuringRange: function(start, end) {
 60        return function(x) {
 61            return x.end > start && x.end <= end;
 62        };
 63    },
 64
 65    /**
 66    * Returns a function that tests if its input `AudioQuantum`\'s `end`
 67    * lies anywhere during the parameter *aq* `AudioQuantum`.
 68    */
 69    endDuring: function(aq) {
 70        return function(x) {
 71            return x.end > aq.start && x.end <= aq.end;
 72        };
 73    },
 74
 75    /**
 76    * Returns a function that tests if its input `AudioQuantum`\'s `start`
 77    * lies in the interval between the parameters *start* and *end*.
 78    */
 79    startDuringRange: function(start, end) {
 80        return function(x) {
 81            return x.start >= start && x.start < end;
 82        };
 83    },
 84
 85    /**
 86    * Returns a function that tests if its input `AudioQuantum`\'s `start`
 87    * lies anywhere during the parameter *aq* `AudioQuantum`.
 88    */
 89    startDuring: function(aq) {
 90        return function(x) {
 91            return x.start >= aq.start && x.start < aq.end;
 92        };
 93    },
 94
 95    /**
 96    * Returns a function that tests if its input `AudioQuantum` contains
 97    * the input parameter *point*, a time offset, in seconds.
 98    */
 99    containPoint: function(point) {
100        return function(x) {
101            return point > x.start && point < x.end;
102        };
103    },
104
105    /**
106    * Returns a function that tests if its input `AudioQuantum` has
107    * a `pitch`\[*pitchmax*] such that it is greater or equal to all
108    * other values in its `pitch` vector.
109    */
110    havePitchMax: function(pitchmax) {
111        return function(x) {
112            return selection._isMaxPitch(pitchmax, x.pitches);
113        };
114    },
115
116    /**
117    * Returns a function that tests if its input `AudioQuantum` has
118    * a maximum `pitch`\[*p*] such that it is greater or equal to all
119    * other values in its `pitch` vector, and *p* is in `List` parameter
120    * *pitchesmax*.
121    */
122    havePitchesMax: function(pitchesmax) {
123        return function(x) {
124            var pitches = x.pitches;
125            for (var i = 0; i < pitchesmax.length; i++) {
126                if (selection._isMaxPitch(pitchesmax[i], x.pitches)) {
127                    return true;
128                }
129            }
130            return false;
131        };
132    },
133
134    /**
135    * Returns a function that tests if its input `AudioQuantum` lies
136    * immediately before the parameter *aq* `AudioQuantum`. That is,
137    * if the tested `AudioQuantum`\'s `end` == *aq*.start .
138    */
139    lieImmediatelyBefore: function(aq) {
140        return function(x) {
141            return x.end == aq.start;
142        };
143    },
144
145    /**
146    * Returns a function that tests if its input `AudioQuantum` lies
147    * immediately after the parameter *aq* `AudioQuantum`. That is,
148    * if the tested `AudioQuantum`\'s `start` == *aq*.end .
149    */
150    lieImmediatelyAfter: function(aq) {
151        return function(x) {
152            return x.start == aq.end;
153        };
154    },
155
156    /**
157    * Returns a function that tests if its input `AudioQuantum` has
158    * a (one-indexed) ordinality within its `group`\() that is equal
159    * to parameter *beatNumber*.
160    */
161    fallOnThe: function(beatNumber) {
162        return function(x) {
163            return x.localContext()[0] == (beatNumber - 1);
164        };
165    },
166
167/* The following take AudioQuantumLists as input arguments: */
168
169    /**
170    * Returns a function that tests if its input `AudioQuantum` contains
171    * the `end` of any of the parameter *aqs*, a `List` of
172    * `AudioQuantum`\s.
173    */
174    overlapEndsOf: function(aqs) {
175        return function(x) {
176            for (var i = 0; i < aqs.length; i++) {
177                var aq = aqs[i];
178                if (x.start <= aq.end && x.end >= aq.end) {
179                    return true;
180                }
181            }
182            return false;
183        };
184    },
185
186    /**
187    * Returns a function that tests if its input `AudioQuantum` contains
188    * the `start` of any of the parameter *aqs*, a `List` of
189    * `AudioQuantum`\s.
190    */
191    overlapStartsOf: function(aqs) {
192        return function(x) {
193            for (var i = 0; i < aqs.length; i++) {
194                var aq = aqs[i];
195                if (x.start <= aq.start && x.end >= aq.start) {
196                    return true;
197                }
198            }
199            return false;
200        };
201    },
202
203    /**
204    * Returns a function that tests if its input `AudioQuantum` has
205    * its `start` lie in any of the parameter *aqs*, a `List` of
206    * `AudioQuantum`\s.
207    */
208    startDuringAny: function(aqs) {
209        return function(x) {
210            for (var i = 0; i < aqs.length; i++) {
211                var aq = aqs[i];
212                if (aq.start <= x.start && aq.end >= aq.start) {
213                    return true;
214                }
215            }
216            return false;
217        };
218    },
219
220    _isMaxPitch: function(pitchmax, pitches) {
221        var max = pitches[pitchmax];
222        for (var i = 0; i < pitches.length; i++) {
223            if (pitches[i] > max) {
224                return false;
225            }
226        }
227        return true;
228    }
229};