// // Programmer: Craig Stuart Sapp // Creation Date: Sat Oct 4 05:50:39 PDT 2008 (extended version of scaperank) // Last Modified: Sun Oct 5 16:40:18 PDT 2008 // Filename: ...sig/examples/all/ismir2008.cpp // Web Address: http://sig.sapp.org/examples/museinfo/ismir2008/ismir2008.cpp // Syntax: C++; museinfo // // Description: Generate a comparison between multiple sets of // number sequences from S0 through S4. // #include "Array.h" #include "Options.h" #include #ifndef OLDCPP using namespace std; #include #else #include #endif #include /* for qsort and bsearch functions */ #include /* for random number seeding */ #include "museinfo.h" /* for humdrum file class */ /////////////////////////////////////////////////////////////////////////// class Performance { public: Performance(void); ~Performance(); int spine; // original spine from input int process; // process = 0 means do not process in analysis int random; // random = 1 => random testing sequence int average; // average = 1 => average performance data const char* pid; // performance id string, if given in input const char* name; // performance name Array data; // array of performance data }; Performance::Performance(void) { spine = process = random = average = -1; pid = ""; name = ""; data.setSize(1000); data.setSize(0); data.setGrowth(1000); } Performance::~Performance() { data.setSize(0); pid = ""; name = ""; spine = process = random = average = -1; } class Scape { public: Scape (void); ~Scape (void); void setScapeSize (int aSize); Array > data; }; Scape::Scape(void) { data.setSize(0); } Scape::~Scape() { // do nothing; } void Scape::setScapeSize(int aSize) { //cout << "SCAPE SIZE: " << aSize << endl; data.setSize(aSize-1); data.allowGrowth(0); int i; for (i=0; i& performances, HumdrumFile& infile); void createScore0Files (Array& performances, HumdrumFile& infile); void printNumberDecimal (ofstream& outfile, double number, int decimals); void printScore (const char* filename, HumdrumFile& infile, Array& performances, Array >& score0data, int decimals); void sortRanks (Array& ranks, Array& values); double pearsonCorrelation (int size, double* a, double* b); void calculateScore0Array (Array >& dataarray, Array& performances); void calculateRankArray (Array >& rankarray, Array >& scorearray); double getMean (int size, double* a); int numbersort (const void* A, const void* B); void getRange (double& minn, double& maxx, Array data); int performerNameQ (const char* string); void printRank (const char* filename, HumdrumFile& infile, Array& performances, Array >& rank0data); void calculateAllCorrelations(Array >& correlations, Array& performances); void calculateCorrelationLayer(Scape& scape, Performance& a, Performance& b); void calculateType1Scores (Array >& score1data, Array >& correlations, Array& perfs); void calculateType1ScoreSingle(Array& performancedata, Array >& correlations, int reference, Array& masklayer); int getMaxCorrIndex (Array >& correlations, int reference, int scaperow, int scapecol, Array& masklayer); void calculateScore0Test (Array >& score0data, Array >& correlations); double getCorrelation (int row, int col, int scaperow, int scapecol, Array >& correlations); void findBestMatch (Array >& correlations, Array& masklayer, int& bestmatch, double& bestscore, int reference); void calculateType2ScoresAndRanks(Array >& score2data, Array >& rank2data, Array >& score1data, Array >& rank1data, Array >& correlations, Array& perfs); void calculateType2ScoresAndRanksSingle(Array& score2data, Array& rank2data, Array& masklayer, Array >& correlations, int reference); void calculateType3Scores (Array >& score3data, Array >& rank2data, Array >& correlations, Array& perfs); void calculateType4Scores (Array >& score4data, Array >& score3data); void removeBetterHalfFromMaskLayer(Array& masklayer, Array& rank2data); void calculateType3ScoreSingle(Array& score3data, Array& masklayer, Array >& correlations, int reference); // User interface variables: Options options; int score0q = 0; // used with --s0 (correlation score) int rank0q = 0; // used with --r0 (correlation score) int score1q = 0; // used with --s1 (correlation score) int rank1q = 0; // used with --r1 (correlation score) int score2q = 0; // used with --s2 (correlation score) int rank2q = 0; // used with --r2 (correlation score) int score3q = 0; // used with --s3 (correlation score) int rank3q = 0; // used with --r3 (correlation score) int score4q = 0; // used with --s4 (correlation score) int rank4q = 0; // used with --r4 (correlation score) string filebase = "data"; // used with -b: output file basename int level = 0; /////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { // process the command-line options checkOptions(options, argc, argv); HumdrumFile infile; Array performances; int i; string hum_filename = ""; if (options.getArgCount() <= 0) { hum_filename = ""; } else { hum_filename = options.getArg(1); } infile.read(hum_filename.c_str()); readData(performances, infile); if (level < 0) { // finished calculating everything requested std::cout << "Warning: no calculations done" << std::endl; exit(0); } // calculate score 0 data: ///////////////////////////////////////// Array > score0data; Array > rank0data; char filename[1024] = {0}; calculateScore0Array(score0data, performances); calculateRankArray(rank0data, score0data); if (score0q) { strcpy(filename, filebase.c_str()); strcat(filename, "-s0.txt"); cout << "Storing score-0 data in " << filename << endl; printScore(filename, infile, performances, score0data, 3); } if (rank0q) { strcpy(filename, filebase.c_str()); strcat(filename, "-r0.txt"); cout << "Storing score-0 ranks in " << filename << endl; printRank(filename, infile, performances, rank0data); } if (level == 0) { // finished calculating everything requested exit(0); } // prepare correlation data: /////////////////////////////////////// cout << "Calculating correlation data" << endl; Array > correlations; calculateAllCorrelations(correlations, performances); // calculate score 1 data: ///////////////////////////////////////// cout << "Calculating Score 1 data:" << endl; Array > score1data; Array > rank1data; score1data.setSize(performances.getSize()); score1data.allowGrowth(0); for (i=0; i > score2data; Array > rank2data; score2data.setSize(performances.getSize()); score2data.allowGrowth(0); rank2data.setSize(performances.getSize()); rank2data.allowGrowth(0); for (i=0; i > score3data; Array > rank3data; score3data.setSize(performances.getSize()); score3data.allowGrowth(0); rank3data.setSize(performances.getSize()); rank3data.allowGrowth(0); for (i=0; i > score4data; Array > rank4data; calculateType4Scores(score4data, score3data); calculateRankArray(rank4data, score4data); if (score4q) { strcpy(filename, filebase.c_str()); strcat(filename, "-s4.txt"); cout << "Storing score-4 data in " << filename << endl; printScore(filename, infile, performances, score4data, 3); } if (rank4q) { strcpy(filename, filebase.c_str()); strcat(filename, "-r4.txt"); cout << "Storing score-4 ranks in " << filename << endl; printRank(filename, infile, performances, rank4data); } return 0; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // calculateType4Scores -- // void calculateType4Scores(Array >& score4data, Array >& score3data) { score4data.setSize(score3data.getSize()); score4data.allowGrowth(0); int i, j; for (i=0; i >& score3data, Array >& rank2data, Array >& correlations, Array& perfs) { int i; Array > masklayer; masklayer.setSize(score3data.getSize()); masklayer.allowGrowth(0); for (i=0; i& masklayer, Array& rank2data) { int i; int cutoff = rank2data.getSize()/2; for (i=0; i >& score2data, Array >& rank2data, Array >& score1data, Array >& rank1data, Array >& correlations, Array& perfs) { int i, j; Array > masklayer; masklayer.setSize(score2data.getSize()); masklayer.allowGrowth(0); for (i=0; i& score2data, Array& rank2data, Array& masklayer, Array >& correlations, int reference) { int i; int iterations = score2data.getSize() - 3; // minus one for self, // minus one for previously found score1data // minus one for very last case int rankcounter = 2; // #0 = self; #1 = best match from before int bestmatch; double bestscore; for (i=0; i templastcount; templastcount = masklayer; int lastcount = 0; for (i=0; i& score3data, Array& masklayer, Array >& correlations, int reference) { Array score1data; score1data.setSize(masklayer.getSize()); score1data.allowGrowth(0); score1data.setAll(-1000); int arraysize = masklayer.getSize(); int i; for (i=0; i >& correlations, Array& masklayer, int& bestmatch, double& bestscore, int reference) { Array score1data; score1data.setSize(masklayer.getSize()); score1data.allowGrowth(0); score1data.setAll(-1000); calculateType1ScoreSingle(score1data, correlations, reference, masklayer); bestmatch = -1000; bestscore = -1000.0; int i; for (i=1; i >& score0data, Array >& correlations) { int i, j; for (i=0; i >& correlations) { int a; int b; if (row < col) { a = col; b = row; } else if (row > col) { a = row; b = col; } else { return 1.0; } //cout << "correlation[" << row << "][" << col << "][" << scaperow // << "][" << scapecol << "] = " // << correlations[a-1][b].data[scaperow][scapecol] // << endl; return correlations[a-1][b].data[scaperow][scapecol]; } ////////////////////////////// // // calculateType1Scores -- calculate the index of the best correlations // for each subsequence. // void calculateType1Scores(Array >& score1data, Array >& correlations, Array& perfs) { Array > masklayer; int arraysize = score1data.getSize(); int i; masklayer.setSize(arraysize); masklayer.allowGrowth(0); for (i=0; i& performancedata, Array >& correlations, int reference, Array& masklayer) { Array sums; sums.setSize(performancedata.getSize()); sums.allowGrowth(0); sums.setAll(0); int i, j; int maxindex; int tsum = 0; int scaperows = correlations[0][0].data.getSize(); int scapecols; for (i=0; i >& correlations, int reference, int scaperow, int scapecol, Array& masklayer) { int maxindex = -2; double maxval = -2.0; double testval; int i; int rowsize = correlations.getSize()+1; for (i=0; i >& correlations, Array& performances) { int i, j; // subtract one for top row since diagonal always one correlations.setSize(performances.getSize()-1); for (i=1; i& performances, HumdrumFile& infile) { int dataspines = infile.getMaxTracks(); performances.setSize(dataspines); performances.allowGrowth(0); // pre-allocate storage space for data int i, j; for (i=0; i= 0.0) && (maxx >= 0.0) && (maxx <= 1.0)) { performances[i].random = 1; } } } ////////////////////////////// // // performerNameQ -- // int performerNameQ(const char* string) { int digitcount = 0; int spacecount = 0; int len = strlen(string); int i; for (i=0; i data) { int i; minn = data[0]; maxx = data[0]; for (i=1; i data[i]) { minn = data[i]; } if (maxx < data[i]) { maxx = data[i]; } } } ////////////////////////////// // // calculateScore0Array -- // void calculateScore0Array(Array >& dataarray, Array& performances) { dataarray.setSize(performances.getSize()); int i, j; for (i=0; i >& rankarray, Array >& scorearray) { rankarray.setSize(scorearray.getSize()); int i; for (i=0; i& ranks, Array& values) { int i; Array presort; Array sortdata; presort.setSize(values.getSize()); sortdata.setSize(values.getSize()); for (i=0; i& performances, Array >& rank0data) { int i, j; ofstream outfile; #ifndef OLDCPP outfile.open(filename); #else outfile.open(filename, ios::noreplace); #endif if (!outfile.is_open()) { std::cout << "Error: could not open " << filename << " for writing" << std::endl; exit(1); } // print bibliographic information: for (i=0; i& performances, Array >& score0data, int decimals) { int i, j; ofstream outfile; #ifndef OLDCPP outfile.open(filename); #else outfile.open(filename, ios::noreplace); #endif if (!outfile.is_open()) { std::cout << "Error: could not open " << filename << " for writing" << std::endl; exit(1); } // print bibliographic information: for (i=0; i decimals) { std::cout << "Error printing number " << number << std::endl; std::cout << "deccount = " << deccount << std::endl; std::cout << "decimals = " << decimals << std::endl; std::cout << "string = >>" << string << "<<" << std::endl; exit(1); } outfile << string; if (decarea == 0) { outfile << '.'; } for (i=0; i valueb) { return -1; } else if (valuea < valueb) { return +1; } else { return 0; } } ////////////////////////////// // // checkOptions -- // void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("s0=b", "create score-0 data file"); opts.define("r0=b", "create rank-0 data file"); opts.define("s1=b", "create score-1 data file"); opts.define("r1=b", "create rank-1 data file"); opts.define("s2=b", "create score-2 data file"); opts.define("r2=b", "create rank-2 data file"); opts.define("s3=b", "create score-3 data file"); opts.define("r3=b", "create rank-3 data file"); opts.define("s4=b", "create score-4 data file"); opts.define("r4=b", "create rank-4 data file"); opts.define("0=b", "create rank-0 and score-0 data file"); opts.define("1=b", "create rank-1 and score-1 data file"); opts.define("2=b", "create rank-2 and score-2 data file"); opts.define("3=b", "create rank-3 and score-2 data file"); opts.define("4=b", "create rank-4 and score-2 data file"); opts.define("a|all=b", "create all score and rank files"); opts.define("b|base|filebase=s:data", "basename for generating filenames"); opts.define("author=b", "author of program"); opts.define("version=b", "compilation info"); opts.define("example=b", "example usages"); opts.define("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, October 2008" << std::endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: October 2008" << std::endl; cout << "compiled: " << __DATE__ << std::endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand().c_str()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } if (opts.getBoolean("s0")) { score0q = 1; } if (opts.getBoolean("r0")) { rank0q = 1; } if (opts.getBoolean("0")) { score0q = rank0q = 1; } if (opts.getBoolean("s1")) { score1q = 1; level = 1; } if (opts.getBoolean("r1")) { rank1q = 1; level = 1; } if (opts.getBoolean("1")) { score1q = rank1q = 1; level = 1; } if (opts.getBoolean("s2")) { score2q = 1; level = 2; } if (opts.getBoolean("r2")) { rank2q = 1; level = 2; } if (opts.getBoolean("2")) { score2q = rank2q = 1; level = 2; } if (opts.getBoolean("s3")) { score3q = 1; level = 3; } if (opts.getBoolean("r3")) { rank3q = 1; level = 3; } if (opts.getBoolean("3")) { score3q = rank3q = 1; level = 3; } if (opts.getBoolean("s4")) { score4q = 1; level = 4; } if (opts.getBoolean("r4")) { rank4q = 1; level = 4; } if (opts.getBoolean("4")) { score4q = rank4q = 1; level = 4; } if (opts.getBoolean("all")) { score0q = rank0q = 1; score1q = rank1q = 1; score2q = rank2q = 1; score3q = rank3q = 1; score4q = rank4q = 1; level = 4; } filebase = opts.getString("filebase"); } ////////////////////////////// // // example -- // void example(void) { } ////////////////////////////// // // usage -- // void usage(const char* command) { } // md5sum: 44b9f9742bb7ed8d81bcd20c2f8d11c5 ismir2008.cpp [20160320]