comparison saitools/header.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1993
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal
9 shipping charge). Anyone wanting to incorporate all or part of this
10 software in a commercial product must obtain a license from the Medical
11 Research Council.
12
13 The MRC makes no representations about the suitability of this
14 software for any purpose. It is provided "as is" without express or
15 implied warranty.
16
17 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19 THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 SOFTWARE.
24 */
25
26 /*-------------------------------------------------------------------------*/
27
28 /* header.c
29 * ----------
30 *
31 * Part of the tr+fg+sai collection
32 *
33 * temporary version only, until I get architectures sussed:
34 * see the comments to 'readheader'
35 *
36 *
37 * M. Akeroyd. 26th May 1993. Revised Winter 1994.
38 *
39 */
40
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include "tip.h"
46
47
48
49 extern char progname[MAX_STRING_LENGTH];
50 extern char header[MAX_LINES_HEADER][MAX_LINE_LENGTH];
51
52 extern int verboseflag;
53 extern int oppositearchflag; /* ON if reading Sun on DEC, or DEC on Sun.*/
54 extern int header_lines; /* number of lines in header */
55
56
57 /* .sai parameters */
58
59 extern int no_frames;
60 extern int frameheight; /* number of channels */
61 extern int framewidth_samples; /* pwidth + nwidth * samplerate */
62 extern int frameshift_samples; /* frstep_aid * samplerate */
63 extern int pwidth; /* in msecs */
64 extern int nwidth; /* in msecs: NEGATIVE */
65 extern int width_win; /* pixels */
66 extern int height_win; /* pixels */
67 extern long samplerate; /* samples per sec */
68 extern int mincf; /* Hz */
69 extern int maxcf; /* Hz */
70
71
72 /*-------------------------------------------------------------------------*/
73
74 int readheader(FILE *inputfp)
75 {
76 /* Version of 20v1993
77 * This is the latest attempt to get it to READ the required bytes.
78 *
79 * Because the headerlines are of the format "string"="number",
80 * with no spaces imbetween, there is this hack ...
81 *
82 * MAA: Autumn 1993: but see comments below about Sparc10s and
83 * DECstations.
84 */
85
86 char *p_equal=" "; /* pointer to where the '=' is in the headerline */
87 char tempstring[MAX_STRING_LENGTH];
88
89 int bytes_required = 0; /* no. of bytes in header, as in "header_bytes=...
90 * This is RETURNed */
91 int bytes_loaded = 0; /* number actually loaded */
92 int counter;
93
94
95
96 /* get first line */
97 header_lines = 0;
98 fgets(header[0], MAX_LINE_LENGTH, inputfp);
99
100 if(strspn(header[0], "header_bytes") == 0) {
101 fprintf(stderr, "%s: is the input an AIM output file?\n", progname);
102 exit(-1);}
103
104 /* find out how many bytes there SHOULD be */
105 p_equal = strchr(header[0], '=');
106 bytes_required = atoi(++p_equal);
107
108 /*---------------------------------*/
109
110 /* loop on remaining lines, saving important information as required */
111
112 while (strncmp(fgets(header[++header_lines], MAX_LINE_LENGTH, inputfp),
113 "Version=", 8) != 0) {
114 if (strncmp(header[header_lines], "frameheight=", 12) == 0 ) {
115 p_equal = strchr(header[header_lines], '=');
116 frameheight = atoi(++p_equal); }
117
118 if (strncmp(header[header_lines], "framewidth=", 11) == 0 ) {
119 p_equal = strchr(header[header_lines], '=');
120 framewidth_samples = atoi(++p_equal); }
121
122 if (strncmp(header[header_lines], "frames=", 7) == 0 ) {
123 p_equal = strchr(header[header_lines], '=');
124 no_frames = atoi(++p_equal); }
125
126 if (strncmp(header[header_lines], "frameshift=", 11) == 0 ) {
127 p_equal = strchr(header[header_lines], '=');
128 frameshift_samples = atoi(++p_equal); }
129
130 if (strncmp(header[header_lines], "samplerate=", 11) == 0 ) {
131 /* For some unknown reason, the samplerate has a "." at the
132 * end of it.
133 */
134 strncpy(tempstring, header[header_lines],
135 (strlen(header[header_lines])-0));
136 p_equal = strchr(tempstring, '=');
137 samplerate = atol(++p_equal); }
138
139 if (strncmp(header[header_lines], "pwidth_aid=", 11) == 0 ) {
140 p_equal = strchr(header[header_lines], '=');
141 pwidth = atoi(++p_equal); }
142
143 if (strncmp(header[header_lines], "nwidth_aid=", 11) == 0 ) {
144 p_equal = strchr(header[header_lines], '=');
145 nwidth = atoi(++p_equal); }
146
147 if (strncmp(header[header_lines], "width_win=", 10) == 0 ) {
148 p_equal = strchr(header[header_lines], '=');
149 width_win = atoi(++p_equal); }
150
151 if (strncmp(header[header_lines], "height_win=", 11) == 0 ) {
152 p_equal = strchr(header[header_lines], '=');
153 height_win = atoi(++p_equal); }
154
155 if (strncmp(header[header_lines], "mincf_afb=", 10) == 0 ) {
156 p_equal = strchr(header[header_lines], '=');
157 mincf = atoi(++p_equal); }
158
159 if (strncmp(header[header_lines], "maxcf_afb=", 10) == 0 ) {
160 p_equal = strchr(header[header_lines], '=');
161 maxcf = atoi(++p_equal); }
162
163 }
164
165 /*------------------------------------*/
166
167 /* how many bytes have we loaded ? */
168 for (counter=0; counter<=header_lines; counter++)
169 bytes_loaded += strlen(header[counter]);
170
171
172 /* include this warning */
173 if (bytes_loaded > bytes_required) {
174 fprintf(stderr, "%s: something's gone wrong ... too many header bytes were loaded.\n");
175 exit(-1);
176 }
177
178 /* read some more bytes, till we are at the end of the header */
179 for (counter = 1; counter <= (bytes_required - bytes_loaded); counter++)
180 fgetc(inputfp);
181
182
183 /* printf("header.c: bytes_required %i\n", bytes_required); */
184 /* printf("header.c: bytes_loaded %i\n", bytes_loaded); */
185
186
187 /* MAA: Autumn 1993: Roy noticied that saisummary didn't always work
188 * properly, giving a "negative data" error. The following line is the
189 * standard working hack: load one extra byte of the header.
190 * It might be that the reason I didn't notice before
191 * is that a Sparc10 seems to require this extra byte, but that a DECstation
192 * 3100 doesn't. At least when BOTH are reading a DECstation .sai file.
193 * But when they read their 'own' .sai files, it isn't needed.
194 * Therefore this is a byte-swapping bug.
195 */
196 /* So, in summary:
197 * KEEP this line if reading DEC .sai on a SPARC, or SPARC .sai on a DEC
198 * REMOVE this line if reading DEc .sai on a DEC, or SPARC .sai on a SPARC
199 */
200 /* fgetc(inputfp); /* THIS ONE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
201
202 /* MAA: Winter 1994: Changed this bit to the "-oparch" option */
203 if (oppositearchflag == ON)
204 fgetc(inputfp); /* THIS ONE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
205
206 return bytes_required;
207 }
208
209
210
211
212 /*---------------------------------------------------------------------------*/
213
214
215
216 void writeheader(FILE *outputfp)
217 {
218 /* Version of 20v1993
219 * The header is written as a sequence of strings. If the total length
220 * is less than the number required ("header_bytes=..."), then send some
221 * NULL bytes, till its the correct length.
222 */
223
224 int counter;
225 int bytes_required = 0;
226 int bytes_outputed = 0;
227 char *p_equal;
228
229
230
231 /* find out how many bytes we are supposed to be writing */
232 p_equal = strchr(header[0], '=');
233 bytes_required = atoi(++p_equal);
234
235 /* write header; in the process add up the bytes */
236 for(counter=0; counter<=header_lines; counter++) {
237 bytes_outputed += strlen(header[counter]);
238 fputs(header[counter], outputfp);}
239
240 fflush(outputfp);
241
242 /* if we haven't yet sent enough bytes, send some more */
243 for (counter=1; counter<=(bytes_required - bytes_outputed); counter++)
244 fputc(NULL, outputfp);
245
246
247 /* include this warning */
248 if (bytes_required < bytes_outputed) {
249 fprintf(stderr, "%s: something's gone wrong ... the new header is %i too big.\n", progname, (bytes_outputed - bytes_required));
250 exit(-1);
251 }
252
253 }
254
255
256
257
258 /*---------------------------------------------------------------------------*/
259
260
261
262 void checkheader()
263 {
264
265 if (framewidth_samples >= MAX_DATA) {
266 fprintf(stderr, "%s: frames are too wide at %i: only alllowed %i samples\n", progname, framewidth_samples, MAX_DATA);
267 exit(-1); }
268
269 if (frameheight >= MAX_CHANNELS) {
270 fprintf(stderr, "%s: too many channels (%i): only alllowed %i \n", progname, frameheight, MAX_CHANNELS);
271 exit(-1); }
272
273 }
274
275
276
277
278 /* The End */
279 /*---------------------------------------------------------------------------*/
280
281
282
283
284