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 import javax.sound.sampled.UnsupportedAudioFileException;
00030
00037 public abstract class Chunk implements Comparable
00038 {
00039
00040 public String srcFile;
00041
00042 public double startTime;
00043
00044 public double length;
00045
00046
00047
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
00068
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
00080 ais.skip(startByte);
00081
00082
00083 ais.read(bytes, 0, byteLen);
00084
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
00115
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