00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package com.meapsoft.composers;
00024
00025 import gnu.getopt.Getopt;
00026
00027 import java.io.IOException;
00028 import java.util.Iterator;
00029 import java.util.Vector;
00030
00031 import com.meapsoft.EDLChunk;
00032 import com.meapsoft.EDLFile;
00033 import com.meapsoft.FeatChunk;
00034 import com.meapsoft.FeatFile;
00035 import com.meapsoft.ParserException;
00036
00042 public class RotComposer extends Composer
00043 {
00044 public static String description = "RotComposer rotates the beats in each measure" +
00045 " by a selectable number of positions. You can set the number of beats/measure, the number " +
00046 "of positions to rotate, and the direction of rotation.";
00047
00048 String outFileName = "rot.edl";
00049
00050 FeatFile featFile;
00051 boolean debug = false;
00052
00053 int beatsPerMeasure = 4;
00054 int numPositions = 1;
00055 boolean rotateRight = true;
00056
00057 public RotComposer(String featFN, String outFN, int beatsPerMeasure, int numPositions, boolean left)
00058 {
00059 this(new FeatFile(featFN), new EDLFile(outFN), beatsPerMeasure, numPositions, left);
00060 }
00061
00062 public RotComposer(FeatFile featFN, EDLFile outFN, int beatsPerMeasure, int numPositions, boolean left)
00063 {
00064 if (featFN == null || outFN == null)
00065 return;
00066
00067 featFile = featFN;
00068 outFile = outFN;
00069
00070 if(outFile == null)
00071 outFile = new EDLFile("");
00072
00073 this.beatsPerMeasure = beatsPerMeasure;
00074 this.numPositions = numPositions;
00075 rotateRight = !left;
00076 }
00077
00078 public void printUsageAndExit()
00079 {
00080 System.out.println("Usage: RotComposer [-options] features.feat \n\n" +
00081 " where options include:\n" +
00082 " -b beats_per_measure the number of beats in a measure (defaults to four)\n" +
00083 " -n num_positions the number of positions to rotate (defaults to one)\n" +
00084 " -l rotate left (default is rotate right)" +
00085 " -o output_file the file to write the output to (defaults to shuffle.edl)\n" +
00086 " -g debug mode (prints out chunk features on each line of output file)");
00087 System.out.println();
00088 System.exit(0);
00089 }
00090
00091 public RotComposer(String[] args)
00092 {
00093 if(args.length == 0)
00094 printUsageAndExit();
00095
00096 Vector features = new Vector();
00097
00098
00099 String argString = "b:n:l:o:g";
00100
00101 Getopt opt = new Getopt("RotComposer", args, argString);
00102 opt.setOpterr(false);
00103
00104 int c = -1;
00105 while ((c =opt.getopt()) != -1)
00106 {
00107 switch(c)
00108 {
00109 case 'b':
00110 beatsPerMeasure = new Integer(opt.getOptarg()).intValue();
00111 break;
00112 case 'n':
00113 numPositions = new Integer(opt.getOptarg()).intValue();
00114 break;
00115 case 'l':
00116 rotateRight = false;
00117 break;
00118 case 'o':
00119 outFileName = opt.getOptarg();
00120 break;
00121 case 'g':
00122 debug = true;
00123 break;
00124 case '?':
00125 printUsageAndExit();
00126 break;
00127 default:
00128 System.out.print("getopt() returned " + c + "\n");
00129 }
00130 }
00131
00132
00133 int ind = opt.getOptind();
00134 if(ind > args.length)
00135 printUsageAndExit();
00136
00137 featFile = new FeatFile(args[args.length-1]);
00138 outFile = new EDLFile(outFileName);
00139
00140 System.out.println("Composing " + outFileName +
00141 " from " + args[args.length-1] + ".");
00142 }
00143
00144 public void setup() throws IOException, ParserException
00145 {
00146 super.setup();
00147
00148 if(!featFile.haveReadFile)
00149 featFile.readFile();
00150
00151 if(featFile.chunks.size() == 0)
00152 throw new ParserException(featFile.filename, "No chunks found");
00153
00154 progress.setMaximum(featFile.chunks.size());
00155 }
00156
00157
00158 public EDLFile compose()
00159 {
00160 Iterator c = featFile.chunks.iterator();
00161 int totalChunks = featFile.chunks.size();
00162
00163 double currTime = 0;
00164 int currStartBeat = 0;
00165 int localStartBeat = 0;
00166 int currPosition = 0;
00167
00168 if (rotateRight)
00169 localStartBeat = beatsPerMeasure - numPositions;
00170 else
00171 localStartBeat = numPositions;
00172
00173
00174
00175 for (int measure = 0; measure < totalChunks/beatsPerMeasure; measure++)
00176 {
00177 FeatChunk chunks[] = new FeatChunk[beatsPerMeasure];
00178 int numBeatsFound = 0;
00179
00180 for (int i = 0; i < beatsPerMeasure; i++)
00181 chunks[i] = (FeatChunk)c.next();
00182
00183 currPosition = localStartBeat;
00184
00185 for (int i = 0; i < beatsPerMeasure; i++)
00186 {
00187
00188
00189 EDLChunk newChunk = new EDLChunk(chunks[currPosition], currTime);
00190 outFile.chunks.add(newChunk);
00191 currTime += newChunk.length;
00192
00193 currPosition++;
00194 currPosition %= beatsPerMeasure;
00195 }
00196
00197 progress.setValue(progress.getValue()+1);
00198 }
00199
00200
00201 outFile.haveReadFile = true;
00202
00203 return outFile;
00204 }
00205
00206 public static void main(String[] args)
00207 {
00208 RotComposer m = new RotComposer(args);
00209 long startTime = System.currentTimeMillis();
00210 m.run();
00211 System.out.println("Done. Took " +
00212 ((System.currentTimeMillis() - startTime)/1000.0)
00213 + "s");
00214 System.exit(0);
00215 }
00216 }