Main Page   Class Hierarchy   Compound List   File List   Compound Members  

EDLFile.java

00001 /*
00002  *  Copyright 2006 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.regex.Matcher;
00031 
00038 public class EDLFile extends FeatFile
00039 {
00040     public EDLFile(String fn)
00041     {
00042         filename = fn;
00043 
00044         // keep chunks somewhat sorted
00045         chunks = new MaxHeap(100);
00046     }
00047 
00051     public void readFile() throws IOException, ParserException
00052     {
00053         BufferedReader in  = new BufferedReader(new FileReader(filename));
00054 
00055         String audioFile="";
00056         double dstTime;
00057         double chunkLength;
00058         double srcTime;
00059 
00060         // each line (excluding comments) should look like: 
00061         // dest_time src_filename src_time chunk_length cmd1 cmd2 ... 
00062 
00063         // Parse each line of the input file            
00064         long lineno = 0;
00065         String line;
00066         while((line = in.readLine()) != null) 
00067         { 
00068             lineno++;
00069             
00070             // extract any comments from the current line
00071             String comment = "";
00072             Matcher c = commentPattern.matcher(line+"\n");
00073             if(c.find())
00074             {
00075                 // comments go all the way to the end of the line
00076                 comment = c.group() + line.substring(c.end()) + "\n";
00077                 line = line.substring(0, c.start());
00078             }
00079 
00080             Matcher p = linePattern.matcher(line);
00081             // is there anything else?
00082             if(!p.find())
00083                 continue;
00084             try { dstTime = Double.parseDouble(p.group(1)); }
00085             catch(NumberFormatException nfe) { 
00086                 throw new ParserException(filename, lineno, 
00087                                           "Could not parse dest start time \"" 
00088                                           + p.group(1)  + "\".");  }
00089 
00090             if(!p.find())
00091                 throw new ParserException(filename, lineno, 
00092                                           "Could not find source filename.");
00093             audioFile = p.group(1);
00094 
00095             if(!p.find())
00096                 throw new ParserException(filename, lineno, 
00097                                           "Could not find source start time.");
00098             try { srcTime = Double.parseDouble(p.group(1)); }
00099             catch(NumberFormatException nfe) { 
00100                 throw new ParserException(filename, lineno, 
00101                                           "Could not parse source start time \"" 
00102                                           + p.group(1)  + "\".");  }
00103 
00104             if(!p.find())
00105                 throw new ParserException(filename, lineno, 
00106                                           "Could not find chunk length.");
00107             try { chunkLength = Double.parseDouble(p.group(1)); }
00108             catch(NumberFormatException nfe) { 
00109                 throw new ParserException(filename, lineno, 
00110                                           "Could not parse chunk length \"" 
00111                                           + p.group(1)  + "\".");  }
00112             
00113             EDLChunk ch = new EDLChunk(audioFile, srcTime, chunkLength, dstTime);
00114             ch.comment = comment;
00115 
00116             // everything else on the current line is a command
00117             while(p.find())
00118                 ch.commands.add(p.group(1));
00119             
00120             chunks.add(ch);
00121         }
00122         
00123         in.close();
00124         haveReadFile = true;
00125     }
00126     
00127 
00128     public double[][] getFeatures(int[] featdim)
00129     {
00130         // want chunks sorted in time;
00131         ((MaxHeap)chunks).sort();
00132 
00133         double[][] f = super.getFeatures(featdim);
00134 
00135         // the above call destroys the Heap property in chunks, need to fix it here.
00136         ((MaxHeap)chunks).rebuildHeap();
00137 
00138         return f;
00139     }
00140 
00144     protected void write(Writer w) throws IOException
00145     {
00146         // write the header
00147         w.write("# dst_time src_filename src_onset_time src_chunk_length [commands]\n");
00148 
00149         // its much easier to read if the output is in sorted in time;
00150         ((MaxHeap)chunks).sort();
00151         
00152         Iterator i = chunks.iterator();
00153         while(i.hasNext())
00154             w.write(i.next().toString());
00155         
00156         // the above call destroys the Heap property in chunks, need to fix it here.
00157         ((MaxHeap)chunks).rebuildHeap();
00158     }
00159 }
00160 

Generated on Thu May 11 15:04:10 2006 for MEAPsoft by doxygen1.2.18