/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. */
  13. var 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. * Returns a function that tests if its input `AudioQuantum` lies
  25. * within the interval of the parameter *aq* `AudioQuantum`,
  26. */
  27. areContainedBy: function(aq) {
  28. return function(x) {
  29. return x.start >= aq.start && x.end <= aq.end;
  30. };
  31. },
  32. /**
  33. * Returns a function that tests if its input `AudioQuantum` overlaps
  34. * in any way the interval between the parameters *start* and *end*.
  35. */
  36. overlapRange: function(start, end) {
  37. return function(x) {
  38. return x.end > start && x.start < end;
  39. };
  40. },
  41. /**
  42. * Returns a function that tests if its input `AudioQuantum` overlaps
  43. * in any way the parameter *aq* `AudioQuantum`.
  44. */
  45. overlap: function(aq) {
  46. return function(x) {
  47. return x.end > aq.start && x.start < aq.end;
  48. };
  49. },
  50. /**
  51. * Returns a function that tests if its input `AudioQuantum`\'s `end`
  52. * lies in the interval between the parameters *start* and *end*.
  53. */
  54. endDuringRange: function(start, end) {
  55. return function(x) {
  56. return x.end > start && x.end <= end;
  57. };
  58. },
  59. /**
  60. * Returns a function that tests if its input `AudioQuantum`\'s `end`
  61. * lies anywhere during the parameter *aq* `AudioQuantum`.
  62. */
  63. endDuring: function(aq) {
  64. return function(x) {
  65. return x.end > aq.start && x.end <= aq.end;
  66. };
  67. },
  68. /**
  69. * Returns a function that tests if its input `AudioQuantum`\'s `start`
  70. * lies in the interval between the parameters *start* and *end*.
  71. */
  72. startDuringRange: function(start, end) {
  73. return function(x) {
  74. return x.start >= start && x.start < end;
  75. };
  76. },
  77. /**
  78. * Returns a function that tests if its input `AudioQuantum`\'s `start`
  79. * lies anywhere during the parameter *aq* `AudioQuantum`.
  80. */
  81. startDuring: function(aq) {
  82. return function(x) {
  83. return x.start >= aq.start && x.start < aq.end;
  84. };
  85. },
  86. /**
  87. * Returns a function that tests if its input `AudioQuantum` contains
  88. * the input parameter *point*, a time offset, in seconds.
  89. */
  90. containPoint: function(point) {
  91. return function(x) {
  92. return point > x.start && point < x.end;
  93. };
  94. },
  95. /**
  96. * Returns a function that tests if its input `AudioQuantum` has
  97. * a `pitch`\[*pitchmax*] such that it is greater or equal to all
  98. * other values in its `pitch` vector.
  99. */
  100. havePitchMax: function(pitchmax) {
  101. return function(x) {
  102. return selection._isMaxPitch(pitchmax, x.pitches);
  103. };
  104. },
  105. /**
  106. * Returns a function that tests if its input `AudioQuantum` has
  107. * a maximum `pitch`\[*p*] such that it is greater or equal to all
  108. * other values in its `pitch` vector, and *p* is in `List` parameter
  109. * *pitchesmax*.
  110. */
  111. havePitchesMax: function(pitchesmax) {
  112. return function(x) {
  113. var pitches = x.pitches;
  114. for (var i = 0; i < pitchesmax.length; i++) {
  115. if (selection._isMaxPitch(pitchesmax[i], x.pitches)) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. };
  121. },
  122. /**
  123. * Returns a function that tests if its input `AudioQuantum` lies
  124. * immediately before the parameter *aq* `AudioQuantum`. That is,
  125. * if the tested `AudioQuantum`\'s `end` == *aq*.start .
  126. */
  127. lieImmediatelyBefore: function(aq) {
  128. return function(x) {
  129. return x.end == aq.start;
  130. };
  131. },
  132. /**
  133. * Returns a function that tests if its input `AudioQuantum` lies
  134. * immediately after the parameter *aq* `AudioQuantum`. That is,
  135. * if the tested `AudioQuantum`\'s `start` == *aq*.end .
  136. */
  137. lieImmediatelyAfter: function(aq) {
  138. return function(x) {
  139. return x.start == aq.end;
  140. };
  141. },
  142. /**
  143. * Returns a function that tests if its input `AudioQuantum` has
  144. * a (one-indexed) ordinality within its `group`\() that is equal
  145. * to parameter *beatNumber*.
  146. */
  147. fallOnThe: function(beatNumber) {
  148. return function(x) {
  149. return x.localContext()[0] == (beatNumber - 1);
  150. };
  151. },
  152. /* The following take AudioQuantumLists as input arguments: */
  153. /**
  154. * Returns a function that tests if its input `AudioQuantum` contains
  155. * the `end` of any of the parameter *aqs*, a `List` of
  156. * `AudioQuantum`\s.
  157. */
  158. overlapEndsOf: function(aqs) {
  159. return function(x) {
  160. for (var i = 0; i < aqs.length; i++) {
  161. var aq = aqs[i];
  162. if (x.start <= aq.end && x.end >= aq.end) {
  163. return true;
  164. }
  165. }
  166. return false;
  167. };
  168. },
  169. /**
  170. * Returns a function that tests if its input `AudioQuantum` contains
  171. * the `start` of any of the parameter *aqs*, a `List` of
  172. * `AudioQuantum`\s.
  173. */
  174. overlapStartsOf: 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.start && x.end >= aq.start) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. };
  184. },
  185. /**
  186. * Returns a function that tests if its input `AudioQuantum` has
  187. * its `start` lie in any of the parameter *aqs*, a `List` of
  188. * `AudioQuantum`\s.
  189. */
  190. startDuringAny: function(aqs) {
  191. return function(x) {
  192. for (var i = 0; i < aqs.length; i++) {
  193. var aq = aqs[i];
  194. if (aq.start <= x.start && aq.end >= aq.start) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. };
  200. },
  201. _isMaxPitch: function(pitchmax, pitches) {
  202. var max = pitches[pitchmax];
  203. for (var i = 0; i < pitches.length; i++) {
  204. if (pitches[i] > max) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. };