"use strict"; // Key detection algorithm, as described at: // http://rnhart.net/articles/key-finding/ // (from this web site) .... // Krumhansl-Schmuckler key-finding algorithm (by Carol L. Krumhansl // and Mark A. Schmuckler). The profile numbers came from experiments // done by Krumhansl and Edward J. Kessler. The experiments consisted // of playing a set of context tones or chords, playing a probe tone, // and asking a listener to rate how well the probe tone fit with the // context. You can read about the experiments and the algorithm in // Krumhansl's book Cognitive Foundations of Musical Pitch. (The // experiments are described in Chapter 2. The key-finding algorithm // is described in Chapter 4.) You may be able to read portions of // the book on Google Books. function khMean(vals) { var len = vals.length; if (len==0) { return 0; } var total = 0; for (var i=0; imax_val) { max_val = correlation_coeff; max_key = key + " (" + mm + ")"; } } } return { "key": max_key, "score": max_val }; } function khKeyDetection(chromatic_scale_durations) { // Work out durations of MIDI events folded into octave (60=Middle-C) // (unbiased data) //var chromatic_scale_durations = [ 432, 231, 0, 405, 12, 316, 4, 126, 612, 0, 191, 1]; var chromatic_scale_durations_unbiased = khUnbiased(chromatic_scale_durations); // Generate all (unbiased) pairings var kh_pairings_unbiased = khCreateAllPairings(chromatic_scale_durations_unbiased,kh_major_profile_unbiased,kh_minor_profile_unbiased) // Compute Correlation Coefficients var kh_alignments = khComputeAllCorrelationCoefficients(kh_pairings_unbiased); // Pick highest values var strongest_profile = khFindMaxCorrelation(kh_alignments); console.log("Predicted Key: " + JSON.stringify(strongest_profile)); return strongest_profile; }