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

📄 header.c

📁 speech signal process tools
💻 C
📖 第 1 页 / 共 4 页
字号:
 */head_append(h, arg, size)   Header         *h;   char           *arg;   int             size;{   register int    newpad, newhead_size, j;   if (h && arg && size) {      newhead_size = size + h->nbytes - h->npad;      if ((j = newhead_size & 3))	/* force modulo-4 bytes */	 newpad = 4 - j;      else	 newpad = 0;      newhead_size += newpad;      if (!(h->header = realloc(h->header, newhead_size + 1))) {	 printf("Can't allocate enough buffer space in head_append()\n");	 return (FALSE);      }      chcopy(h->header + h->nbytes - h->npad, arg, size);      h->npad = newpad;      for (; newpad; newpad--)	 h->header[newhead_size - newpad] = ' ';	/* pad with spaces */      h->header[newhead_size] = 0;      h->nbytes = newhead_size;      return (TRUE);   }   return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Read a signal header and initialize the Signal structure values * accordingly.  Note that multiple versions of the header may be read, but * only the most recently specified values are retained in the Signal * structure.  Sig->bytes_skip will be set to the number of bytes offset in * the file required to skip the header.  Minimal support is provided here * for Nirvonics headers and Peter Kroon's headers.  Normal return is TRUE, * else FALSE. */w_read_header(sig, head)   Signal         *sig;   Header         *head;{   if (sig && head) {      if (debug_level >= 1)	 printf("w_read_header: head->magic = 0x%x.\n", head->magic);      if (head->magic == SIGNAL_MAGIC) {	 if (debug_level >= 1)	    printf("w_read_header: calling setup_access & get_header_args.\n");	 setup_access(sig);	 get_header_args(head->header, &head_a0);	 sig->bytes_skip = head->nbytes + 12;	 if (debug_level >= 1)	    printf("w_read_header: sig->bytes_skip = %d.\n", sig->bytes_skip);	 return (TRUE);      }      switch (head->magic) {      case ESPS_MAGIC:	 if (debug_level >= 1)	    printf("w_read_header: calling read_esps_hdr.\n");	 if (!read_esps_hdr(sig, head->esps_hdr)) {	    printf("w_read_header: call to read_esps_hdr failed.\n");	    return FALSE;	 }	 sig->bytes_skip = head->esps_nbytes;	 {	    Header         *h;	    h = w_write_header(sig);	    head->header = h->header;	    head->nbytes = h->nbytes;	    free(h);	 }	 if (debug_level >= 1)	    printf("w_read_header: sig->bytes_skip = %d.\n", sig->bytes_skip);	 return TRUE;	 break;#ifdef IBM_RS6000      case -0xfeff01:#else      case HEADER_MAGIC:#endif      case RHEADR_MAGIC:	 {#define pk2(byt) (p=bpos+byt, ((int)p[1])&255 | (((int)p[0])&255)<<8)#define pk4(byt) \(p=bpos+byt, ((int)p[1])&255 | (((int)p[0])&255)<<8 \	 | (((int)p[3])&255)<<16 | (((int)p[2])&255)<<24);	    char           *bpos = head->header, *p;	    int             type, size;	    for (;;) {	       size = pk4(0);	       if (size < 8)		  size = 8;	       type = pk4(4);	       if (type == 2) {		  sig->freq = pk4(8);		  sig->type = (pk2(14) ? P_SHORTS : P_USHORTS);	       }	       if (type == 0)		  break;	       bpos += size;	    }	    sig->bytes_skip = head->nbytes + 4;	    return (TRUE);	 }      case SPPACK_MAGIC:	 {	    Header         *h;	    Sppack         *sphdr;	    int             length = 0;	    int             fd = -1;	    sig->bytes_skip = 01000;	    if (sig->file >= 0) {	/* already open */	       length = lseek(sig->file, 0L, 2) - sig->bytes_skip;	       lseek(sig->file, (long) sig->bytes_skip, 0);	    } else if ((fd = open(sig->name, O_RDONLY)) >= 0) {	       length = lseek(fd, 0L, 2) - sig->bytes_skip;	       close(fd);	    }	    if (length < 0)	       length = 0;	    sphdr = (Sppack *) (head->header);	    if (sphdr->type < 0)	       sphdr->type = -sphdr->type;	    switch (sphdr->type / 1000) {	    case 1:	       sig->type = 4;	       sig->file_size = length / sizeof(short);	       break;	    case 2:	       sig->type = 5;	       sig->file_size = length / sizeof(int);	       break;	    case 3:	       sig->type = 7;	       sig->file_size = length / sizeof(float);	       break;	    case 4:	       sig->type = 8;	       sig->file_size = length / sizeof(double);	       break;	    default:	       printf("not able to handle this data type\n");	       return (FALSE);	    }	    sig->freq = sphdr->frequency;	    sig->band = sig->freq / 2.0;	    if (sig->freq > 0)	       sig->end_time = sig->start_time + sig->file_size / sig->freq;	    if (head->header)	       free(head->header);	    if ((h = w_write_header(sig))) {	       head->header = h->header;	       head->nbytes = h->nbytes;	       free(h);	       return (TRUE);	    } else {	       printf("Problems with spp header in w_read_header\n");	       return (FALSE);	    }	 }      default:	 printf("Header type %d is not yet recognized.\n", head->magic);	 printf("Please add processing for this type to w_read_header()");	 printf(" in header.c\n");	 return (FALSE);      }   }   return (FALSE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Write a SIG header to a file.  The file must be open for writing and the * file pointer must be properly set (i.e. to the beginning of the file for * single-segment files, or the beginning of a segment for multi-segment * files). */put_header(h, fd)   Header         *h;   int             fd;{   char            temp[10];   int             i;   if (fd >= 0) {      if (h && h->nbytes && (h->magic == SIGNAL_MAGIC) && h->header) {	 i = SIGNAL_MAGIC;	 sprintf(temp, "%7d\n", h->nbytes);	 if ((write(fd, &i, 4) == 4) && (write(fd, temp, 8) == 8)) {	    if (write(fd, h->header, h->nbytes) == h->nbytes)	       return (TRUE);	    else	       printf("Can't write body of header in put_header()\n");	 } else	    printf("Can't write header header in put_header()\n");      } else if (h && h->magic == ESPS_MAGIC && h->esps_hdr && h->strm) {	 write_header(h->esps_hdr, h->strm);	 h->esps_nbytes = ftell(h->strm);	 return TRUE;      } else	 printf("Bad header in put_header()\n");   } else      printf("File is not open in put_header()\n");   return (FALSE);}voidbyte_swap(buf, nbyt)   char           *buf;   int             nbyt;{   register char  *p = buf - 1, c, *p2 = p + (nbyt & -2);   while (++p < p2) {      c = *p;      *p = p[1];      *++p = c;   }}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */its_a_pipe(fd)   int             fd;{   return ((fd >= 0) && (lseek(fd, 0L, SEEK_CUR) < 0));}#ifdef NOT_USING_ESPS_DEF/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */is_an_esps_header(fd)   int             fd;{   int             ok = 0;   if (fd >= 0) {      int             here = lseek(fd, 0L, SEEK_CUR);      int             newd = dup(fd);      FILE           *s;      struct header  *h = NULL;      if (newd > 0) {	 if ((s = fdopen(newd, "r"))) {	    if ((h = read_header(s))) {	       free_header(h, (long) 0, (char *) NULL);	       ok = TRUE;	    }	    fclose(s);	 } else	    close(newd);      }#ifdef OS5      lseek(fd, here, SEEK_SET);#else      lseek(fd, here, L_SET);#endif   }   return (ok);}#endif/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Read the header from the file whose file descriptor is "file." Return the * loaded Header structure. */Header         *get_header(file)   int             file;{   int             magic, nbytes, sum, type;   Header         *h = NULL;   char            count[8];   FILE           *strm = NULL;   if (debug_level >= 1)      printf("get_header: function entered; file descriptor %d.\n", file);   if (file >= 0) {      /* first we try to read an ESPS header */      if (its_a_pipe(file) || is_an_esps_header(file)) {	 struct header  *hd;	 strm = fdopen(file, "r");	 if (!strm) {	    printf("get_header: Can't make stream for file descriptor.\n");	    return (NULL);	 }	 hd = read_header(strm);	 if (hd == NULL && debug_level >= 1)	    printf("get_header: couldn't read ESPS header; maybe it's another kind.\n");	 if (hd != NULL) {	/* process as ESPS file */	    if (debug_level >= 1)	       if (hd->common.type == FT_FEA)		  printf("get_header: this is an ESPS file, type FT_FEA, subtype %s.\n",			 fea_file_type[hd->hd.fea->fea_type]);	       else		  printf("get_header: this is an ESPS file, type %s.\n",			 file_type[hd->common.type]);	    h = w_new_header(0);	    if (!h) {	       printf("get_header: Can't create Waves header structure.\n");	       return (NULL);	    }	    h->magic = ESPS_MAGIC;	    h->esps_nbytes = ftell(strm);	    if (debug_level >= 1)	       printf("get_header: h->esps_nbytes = %d.\n", h->esps_nbytes);	    h->e_scrsd = 0;	    h->e_short = 0;	    h->strm = strm;	    /*	     * here's the place to catch FT_SD and stick in a FEA_SD header	     */	    /* unless we want old-style SD to result in old-style SD */	    h->esps_hdr = hd;            h->esps_clean = 1;	    return h;	 }	 fclose(strm);	 return (NULL);      } else {			/* Not an ESPS file */#ifdef OS5	 lseek(file, 0L, SEEK_SET);#else	 lseek(file, 0L, L_SET);#endif	 if ((read(file, &magic, 4) == 4)) {	    if (debug_level >= 1)	       printf("get_header: magic = %d.\n", magic);	    if (magic == SIGNAL_MAGIC) {	       if ((read(file, count, 8) == 8) && (count[7] == '\n')) {		  nbytes = 0;		  count[7] = 0;		  if (strlen(count))		     sscanf(count, "%d", &nbytes);		  if (nbytes) {		     if ((h = w_new_header(nbytes))			 && (read(file, h->header, nbytes) == nbytes)) {			h->header[nbytes] = 0;			return (h);		     } else			printf("Can't allocate header and read file in get_header()\n");		  } else		     printf("Zero header size in get_header()\n");	       } else		  printf("Can't read header count in get_header()\n");	       return (NULL);	    }	    switch (magic) {#ifdef IBM_RS6000	    case -0xfeff01:#else	    case HEADER_MAGIC:#endif	       {#define tran4(x) \    (magic == RHEADR_MAGIC ? \	(int)(	 ((((unsigned) x)&0xff000000)>>24) \		|((((unsigned) x)&0x00ff0000)>>8) \		|((((unsigned) x)&0x0000ff00)<<8) \		|((((unsigned) x)&0x000000ff)<<24)) \	:(int)(  ((((unsigned) x)&0xffff0000)>>16) \		|((((unsigned) x)&0x000000ff)<<16)))		  sum = 4;		  while ((read(file, &nbytes, 4) == 4) &&			 (read(file, &type, 4) == 4)) {		     type = tran4(type);		     nbytes = tran4(nbytes);		     if (nbytes < 8)			nbytes = 8;		     sum += nbytes;		     if (type == 0)			break;#ifdef OS5		     if (nbytes > 8)			lseek(file, nbytes - 8, SEEK_CUR);#else		     if (nbytes > 8)			lseek(file, nbytes - 8, L_INCR);#endif		  }#ifdef OS5		  lseek(file, 4, SEEK_SET);#else		  lseek(file, 4, L_SET);#endif		  if (h = w_new_header(sum)) {		     if ((nbytes = read(file, h->header, sum)) != sum) {			printf("Trouble reading Nirvonics header");			break;		     }		     if (magic == RHEADR_MAGIC)			byte_swap(h->header, sum);		     h->magic = magic;		     return (h);		  } else		     printf("Can't allocate header and read file in get_header()\n");	       }	       break;	    default:		/* Check if it is an SPPACK header */	       {		  Sppack         *sphdr;		  lseek(file, 0, 0);		  if (!(sphdr = (Sppack *) malloc(sizeof(Sppack)))) {		     printf("Can't allocate Sppack in get_header()\n");		     return (NULL);		  }		  if ((read(file, sphdr, sizeof(Sppack)) == sizeof(Sppack))) {		     if (sphdr->magic == SPPACK_MAGIC) {			if ((h = w_new_header(0))) {			   h->header = (char *) sphdr;			   h->magic = sphdr->magic;			   return (h);			} else			   printf("Can't allocate header and read file in get_header()\n");			return (NULL);		     }		  }	       }	       /* printf("Unknown header type: %d in get_header()\n"); */	       return (NULL);	    }	 } else	    printf("Can't read the file\n");      }   } else      printf("Bad file descriptor in get_header()\n");   if (h)      free(h);   return (NULL);}/* ----------------------------------------------------------      */char           *get_date(){   time_t          tim, time();   char           *date, *ctime(), *c;   tim = time((long *) 0);   if ((c = date = ctime(&tim))) {      while (*c != '\n')	 c++;      *c = 0;   }   return (date);}/* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *  */update_hdr_for_mod(sig)		/* add date & comment for modification */   Signal         *sig;{   char            comment[256];   head_printf(sig->header, "time", get_date());   sprintf(comment, "modify_signal: signal %s", sig->name);   head_printf(sig->header, "operation", comment);   if (sig->header->magic == ESPS_MAGIC) {      sprintf(comment, "%s\n", get_date());      add_comment(sig->header->esps_hdr, comment);      sprintf(comment, "modify_signal: signal %s\n", sig->name);      add_comment(sig->header->esps_hdr, comment);   }   sig->header->esps_clean = 0;   return;}/* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *  *//* * These are currently only used in xspectrum.   If an ESPS file that is * really a NIST sphere file is close, it's header must be also closed or * else a memory leak results. */intis_feasd_sphere(sig)   Signal         *sig;{   struct header  *esps_hdr = NULL;   if (sig) {      if (sig->header)	 esps_hdr = sig->header->esps_hdr;      if (esps_hdr &&	  (esps_hdr->common.type == FT_FEA) &&	  (esps_hdr->hd.fea->fea_type == FEA_SD) &&	  get_genhd_val("sphere_hd_ptr", esps_hdr, (double) 0.0))	 return 1;   }   return 0;}intclose_feasd_sphere(sig)   Signal         *sig;{   struct header  *esps_hdr = NULL;   unsigned long   tmp;   if (!sig)      return;   if (sig->header)      esps_hdr = sig->header->esps_hdr;   if (esps_hdr) {      tmp = (unsigned long) get_genhd_val("sphere_hd_ptr",					  esps_hdr, (double) 0.0);      sp_close(tmp);   }   return 1;}intis_sphere_tempfile(sig)   Signal         *sig;{   struct header  *esps_hdr = NULL;   char           *sp;   if (sig && is_feasd_sphere(sig)) {      esps_hdr = sig->header->esps_hdr;      sp = (char *) (long) get_genhd_val("sphere_hd_ptr", esps_hdr, 0.0);      return sphere_temp(sp);   } else      return 0;}

⌨️ 快捷键说明

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