Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Chunk.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.IOException;
00026 
00027 import javax.sound.sampled.AudioFormat;
00028 import javax.sound.sampled.AudioInputStream;
00029 
00036 public abstract class Chunk implements Comparable
00037 {
00038     public String srcFile;
00039     public double startTime;
00040     public double length;
00041    
00042     public String comment = "";
00043 
00047     public Chunk(String sf, double st, double l)
00048     {
00049         srcFile = sf;
00050         startTime = st;
00051         length = l;
00052     }
00053     
00057     public double[] getSamples(AudioFormat format) throws IOException
00058     {
00059         AudioInputStream ais = MEAPUtil.openInputStream(srcFile, format);
00060 
00061         // this format had better match the output file format or we
00062         // have problems
00063         AudioFormat fmt = ais.getFormat();
00064         
00065         int frameSize = fmt.getFrameSize();
00066         float frameRate = fmt.getFrameRate();
00067         
00068         int startByte = (int)(startTime*frameRate*frameSize);
00069         int byteLen = (int)(length*frameRate*frameSize);
00070         
00071         byte[] bytes = new byte[byteLen];
00072 
00073         //System.out.print("getSamples: ais.skip: "+new Date()+" - ");
00074         ais.skip(startByte);
00075         //System.out.println(new Date());
00076         //System.out.print("getSamples: ais.read: "+new Date()+" - ");
00077         ais.read(bytes, 0, byteLen);
00078         //System.out.println(new Date());
00079         ais.close();
00080         
00081         double[] samples = new double[(int)(frameRate*length)];
00082         MEAPUtil.bytes2doubles(bytes, samples, fmt);
00083 
00084         return samples;
00085     }
00086 
00091     public int compareTo(Object o) throws ClassCastException
00092     {
00093         Chunk c = (Chunk)o;
00094         return Double.compare(startTime, c.startTime);
00095     }
00096 
00097     public String toString()
00098     {
00099         // concatenating strings is super slow.  Better to use StringBuffer
00100         // guesstimate the string length
00101         StringBuffer s = new StringBuffer(200);
00102         s.append(srcFile).append(" ").append(startTime).append(" ");
00103         s.append(length).append(" ").append(comment).append("\n");
00104         
00105         return s.toString();
00106     }
00107 }
00108 

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