Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

EDLFile.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;
00024 
00025 import java.io.BufferedReader;
00026 import java.io.FileReader;
00027 import java.io.IOException;
00028 import java.io.Writer;
00029 import java.util.Iterator;
00030 import java.util.Vector;
00031 import java.util.regex.Matcher;
00032 
00039 public class EDLFile extends FeatFile //implements Cloneable
00040 {
00041     public EDLFile(String fn)
00042     {
00043         filename = fn;
00044 
00045         // keep chunks somewhat sorted
00046         chunks = new MaxHeap(100);
00047     }
00048 
00052     public void readFile() throws IOException, ParserException
00053     {
00054         BufferedReader in  = new BufferedReader(new FileReader(filename));
00055 
00056         String audioFile="";
00057         double dstTime;
00058         double chunkLength;
00059         double srcTime;
00060 
00061         // each line (excluding comments) should look like: 
00062         // dest_time src_filename src_time chunk_length cmd1 cmd2 ... 
00063 
00064         // Parse each line of the input file            
00065         long lineno = 0;
00066         String line;
00067         while((line = in.readLine()) != null) 
00068         { 
00069             lineno++;
00070             
00071             // extract any comments from the current line
00072             String comment = "";
00073             Matcher c = commentPattern.matcher(line+"\n");
00074             if(c.find())
00075             {
00076                 // comments go all the way to the end of the line
00077                 comment = c.group() + line.substring(c.end()) + "\n";
00078                 line = line.substring(0, c.start());
00079             }
00080 
00081             Matcher p = linePattern.matcher(line);
00082             // is there anything else?
00083             if(!p.find())
00084                 continue;
00085             try { dstTime = Double.parseDouble(p.group(1)); }
00086             catch(NumberFormatException nfe) { 
00087                 throw new ParserException(filename, lineno, 
00088                                           "Could not parse dest start time \"" 
00089                                           + p.group(1)  + "\".");  }
00090 
00091             if(!p.find())
00092                 throw new ParserException(filename, lineno, 
00093                                           "Could not find source filename.");
00094             audioFile = p.group(1);
00095             // decode spaces in the file name
00096             audioFile = audioFile.replaceAll("%20", " ");
00097 
00098             if(!p.find())
00099                 throw new ParserException(filename, lineno, 
00100                                           "Could not find source start time.");
00101             try { srcTime = Double.parseDouble(p.group(1)); }
00102             catch(NumberFormatException nfe) { 
00103                 throw new ParserException(filename, lineno, 
00104                                           "Could not parse source start time \"" 
00105                                           + p.group(1)  + "\".");  }
00106 
00107             if(!p.find())
00108                 throw new ParserException(filename, lineno, 
00109                                           "Could not find chunk length.");
00110             try { chunkLength = Double.parseDouble(p.group(1)); }
00111             catch(NumberFormatException nfe) { 
00112                 throw new ParserException(filename, lineno, 
00113                                           "Could not parse chunk length \"" 
00114                                           + p.group(1)  + "\".");  }
00115             
00116             EDLChunk ch = new EDLChunk(audioFile, srcTime, chunkLength, dstTime);
00117             ch.comment = comment;
00118 
00119             // everything else on the current line is a command
00120             while(p.find())
00121                 ch.commands.add(p.group(1));
00122             
00123             chunks.add(ch);
00124         }
00125         
00126         in.close();
00127         haveReadFile = true;
00128     }
00129     
00134     public double[][] getFeatures()
00135     {
00136         return getFeatures(null);
00137     }
00138 
00143     public double[][] getFeatures(int[] featdim)
00144     {
00145         // want chunks sorted in time;
00146         ((MaxHeap)chunks).sort();
00147 
00148         double[][] f = super.getFeatures(featdim);
00149 
00150         // the above call destroys the Heap property in chunks, need
00151         // to fix it here.
00152         if(!((Heap)chunks).isHeap())
00153             ((MaxHeap)chunks).rebuildHeap();
00154 
00155         return f;
00156     }
00157 
00161         /*
00162         public Object clone()
00163         {
00164                 EDLFile o = new EDLFile(this.filename);
00165        
00166                 // superclass (MEAPFile) fields
00167                 o.haveReadFile = this.haveReadFile;
00168                 o.haveWrittenFile = this.haveWrittenFile;
00169 
00170                 // local fields
00171                 o.featureDescriptions = new Vector(this.featureDescriptions);
00172 
00173                 o.chunks = new Vector();
00174                 Iterator i = this.chunks.iterator();
00175                 while(i.hasNext())
00176                         o.chunks.add(((EDLChunk)i.next()).clone());
00177 
00178                 return o;
00179         }
00180         */
00181         
00185     protected void write(Writer w) throws IOException
00186     {
00187         // write the header
00188         w.write("# dst_time src_filename src_onset_time src_chunk_length [commands]\n");
00189 
00190         // its much easier to read if the output is in sorted in time;
00191         ((MaxHeap)chunks).sort();
00192         
00193         Iterator i = chunks.iterator();
00194         while(i.hasNext())
00195             w.write(i.next().toString());
00196         
00197         // the above call destroys the Heap property in chunks, need to fix it here.
00198         ((MaxHeap)chunks).rebuildHeap();
00199     }
00200 }
00201 

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