//Public class for reading in lilypond (.ly) files containing a sequence of chords //Please take note of the following input file requirements. Lilypond files not following these requirements or are erraneous to begin with will not run correctly and may crash. //File Requirements: /*One command variable per line, leave a space between EVERY SINGLE ELEMENT(even a bracket) -Only accept chord sequences written in the lilypond chord mode (i.e. anything not in chord mode will not be read) -Be sure to enclose contents in chord mode with two curly brackets. (this is part of lilypond requirement) -Only full bars will work accurately. Do not truncate bars or overload bars. (this is allowed in lilypond printing, but screws up playback) -Using a variable and then repeating it will not work. (only sequences following \chordname, \key and \time will be recognized) -Only accept basic notation with time and key signatures. Ornaments, ties, slurs and other expressions will not be read or recognized. -relative c or other octave markings as well as clef are overlooked (only the notes count, the octave does not matter because the variation will change it anyway) -only 3/4, 4/4 accepted (for now).This is because variations of time signature will also be used as part of the variations. -only notes longer than or equal to a quarter note/quaver is accepted. This is for easy of computation of rhythms in the variations and also to allow for flexibility. (Don't make my life so difficult yeah?) -END on a FULL BAR please. (one chord. ONE SINGLE CHORD.) //Also parses and stores the following information for use later: key and time signature. */ //this class is adapted from the examples on CCRMA 220a Github by Hong Chan: token.ck, read-str.ck public class fileReader { // instance variables FileIO lily; string lilyHolder; StringTokenizer tok; string rawChords[100]; int numChords; //Key Signature variable. Default to C Major "c \\major" => string key; //Time Signature variable. Default to 4/4 "4/4" =>string time; //loads file and reads content into respective variables. //returns 1 upon success file read. fun int load(string filename) { // open a file lily.open( filename, FileIO.READ); // ensure it's ok if( !lily.good() ) { cherr <= "Cannot open file: " <= filename <= IO.newline(); me.exit(); } // read until end and save in lilyHolder. // this solves the backslash = escape sequence issue. while(lily.more()) { lily.readLine() => lilyHolder; tok.set( lilyHolder ); while( tok.more() ) { tok.next() =>string marker; if(marker == "\\time") { tok.next() => time; } if(marker == "\\key") { tok.next() => key; " " + tok.next() +=> key; } if(marker == "\\chordmode") { tok.next(); while((tok.next()=> string chordSeq)!= "}") { chordSeq => rawChords[numChords]; numChords++; } if(numChords == 0) { cherr <= "You didn't put in any chords. " <= IO.newline(); me.exit(); } } } } return 1; } //returns time signature for printing purposes fun string getTimeSig() { return time; } //returns time signature information fun int getNom() { return Std.atoi(time); } fun int getDenom() { return Std.atoi(time); } //returns the raw chord array fun string[] getRaw() { return rawChords; } //returns the number of chords scanned in fun int getNumChords() { return numChords; } //returns key signature for printing purposes fun string getKeySig() { return key; } }