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
00052 public class ThresholdComposer extends Composer
00053 {
00054 public static String description = "ThresholdComposer selects chunks with feature values falling " +
00055 "inside the top and bottom thresholds. It then creates an output file composed exclusively of either " +
00056 "the selected chunks or the not-selected chunks. ThresholdComposer only really makes sense for " +
00057 "one-dimensional features like pitch and power.";
00058
00059 String outFileName = "threshold.edl";
00060
00061 FeatFile featFile;
00062 boolean debug = false;
00063
00064 double thresholdTop = 50.0;
00065 double thresholdBottom = 25.0;
00066 boolean insideThreshold = true;
00067
00068 public ThresholdComposer(String featFN, String outFN, double thresholdTop, double thresholdBottom, boolean insideThreshold)
00069 {
00070 this(new FeatFile(featFN), new EDLFile(outFN), thresholdTop, thresholdBottom, insideThreshold);
00071 }
00072
00073 public ThresholdComposer(FeatFile featFN, EDLFile outFN, double thresholdTop, double thresholdBottom, boolean insideThreshold)
00074 {
00075 if (featFN == null || outFN == null)
00076 return;
00077
00078 featFile = featFN;
00079 outFile = outFN;
00080 this.thresholdTop = thresholdTop;
00081 this.thresholdBottom = thresholdBottom;
00082 this.insideThreshold = insideThreshold;
00083
00084
00085 if(outFile == null)
00086 outFile = new EDLFile("");
00087 }
00088
00089 public void printUsageAndExit()
00090 {
00091 System.out.println("Usage: ThresholdComposer [-options] features.feat \n\n" +
00092 " where options include:\n" +
00093 " -o output_file the file to write the output to (defaults to shuffle.edl)\n" +
00094 " -t threshold top sets the top threshold\n" +
00095 " -b threshold bottom sets the bottom threshold\n" +
00096 " -e exclude chunks inside the thresholds; default is to include chunks\n" +
00097 " -g debug mode (prints out chunk features on each line of output file)");
00098 System.out.println();
00099 System.exit(0);
00100 }
00101
00102 public ThresholdComposer(String[] args)
00103 {
00104 if(args.length == 0)
00105 printUsageAndExit();
00106
00107 Vector features = new Vector();
00108
00109
00110 String argString = "o:t:b:e:g";
00111
00112 Getopt opt = new Getopt("ThresholdComposer", args, argString);
00113 opt.setOpterr(false);
00114
00115 int c = -1;
00116 while ((c =opt.getopt()) != -1)
00117 {
00118 switch(c)
00119 {
00120 case 'o':
00121 outFileName = opt.getOptarg();
00122 break;
00123 case 't':
00124 thresholdTop = new Double(opt.getOptarg()).doubleValue();
00125 case 'b':
00126 thresholdBottom = new Double(opt.getOptarg()).doubleValue();
00127 case 'e':
00128 insideThreshold = false;
00129 case 'g':
00130 debug = true;
00131 break;
00132 case '?':
00133 printUsageAndExit();
00134 break;
00135 default:
00136 System.out.print("getopt() returned " + c + "\n");
00137 }
00138 }
00139
00140
00141 int ind = opt.getOptind();
00142 if(ind > args.length)
00143 printUsageAndExit();
00144
00145 featFile = new FeatFile(args[args.length-1]);
00146 outFile = new EDLFile(outFileName);
00147
00148 System.out.println("Composing " + outFileName +
00149 " from " + args[args.length-1] + ".");
00150 }
00151
00152 public void setup() throws IOException, ParserException
00153 {
00154 super.setup();
00155
00156 if(!featFile.haveReadFile)
00157 featFile.readFile();
00158
00159 if(featFile.chunks.size() == 0)
00160 throw new ParserException(featFile.filename, "No chunks found");
00161
00162 progress.setMaximum(featFile.chunks.size());
00163 }
00164
00165
00166 public EDLFile compose()
00167 {
00168 Iterator c = featFile.chunks.iterator();
00169 double currTime = 0;
00170
00171
00172 while(c.hasNext())
00173 {
00174
00175 FeatChunk ch = (FeatChunk)c.next();
00176
00177 double[] feats = ch.getFeatures();
00178
00179
00180
00181
00182
00183
00184 boolean inSweetSpot = false;
00185
00186
00187 if (feats[0] > thresholdBottom && feats[0] < thresholdTop)
00188 inSweetSpot = true;
00189
00190
00191
00192
00193 if (inSweetSpot && insideThreshold)
00194 {
00195
00196 EDLChunk newChunk = new EDLChunk(ch, currTime);
00197 outFile.chunks.add(newChunk);
00198 currTime += ch.length;
00199
00200 }
00201
00202 else if (!inSweetSpot && !insideThreshold)
00203 {
00204
00205 EDLChunk newChunk = new EDLChunk(ch, currTime);
00206 outFile.chunks.add(newChunk);
00207 currTime += ch.length;
00208
00209 }
00210
00211
00212
00213 progress.setValue(progress.getValue()+1);
00214 }
00215
00216
00217 outFile.haveReadFile = true;
00218
00219 return outFile;
00220 }
00221
00222 public static void main(String[] args)
00223 {
00224 ThresholdComposer m = new ThresholdComposer(args);
00225 long startTime = System.currentTimeMillis();
00226 m.run();
00227 System.out.println("Done. Took " +
00228 ((System.currentTimeMillis() - startTime)/1000.0)
00229 + "s");
00230 System.exit(0);
00231 }
00232 }