Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

Chunk.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.IOException;
00026 
00027 import javax.sound.sampled.AudioFormat;
00028 import javax.sound.sampled.AudioInputStream;
00029 import javax.sound.sampled.UnsupportedAudioFileException;
00030 
00037 public abstract class Chunk implements Comparable
00038 {
00039     // Filename of audio file that contains this chunk
00040     public String srcFile;
00041     // Start time (in seconds) of this chunk in srcFile
00042     public double startTime;
00043     // Length (in seconds) of this chunk
00044     public double length;
00045    
00046     // Comment string associated with this chunk - used so that
00047     // FeatExtractor will not strip the comments from a segment file
00048     public String comment = "";
00049 
00053     public Chunk(String sf, double st, double l)
00054     {
00055         srcFile = sf;
00056         startTime = st;
00057         length = l;
00058     }
00059     
00063     public double[] getSamples(AudioFormat format) throws IOException, UnsupportedAudioFileException
00064     {
00065         AudioInputStream ais = MEAPUtil.openInputStream(srcFile, format);
00066 
00067         // this format had better match the output file format or we
00068         // have problems
00069         AudioFormat fmt = ais.getFormat();
00070         
00071         int frameSize = fmt.getFrameSize();
00072         float frameRate = fmt.getFrameRate();
00073         
00074         int startByte = (int)(startTime*frameRate*frameSize);
00075         int byteLen = (int)(length*frameRate*frameSize);
00076         
00077         byte[] bytes = new byte[byteLen];
00078 
00079         //System.out.print("getSamples: ais.skip: "+new Date()+" - ");
00080         ais.skip(startByte);
00081         //System.out.println(new Date());
00082         //System.out.print("getSamples: ais.read: "+new Date()+" - ");
00083         ais.read(bytes, 0, byteLen);
00084         //System.out.println(new Date());
00085         ais.close();
00086         
00087         double[] samples = new double[(int)(frameRate*length)];
00088         MEAPUtil.bytes2doubles(bytes, samples, fmt);
00089 
00090         return samples;
00091     }
00092 
00097     public int compareTo(Object o) throws ClassCastException
00098     {
00099         Chunk c = (Chunk)o;
00100 
00101         int compare = this.srcFile.compareTo(c.srcFile);
00102         if(compare == 0)
00103             compare = Double.compare(this.startTime, c.startTime);
00104 
00105         return compare;
00106     }
00107 
00112     public String toString()
00113     {
00114         // concatenating strings is super slow.  Better to use StringBuffer
00115         // guesstimate the string length
00116         StringBuffer s = new StringBuffer(200);
00117         s.append(srcFile.replaceAll(" ", "%20")).append(" ").append(startTime).append(" ");
00118         s.append(length).append(" ").append(comment).append("\n");
00119         
00120         return s.toString();
00121     }
00122 }
00123 

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