upstream version 1.2.2
[debian/freetts] / tools / ArcticToFreeTTS / src / Track.java
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;
6
7 import java.util.StringTokenizer;
8
9 public class Track {
10     public String filename;
11     public int numFrames;
12     public int numChannels;
13     public int sampleRate;
14     public float min;
15     public float range;   
16     public Frame[] frames;
17
18     /* Set up later - says where this track is in the big monolithic
19      * sts and mcep files.
20      */
21     public int startIndex;
22     
23     static public final int STS = 1;
24     static public final int MCEP = 2;
25     
26     public Track(String filename, int type) throws IOException {
27         this(filename, type, 0.0f, 0.0f);
28     }
29
30     public Track(String filename, int type, float min, float range)
31         throws IOException {
32         this.min = min;
33         this.range = range;
34         if (type == STS) {
35             readSTS(filename);
36         } else if (type == MCEP) {
37             readMCEP(filename);
38         } else {
39             throw new Error("unknown type: " + type);
40         }
41     }
42     
43     void readSTS(String filename) throws IOException {
44         this.filename = filename;
45         BufferedReader reader =
46             new BufferedReader(
47                 new InputStreamReader(
48                     new FileInputStream(filename)));
49
50         /* Read STS meta info
51          */
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());
59         
60         /* Read in the STS frame data from the file.
61          */
62         frames = new STSFrame[numFrames];
63         for (int i = 0; i < numFrames; i++) {
64             frames[i] = new STSFrame(numChannels, reader);
65         }
66         
67         reader.close();
68     }
69
70     void readMCEP(String filename) throws IOException {
71         this.filename = filename;
72         BufferedReader reader =
73             new BufferedReader(
74                 new InputStreamReader(
75                     new FileInputStream(filename)));
76
77         /* Read MCEP meta info
78          */
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.
87                  */
88                 numChannels = Integer.parseInt(line.substring(12)) - 1;
89             }
90         }
91
92         /* Read each of the frames.
93          */
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());
103
104                 /* With MCEP, the first channel is the energy.  We
105                  * drop this because it is not used to select units.
106                  */
107                 if (j == 0) {
108                     continue;
109                 } else {
110                     /* Normalize the parameter to 0 - 65535.
111                      */
112                     mcepParameters[j - 1] = (int)
113                         (65535.0f * (mcepParameter - min) / range);
114                 }
115             }
116             frames[i] = new Frame(pitchmarkTime, mcepParameters);
117         }
118         
119         reader.close();
120     }
121
122     /**
123      * Returns all the frames for this file.
124      */
125     public Frame[] getFrames() {
126         return frames;
127     }
128     
129     /**
130      * Finds the index of the frame closest to the given time.
131      *
132      * @param time the time in seconds
133      */
134     public int findTrackFrameIndex(float time) {
135         int frameNum = 0;
136         while ((frameNum < (frames.length - 1))
137                && (Math.abs(time - frames[frameNum].pitchmarkTime)
138                    > Math.abs(time - frames[frameNum + 1].pitchmarkTime))) {
139             frameNum++;
140         }
141         return frameNum;
142     }
143
144     /**
145      * Dumps the ASCII form of this track.
146      */
147     public void dumpData(PrintStream out) {
148         for (int i = 0; i < frames.length; i++) {
149             frames[i].dumpData(out);
150         }
151     }
152     
153     /**
154      * For testing.
155      *
156      *  args[0] = filename
157      *  args[1] = type (1 = STS, 2 = MCEP)
158      */
159     static public void main(String[] args) {
160         try {
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) {
169             e.printStackTrace();
170         }
171     }
172 }