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

📄 sp_utils.c

📁 speech signal process tools
💻 C
📖 第 1 页 / 共 2 页
字号:
/** File: sp_utils.c **/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#define SPHERE_LIBRARY_CODE#include <sp/sphere.h>extern int farray_fields;extern struct field_t *farray[];/***************************************************************//* Reads an existing header in from file pointer "fp".         *//* The file pointer is assumed to be positioned at the         *//*    beginning of a speech file with a header in NIST SPHERE  *//*    format.                                                  *//* On success, "fp" is positioned at the end of the header     *//*    (ready to read samples) and a pointer to a header        *//*    structure is returned.                                   *//* On failure, argument "error" will point to a string         *//*    describing the problem.                                  *//* If "parse_flag" is false (zero), the fields in the header   *//*    will not be parsed and inserted into the header          *//*    structure; the structure will contain zero fields.       *//*    This is useful for operations on files when the contents *//*    of the header are not important, for example when        *//*    stripping the header.                                    *//***************************************************************/struct header_t *sp_open_header(register FILE *fp, int parse_flag,				char **error){register struct header_t *h;int header_size, i;struct field_t **fv;if (fp == FPNULL)	return HDRNULL;	/* check sanity of arguments */if (spx_read_header(fp,&header_size,parse_flag,error) < 0)	return HDRNULL;if ((! parse_flag) || (farray_fields == 0))	fv = FVNULL;else {	fv = spx_get_field_vector(farray_fields);	if (fv == FVNULL) {		for (i=0; i<farray_fields; i++)			(void) spx_deallocate_field(farray[i]);		return HDRNULL;	}	(void) spx_copy_field_vector(farray, fv, farray_fields);}h = spx_allocate_header(farray_fields,fv);if (h == HDRNULL)	for (i=0; i<farray_fields; i++)		(void) spx_deallocate_field(farray[i]);return h;}/*******************************************************************//* Deletes all fields from the header pointed to by h.             *//*******************************************************************/int sp_clear_fields(register struct header_t *h){register int i, j, errors = 0;if (h == HDRNULL)	return -1;	/* check sanity of arguments */for (i=0, j = h->fc; i<j; i++) {	if (spx_deallocate_field(h->fv[i]) < 0)		errors++;	h->fv[i] = FNULL;}if (h->fv != FVNULL)	mtrf_free((char *) h->fv);h->fv = FVNULL;h->fc = 0;return errors ? -1 : 0;}/***********************************************************************//* Reclaims the space allocated for the header structure pointed to    *//* by h. First reclaims all space allocated for the header's fields,   *//* if any exist.                                                       *//***********************************************************************/int sp_close_header(register struct header_t *h){    (void) sp_clear_fields(h);    mtrf_free((char *) h);    return 0;}/**************************************************************************//*    make an exact copy of the header pointed to by h, and then return   *//*    the new header.                                                     *//**************************************************************************/struct header_t *sp_dup_header(struct header_t *h){    struct header_t *duph;    struct field_t *nf, **fv;    int i;    if (h == HDRNULL) return(0);    duph = sp_create_header();    if ( duph == HDRNULL ) {	fprintf(spfp,"Error: Unable to dup header, can't allocate mem.\n");	return(HDRNULL);    }    /* just loop through all the names, adding each field */    for (i=0; i < h->fc ; i++){        nf = spx_allocate_field_str(h->fv[i]->type,h->fv[i]->name,				    h->fv[i]->data,h->fv[i]->datalen);	if (nf == FNULL)	    return(HDRNULL);	fv = spx_get_field_vector(duph->fc + 1);        if (fv == FVNULL)	    return(HDRNULL);	if (duph->fc > 0) {	    (void) spx_copy_field_vector(duph->fv, fv, duph->fc);	    mtrf_free((char *) duph->fv);	}	fv[duph->fc++] = nf;	duph->fv = fv;    }    return(duph);}int sp_copy_header(SP_FILE *spin, SP_FILE *spout){    char *proc_name="sp_copy_header " SPHERE_VERSION_STR;    struct header_t *h;    int i;    SP_INTEGER l_int;    SP_REAL real;    if (spin->open_mode == SP_mode_read)	h = spin->read_spifr->header;    else if (spin->open_mode == SP_mode_write)	h = spin->write_spifr->header;    else	return_err(proc_name,100,100,"Unable to dup header opened for update");    /* just loop through all the names, adding each field */    for (i=0; i < h->fc ; i++){	switch (h->fv[i]->type){	  case T_STRING:	    if (sp_h_set_field(spout,h->fv[i]->name,			       h->fv[i]->type,h->fv[i]->data) != 0){		sp_print_return_status(spfp);		return_err(proc_name,200,200,			   rsprintf("Unable to copy STRING field '%s'",				    h->fv[i]->name));	    }	    break;	  case T_INTEGER:	    l_int=atol(h->fv[i]->data);	    if (sp_h_set_field(spout,h->fv[i]->name,			       h->fv[i]->type,&l_int) != 0){		sp_print_return_status(spfp);		return_err(proc_name,200,200,			   rsprintf("Unable to copy INTEGER field '%s'",				    h->fv[i]->name));	    }	    break;	  case T_REAL:	    real=atof(h->fv[i]->data);	    if (sp_h_set_field(spout,h->fv[i]->name,h->fv[i]->type,&real) != 0){		sp_print_return_status(spfp);		return_err(proc_name,200,200,			   rsprintf("Unable to copy REAL field '%s'",				    h->fv[i]->name));	    }	    break;	}    }    /* Special Check, if the input file is a pipe and the sample_count  */    /* field is missing, (this is legal), AND the output is a disk file */    /* add a dummy sample_count field to the output header              */    /*   Added June 22, 1994 */    {   int type, size, is_disk_file, out_is_disk_file;	is_disk_file = (spin->open_mode == SP_mode_read) ? 	    spin->read_spifr->status->is_disk_file :		spin->write_spifr->status->is_disk_file;	out_is_disk_file = (spout->open_mode == SP_mode_read) ? 	    spout->read_spifr->status->is_disk_file :		spout->write_spifr->status->is_disk_file;	if (! is_disk_file)	    if (sp_get_field(h,SAMPLE_COUNT_FIELD,&type,&size) < 0){		/* add the field to the header */		l_int = 999999999;		if (sp_h_set_field(spout,SAMPLE_COUNT_FIELD,				   T_INTEGER,&l_int) != 0){		    sp_print_return_status(spfp);		    return_err(proc_name,400,400,			       rsprintf("Unable to copy INTEGER field '%s'",				    h->fv[i]->name));		}	    }    }    if (sp_set_default_operations(spout) != 0)	return_err(proc_name,300,300,		   "Unable to set default operations duplicated file");    return_success(proc_name,0,0,"ok");}/*********************************************************************//* Returns the number of fields stored in the specified header.      *//*********************************************************************/int sp_get_nfields(struct header_t *h){if (h == HDRNULL)	return -1;	/* check sanity of arguments */return h->fc;}/*********************************************************************//* Fills in an array of character pointers with addresses of the     *//* fields in the specified header. No more than n pointers in the    *//* array will be set.                                                *//* Returns the number of pointers set.                               *//*********************************************************************/int sp_get_fieldnames(struct header_t *h, int n, char **v){register struct field_t **fv;int i, fc;if (h == HDRNULL)	return -1;	/* check sanity of arguments */if (v == (char **) NULL)	return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc && i < n; i++)	v[i] = fv[i]->name;return i;}/***********************************************************************//* Returns the type and size (in bytes) of the specified header field. *//* Types are T_INTEGER, T_REAL, T_STRING (defined in header.h).        *//* The size of a T_INTEGER field is sizeof(SP_INTEGER).                *//* The size of a T_REAL field is sizeof(SP_REAL).                      *//* The size of a string is variable and does not includes a            *//*    null-terminator byte (null bytes are allowed in a string).       *//***********************************************************************/int sp_get_field(struct header_t *h, char *name, int *type, int *size){register int i, fc;register struct field_t **fv;if (h == HDRNULL)	return -1;	/* check sanity of arguments */if (name == CNULL)	return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc ; i++, fv++)	if (strcmp(name,(*fv)->name) == 0) {		switch ((*fv)->type) {			case T_INTEGER:				*size = sizeof(SP_INTEGER);				break;			case T_REAL:				*size = sizeof(SP_REAL);				break;			case T_STRING:				*size = (*fv)->datalen;				break;			default:				return -1;		}		*type = (*fv)->type;		return 0;	}return -1;}/*********************************************************************//* Returns the type of the specified header field.                   *//* Types are T_INTEGER, T_REAL, T_STRING (defined in header.h).      *//*********************************************************************/int sp_get_type(struct header_t *h, char *name){register int i, fc;register struct field_t **fv;if (h == HDRNULL)	return -1;	/* check sanity of arguments */if (name == CNULL)	return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc ; i++, fv++)	if (strcmp(name,(*fv)->name) == 0)		switch ((*fv)->type) {			case T_INTEGER:			case T_REAL:			case T_STRING:				return (*fv)->type;			default:				return -1;		}return -1;}

⌨️ 快捷键说明

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