1 import java.io.BufferedReader;
2 import java.io.FileInputStream;
3 import java.io.InputStreamReader;
4 import java.io.IOException;
5 import java.io.PrintStream;
7 import java.util.StringTokenizer;
10 public String filename;
12 public int numChannels;
13 public int sampleRate;
16 public Frame[] frames;
18 /* Set up later - says where this track is in the big monolithic
21 public int startIndex;
23 static public final int STS = 1;
24 static public final int MCEP = 2;
26 public Track(String filename, int type) throws IOException {
27 this(filename, type, 0.0f, 0.0f);
30 public Track(String filename, int type, float min, float range)
36 } else if (type == MCEP) {
39 throw new Error("unknown type: " + type);
43 void readSTS(String filename) throws IOException {
44 this.filename = filename;
45 BufferedReader reader =
47 new InputStreamReader(
48 new FileInputStream(filename)));
52 String line = reader.readLine();
53 StringTokenizer tokenizer = new StringTokenizer(line);
54 numFrames = Integer.parseInt(tokenizer.nextToken());
55 numChannels = Integer.parseInt(tokenizer.nextToken());
56 sampleRate = Integer.parseInt(tokenizer.nextToken());
57 min = Float.parseFloat(tokenizer.nextToken());
58 range = Float.parseFloat(tokenizer.nextToken());
60 /* Read in the STS frame data from the file.
62 frames = new STSFrame[numFrames];
63 for (int i = 0; i < numFrames; i++) {
64 frames[i] = new STSFrame(numChannels, reader);
70 void readMCEP(String filename) throws IOException {
71 this.filename = filename;
72 BufferedReader reader =
74 new InputStreamReader(
75 new FileInputStream(filename)));
77 /* Read MCEP meta info
79 String line = reader.readLine();
80 while (!line.equals("EST_Header_End")) {
81 line = reader.readLine();
82 if (line.startsWith("NumFrames")) {
83 numFrames = Integer.parseInt(line.substring(10));
84 } else if (line.startsWith("NumChannels")) {
85 /* With MCEP, the first channel is the energy. We
86 * drop this because it is not used to select units.
88 numChannels = Integer.parseInt(line.substring(12)) - 1;
92 /* Read each of the frames.
94 frames = new Frame[numFrames];
95 for (int i = 0; i < numFrames; i++) {
96 line = reader.readLine();
97 StringTokenizer tokenizer = new StringTokenizer(line);
98 float pitchmarkTime = Float.parseFloat(tokenizer.nextToken());
99 tokenizer.nextToken(); /* no clue what 1 means in the file */
100 int mcepParameters[] = new int[numChannels];
101 for (int j = 0; j < numChannels + 1; j++) {
102 float mcepParameter = Float.parseFloat(tokenizer.nextToken());
104 /* With MCEP, the first channel is the energy. We
105 * drop this because it is not used to select units.
110 /* Normalize the parameter to 0 - 65535.
112 mcepParameters[j - 1] = (int)
113 (65535.0f * (mcepParameter - min) / range);
116 frames[i] = new Frame(pitchmarkTime, mcepParameters);
123 * Returns all the frames for this file.
125 public Frame[] getFrames() {
130 * Finds the index of the frame closest to the given time.
132 * @param time the time in seconds
134 public int findTrackFrameIndex(float time) {
136 while ((frameNum < (frames.length - 1))
137 && (Math.abs(time - frames[frameNum].pitchmarkTime)
138 > Math.abs(time - frames[frameNum + 1].pitchmarkTime))) {
145 * Dumps the ASCII form of this track.
147 public void dumpData(PrintStream out) {
148 for (int i = 0; i < frames.length; i++) {
149 frames[i].dumpData(out);
157 * args[1] = type (1 = STS, 2 = MCEP)
159 static public void main(String[] args) {
161 Track track = new Track(args[0], Integer.parseInt(args[1]));
162 System.out.println(track.filename);
163 System.out.println(track.numFrames);
164 System.out.println(track.numChannels);
165 System.out.println(track.min);
166 System.out.println(track.range);
167 System.out.println(track.frames.length);
168 } catch (Exception e) {