File: ssff.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (302 lines) | stat: -rw-r--r-- 9,493 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                         Copyright (c) 1999                            */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                 Author :  Alan W Black                                */
/*                 Date   :  November 1999                               */
/*-----------------------------------------------------------------------*/
/* Version of code for reading and writing MacQuarie's SSFF format as    */
/* used in emulabel                                                      */
/*                                                                       */
/*=======================================================================*/
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include "EST_unix.h"
#include "EST_types.h"
#include "EST_Track.h"
#include "EST_track_aux.h"
#include "EST_TrackMap.h"
#include "EST_cutils.h"
#include "EST_Token.h"
#include "EST_TList.h"
#include "EST_string_aux.h"
#include "EST_walloc.h"
#include "EST_TrackFile.h"
#include "EST_FileType.h"

EST_read_status EST_TrackFile::load_ssff(const EST_String filename, 
					EST_Track &tr, float ishift, float startt)
{
    EST_TokenStream ts;
    
    if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
    {
	cerr << "Can't open track file " << filename << endl;
	return misc_read_error;
    }
    tr.set_name(filename);
    return load_ssff_ts(ts, tr, ishift, startt);
}

EST_read_status EST_TrackFile::load_ssff_ts(EST_TokenStream &ts, EST_Track &tr, float ishift, float startt)
{
    (void)ishift;
    (void)startt;
    int num_frames, num_channels;
    int swap = FALSE;
    int i,j,pos,end;
    float Start_Time, Record_Freq;
    EST_Features channels;
    EST_String c, name, type, size, cname;
    FILE *fp;
    double dbuff[2];
    short  sbuff[2];

    num_frames = num_channels = 0;
    Start_Time = Record_Freq = 0;

    if (ts.get() != "SSFF")
	return wrong_format;
    
    if ((ts.get() != "--") ||
	(ts.get() != "(c)") ||
	(ts.get() != "SHLRC"))
    {
	cerr << "ssff load track \"" << ts.filename() << "\": bad header"
	     << endl;
	return misc_read_error;
    }
    
    while (ts.peek() != "-----------------")
    {
	c = (EST_String)ts.get();
	if (c == "Comment")
	    ts.get_upto_eoln();
	else if (c == "Start_Time")
	{
	    Start_Time = atof(ts.get().string());
	    tr.f_set("Start_Time",Start_Time);
	}
	else if (c == "Record_Freq")
	{
	    Record_Freq = atof(ts.get().string());
	    tr.f_set("Record_Freq",Record_Freq);
	}
	else if (c == "Machine")
	{
	    if (ts.get() == "SPARC")
	    {
		if (EST_NATIVE_BO != bo_big)
		    swap = TRUE;
	    }
	    else if (EST_NATIVE_BO == bo_big)
		swap = TRUE;
	}
	else if (c == "Column")
	{
	    name = (EST_String)ts.get();
	    type = (EST_String)ts.get();
	    size = (EST_String)ts.get();
	    cname = EST_String("Channel_")+itoString(num_channels);
	    channels.set(cname+".name",name);
	    channels.set(cname+".type",type);
	    channels.set(cname+".size",atoi(size));
	    num_channels++;
	}
	else if ((c == "window_type") ||
		 (c == "window_duration") ||
		 (c == "lpc_order") ||
		 (c == "lpc_type") ||
		 (c == "end_time") ||
		 (c == "preemphasis") ||
		 (c == "frame_duration"))
	{
		type = (EST_String)ts.get();
		if (type == "SHORT")
		    tr.f_set(c,atoi(ts.get().string()));
		else if (type == "DOUBLE")
		    tr.f_set(c,(float)atof(ts.get().string()));
		else
		    tr.f_set(c,ts.get().string());
	}
	else if (ts.eof())
	{
	    cerr << "ssff load track \"" << ts.filename() << 
		"\": bad header unexpected eof" << endl;
	    return misc_read_error;
	}
        //	else 
        //	{
        //	    cerr << "ssff load track \"" << ts.filename() << 
        //		"\": unknown header value \"" << c << "\"" << endl;
        //	}
    }
    ts.get();  // skip over end of header line

    // There's no num_records field in the header so have to use file's 
    // length to calculate it
    fp = ts.filedescriptor();
    pos = ftell(fp);
    fseek(fp,0,SEEK_END);
    end = ftell(fp);
    fseek(fp,pos,SEEK_SET);
    num_frames = (end - pos)/(num_channels*sizeof(double));
    
    // Finished reading header
    tr.resize(num_frames,num_channels);
    tr.fill_time(1.0/Record_Freq);
    tr.set_equal_space(true);
    
    for (i=0; i<num_channels; i++)
	tr.set_channel_name(channels.S(EST_String("Channel_")+
				       itoString(i)+".name"),i);
    
    for (i=0; i < num_frames; i++)
	for (j=0; j<num_channels; j++)
	{
	    type = channels.S(EST_String("Channel_")+ itoString(j)+".type");
	    if (type == "DOUBLE")
	    {
		ts.fread(dbuff,sizeof(double),1);
		if (swap)
		    swap_bytes_double(dbuff,1);
		tr(i,j) = *dbuff;
	    }
	    else if (type == "SHORT")
	    {
		ts.fread(sbuff,sizeof(short),1);
		if (swap)
		    swap_bytes_short(sbuff,1);
		tr(i,j) = (float)(*sbuff);
	    }
	    else
	    {
		cerr << "ssff load track \"" << ts.filename() << 
  	         "\": unknown channel type value \"" << type << "\"" << endl;
		return misc_read_error;
	    }
	}

    return format_ok;
}

