// // Programmer: Craig Stuart Sapp // Creation Date: Sat May 1 16:20:50 PDT 2004 // Last Modified: Sat May 1 16:20:52 PDT 2004 // Last Modified: Wed May 27 01:00:49 PDT 2009 (fixed out filename in cwd) // Filename: ...museinfo/examples/all/thememakerx.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/thememakerx.cpp // Syntax: C++; museinfo // // Description: Extract incipits from monophonic Humdrum files which contain // phrase markings (preferrably). // #include "humdrum.h" #ifndef OLDCPP #include #include using namespace std; #else #include #include #endif // includes needed for file/directory processing: #include #include #include #include // function declarations: void checkOptions (Options& opts, int argc, char** argv); void example (void); void usage (const char* command); void createIncipit (HumdrumFile& hfile, const char* filename, const char* sourcebase, int limit); void extractPitchSequence (Array& pitches, Array& phrase, HumdrumFile& hfile); int is_directory (const char* path); int is_file (const char* path); void processArgument (const char* path); void createOutputName (char* outfile, const char* filename, const char* target, const char* sourcebase); void checkTargetDirectory (const char* outfile); void getPhrasesAndNoteLines (Array& lines, Array& phrase, HumdrumFile& hfile); int getEndingLine (Array& lines, Array& phrase, HumdrumFile& hfile, int limit); // User interface variables: Options options; int debugQ = 0; // used with --debug option int limitQ = 0; // used with -l option int limit = 30; // used with -l option int minval = 10; // used with -m option const char* target = "."; // used with -b option char sourcebase[2048] = {0}; ////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { checkOptions(options, argc, argv); // process the command-line options int i; int numinputs = options.getArgCount(); HumdrumFile hfile; strcpy(sourcebase, "."); for (i=0; id_name, ".", 1) == 0) { entry = readdir(dir); continue; } fullname = new char[strlen(path) + 1 + strlen(entry->d_name) + 1]; strcpy(fullname, path); strcat(fullname, "/"); strcat(fullname, entry->d_name); processArgument(fullname); entry = readdir(dir); } } closedir(dir); } ////////////////////////////// // // is_file -- returns true if the string is a file. // int is_file(const char* path) { struct stat filestat; if (stat(path, &filestat)) { return 0; } return S_ISREG(filestat.st_mode); } ////////////////////////////// // // is_directory -- returns true if the string is a directory. // int is_directory(const char* path) { struct stat filestat; if (stat(path, &filestat)) { return 0; } return S_ISDIR(filestat.st_mode); } ////////////////////////////// // // createIncipit -- // void createIncipit(HumdrumFile& hfile, const char* filename, const char* sourcebase, int limit) { char outfile[4096] = {0}; createOutputName(outfile, filename, target, sourcebase); cout << "creating incipit for: " << filename << "\toutput: " << outfile << endl; fstream output; output.open(outfile, ios::out); if (!output.is_open()) { cout << "Error opening file to write: " << outfile << endl; exit(1); } Array lines; Array phrase; getPhrasesAndNoteLines(lines, phrase, hfile); int endline = getEndingLine(lines, phrase, hfile, limit); int i; for (i=0; i& lines, Array& phrase, HumdrumFile& hfile, int limit) { int i; int start = lines.getSize()-1; if (start > limit) { start = limit; } int phraseboundary = start; if (phrase[phraseboundary] <= 1) { if (phraseboundary < minval) { phraseboundary = limit; } if (phraseboundary >= lines.getSize()) { phraseboundary = lines.getSize()-1; } return lines[phraseboundary]; } for (i=start; i>=0; i--) { if (i>0 && (phrase[i] != phrase[i-1])) { phraseboundary = i; break; } } if (phraseboundary < minval) { phraseboundary = limit; } if (phraseboundary >= lines.getSize()) { phraseboundary = lines.getSize()-1; } return lines[phraseboundary]; } ////////////////////////////// // // getPhrasesAndNoteLines -- // void getPhrasesAndNoteLines(Array& lines, Array& phrase, HumdrumFile& hfile) { lines.setSize(hfile.getNumLines()); lines.setSize(0); phrase.setSize(hfile.getNumLines()); phrase.setSize(0); int pcount = 0; int i; for (i=0; i& pitches, HumdrumFile& hfile) { pitches.setSize(10000); pitches.setGrowth(10000); pitches.setSize(0); pitches.allowGrowth(); int pitch = 0; int i; for (i=0; i 10000)) { // ignore rests and other strange things break; } pitches.append(pitch); if (limitQ) { if (pitches.getSize() >= limit) { return; } } break; default: break; } } } ////////////////////////////// // // checkOptions -- // void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("debug=b", "print debug information"); opts.define("l|limit=i:30", "upper limit the number of extracted notes"); opts.define("m|min=i:10", "lower limit on number of extracted notes"); opts.define("t|target=s:.", "filename target base for recursive output"); 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, May 2004" << endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: 1 May 2004" << endl; cout << "compiled: " << __DATE__ << endl; cout << MUSEINFO_VERSION << endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand().data()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } debugQ = opts.getBoolean("debug"); limitQ = opts.getBoolean("limit"); limit = opts.getInteger("limit"); minval = opts.getInteger("min"); target = opts.getString("target").data(); if (!is_directory(target)) { cout << "Error: target directory does not exist: " << target << endl; exit(1); } } ////////////////////////////// // // example -- // void example(void) { } ////////////////////////////// // // usage -- // void usage(const char* command) { } // md5sum: ad9a3973b510c8d54c1c04aa0afb2df1 thememakerx.cpp [20151120]