1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 1999 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black (awb@cs.cmu.edu) */
34 /* Date: August 2000 */
35 /*************************************************************************/
36 /* */
37 /* Track i/o */
38 /* */
39 /*************************************************************************/
40 #include "cst_string.h"
41 #include "cst_endian.h"
42 #include "cst_tokenstream.h"
43 #include "cst_track.h"
44
cst_track_save_est(cst_track * t,const char * filename)45 int cst_track_save_est(cst_track *t, const char *filename)
46 {
47 cst_file fd;
48 int i,j;
49
50 if ((fd = cst_fopen(filename,CST_OPEN_WRITE|CST_OPEN_BINARY)) == NULL)
51 {
52 cst_errmsg("cst_track_save_est: can't open file \"%s\"\n",
53 filename);
54 return -1;
55 }
56
57 cst_fprintf(fd,"EST_File Track\n");
58 cst_fprintf(fd,"DataType ascii\n");
59 cst_fprintf(fd,"NumFrames %d\n",t->num_frames);
60 cst_fprintf(fd,"NumChannels %d\n",t->num_channels);
61 cst_fprintf(fd,"BreaksPresent true\n");
62 cst_fprintf(fd,"EST_Header_End\n");
63
64 for (i=0; i < t->num_frames; i++)
65 {
66 cst_fprintf(fd,"%f\t1 \t",t->times[i]);
67 for (j=0; j < t->num_channels; j++)
68 cst_fprintf(fd,"%f ",t->frames[i][j]);
69 cst_fprintf(fd,"\n");
70 }
71
72 cst_fclose(fd);
73
74 return 0;
75 }
76
cst_track_save_est_binary(cst_track * t,const char * filename)77 int cst_track_save_est_binary(cst_track *t, const char *filename)
78 {
79 cst_file fd;
80 float foo;
81 int i,j;
82
83 if ((fd = cst_fopen(filename,CST_OPEN_WRITE|CST_OPEN_BINARY)) == NULL)
84 {
85 cst_errmsg("cst_track_save_est_binary: can't open file \"%s\"\n",
86 filename);
87 return -1;
88 }
89
90 cst_fprintf(fd,"EST_File Track\n");
91 cst_fprintf(fd,"DataType binary\n");
92 cst_fprintf(fd,"ByteOrder %s\n",
93 CST_LITTLE_ENDIAN ? BYTE_ORDER_LITTLE : BYTE_ORDER_BIG);
94 cst_fprintf(fd,"NumFrames %d\n",t->num_frames);
95 cst_fprintf(fd,"NumChannels %d\n",t->num_channels);
96 cst_fprintf(fd,"BreaksPresent true\n");
97 cst_fprintf(fd,"EST_Header_End\n");
98
99 foo = 1.0; /* put a bogus 'breaks' value in for now */
100 for (i=0; i < t->num_frames; i++)
101 {
102 cst_fwrite(fd, t->times + i, sizeof(float), 1);
103 cst_fwrite(fd, &foo, sizeof(float), 1);
104 for (j=0; j < t->num_channels; j++)
105 cst_fwrite(fd, &(t->frames[i][j]), sizeof(float), 1);
106 }
107
108 cst_fclose(fd);
109
110 return 0;
111 }
112
load_frame_ascii(cst_track * t,int i,cst_tokenstream * ts)113 static int load_frame_ascii(cst_track *t, int i, cst_tokenstream *ts)
114 {
115 int j;
116
117 t->times[i] = cst_atof(ts_get(ts));
118 ts_get(ts); /* the can be only 1 */
119 for (j=0; j < t->num_channels; j++)
120 t->frames[i][j] = cst_atof(ts_get(ts));
121 if ((i+1 < t->num_frames) && (ts_eof(ts)))
122 {
123 return -1;
124 }
125 return 0;
126 }
127
load_frame_binary(cst_track * t,int i,cst_tokenstream * ts,int swap)128 static int load_frame_binary(cst_track *t, int i, cst_tokenstream *ts, int swap)
129 {
130 float val;
131 int j;
132
133 if (cst_fread(ts->fd, &val, sizeof(float), 1) != 1)
134 return -1;
135 if (swap)
136 swapfloat(&val);
137 t->times[i] = val;
138
139 /* Ignore the 'breaks' field */
140 if (cst_fread(ts->fd, &val, sizeof(float), 1) != 1)
141 return -1;
142
143 for (j=0; j < t->num_channels; j++)
144 {
145 if (cst_fread(ts->fd, &val, sizeof(float), 1) != 1)
146 return -1;
147 if (swap)
148 swapfloat(&val);
149 t->frames[i][j] = val;
150 }
151
152 return 0;
153 }
154
cst_track_load_est(cst_track * t,const char * filename)155 int cst_track_load_est(cst_track *t, const char *filename)
156 {
157 cst_tokenstream *ts;
158 const char *tok;
159 int num_frames,num_channels;
160 int i,ascii = 1,swap = 0,rv;
161
162 num_frames=0;
163 num_channels=0;
164 ts = ts_open(filename,NULL,NULL,NULL,NULL);
165 if (ts == NULL)
166 {
167 cst_errmsg("cst_track_load: can't open file \"%s\"\n",
168 filename);
169 return -1;
170 }
171
172 if (!cst_streq(ts_get(ts),"EST_File"))
173 {
174 cst_errmsg("cst_track_load: not an EST file \"%s\"\n",
175 filename);
176 ts_close(ts); return -1;
177 }
178 if (!cst_streq(ts_get(ts),"Track"))
179 {
180 cst_errmsg("cst_track_load: not an track file \"%s\"\n",
181 filename);
182 ts_close(ts); return -1;
183 }
184
185 while (!cst_streq("EST_Header_End",(tok=ts_get(ts))))
186 {
187 if (cst_streq("DataType",tok))
188 {
189 tok = ts_get(ts);
190 if (cst_streq("ascii",tok))
191 {
192 ascii = 1;
193 }
194 else if (cst_streq("binary",tok))
195 {
196 ascii = 0;
197 }
198 else
199 {
200 cst_errmsg("cst_track_load: don't know how to deal "
201 "with type \"%s\"\n", tok);
202 ts_close(ts);
203 return -1;
204 }
205 }
206 else if (cst_streq("ByteOrder",tok))
207 {
208 tok = ts_get(ts);
209 swap = (cst_streq(tok, BYTE_ORDER_BIG) && CST_LITTLE_ENDIAN)
210 || (cst_streq(tok, BYTE_ORDER_LITTLE) && CST_BIG_ENDIAN);
211 }
212 else if (cst_streq("NumFrames",tok))
213 num_frames = atoi(ts_get(ts));
214 else if (cst_streq("NumChannels",tok))
215 num_channels = atoi(ts_get(ts));
216 else
217 ts_get(ts);
218 if (ts_eof(ts))
219 {
220 cst_errmsg("cst_track_load: EOF in header \"%s\"\n",
221 filename);
222 ts_close(ts); return -1;
223 }
224 }
225
226 cst_track_resize(t,num_frames,num_channels);
227
228 for (i=0; i < t->num_frames; i++)
229 {
230 if (ascii)
231 rv = load_frame_ascii(t, i, ts);
232 else
233 rv = load_frame_binary(t, i, ts, swap);
234 if (rv < 0)
235 {
236 ts_close(ts);
237 cst_errmsg("cst_track_load: EOF in data \"%s\"\n",
238 filename);
239 return rv;
240 }
241 }
242
243 ts_get(ts);
244 if (!ts_eof(ts))
245 {
246 cst_errmsg("cst_track_load: not EOF when expected \"%s\"\n",
247 filename);
248 ts_close(ts); return -1;
249 }
250
251 ts_close(ts);
252
253 return 0;
254 }
255
256