00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00062
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
00074 ais.skip(startByte);
00075
00076
00077 ais.read(bytes, 0, byteLen);
00078
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
00100
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