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