Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

ThresholdComposer.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
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                 // Parse arguments
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                 // parse arguments
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                 //iterate through all the chunks that the segmenter found
00172                 while(c.hasNext())
00173                 {
00174                         //your current features chunk
00175                         FeatChunk ch = (FeatChunk)c.next();
00176                         
00177                         double[] feats = ch.getFeatures();
00178                         
00179                         //System.out.println("feature[0]: " + feats[0]);
00180                         //System.out.println("insideThreshold: " +  insideThreshold + " thresholdBottom: " + thresholdBottom +
00181                         //      " thresholdTop: " + thresholdTop);
00182                         
00183                         //sweetspot is area between thresholds
00184                         boolean inSweetSpot = false;
00185                         
00186                         //see if we're in the sweet spot
00187                         if (feats[0] > thresholdBottom && feats[0] < thresholdTop)
00188                                 inSweetSpot = true;
00189                         
00190                         //System.out.println("inSweetSpot: " + inSweetSpot);
00191                         
00192                         //if we want chunks that are in the sweetspot
00193                         if (inSweetSpot && insideThreshold)
00194                         {
00195 //                              make a new EDL chunk from the current features chunk
00196                                 EDLChunk newChunk = new EDLChunk(ch, currTime);
00197                                 outFile.chunks.add(newChunk);
00198                                 currTime += ch.length;
00199                                 //System.out.println("inSweetSpot && insideThreshold, so writing chunk!");
00200                         }
00201                         //if we want chunks that are not in the sweetspot
00202                         else if (!inSweetSpot && !insideThreshold)
00203                         {
00204 //                              make a new EDL chunk from the current features chunk
00205                                 EDLChunk newChunk = new EDLChunk(ch, currTime);
00206                                 outFile.chunks.add(newChunk);
00207                                 currTime += ch.length;
00208                                 //System.out.println("!inSweetSpot && !insideThreshold, so writing chunk!");
00209                         }
00210                         //else
00211                                 //System.out.println("not writing chunk!!!");
00212                                 
00213             progress.setValue(progress.getValue()+1);   
00214                 }
00215 
00216                 // outFile now contains some chunks.
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 }

Generated on Tue Feb 6 19:02:27 2007 for MEAPsoft by doxygen1.2.18