/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. var sorting = {
  17. /**
  18. * Returns the `AudioQuantum`\'s `confidence` as a sorting value.
  19. */
  20. confidence: function(x) {
  21. return x.confidence;
  22. },
  23. /**
  24. * Returns the `AudioQuantum`\'s `duration` as a sorting value.
  25. */
  26. duration: function(x) {
  27. return x.duration;
  28. },
  29. /**
  30. * Returns a function that returns the value of `timbre`\[*index*]
  31. * of its input `AudioQuantum`. Sorts by the values of the *index*-th
  32. * value in the timbre vector.
  33. */
  34. timbreValue: function(index) {
  35. return function(x) {return x.timbre[index];};
  36. },
  37. /**
  38. * Returns a function that returns the value of `pitch`\[*index*]
  39. * of its input `AudioQuantum`. Sorts by the values of the *index*-th
  40. * value in the pitch vector.
  41. */
  42. pitchValue: function(index) {
  43. return function(x) {return x.pitches[index];};
  44. },
  45. /**
  46. * Returns a function that returns the sum of the squared differences
  47. * between the `pitch` vector of its input `AudioQuantum` and the `pitch`
  48. * vector of the reference parameter *seg*. Sorts by the pitch distance
  49. * from the reference `AudioSegment`.
  50. */
  51. pitchDistanceFrom: function(seg) {
  52. return function(x) {return sorting._sumDiffSquared(seg.pitches, x.pitches);};
  53. },
  54. /**
  55. * Returns a function that returns the sum of the squared differences
  56. * between the `pitch` vector of its input `AudioQuantum` and the `pitch`
  57. * vector of the reference parameter *seg*. Sorts by the pitch distance
  58. * from the reference `AudioSegment`.
  59. */
  60. timbreDistanceFrom: function(seg) {
  61. return function(x) {return sorting._sumDiffSquared(seg.timbre, x.timbre);};
  62. },
  63. /**
  64. * Returns the sum of the twelve pitch vectors' elements. This is a very
  65. * fast way of judging the relative noisiness of a segment.
  66. */
  67. noisiness: function(x) {
  68. return x.pitches.sum();
  69. },
  70. /* local helper functions: */
  71. /**
  72. * Local helper function. The square of the difference between a and b.
  73. */
  74. _sumDiffSquared: function(a, b) {
  75. var result = 0;
  76. for (var i = 0; i < a.length; i++) {
  77. result += Math.pow(a[i] - b[i], 2);
  78. }
  79. return result;
  80. }
  81. };