miio.c

来自「speech signal process tools」· C语言 代码 · 共 1,388 行 · 第 1/3 页

C
1,388
字号
/*  This material contains proprietary software of Entropic Speech, Inc.     Any reproduction, distribution, or publication without the prior	     written permission of Entropic Speech, Inc. is strictly prohibited.  Any public distribution of copies of this work authorized in writing by  Entropic Speech, Inc. must bear the notice			 								      "Copyright (c) 1989 Entropic Speech, Inc.; All rights reserved" 				  This module provides the machine independent i/o routines.  This  version can read and write files in the format for the Sun 386i,  the DEC Station 3100, for all other Suns, Masscomp, HP, and   Convex with IEEE floating point. 	  Some changes will be needed to handle VAX floating point.  Alan Parker, ESI, May 1989.*/static char *sccs_id = "@(#)miio.c	1.19 11/25/96	ERL";#include <stdio.h>#include <esps/esps.h>static union {	double d_val;#ifndef DEC_ALPHA	long i_val[2];#else	int i_val[2];#endif	char byte[8];       } u1;static union {	float f_val;#ifndef DEC_ALPHA	long l_val;#else	int l_val;#endif	char byte[4];       } u2;static union {	short s_val;	char byte[2];       } u3;static voidmiio_input_error(s)char *s;{	Fprintf(stderr,	 "miio_get_%s: not enough bytes to make up the requested data type.\n",	 s);	Fprintf(stderr,	 "miio_get_%s: something is very wrong with your data file.\n",s);	exit(1);}/* this function returns 1 for those machines that by default produce    ESPS standard format files*/intedr_default(mach)int mach;{	if (mach == MASSCOMP_CODE || mach == SUN3_CODE || mach == SUN4_CODE ||	    mach == CONVEX_CODE || mach == HP300_CODE || mach == MACII_CODE ||	    mach == SG_CODE || mach == HP800_CODE || mach == SONY_RISC_CODE || 	    mach == SONY_68K_CODE || mach == APOLLO_68K_CODE ||	    mach == STARDENT_3000_CODE || mach == IBM_RS6000_CODE ||	    mach == HP700_CODE || mach == HP400_CODE) 		return 1;	else		return 0;}/* this section is used by these machines only;  they do not requireconversions, but can detect Sun 386i, DS3100, or DEC_ALPHA code and handle it*/#if defined(M5500) || defined(M5600) || defined(SUN3) || defined(SUN4) || defined(HP300) || defined(CONVEX) || defined(MACII) || defined(SG) || defined(HP800) || defined(SONY) || defined(APOLLO_68K) || defined(STARDENT_3000) || defined(IBM_RS6000) || defined(HP400) || defined(HP700)/* this section is defined for those machines that do EDR by default */intmiio_get_double(ptr, n, flag, mach, fp)double *ptr;int n;int flag;int mach;FILE *fp;{	int item_count = 0;		/* count of items read *//*  check arguments*/	spsassert(ptr, "miio_get_double: ptr is NULL");		spsassert(fp,  "miio_get_double: fp is NULL");	spsassert(n >= 0, "miio_get_double: n not > 0");	spsassert(flag == NO || flag == YES, "miio_get_double: bad flag");	if (n == 0) return(0);	if (flag == YES || edr_default(mach))  /*  simple, just use fread since this machine is EDR by default */		return (fread((char *)ptr, sizeof(*ptr), n, fp));	else if (mach == SUN386i_CODE || mach == DS3100_CODE || 	         mach == DEC_ALPHA_CODE || mach == LINUX_CODE) { 		/*  translate from 386i, DS3100, LINUX or ALPHA native format */	    while (n--) {		if (fread(&u1.byte[7], 1, 1, fp) != 1) 		  break;		if (fread(&u1.byte[6], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[5], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[4], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[3], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[2], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[1], 1, 1, fp) != 1)		  miio_input_error("double");		if (fread(&u1.byte[0], 1, 1, fp) != 1)		  miio_input_error("double");		*ptr++ = u1.d_val;		item_count++;	     }	    return item_count;	}	else {	    Fprintf(stderr,	     "ESPS file input routine: this file was written on a %s ",	     machine_codes[mach]);	    Fprintf(stderr, "and cannot be read on this machine.\n");	    exit(1);	}}intmiio_get_float(ptr, n, flag, mach, fp)float *ptr;int n;int flag;int mach;FILE *fp;{	int item_count=0;	/* count of items read *//*  check arguments*/	spsassert(ptr, "miio_get_float: ptr is NULL");		spsassert(fp,  "miio_get_float: fp is NULL");	spsassert(n >= 0, "miio_get_float: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_float: bad flag");	if (n == 0) return(0);	if (flag == YES || edr_default(mach))  /*  simple, just use fread since this machine is EDR by default */		return (fread((char *)ptr, sizeof(*ptr), n, fp));	else if (mach == SUN386i_CODE || mach == DS3100_CODE || 	         mach == DEC_ALPHA_CODE || mach == LINUX_CODE) { 		/*  translate from 386i, DS3100, or ALPHA native format */	   while (n--) {		if (fread(&u2.byte[3], 1, 1, fp) != 1)		  break;		if (fread(&u2.byte[2], 1, 1, fp) != 1)		  miio_input_error("float");		if (fread(&u2.byte[1], 1, 1, fp) != 1)		  miio_input_error("float");		if (fread(&u2.byte[0], 1, 1, fp) != 1)		  miio_input_error("float");		*ptr++ = u2.f_val;		item_count++;	   }	  return item_count;	}	else {	    Fprintf(stderr,	     "ESPS file input routine: this file was written on a %s ",	     machine_codes[mach]);	    Fprintf(stderr, "and cannot be read on this machine.\n");	    exit(1);	}}intmiio_get_long(ptr, n, flag, mach, fp)long *ptr;int n;int flag;int mach;FILE *fp;{	int item_count=0;	/* count of items read */	spsassert(ptr, "miio_get_long: ptr is NULL");		spsassert(fp,  "miio_get_long: fp is NULL");	spsassert(n >= 0, "miio_get_long: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_long: bad flag");	if (n == 0) return(0);	if (flag == YES || edr_default(mach))  /*  simple, just use fread since this machine is EDR by default */		return (fread((char *)ptr, sizeof(*ptr), n, fp));	else if (mach == SUN386i_CODE || mach == DS3100_CODE || 		 mach == LINUX_CODE) { 		/*  translate from 386i, DS3100,  native format */	  while (n--) {		if (fread(&u2.byte[3], 1, 1, fp) != 1)		  break;		if (fread(&u2.byte[2], 1, 1, fp) != 1)		  miio_input_error("long");		if (fread(&u2.byte[1], 1, 1, fp) != 1)		  miio_input_error("long");		if (fread(&u2.byte[0], 1, 1, fp) != 1)		  miio_input_error("long");		*ptr++ = u2.l_val;		item_count++;	  }	  return item_count;	}/*  translate a file from DEC ALPHA native format*/        else if (mach == DEC_ALPHA_CODE) {	  while (n--) {                char dummy[4];		if (fread(&u2.byte[3], 1, 1, fp) != 1)		  break;		if (fread(&u2.byte[2], 1, 1, fp) != 1)		  miio_input_error("long");		if (fread(&u2.byte[1], 1, 1, fp) != 1)		  miio_input_error("long");		if (fread(&u2.byte[0], 1, 1, fp) != 1)		  miio_input_error("long");		*ptr++ = u2.l_val;/* just discard the extra four bytes */		if (fread(dummy, 1, 4, fp) != 4)		  miio_input_error("long");		item_count++;	  }	  return item_count;        }	else {	    Fprintf(stderr,	     "ESPS file input routine: this file was written on a %s ",	     machine_codes[mach]);	    Fprintf(stderr, "and cannot be read on this machine.\n");	    exit(1);	}}intmiio_get_short(ptr, n, flag, mach, fp)short *ptr;int n;int flag;int mach;FILE *fp;{	int item_count=0;	/* count of items read */	spsassert(ptr, "miio_get_short: ptr is NULL");		spsassert(fp,  "miio_get_short: fp is NULL");	spsassert(n >= 0, "miio_get_short: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_short: bad flag");	if (n == 0) return(0);	if (flag == YES || edr_default(mach))  /*  simple, just use fread since this machine is EDR by default */		return (fread((char *)ptr, sizeof(*ptr), n, fp));	else if (mach == SUN386i_CODE || mach == DS3100_CODE || 	         mach == DEC_ALPHA_CODE || mach == LINUX_CODE) { 		/*  translate from 386i, DS3100, or ALPHA native format */	  while (n--) {		if (fread(&u3.byte[1], 1, 1, fp) != 1)		  break;		if (fread(&u3.byte[0], 1, 1, fp) != 1)		  miio_input_error("short");		*ptr++ = u3.s_val;		item_count++;	  }	  return item_count;	}	else {	    Fprintf(stderr,	     "ESPS file input routine: this file was written on a %s ",	     machine_codes[mach]);	    Fprintf(stderr, "and cannot be read on this machine.\n");	    exit(1);	}}intmiio_get_int(ptr, n, flag, mach, fp)int *ptr;int n;int flag;int mach;FILE *fp;{	int item_count=0;	spsassert(ptr, "miio_get_int: ptr is NULL");		spsassert(fp,  "miio_get_int: fp is NULL");	spsassert(n >= 0, "miio_get_int: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_int: bad flag");	if (n == 0) return(0);	if (flag == YES || edr_default(mach))  /*  simple, just use fread since this machine is EDR by default */		return (fread((char *)ptr, sizeof(*ptr), n, fp));	else if (mach == SUN386i_CODE || mach == DS3100_CODE || 	         mach == DEC_ALPHA_CODE || mach == LINUX_CODE) { 		/*  translate from 386i, DS3100, or ALPHA native format */	   while (n--) {		if (fread(&u2.byte[3], 1, 1, fp) != 1)		  break;		if (fread(&u2.byte[2], 1, 1, fp) != 1)		  miio_input_error("int");		if (fread(&u2.byte[1], 1, 1, fp) != 1)		  miio_input_error("int");		if (fread(&u2.byte[0], 1, 1, fp) != 1)		  miio_input_error("int");		*ptr++ = u2.l_val;		item_count++;	   }	  return item_count;	}	else {	    Fprintf(stderr,	     "ESPS file input routine: this file was written on a %s ",	     machine_codes[mach]);	    Fprintf(stderr, "and cannot be read on this machine.\n");	    exit(1);	}}intmiio_get_char(ptr, n, flag, mach, fp)char *ptr;int n;int flag;int mach;FILE *fp;{	spsassert(ptr, "miio_get_char: ptr is NULL");		spsassert(fp,  "miio_get_char: fp is NULL");	spsassert(n >= 0, "miio_get_char: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_char: bad flag");	if (n == 0) return(0);	return (fread((char *)ptr, sizeof(*ptr), n, fp));}intmiio_get_byte(ptr, n, flag, mach, fp)char *ptr;int n;int flag;int mach;FILE *fp;{	spsassert(ptr, "miio_get_byte: ptr is NULL");		spsassert(fp,  "miio_get_byte: fp is NULL");	spsassert(n >= 0, "miio_get_byte: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_get_byte: bad flag");	if (n == 0) return(0);	return (fread((char *)ptr, sizeof(*ptr), n, fp));}intmiio_put_double(ptr, n, flag, fp)double *ptr;int n;int flag;FILE *fp;{	spsassert(ptr, "miio_put_double: ptr is NULL");		spsassert(fp,  "miio_put_double: fp is NULL");	spsassert(n >= 0, "miio_put_double: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_put_double: bad flag");	if (n == 0) return(0);	return (fwrite((char *)ptr, sizeof(*ptr), n, fp));}intmiio_put_float(ptr, n, flag, fp)float *ptr;int n;int flag;FILE *fp;{	spsassert(ptr, "miio_put_float: ptr is NULL");		spsassert(fp,  "miio_put_float: fp is NULL");	spsassert(n >= 0, "miio_put_float: n not > 0");	spsassert(flag == 0 || flag == 1, "miio_put_float: bad flag");	if (n == 0) return(0);	return (fwrite((char *)ptr, sizeof(*ptr), n, fp));}intmiio_put_int(ptr, n, flag, fp)int *ptr;int n;int flag;FILE *fp;{	spsassert(ptr, "miio_put_int: ptr is NULL");	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?