⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headers.c

📁 speech signal process tools
💻 C
📖 第 1 页 / 共 5 页
字号:
#ifdef DEMOstatic long check_code;#endifextern int sphere_to_sd_ctrl;		/* defined in sphere.c */struct header  *read_header (fd)FILE * fd;{    static struct header  *recursive_rh ();    struct header  *sphere_to_feasd();    long	   nrec(), k;    struct header  *p;    long   foreign_hd_length;    if (getenv("ESPSDEBUG"))	EspsDebug = 1;#ifndef NO_LIC       if((sphere_to_sd_ctrl != 123))         check_header(MACH_CODE);#endif    p = recursive_rh(fd, 0);    if(p == NULL) {           	if (EspsDebug) Fprintf(stderr,"read_header: recursive_rh == NULL\n");/*   check and see if it is a NIST sphere header */    	if (fseek(fd, 0L, 0) == 0) { 	/* its not a pipe */         if (EspsDebug) Fprintf(stderr,"not a pipe\n");	 if((p = sphere_to_feasd(fd)) == NULL)	{ /* not a NIST either */           char *def_header;	   FILE *header_fd;	   if (EspsDebug) Fprintf(stderr,"not a NIST, rewinding\n");           (void)fseek(fd, 0L, 0); /* rewind *//*  if the file is a SIG header file, then just return  NULL, so that the code in waves does the right thing*/	   if (EspsDebug) Fprintf(stderr,"read_header: see if its SIG\n");           if (is_SIG_file(fd)) {	        if (EspsDebug) Fprintf(stderr,"read_header: it is SIG\n");		return NULL;	   }/*  In this case its not an ESPS, NIST or SIG file, so if DEF_HEADER is defined, then try and add a default ESPS header to it*/	   def_header = getenv("DEF_HEADER");	   if(EspsDebug) Fprintf(stderr,"read_header: not a Sphere\n");	   if (def_header && (header_fd = fopen(def_header,"r"))) {	        if(EspsDebug) 		 Fprintf(stderr,"read_header: def_header: %s\n",def_header);		p = recursive_rh(header_fd, 0);		(void)fclose(header_fd);		if (p == NULL) {		  fprintf(stderr,"DEF_HEADER not ESPS\n");		  return NULL;		 }		(void)fseek(fd, 0L, 0);	   }	   else {		        if(EspsDebug) 		 Fprintf(stderr,"read_header: no default header\n");		return NULL;	   }	 }        }	else {         if(EspsDebug) Fprintf(stderr,"read_header: cannot rewind\n");	 return NULL;	}    }    if (!getenv("ESPS_NOALIGN")) {     if(EspsDebug) Fprintf(stderr,"read_header: NOALIGN not set\n");     if (!old_version(p->common.hdvers,PRE_ALIGNMENT)){     	if(EspsDebug && (align_pad_size != 0))	     Fprintf(stderr,"read_header: skipping alignment pad\n");	if(EspsDebug) {		Fprintf(stderr,"offset: %x\n",data_offset);		Fprintf(stderr,"align pad size: %x\n",align_pad_size);	}    	if(align_pad_size != 0) {		char *align_pad = malloc((unsigned)align_pad_size);		spsassert(align_pad,"read_header: failed to alloc align pad");		if (!miio_get_byte(align_pad, align_pad_size, YES, NONE, fd))	  	  BADHEAD (0, "error reading alignment pad");		free(align_pad);	}     }     else     	if(EspsDebug)      	     Fprintf(stderr,"read_header: old_version or alignpad_size == 0\n");    }    foreign_hd_length = (long)get_genhd_val("foreign_hd_length",p,0.0);    if(EspsDebug) 	Fprintf(stderr,	 "read_header: foreign_hd_length: %ld\n",foreign_hd_length);    if (foreign_hd_length > 0) {	char * foreign_hd_ptr = malloc((unsigned)foreign_hd_length);	spsassert(foreign_hd_ptr,"read_header: failed to alloc foreign hd\n");	if (!fread(foreign_hd_ptr, 1, foreign_hd_length,  fd))	  	  BADHEAD (0, "error reading foreign header");	*add_genhd_l("foreign_hd_ptr",(long *)NULL,1,p)=(long)foreign_hd_ptr;    }    #ifdef DEC_ALPHA    if (edr_default(p->common.machine_code)) /* default edr */	p->common.edr = 1;#endif    k = nrec(p, fd);#ifdef NDREC1    if(k != -1) #endif    p->common.ndrec = k;#define ALTERNATE1#ifdef  ALTERNATE1/* if the header has the encoding generic and is FEA_SD then set the   data type to SHORT*/    if (p->common.type == FT_FEA && p->hd.fea->fea_type == FEA_SD &&	get_genhd_val("encoding",p,(double)0.0)) {		int i;		for(i=0;i<p->hd.fea->field_count;i++) {			if (strcmp("samples",p->hd.fea->names[i]) == 0)  {				p->hd.fea->types[i] = ESPS_SHORT;/* This assumes that the file might have other fields,  I think files with encoding cannot have any other fields other than samples.  				p->hd.fea->nshort += p->hd.fea->sizes[i];				p->hd.fea->nbyte -= p->hd.fea->sizes[i];				p->common.nshort += p->hd.fea->sizes[i];				p->common.nchar -= p->hd.fea->sizes[i];*//* This will only work if the file with encoding only has the samples   field*/				p->hd.fea->nshort = p->hd.fea->sizes[i];				p->hd.fea->nbyte = 0;        			p->common.nshort = p->hd.fea->nshort;				p->common.nchar = 0;	                        }		}    }#endif				    return p;}static struct header   *recursive_rh (fd, lev)FILE * fd;int     lev;{    struct header  *p = new_header (0);				/* new_header makes an all zero header */    short   code, len;    int     nh = 0, ns = 0;    int	    got_esps_preamble = NO;    int	    got_common_part = NO;/* some debug code*/    if (EspsDebug)	Fprintf(stderr,"recursive_rh, lev: %d\n",lev);/* see if this file has a preamble (older esps files might not)    only the outer most header has a preamble */    if (lev == 0)    {	struct preamble	pream;	char		*prefix, *next_byte;	int		prefix_size, num_read;#define CANT_EXTEND(NewSize) \	(num_read < NewSize \	 && ((num_read \	      += fread(prefix + num_read, 1, NewSize - num_read, fd)) \	     < NewSize))	prefix_size = ESPS_PREFIX_SIZE;	if (prefix_size < ESIGNAL_PREFIX_SIZE)	    prefix_size = ESIGNAL_PREFIX_SIZE;	if (prefix_size < PC_WAV_PREFIX_SIZE)	    prefix_size = PC_WAV_PREFIX_SIZE;	prefix = malloc(prefix_size);	num_read = 0;	data_offset = -1;	align_pad_size = 0;#ifdef DEMO	check_code = 3000;#endif	if (CANT_EXTEND(ESPS_PREFIX_SIZE))	{	    BADHEAD(lev, "error reading preamble");	}	if (edr_bytes_to_long(prefix + ESPS_MAGIC_OFFSET)		 == HD_CHECK_VAL)	{	    if (EspsDebug) Fprintf(stderr, "ESPS preamble, EDR order.\n");	    prefix = realloc(prefix, sizeof(struct preamble));	    if (CANT_EXTEND(sizeof(struct preamble)))		BADHEAD(lev, "error reading preamble");	    pream.machine_code = edr_bytes_to_long(next_byte = prefix);	    pream.check_code = edr_bytes_to_long(next_byte += 4);	    pream.data_offset = edr_bytes_to_long(next_byte += 4);	    pream.record_size = edr_bytes_to_long(next_byte += 4);	    pream.check = edr_bytes_to_long(next_byte += 4);	    pream.edr = edr_bytes_to_long(next_byte += 4);	    pream.align_pad_size = edr_bytes_to_long(next_byte += 4);	    pream.foreign_hd = edr_bytes_to_long(next_byte += 4);	    free(prefix);	    got_esps_preamble = YES;	}	else if (lsbf_bytes_to_long(prefix + ESPS_MAGIC_OFFSET)		 == HD_CHECK_VAL)	{	    if (EspsDebug) Fprintf(stderr, "ESPS preamble, byte-swapped.\n");	    prefix = realloc(prefix, sizeof(struct preamble));	    if (CANT_EXTEND(sizeof(struct preamble)))		BADHEAD(lev, "error reading preamble");	    pream.machine_code = lsbf_bytes_to_long(next_byte = prefix);	    pream.check_code = lsbf_bytes_to_long(next_byte += 4);	    pream.data_offset = lsbf_bytes_to_long(next_byte += 4);	    pream.record_size = lsbf_bytes_to_long(next_byte += 4);	    pream.check = lsbf_bytes_to_long(next_byte += 4);	    pream.edr = NO;	    next_byte += 4;	    pream.align_pad_size = lsbf_bytes_to_long(next_byte += 4);	    pream.foreign_hd = lsbf_bytes_to_long(next_byte += 4);	    free(prefix);	    got_esps_preamble = YES;	}	else if (CANT_EXTEND(ESIGNAL_PREFIX_SIZE))	{	    BADHEAD(lev, "error reading preamble");	}	else if (esignal_check_prefix(prefix))	{	    if (EspsDebug) Fprintf(stderr, "Esignal preamble.\n");	    free((char *) p);	    p = esignal_to_fea(prefix, num_read, fd);	    free(prefix);	    return p;	}	else if (CANT_EXTEND(PC_WAV_PREFIX_SIZE))	{	    BADHEAD(lev, "error reading prefix");	}	else if (pc_wav_check_prefix(prefix))	{	    if (EspsDebug) Fprintf(stderr, "PC WAVE prefix.\n");	    free((char *) p);	    p = pc_wav_to_feasd(prefix, num_read, fd);	    free(prefix);	    return p;	}	else	{	    if (EspsDebug) Fprintf(stderr, "no preamble here.\n");/* if the preamble is not valid, we must copy the stuff we read back * to a standard common header structure and then read the rest of it  */	    if (num_read > FIXPART_LEN)		BADHEAD(lev, "read too far ahead");	    (void) bcopy((char *) prefix, (char *) p, num_read);	    if (fread((char *) p + num_read,		      FIXPART_LEN - num_read, 1, fd) == 0)		BADHEAD(lev, "error reading rest of common, after preamble");/* if this is an older type file, then it is not EDR_ESPS */	    edr_flag = NO;	    p->common.edr = NO;	    machine_code = p->common.machine_code;	    got_common_part = YES;	}	if (got_esps_preamble)	{	    if (EspsDebug)	  	Fprintf(stderr,"found preamble\n");	    p->common.edr = pream.edr;	    edr_flag = pream.edr;	    p->common.machine_code = pream.machine_code;	    machine_code = pream.machine_code;	    if(EspsDebug)		Fprintf(stderr,"read_header: edr_flag: %d, machine: %s\n",			edr_flag, machine_codes[machine_code]);	    data_offset = pream.data_offset;	    align_pad_size = pream.align_pad_size;#ifdef DEMO	    check_code = pream.check_code;#endif	}    }    if (!got_common_part) {	  p->common.machine_code = machine_code;	  p->common.edr = edr_flag;	  if (miio_get_short(&p->common.type, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");#ifndef NOPAD	  if (miio_get_short(&p->common.pad1, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");#endif	  if (miio_get_long(&p->common.check, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.date, DATESIZE, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.hdvers, VERSIONSIZE, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.prog, PROGSIZE, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.vers, VERSIONSIZE, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.progdate, DATESIZE, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.ndrec, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_short(&p->common.tag, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_short(&p->common.nd1, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.ndouble, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.nfloat, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.nlong, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.nshort, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.nchar, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.fixpartsiz, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_long(&p->common.hsize, 1, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_char(p->common.user, USERSIZ, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");	  if (miio_get_short(p->common.spares, NSPARES, edr_flag, 	      machine_code, fd) == 0)		BADHEAD (lev, "error reading common part of header");          if (EspsDebug)		fprintf(stderr,"read_header: header version: %s\n",			p->common.hdvers);#ifdef DEMO	  if (got_esps_preamble && check_code != (3000 + hash(p->common.date)))	      return NULL;#endif    }/* validate the header */    if (p -> common.check != HD_CHECK_VAL) {        free(p);	BADHEAD (lev, "bad check value");    }#if defined(M5500) || defined(M5600) /* if its an old header, then we have to shift ndouble through fixpartsiz   from shorts to longs*/    if (old_version(p->common.hdvers,PRE_NEWSIZ)){	short ndouble, nfloat, nlong, nshort, nchar, fixpartsiz;	long hsize;	char *uname;/*	(void)fprintf(stderr,"old header found, lev: %d, version: %s\n",

⌨️ 快捷键说明

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