// // Programmer: Craig Stuart Sapp // Creation Date: Sun May 13 14:15:43 PDT 2001 // Last Modified: Sun May 13 14:15:46 PDT 2001 // Last Modified: Sun Apr 24 12:48:19 PDT 2005 // Last Modified: Thu May 28 22:30:54 PDT 2009 (added continuous analysis) // Filename: ...sig/examples/all/keycordl.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/keycorel.cpp // Syntax: C++; museinfo // // Description: Key correlation measurements using the Krumhansl-Schmuckler // key-finding algorithm. (and also the Gabura algorithm). // // #include "humdrum.h" #include #include // function declarations void checkOptions (Options& opts, int argc, char* argv[]); void example (void); void printAnalysis (int bestkey, Array& scores, Array& durhist); void usage (const char* command); void readWeights (const char* filename); int analyzeKeyRawCorrelation (double* scores, double* distribution, int* pitch, double* durations, int size, int rhythmQ, double* majorKey, double* minorKey); int analyzeKeyEuclidean (double* scores, double* distribution, int* pitch, double* durations, int size, int rhythmQ, double* majorKey, double* minorKey); void normalizeData (double* data, int asize); void adjustData (double* data, int asize, double mean, double sd); double getStandardDeviation (double mean, double* data, int asize); double getMean (double* data, int asize); void equalizeData (double* data, int asize, double summation); void analyzeContinuously (HumdrumFile& infile, int windowsize, double stepsize, double* majorKey, double* minorKey); void loadHistograms (Array >& histograms, HumdrumFile& infile, int segments); void addToHistogramDouble (Array >& histogram, int pc, double start, double dur, double tdur, int segments); void createHistogram (Array& pitchhist, int start, int count, Array >& segments); void identifyKeyDouble (Array& histogram, Array& correlations, double* majorKey, double* minorKey); void printBestKey (int keynumber); double pearsonCorrelation (int size, double* x, double* y); void printCorrelation (double value, int style); void printHistogramTotals (Array >& segments); double getConfidence (Array& cors, int best); void getLocations (Array& measures, HumdrumFile& infile, int segments); // user interface variables Options options; // database for command-line arguments int frequencyQ = 0; // used with -f option int allQ = 0; // used with -a option int rhythmQ = 1; // used with -q option int binaryQ = 0; // used with -b option int rawQ = 0; // used with --raw option int normalizeQ = 0; // used with -n option int euclideanQ = 0; // used with -e option int mmaQ = 0; // used with -F option double stepsize = 1.0; // used with --step option int windowsize = 32; // used with --window option int continuousQ = 0; // used with -c option int roundQ = 1; // used with -R option int debugQ = 0; // used with --debug option double* majorKey; double* minorKey; // page 35 of Krumhansl: Cognitive foundations of musical pitch double majorKeyKrumhansl[12] = { 6.35, // C 2.23, // C# 3.48, // D 2.33, // D# 4.38, // E 4.09, // F 2.52, // F# 5.19, // G 2.39, // G# 3.66, // A 2.29, // A# 2.88}; // B double minorKeyKrumhansl[12] = { 6.33, // C 2.68, // C# 3.52, // D 5.38, // D# 2.60, // E 3.53, // F 2.54, // F# 4.75, // G 3.98, // G# 2.69, // A 3.34, // A# 3.17}; // B // page 85 of Temperley: Music and Probability double majorKeyKostkaPayne[12] = { 0.748, // C 0.060, // C# 0.488, // D 0.082, // D# 0.670, // E 0.460, // F 0.096, // F# 0.715, // G 0.104, // G# 0.366, // A 0.057, // A# 0.400}; // B double minorKeyKostkaPayne[12] = { 0.712, // C 0.084, // C# 0.474, // D 0.618, // D# 0.049, // E 0.460, // F 0.105, // F# 0.747, // G 0.404, // G# 0.067, // A 0.133, // A# 0.330}; // B // from Aarden's dissertation, also displayed graphically in // Huron: Sweet Anticipation double majorKeyAarden[12] = { 17.7661, // C 0.145624, // C# 14.9265, // D 0.160186, // D# 19.8049, // E 11.3587, // F 0.291248, // F# 22.062, // G 0.145624, // G# 8.15494, // A 0.232998, // A# 4.95122}; // B double minorKeyAarden[12] = { 18.2648, // C 0.737619, // C# 14.0499, // D 16.8599, // D# 0.702494, // E 14.4362, // F 0.702494, // F# 18.6161, // G 4.56621, // G# 1.93186, // A 7.37619, // A# 1.75623 }; // B // from Bellman's CMMR 2005 paper double majorKeyBellman[12] = { 16.80, // C 0.86, // C# 12.95, // D 1.41, // D# 13.49, // E 11.93, // F 1.25, // F# 20.28, // G 1.80, // G# 8.04, // A 0.62, // A# 10.57 }; // B double minorKeyBellman[12] = { 18.16, // C 0.69, // C# 12.99, // D 13.34, // D# 1.07, // E 11.15, // F 1.38, // F# 21.07, // G 7.49, // G# 1.53, // A 0.92, // A# 10.21 }; // B // Made up by Craig Sapp (see ICMPC10 paper) double majorKeySimple[12] = { 2.0, // C 0.0, // C# 1.0, // D 0.0, // D# 1.0, // E 1.0, // F 0.0, // F# 2.0, // G 0.0, // G# 1.0, // A 0.0, // A# 1.0}; // B double minorKeySimple[12] = { 2.0, // C 0.0, // C# 1.0, // D 1.0, // D# 0.0, // E 1.0, // F 0.0, // F# 2.0, // G 1.0, // G# 0.0, // A 1.0, // A# 0.0}; // B double majorKeyUser[12] = {0}; double minorKeyUser[12] = {0}; /////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { HumdrumFile infile; majorKey = majorKeyBellman; minorKey = minorKeyBellman; // process the command-line options checkOptions(options, argc, argv); // figure out the number of input files to process int numinputs = options.getArgCount(); Array absbeat; Array pitch; Array duration; Array level; Array distribution(12); Array scores(24); int bestkey = 0; int i, j; for (i=0; i > segments; infile.analyzeRhythm("4"); int segmentCount = int(infile.getTotalDuration() / stepsize + 0.5); if (segmentCount < windowsize) { cout << "Not enough data for requested analysis" << endl; return; } segments.setSize(segmentCount); segments.allowGrowth(0); int i; for (i=0; i > pitchhist; Array > correlations; pitchhist.setSize(segmentCount-windowsize); correlations.setSize(segmentCount-windowsize); pitchhist.allowGrowth(0); correlations.allowGrowth(0); for (i=0; i measures; getLocations(measures, infile, segmentCount); cout << "**key\t**rval\t**conf\t**start\t**mid\t**end\n"; for (i=0; i& measures, HumdrumFile& infile, int segments) { infile.analyzeRhythm("4"); double totaldur = infile.getTotalDuration(); measures.setSize(segments); measures.allowGrowth(0); measures.setAll(-1); int barnum = 0; int index; int i; for (i=0; i= segments) { return; } for (i=index; i>=0; i--) { measures[i] = measures[index+1] - 1; } } ////////////////////////////// // // printHistogramTotals -- // double getConfidence(Array& cors, int best) { int i; int start = 1; int secondbest = 0; if (best == 0) { secondbest = 1; start = 2; } for (i=start; i<24; i++) { if (best == i) { continue; } if (cors[i] > cors[secondbest]) { secondbest = i; } } double output = (cors[best] - cors[secondbest]) * 300; output = int(output + 0.5); if (output > 100.0) { output = 100.0; } return output; } ////////////////////////////// // // printHistogramTotals -- // void printHistogramTotals(Array >& segments) { Array sums(12); sums.allowGrowth(0); sums.setAll(0); int i, j; for (i=0; i& pitchhist, int start, int count, Array >& segments) { pitchhist.setAll(0); int i, j; for (i=0; i& histogram, Array& correlations, double* majorKey, double* minorKey) { int i; double h[24]; for (i=0; i<12; i++) { h[i] = histogram[i]; h[i+12] = h[i]; } double testsum = 0.0; for (i=0; i<12; i++) { testsum += h[i]; correlations[i] = pearsonCorrelation(12, majorKey, h+i); correlations[i+12] = pearsonCorrelation(12, minorKey, h+i); } if (testsum == 0.0) { histogram[12] = 24; // empty histogram, so going to display black return; } // find max value int besti = 0; double bestsum = correlations[0]; for (i=1; i<24; i++) { if (correlations[i] > bestsum) { besti = i; bestsum = correlations[i]; } } histogram[12] = besti; } ////////////////////////////// // // loadHistograms -- // void loadHistograms(Array >& histograms, HumdrumFile& infile, int segments) { infile.analyzeRhythm("4"); double totalduration = infile.getTotalDuration(); double duration; int i; int j; int k; char buffer[10000] = {0}; int pitch; double start; int tokencount; for (i=0; i >& histogram, int pc, double start, double dur, double tdur, int segments) { pc = pc % 12; double startseg = start / tdur * segments; double startfrac = startseg - (int)startseg; double segdur = dur / tdur * segments; if (segdur <= 1.0 - startfrac) { histogram[(int)startseg][pc] += segdur; return; } else if (1.0 - startfrac > 0.0) { histogram[(int)startseg][pc] += (1.0 - startfrac); segdur -= (1.0 - startfrac); } int i = (int)(startseg + 1); while (segdur > 0.0 && i < histogram.getSize()) { if (segdur < 1.0) { histogram[i][pc] += segdur; segdur = 0.0; } else { histogram[i][pc] += 1.0; segdur -= 1.0; } i++; } } ////////////////////////////// // // printAnalysis -- // void printAnalysis(int bestkey, Array& scores, Array& durhist) { char buffer[64] = {0}; if (mmaQ) { cout << "{"; cout << durhist[8] << ", "; // G# cout << durhist[3] << ", "; // D# cout << durhist[10] << ", "; // A# cout << durhist[5] << ", "; // F cout << durhist[0] << ", "; // C cout << durhist[7] << ", "; // G cout << durhist[2] << ", "; // D cout << durhist[9] << ", "; // A cout << durhist[4] << ", "; // E cout << durhist[11] << ", "; // B cout << durhist[6] << ", "; // F# cout << durhist[1] << ""; // C# cout << "};" << endl; return; } if (bestkey < 12) { cout << "The best key is: " << Convert::base12ToKern(buffer, bestkey+12*4) << " Major" << "\n"; } else { cout << "The best key is: " << Convert::base12ToKern(buffer, bestkey+12*3) << " Minor" << "\n"; } int i; if (allQ) { for (i=0; i<12; i++) { cout << "Major[" << i << "] = " << scores[i] << "\t\t\t" << "Minor[" << i << "] = " << scores[i+12] << "\n"; } } if (frequencyQ) { for (i=0; i<12; i++) { cout << "Pitch[" << i << "] = " << durhist[i] << "\n"; } } } ////////////////////////////// // // checkOptions -- validate and process command-line options. // void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("a|all=b", "show all scores"); opts.define("Aarden|aarden|aa=b", "use Aarden profiles"); opts.define("Bellman|bellman|bb=b","use Bellman profiles"); opts.define("Krumhansl|k|kk=b", "use Krumhansl-Kessler profiles"); opts.define("Temperley|temperley|kp|=b","use Kostka-Payne profiles"); opts.define("G|gabura|raw=b", "use raw correlation"); opts.define("n|normalize=b", "normalize raw correlation input data"); opts.define("e|euclidean=b", "euclidean keyfinding method"); opts.define("s|simple=b", "do simple profile"); opts.define("D|no-duration=b", "ignore duration of notes in input"); opts.define("f|frequency|freq=b", "show pitch frequencies"); opts.define("F|Freq=b", "pitch frequencies MMA by fifths"); opts.define("w|weights=s:", "weighting factor file"); opts.define("step=d:1.0", "step size for continuous analysis"); opts.define("window=i:32", "window size for continuous analysis"); opts.define("c|continuous=b", "continuous analysis"); opts.define("R|no-round=b", "don't round correlation values"); opts.define("debug=b", "trace input parsing"); opts.define("author=b", "author of the program"); opts.define("version=b", "compilation information"); opts.define("example=b", "example usage"); opts.define("h|help=b", "short description"); opts.process(argc, argv); // handle basic options: if (opts.getBoolean("author")) { cout << "Written by Craig Stuart Sapp, " << "craig@ccrma.stanford.edu, May 2001" << endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: 26 May 2009" << endl; cout << "compiled: " << __DATE__ << endl; cout << MUSEINFO_VERSION << endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } allQ = opts.getBoolean("all"); rhythmQ = !opts.getBoolean("no-duration"); frequencyQ = opts.getBoolean("frequency"); if (opts.getBoolean("F")) { frequencyQ = 1; mmaQ = 1; } rawQ = opts.getBoolean("raw"); normalizeQ = opts.getBoolean("normalize"); euclideanQ = opts.getBoolean("euclidean"); continuousQ = opts.getBoolean("continuous"); windowsize = opts.getInteger("window"); stepsize = opts.getDouble("step"); roundQ = !opts.getBoolean("no-round"); debugQ = opts.getBoolean("debug"); if (opts.getBoolean("Krumhansl")) { majorKey = majorKeyKrumhansl; minorKey = minorKeyKrumhansl; } else if (opts.getBoolean("Aarden")) { majorKey = majorKeyAarden; minorKey = minorKeyAarden; } else if (opts.getBoolean("simple")) { majorKey = majorKeySimple; minorKey = minorKeySimple; } else if (opts.getBoolean("Bellman")) { majorKey = majorKeyBellman; minorKey = minorKeyBellman; } else if (opts.getBoolean("Temperley")) { majorKey = majorKeyKostkaPayne; minorKey = minorKeyKostkaPayne; } else if (opts.getBoolean("weights")) { readWeights(opts.getString("weights")); majorKey = majorKeyUser; minorKey = minorKeyUser; } else { // default weightings set at start of main(). } } ////////////////////////////// // // readWeights -- // void readWeights(const char* filename) { int i; int j; int key; double value; HumdrumFile wfile; wfile.read(filename); for (i=0; i= 0) && (key < 24) && (value != -1000000.0)) { if (key < 12) { majorKeyUser[key] = value; } else { minorKeyUser[key-12] = value; } } } } ////////////////////////////// // // example -- example usage of the maxent program // void example(void) { cout << " \n" << endl; } ////////////////////////////// // // usage -- gives the usage statement for the quality program // void usage(const char* command) { cout << " \n" << endl; } ////////////////////////////// // // analyzeKeyEuclidean -- // int analyzeKeyEuclidean (double* scores, double* distribution, int* pitch, double* durations, int size, int rhythmQ, double* majorKey, double* minorKey) { int i, j; int histogram[12] = {0}; for (i=0; i<24; i++) { scores[i] = 0.0; } if (size == 0) { return -1; // return -1 if no data to analyze } for (i=0; i<12; i++) { distribution[i] = 0.0; } // generate a histogram of pitches for (i=0; i scores[bestkey]) { bestkey = i; } } return bestkey; } ////////////////////////////// // // normalizeData -- // void normalizeData(double* data, int asize) { double mean = getMean(data, asize); double sd = getStandardDeviation(mean, data, asize); adjustData(data, 12, mean, sd); } ////////////////////////////// // // adjustData -- apply mean shift and standard deviation scaling. // void adjustData(double* data, int asize, double mean, double sd) { int i; for (i=0; i