// // Programmer: Craig Stuart Sapp // Creation Date: Sun May 13 14:15:43 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 // Last Modified: Mon Mar 7 14:58:02 PST 2011 Added --name option // Last Modified: Mon Sep 10 15:43:07 PDT 2012 Added enharmonic key labeling // Last Modified: Thu Apr 18 13:40:06 PDT 2013 Enabled multiple segment input // Last Modified: Sun Apr 21 21:52:30 PDT 2013 Added -e option // Filename: ...sig/examples/all/keycordl.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/keycor.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, const string& filename, Array& b40hist, HumdrumFile& infile); 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); void getBase40Histogram (Array& base40, HumdrumFile& infile); int identifyBranchCut (int base12, Array& base40); void printErrorMarker (HumdrumFile& infile, int best40, const char* mode); // 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 int nameQ = 0; // used with --name option int errorQ = 0; // used with -e 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[]) { HumdrumFileSet infiles; 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); string filename; Array b40hist; int bestkey = 0; int i, j; if (numinputs < 1) { infiles.read(cin); } else { 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, const string& filename, Array& b40hist, HumdrumFile& infile) { 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; } int best40; if (bestkey < 12) { if (nameQ) { cout << filename << ":\t"; } else { cout << "The best key is: "; } best40 = identifyBranchCut(bestkey, b40hist); // cout << Convert::base12ToKern(buffer, bestkey+12*4) cout << Convert::base40ToKern(buffer, best40+40*3) << " Major"; if (errorQ) { printErrorMarker(infile, best40, "major"); } cout << "\n"; } else { if (nameQ) { cout << filename << ":\t"; } else { cout << "The best key is: "; } best40 = identifyBranchCut(bestkey, b40hist); // cout << Convert::base12ToKern(buffer, bestkey+12*3) cout << Convert::base40ToKern(buffer, best40+40*3) << " Minor"; if (errorQ) { printErrorMarker(infile, best40, "minor"); } cout << "\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"; } } } ////////////////////////////// // // printErrorMarker -- Compare the computer's analysis against one // found at the start of the file. // void printErrorMarker(HumdrumFile& infile, int best40, const char* mode) { PerlRegularExpression pre; int foundQ = 0; int i, j; 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& base40, HumdrumFile& infile) { base40.setSize(40); base40.allowGrowth(0); base40.setAll(0); int tcount; int i, j, k; int b40; char buffer[1024] = {0}; for (i=0; i& base40) { int tval = Convert::base12ToBase40(base12+5*12); int accidental = Convert::base40ToAccidental(tval); int diatonic = Convert::base40ToDiatonic(tval) % 7; if (accidental == 0) { return tval % 40; } int sdia, fdia; if (accidental == -1) { sdia = (diatonic+6)%7; fdia = diatonic; } else if (accidental == +1) { sdia = diatonic; fdia = (diatonic+1)%7; } else { // something strange happened return tval % 40; } int sb40, fb40; sb40 = (Convert::base7ToBase40(sdia+4*7) + 1)%40; fb40 = (Convert::base7ToBase40(fdia+4*7) - 1)%40; int sscore, fscore; sscore = base40[sb40] + base40[(sb40+23)%40]; fscore = base40[fb40] + base40[(fb40+23)%40]; if (sscore > fscore) { return sb40; } else if (fscore > sscore) { return fb40; } // some strange condition where scores are equivalent. return tval%40; } // md5sum: 98f098fc423b2fffed2dc75fc8970c75 keycor.cpp [20170605]