Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Segmenter.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 gnu.getopt.Getopt;
00026 
00027 import java.io.IOException;
00028 
00029 import javax.sound.sampled.AudioInputStream;
00030 //import featextractors.*;
00031 
00039 public class Segmenter extends MEAPUtil
00040 {
00041     // Files to use
00042     String[] audioFiles;
00043     String outFileName="out.segments";
00044     FeatFile outFile; 
00045     
00046     // Extractor parts
00047     AudioInputStream ais;
00048     STFT stft;
00049     boolean useBeatOD = true;
00050     OnsetDetector detector;
00051     SegmentExtractor extractor;
00052     
00053     boolean onsetAtFirstFrame = false;
00054     
00055     public Segmenter(String[] inFiles, String outputFile)
00056     {
00057         audioFiles = inFiles;
00058 
00059         outFileName = outputFile;
00060         outFile = new FeatFile(outFileName);
00061     }
00062 
00063     public Segmenter(String inFile, String outputFile)
00064     {
00065         audioFiles = new String[1];
00066         audioFiles[0] = inFile;
00067 
00068         outFileName = outputFile;
00069         outFile = new FeatFile(outFileName);
00070     }
00071 
00072     public Segmenter(String[] inFiles, String outputFile, int mixer, double bandThr, double bandFr, boolean beatOD, boolean oaff)
00073     {
00074         this(inFiles, outputFile);
00075 
00076         mixerToUse = mixer;
00077         bandThresh = bandThr;
00078         bandFrac = bandFr;
00079         useBeatOD = beatOD;
00080         onsetAtFirstFrame = oaff;
00081     }
00082 
00083     public Segmenter(String inFile, String outputFile, int mixer, double bandThr, double bandFr, boolean beatOD, boolean oaff)
00084     {
00085         this(inFile, outputFile);
00086 
00087         mixerToUse = mixer;
00088         bandThresh = bandThr;
00089         bandFrac = bandFr;
00090         useBeatOD = beatOD;
00091         onsetAtFirstFrame = oaff;
00092     }
00093 
00094     public void printUsageAndExit() 
00095     {
00096         System.out.println("Usage: Segmenter [-options] source1.wav source2.mp3 ... \n\n" + 
00097                "  where options include:\n" + 
00098                        "    -o output_file  the file to write the output to (defaults to ./out.segments)\n" +
00099                        "    -m N      [0]   use mixer N for input\n" +
00100                        "    -b D.D  [2.0]   fraction of the band mean needed to trigger a band onset\n" +
00101                        "    -f D.D   [.3]   fraction of bands that need to onset for a global onset\n" +
00102                "    -d              use the old style onset detector (defaults to BeatOnsetDetector)\n" +
00103                "    -0              add an onset at the very beginning of the file\n" +
00104                        "");
00105         System.exit(0);
00106     }
00107 
00111     public Segmenter(String[] args) 
00112     {
00113         if(args.length == 0)
00114             printUsageAndExit();
00115         
00116         // Parse arguments
00117         Getopt opt = new Getopt("Segmenter", args, "m:b:f:do:0");
00118         opt.setOpterr(false);
00119         
00120         int c = -1;
00121         while ((c = opt.getopt()) != -1) 
00122         {
00123             switch(c) 
00124             {
00125             case 'm':
00126                 mixerToUse = Integer.parseInt(opt.getOptarg());
00127                 System.out.println("Using mixer " + mixerToUse);
00128                 break;
00129             case 'b':
00130                 bandThresh = Double.parseDouble(opt.getOptarg());
00131                 System.out.println("bandThresh = " + bandThresh);
00132                 break;
00133             case 'f':
00134                 bandFrac = Double.parseDouble(opt.getOptarg());
00135                 System.out.println("bandFrac = " + bandFrac);
00136                 break;
00137             case 'd':
00138                 useBeatOD = false;
00139                 break;
00140             case 'o':
00141                 outFileName = opt.getOptarg();
00142                 break;
00143             case '0':
00144                 onsetAtFirstFrame = true;
00145                 break;
00146             case '?':
00147                 printUsageAndExit();
00148                 break;
00149             default:
00150                 System.out.print("getopt() returned " + c + "\n");
00151             }
00152         }
00153         
00154         // parse arguments
00155         int ind = opt.getOptind();
00156         if(ind > args.length)
00157             printUsageAndExit();
00158         
00159         audioFiles = new String[args.length - ind];
00160         for(int i=ind; i<args.length; i++)
00161             audioFiles[i-ind] = args[i];
00162 
00163         outFile = new FeatFile(outFileName);
00164     }
00165 
00166     public FeatFile processAudioFile()
00167     {
00168         return processAudioFile(outFile.filename);
00169     }
00170 
00171     public FeatFile processAudioFile(String fn)
00172     {
00173         // Create Extractor
00174         ais = openInputStream(fn);
00175         
00176         stft = new STFT(ais, frameSize, frameLatency);
00177         if(useBeatOD)
00178             detector = new BeatOnsetDetector(stft, bandThresh, bandFrac);
00179         else
00180             detector = new OnsetDetector(stft, bandThresh, bandFrac);
00181         extractor = new SegmentExtractor(stft, detector, fn, outFile);
00182         detector.addOnsetListener(extractor);
00183 
00184         // Hook things together
00185         stft.addFrameListener(extractor);
00186         
00187         // insert an onset at frame 0?
00188         if(onsetAtFirstFrame) 
00189             extractor.newOnset(0, 0);
00190 
00191         // Extract the entire source file
00192         extractor.run();
00193 
00194         // outFile now contains some chunks.
00195         outFile.haveReadFile = true;
00196 
00197         return outFile;
00198     }
00199    
00203     public void go() 
00204     {
00205         // process supplied files
00206         for(int i=0; i<audioFiles.length; i++) 
00207         {
00208             System.out.println("Writing segments from " + audioFiles[i] + " to " 
00209                                + outFileName + ".");
00210             
00211             long startTime = System.currentTimeMillis();
00212             processAudioFile(audioFiles[i]);
00213 
00214             try 
00215             { 
00216                 outFile.writeFile();
00217             }
00218             catch(IOException e)
00219             { 
00220                 e.printStackTrace();
00221                 System.exit(1);
00222             }
00223             
00224             System.out.println("Done.  Took " + 
00225                                ((System.currentTimeMillis() - startTime)/1000.0) 
00226                                + "s");
00227         }
00228     }
00229 
00230     public static void main(String[] args) 
00231     {
00232         Segmenter o2or = new Segmenter(args);
00233         o2or.go();
00234         System.exit(0);
00235     }
00236 }

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