68 #pragma warning (disable: 4996) 76 #define BIO_HDRARG_MAX 32 77 #define END_COMMENT "*end_comment*\n" 81 bcomment_read(FILE * fp)
83 __BIGSTACKVARIABLE__
char iline[16384];
85 while (fgets(iline,
sizeof(iline), fp) != NULL) {
86 if (strcmp(iline, END_COMMENT) == 0)
89 E_FATAL(
"Missing %s marker\n", END_COMMENT);
98 if (fread(&magic,
sizeof(uint32), 1, fp) != 1) {
99 E_ERROR(
"Cannot read BYTEORDER MAGIC NO.\n");
103 if (magic != BYTE_ORDER_MAGIC) {
107 if (magic == BYTE_ORDER_MAGIC)
111 E_ERROR(
"Bad BYTEORDER MAGIC NO: %08x, expecting %08x\n",
112 magic, BYTE_ORDER_MAGIC);
127 for (i = 0; argname[i]; i++) {
142 fprintf(fp,
"version %s\n", version);
143 fprintf(fp,
"endhdr\n");
146 b = (uint32) BYTE_ORDER_MAGIC;
147 fwrite(&b,
sizeof(uint32), 1, fp);
163 while ((key = va_arg(args,
char const *)) != NULL) {
164 char const *val = va_arg(args,
char const *);
166 E_ERROR(
"Wrong number of arguments\n");
170 fprintf(fp,
"%s %s\n", key, val);
174 fprintf(fp,
"endhdr\n");
177 b = (uint32) BYTE_ORDER_MAGIC;
178 if (fwrite(&b,
sizeof(uint32), 1, fp) != 1)
187 bio_readhdr(FILE * fp,
char ***argname,
char ***argval, int32 * swap)
189 __BIGSTACKVARIABLE__
char line[16384], word[4096];
193 *argname = (
char **)
ckd_calloc(BIO_HDRARG_MAX + 1,
sizeof(
char *));
194 *argval = (
char **)
ckd_calloc(BIO_HDRARG_MAX,
sizeof(
char *));
197 if (fgets(line,
sizeof(line), fp) == NULL){
198 E_ERROR(
"Premature EOF, line %d\n", lineno);
203 if ((line[0] ==
's') && (line[1] ==
'3') && (line[2] ==
'\n')) {
206 if (fgets(line,
sizeof(line), fp) == NULL) {
207 E_ERROR(
"Premature EOF, line %d\n", lineno);
212 if (sscanf(line,
"%s%n", word, &l) != 1) {
213 E_ERROR(
"Header format error, line %d\n", lineno);
216 if (strcmp(word,
"endhdr") == 0)
221 if (i >= BIO_HDRARG_MAX) {
223 (
"Max arg-value limit(%d) exceeded; increase BIO_HDRARG_MAX\n",
229 if (sscanf(line + l,
"%s", word) != 1) {
230 E_ERROR(
"Header format error, line %d\n", lineno);
239 if (sscanf(line,
"%s", word) != 1) {
240 E_ERROR(
"Header format error, line %d\n", lineno);
250 (*argname)[i] = NULL;
252 if ((*swap = swap_check(fp)) < 0) {
253 E_ERROR(
"swap_check failed\n");
260 *argname = *argval = NULL;
266 chksum_accum(
const void *buf, int32 el_sz, int32 n_el, uint32 sum)
276 for (i = 0; i < n_el; i++)
277 sum = (sum << 5 | sum >> 27) + i8[i];
280 i16 = (uint16 *) buf;
281 for (i = 0; i < n_el; i++)
282 sum = (sum << 10 | sum >> 22) + i16[i];
285 i32 = (uint32 *) buf;
286 for (i = 0; i < n_el; i++)
287 sum = (sum << 20 | sum >> 12) + i32[i];
290 E_FATAL(
"Unsupported elemsize for checksum: %d\n", el_sz);
299 swap_buf(
void *buf, int32 el_sz, int32 n_el)
309 buf16 = (uint16 *) buf;
310 for (i = 0; i < n_el; i++)
311 SWAP_INT16(buf16 + i);
314 buf32 = (uint32 *) buf;
315 for (i = 0; i < n_el; i++)
316 SWAP_INT32(buf32 + i);
319 E_FATAL(
"Unsupported elemsize for byteswapping: %d\n", el_sz);
326 bio_fread(
void *buf, int32 el_sz, int32 n_el, FILE * fp, int32 swap,
329 if (fread(buf, el_sz, n_el, fp) != (
size_t) n_el)
333 swap_buf(buf, el_sz, n_el);
336 *chksum = chksum_accum(buf, el_sz, n_el, *chksum);
342 bio_fwrite(
const void *buf, int32 el_sz, int32 n_el, FILE *fp,
343 int32 swap, uint32 *chksum)
346 *chksum = chksum_accum(buf, el_sz, n_el, *chksum);
352 memcpy(nbuf, buf, n_el * el_sz);
353 swap_buf(nbuf, el_sz, n_el);
354 rv = fwrite(nbuf, el_sz, n_el, fp);
359 return fwrite(buf, el_sz, n_el, fp);
365 int32 sw, uint32 * ck)
368 if (
bio_fread(n_el,
sizeof(int32), 1, fp, sw, ck) != 1)
369 E_FATAL(
"fread(arraysize) failed\n");
371 E_FATAL(
"Bad arraysize: %d\n", *n_el);
377 if (
bio_fread(*buf, el_sz, *n_el, fp, sw, ck) != *n_el)
378 E_FATAL(
"fread(arraydata) failed\n");
397 ret =
bio_fread(&l_d1,
sizeof(uint32), 1, fp, swap, chksum);
407 ret =
bio_fread(&l_d2,
sizeof(uint32), 1, fp, swap, chksum);
417 if (
bio_fread_1d(&raw, e_sz, &n, fp, swap, chksum) != n)
420 assert(n == l_d1*l_d2);
446 ret =
bio_fread(&l_d1,
sizeof(uint32), 1, fp, swap, chksum);
456 ret =
bio_fread(&l_d2,
sizeof(uint32), 1, fp, swap, chksum);
466 ret =
bio_fread(&l_d3,
sizeof(uint32), 1, fp, swap, chksum);
477 if (
bio_fread_1d(&raw, e_sz, &n, fp, swap, chksum) != n) {
481 assert(n == l_d1 * l_d2 * l_d3);
496 if (fread(&file_chksum,
sizeof(uint32), 1, fp) != 1)
497 E_FATAL(
"fread(chksum) failed\n");
499 SWAP_INT32(&file_chksum);
500 if (file_chksum != chksum)
502 (
"Checksum error; file-checksum %08x, computed %08x\n",
503 file_chksum, chksum);
518 ret =
bio_fwrite(&d1,
sizeof(uint32), 1, fp, 0, chksum);
530 ret =
bio_fwrite(&d2,
sizeof(uint32), 1, fp, 0, chksum);
542 ret =
bio_fwrite(&d3,
sizeof(uint32), 1, fp, 0, chksum);
554 return bio_fwrite_1d(arr[0][0], e_sz, d1 * d2 * d3, fp, chksum);
565 ret =
bio_fwrite(&d1,
sizeof(uint32), 1, fp, 0, chksum);
576 ret =
bio_fwrite(arr, e_sz, d1, fp, 0, chksum);
593 char const *filename,
594 char const *extension,
604 n = strlen(extension);
605 l = strlen(filename);
606 if ((n <= l) && (0 == strcmp(filename + l - n, extension)))
608 inputfile =
ckd_calloc(strlen(directory) + l + n + 2, 1);
610 sprintf(inputfile,
"%s/%s%s", directory, filename, extension);
612 sprintf(inputfile,
"%s%s", filename, extension);
615 if ((uttfp = fopen(inputfile,
"rb")) == NULL) {
616 E_FATAL_SYSTEM(
"Failed to open file '%s' for reading", inputfile);
618 fseek(uttfp, 0, SEEK_END);
620 fseek(uttfp, 0, SEEK_SET);
622 if (fseek(uttfp, header, SEEK_SET) < 0) {
623 E_ERROR_SYSTEM(
"Failed to move to an offset %d in a file '%s'", header, inputfile);
632 if ((l = fread(data,
sizeof(int16), n, uttfp)) < n) {
633 E_ERROR_SYSTEM(
"Failed to read %d samples from %s: %d", n, inputfile, l);
641 if (nsamps) *nsamps = n;
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
#define ckd_alloc_2d_ptr(d1, d2, bf, sz)
Macro for ckd_alloc_2d_ptr
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
#define ckd_alloc_3d_ptr(d1, d2, d3, bf, sz)
Macro for ckd_alloc_3d_ptr
Sphinx's memory allocation/deallocation routines.
Cross platform binary IO to process files in sphinx3 format.
SPHINXBASE_EXPORT int32 bio_fread_3d(void ****arr, size_t e_sz, uint32 *d1, uint32 *d2, uint32 *d3, FILE *fp, uint32 swap, uint32 *chksum)
Read a 3-d array (set of matrices)
#define ckd_salloc(ptr)
Macro for ckd_salloc
SPHINXBASE_EXPORT int16 * bio_read_wavfile(char const *directory, char const *filename, char const *extension, int32 header, int32 endian, size_t *nsamps)
Read raw data from the wav file.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
SPHINXBASE_EXPORT int bio_fwrite_1d(void *arr, size_t e_sz, uint32 d1, FILE *fp, uint32 *chksum)
Write a 1-d array.
SPHINXBASE_EXPORT int32 bio_writehdr(FILE *fp,...)
Write a simple binary file header with only byte order magic word.
SPHINXBASE_EXPORT int32 bio_fwrite(const void *buf, int32 el_sz, int32 n_el, FILE *fp, int32 swap, uint32 *chksum)
Like fwrite but perform byteswapping and accumulate checksum (the 2 extra arguments).
Implementation of logging routines.
SPHINXBASE_EXPORT int32 bio_fread_1d(void **buf, size_t el_sz, uint32 *n_el, FILE *fp, int32 sw, uint32 *ck)
Read a 1-d array (fashioned after fread):
#define E_FATAL_SYSTEM(...)
Print error text; Call perror(""); exit(errno);.
SPHINXBASE_EXPORT int bio_fwrite_3d(void ***arr, size_t e_sz, uint32 d1, uint32 d2, uint32 d3, FILE *fp, uint32 *chksum)
Write a 3-d array (set of matrices).
SPHINXBASE_EXPORT int32 bio_fread_2d(void ***arr, size_t e_sz, uint32 *d1, uint32 *d2, FILE *fp, uint32 swap, uint32 *chksum)
Read a 2-d matrix:
SPHINXBASE_EXPORT int32 bio_writehdr_version(FILE *fp, char *version)
Write a simple binary file header, containing only the version string.
#define E_FATAL(...)
Exit with non-zero status after error message.
SPHINXBASE_EXPORT int32 bio_fread(void *buf, int32 el_sz, int32 n_el, FILE *fp, int32 swap, uint32 *chksum)
Like fread but perform byteswapping and accumulate checksum (the 2 extra arguments).
SPHINXBASE_EXPORT void bio_verify_chksum(FILE *fp, int32 byteswap, uint32 chksum)
Read and verify checksum at the end of binary file.
SPHINXBASE_EXPORT void bio_hdrarg_free(char **name, char **val)
Free name and value strings previously allocated and returned by bio_readhdr.
SPHINXBASE_EXPORT int32 bio_readhdr(FILE *fp, char ***name, char ***val, int32 *swap)
Read binary file format header: has the following format.