// // Programmer: Craig Stuart Sapp // Creation Date: Wed Apr 4 20:40:28 PDT 2012 // Last Modified: Wed Apr 4 20:40:34 PDT 2012 // Filename: ...sig/examples/all/menpat.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/menpat.cpp // Syntax: C++; museinfo // // Description: Extract mensural rhythmic patterns from each measure in // each voice. // #include "humdrum.h" #include "PerlRegularExpression.h" #ifndef OLDCPP #include #define SSTREAM stringstream #define CSTRING str().c_str() using namespace std; #else #ifdef VISUAL #include /* for windows 95 */ #else #include #endif #define SSTREAM strstream #define CSTRING str() #endif // function declarations void checkOptions (Options& opts, int argc, char* argv[]); void example (void); void usage (const char* command); void processVoice (int track, int voice, HumdrumFile& infile); char* getNoteDuration (char* buffer, RationalNumber& dur, int dot); void adjustMensuration (char* mensuration); int getDot (const char* strang); int getLastDataLine (HumdrumFile& infile); void printExclusiveInterpretations (void); void printDataTerminator (void); void getRhythmString (char* buffer, HumdrumFile& infile, int line, int track, int voice, RationalNumber& rscale, char* mensuration); void processVoice2 (int track, int voice, HumdrumFile& infile); void markRhythm (char* buffer, HumdrumFile& infile, int line, int track, int voice); RationalNumber getNextMeasureAbsTime(HumdrumFile& infile, int line, int track); char* cleanRhythmString (char* buffer); // global variables Options options; // database for command-line arguments int voiceQ = 0; // used with -v option int barQ = 0; // used with -b option int mensurationQ = 1; // used with -M option int fileQ = 0; // used with -f option int pathQ = 0; // used with --path option int humdrumQ = 0; // used with -H option string Filename = "STDIN"; // used with -f option int nameQ = 0; // used with -n option Array > Names; // used with -n option int LastDataLine = 0; int markQ = 0; // used with -m option string markSearch = ""; // used with -m option /////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { HumdrumFile infile; // process the command-line options checkOptions(options, argc, argv); // figure out the number of input files to process int numinputs = options.getArgCount(); int i, j; if ((!markQ) && humdrumQ) { printExclusiveInterpretations(); } Array filebuffer; PerlRegularExpression pre; for (i=0; i kerntracks; infile.getTracksByExInterp(kerntracks, "**kern"); Names.setSize(kerntracks.getSize()); for (j=0; j=0; i--) { if (infile[i].isData()) { return i; } } return -1; } /////////////////////////////// // // processVoice2 -- // void processVoice2(int track, int voice, HumdrumFile& infile) { RationalNumber rscale(1,1); int i, j; PerlRegularExpression pre; RationalNumber noteduration; RationalNumber tieduration; char mensuration[1024] = {0}; // double metpos; //int mstart = 1; // double measuresum = 0.0; //int measure = 1; int xtrack; // int content = 0; // int count = 0; int trackcol; // int ignore = 0; char buffer[123123] = {0}; for (i=0; i<=LastDataLine; i++) { if (infile[i].isGlobalComment()) { //if (pre.search(infile[i][0], // "primary-mensuration.*met\\(([^)]+)\\)")) { // strcpy(mensuration, "m("); // strcat(mensuration, pre.getSubmatch(1)); // strcat(mensuration, ")"); // adjustMensuration(mensuration); //} continue; } if (infile[i].isInterpretation()) { for (j=0; j 1) { // content = 1; strcat(buffer, "_"); } mstart = 0; // metpos = infile[i].getBeat(); // noteduration = Convert::kernToDurationR(infile[i][j]); noteduration = infile.getTiedDurationR(i,j); absbeat = infile[i].getAbsBeatR(); if (absbeat + noteduration > nextmeasure) { noteduration = nextmeasure - absbeat; } noteduration *= rscale; dot = getDot(infile[i][j]); if (dot) { noteduration *= 2; noteduration /= 3; } strcat(buffer, getNoteDuration(tbuffer, noteduration, dot)); // content = 1; // cout << metpos; if (strchr(infile[i][j], 'r') != NULL) { // content = 1; strcat(buffer, "r"); } break; } } cleanRhythmString(buffer); } /////////////////////////////// // // processVoice -- // void processVoice(int track, int voice, HumdrumFile& infile) { int i, j; PerlRegularExpression pre; RationalNumber noteduration; RationalNumber tieduration; RationalNumber absbeat; RationalNumber nextmeasure(0,1); RationalNumber rscale(1,1); char durbuffer[128] = {0}; char mensuration[1024] = {0}; // double metpos; int mstart = 1; // double measuresum = 0.0; int measure = 1; int xtrack; int content = 0; int count = 0; int trackcol; char buffer[10123] = {0}; // int ignore = 0; // turned on with *tacet and off with *xtacet for (i=0; i<=LastDataLine; i++) { if (infile[i].isGlobalComment()) { //if (pre.search(infile[i][0], // "primary-mensuration.*met\\(([^)]+)\\)")) { // strcpy(mensuration, "m("); // strcat(mensuration, pre.getSubmatch(1)); // strcat(mensuration, ")"); // adjustMensuration(mensuration); //} continue; } if (infile[i].isInterpretation()) { for (j=0; j 1) { content = 1; // cout << "_"; strcat(buffer, "_"); } mstart = 0; // metpos = infile[i].getBeat(); // noteduration = Convert::kernToDurationR(infile[i][j]); noteduration = infile.getTiedDurationR(i,j); absbeat = infile[i].getAbsBeatR(); if (absbeat + noteduration > nextmeasure) { noteduration = nextmeasure - absbeat; } noteduration *= rscale; dot = getDot(infile[i][j]); if (dot) { noteduration *= 2; noteduration /= 3; } strcat(buffer, getNoteDuration(durbuffer, noteduration, dot)); //cout << getNoteDuration(durbuffer, noteduration, dot); content = 1; // cout << metpos; if (strchr(infile[i][j], 'r') != NULL) { content = 1; strcat(buffer, "r"); // cout << "r"; } break; } } if (strcmp(buffer, "") != 0) { cleanRhythmString(buffer); cout << buffer; } } ////////////////////////////// // // getNextMeasureAbsTime -- // RationalNumber getNextMeasureAbsTime(HumdrumFile& infile, int line, int track) { int i; PerlRegularExpression pre; RationalNumber noteduration; RationalNumber tieduration; RationalNumber nextmeasure; RationalNumber rscale(1,1); int trackcol; int targeti = LastDataLine+1; for (i=line; i<=LastDataLine; i++) { if (infile[i].isMeasure()) { trackcol = infile[i].getTrackColumn(track); if (trackcol < 0) { continue; } if (strcmp(infile[i][trackcol], "=-") == 0) { // invisible barline not at measure boundary, ignore and continue; continue; } if (pre.search(infile[i][trackcol], "[-.]") && (strcmp(infile[i][trackcol], "=1-"))) { // ignore invisible barlines and dotted barlines. continue; } targeti = i; break; } } return infile[targeti].getAbsBeatR(); } ////////////////////////////// // // getDot -- // int getDot(const char* strang) { int len = strlen(strang); int output = 0; int i; for (i=0; i= 16) { strcat(buffer, "l"); } else { strcat(buffer, "X"); sprintf(tbuf, "%dx%d", dur.getNumerator(), dur.getDenominator()); strcat(buffer, tbuf); } // not bothering to check for double dotting... if (dot) { strcat(buffer, "d"); } if (triplet) { strcat(buffer, "3"); } return buffer; } ////////////////////////////// // // checkOptions -- validate and process command-line options. // void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("v|voice=b", "Display voice number"); opts.define("b|bar=b", "Display bar number"); opts.define("H|humdrum=b", "Output in Humdrum format"); opts.define("n|name=b", "Display the name of the parts"); opts.define("p|path=b", "Keep full pathname of file"); opts.define("f|filename=b", "Display filename"); opts.define("M|no-mensuration=b", "Display mensuration"); opts.define("m|mark=s", "Mark given rhythmic pattern"); opts.define("debug=b"); // determine bad input line num opts.define("author=b"); // author of program opts.define("version=b"); // compilation info opts.define("example=b"); // example usages 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, Apr 2012" << endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: 4 Apr 2012" << endl; cout << "compiled: " << __DATE__ << endl; cout << MUSEINFO_VERSION << endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand().c_str()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } voiceQ = opts.getBoolean("voice"); barQ = opts.getBoolean("bar"); fileQ = opts.getBoolean("filename"); pathQ = opts.getBoolean("path"); nameQ = opts.getBoolean("name"); markQ = opts.getBoolean("mark"); markSearch = opts.getString("mark"); humdrumQ = opts.getBoolean("humdrum"); mensurationQ = !opts.getBoolean("no-mensuration"); } ////////////////////////////// // // example -- example usage of the quality program // void example(void) { cout << " \n" << endl; } ////////////////////////////// // // usage -- gives the usage statement for the meter program // void usage(const char* command) { cout << " \n" << endl; } // md5sum: cac82a10d67ba04b1b1ecc91e18d8562 menpat.cpp [20170605]