EST_write_status EST_TrackFile::save_ssff(const EST_String filename, EST_Track tr)
{
    FILE *fd;
    EST_write_status r;

    if (filename == "-")
	fd = stdout;
    else if ((fd = fopen(filename,"wb")) == NULL)
	return write_fail;

    r = save_ssff_ts(fd,tr);
    
    if (fd != stdout)
	fclose(fd);
    return r;

}

EST_write_status EST_TrackFile::save_ssff_ts(FILE *fp, EST_Track tr)
{
    int i,j;
    int need_prob_voice = 0;
    
    if (tr.equal_space() != 1)
    {
	cerr << "ssf save track: can't save variable spaced track as SSFF"
	     << endl;
	return write_error;
    }

    fprintf(fp,"SSFF -- (c) SHLRC\n");
    if (EST_NATIVE_BO == bo_big)
	fprintf(fp,"Machine SPARC\n");
    else 
	fprintf(fp,"Machine IBM-PC\n");
    if (tr.f_present("Start_Time"))
	fprintf(fp,"Start_Time %g\n",(double)tr.f_F("Start_Time"));
    else
	fprintf(fp,"Start_Time 0.000000\n");
    // If there are less than two sample points I can't get this value
    if (tr.f_present("Record_Freq"))
	fprintf(fp,"Record_Freq %g\n",(double)tr.f_F("Record_Freq"));
    else if (tr.num_frames() < 2)
	fprintf(fp,"Record_Freq %d\n", 100);
    else
	fprintf(fp,"Record_Freq %g\n", 1/(tr.t(1)-tr.t(0)));
    
    for (i = 0; i < tr.num_channels(); ++i)
	fprintf(fp, "Column %s DOUBLE 1\n",(const char *)(tr.channel_name(i)));
    // For EMULABEL to read this is needs prob_voice too
    if ((tr.num_channels() == 1) &&
	(tr.channel_name(0) == "F0"))
    {
	need_prob_voice = 1;
	fprintf(fp, "Column prob_voice DOUBLE 1\n");
    }
    EST_Featured::FeatEntries p;

    for (p.begin(tr); p; ++p)
    {
	if ((p->k == "Start_Time") ||
	    (p->k == "Record_Freq"))
	    continue;
	else
	    fprintf(fp, "%s DOUBLE %s\n", (const char *)p->k,
		    (const char *) p->v.String());
    }
    fprintf(fp,"-----------------\n");
    for (i=0; i< tr.num_frames(); i++)
    {
	double prob_voice;
	double dd;
	for (j=0; j< tr.num_channels(); j++)
	{
	    dd = tr(i,j);
	    fwrite(&dd,sizeof(double),1,fp);
	}
	if (need_prob_voice)
	{
	    if (tr(i,0) == 0)
		prob_voice = 0;
	    else
		prob_voice = 1;
	    fwrite(&prob_voice,sizeof(double),1,fp);
	}
    }

    return write_ok;
}