feaspecsupp.c

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

C
551
字号
	    for (i = 0; i < *num_freqs; i++) spec_rec->frqs[i] = freqs[i];	    break;	default:	    break;	}    }    if (!spec_rec->frame_len)    {	frame_meth = get_genhd_s("frame_meth", hd);	spsassert(frame_meth, "allo_feaspec_rec: frame_meth undefined.")	switch(*frame_meth)	{	case SPFRM_NONE:	    break;	case SPFRM_FIXED:	    frmlen = get_genhd_l("frmlen", hd);	    spsassert(frmlen, "allo_feaspec_rec: frmlen undefined.")	    spec_rec->frame_len = malloc_l((unsigned) 1);	    spsassert(spec_rec->frame_len,		"allo_feaspec_rec: malloc failed on frame_len.")	    *spec_rec->frame_len = *frmlen;	    break;	case SPFRM_VARIABLE:	    Fprintf(stderr,		"%s: frame_len field required by frame_meth but not present.\n",		"allo_feaspec_rec");	    exit(1);	    break;	}    }    return spec_rec;}/* * This routine writes a record of the FEA file subtype FEA_SPEC. */longput_feaspec_rec(spec_rec, hd, file)    struct feaspec  *spec_rec;    struct header   *hd;    FILE            *file;{    int re_spec_type = get_fea_type("re_spec_val", hd);    long overflow = 0;    spsassert(hd, "put_feaspec_rec: hd is NULL");    spsassert(spec_rec, "put_feaspec_rec: spec_rec is NULL");    spsassert(file, "put_feaspec_rec: file is NULL");    spsassert(hd->common.type == FT_FEA		&& hd->hd.fea->fea_type == FEA_SPEC,	"put_feaspec_rec: file not FEA or not FEA_SPEC");    if ((spec_rec->re_spec_val_b && re_spec_type == BYTE)    || (spec_rec->re_spec_val && re_spec_type == FLOAT))    	put_fea_rec(spec_rec->fea_rec, hd, file);    else {	long *num_freqs = get_genhd_l("num_freqs", hd);	long i;	spsassert(num_freqs, "put_feaspec_rec: num_freqs undefined");	if (spec_rec->re_spec_val) { 	/* convert from float to byte */		char *re_spec_val_b = get_fea_ptr(spec_rec->fea_rec, 					"re_spec_val", hd);		for (i=0; i< *num_freqs; i++) {			if (spec_rec->re_spec_val[i]+64.5 >= 1 + CHAR_MAX) {				re_spec_val_b[i] = CHAR_MAX;				overflow++;			}			else if (spec_rec->re_spec_val[i]+64.5 < 0) {				re_spec_val_b[i] = 0;				overflow++;			}			else re_spec_val_b[i] = spec_rec->re_spec_val[i]+64.5;		}	}	else {	/* convert from byte to float */		float *re_spec_val = (float *)get_fea_ptr(spec_rec->fea_rec,					"re_spec_val", hd);		for (i=0; i< *num_freqs; i++) {			re_spec_val[i] = spec_rec->re_spec_val_b[i]-64;		}	}        put_fea_rec(spec_rec->fea_rec, hd, file);    }    return overflow;}/* * This routine reads a record of the FEA file subtype FEA_SPEC. */longget_feaspec_rec(spec_rec, hd, file)    struct feaspec  *spec_rec;    struct header   *hd;    FILE            *file;{    int re_spec_type;    long overflow = 0;    spsassert(hd, "get_feaspec_rec: hd is NULL");    spsassert(spec_rec, "get_feaspec_rec: spec_rec is NULL");    spsassert(file, "get_feaspec_rec: file is NULL");    spsassert(hd->common.type == FT_FEA		&& hd->hd.fea->fea_type == FEA_SPEC,	"get_feaspec_rec: file not FEA or not FEA_SPEC");    re_spec_type = get_fea_type("re_spec_val", hd);    if ((spec_rec->re_spec_val_b && re_spec_type == BYTE)    || (spec_rec->re_spec_val && re_spec_type == FLOAT))    	return (get_fea_rec(spec_rec->fea_rec, hd, file) == EOF) ? EOF : 0;    else {	long *num_freqs = get_genhd_l("num_freqs", hd);	long i;	spsassert(num_freqs, "put_feaspec_rec: num_freqs undefined");	if (get_fea_rec(spec_rec->fea_rec, hd, file) == EOF) return EOF;	if (spec_rec->re_spec_val) { 	/* convert from byte to float */		char *re_spec_val_b = get_fea_ptr(spec_rec->fea_rec, 					"re_spec_val", hd);		for (i=0; i< *num_freqs; i++) {			spec_rec->re_spec_val[i] = (int)re_spec_val_b[i] - 64;		}	} else {			/* convert from float to byte */		float *re_spec_val = (float *)get_fea_ptr(spec_rec->fea_rec, 					"re_spec_val", hd);		for (i=0; i< *num_freqs; i++) {			if (re_spec_val[i]+64.5 >= 1 + CHAR_MAX) {				spec_rec->re_spec_val_b[i] = CHAR_MAX;				overflow++;			}			else if (re_spec_val[i]+64.5 < 0) {				spec_rec->re_spec_val_b[i] = 0;				overflow++;			}			else spec_rec->re_spec_val_b[i] = re_spec_val[i]+64.5;		}        }    }    return overflow;}/* this routine prints a feaspec record (used by psps, etc) */voidprint_feaspec_rec(p, hd, file)struct feaspec *p;struct header *hd;FILE *file;{	int	i;	int	j;	long	n;	float	sf;	double	freqmin;	short	*freq_format = NULL;	short	*spec_type = NULL;	long	*num_freqs = NULL;	char	*sep = "";	char	**xfields;/* check the validity of the input parameters */	spsassert(hd != NULL, "print_feaspec_rec: hd is NULL");	spsassert(p != NULL, "print_feaspec_rec: p is NULL");	spsassert(file != NULL, "print_feaspec_rec: file is NULL");	spsassert(hd->common.type == FT_FEA && hd->hd.fea->fea_type == FEA_SPEC,		"print_feaspec_rec: file not FEA_SPEC");	spsassert(get_genhd_val("num_freqs", hd, -1.0) >= 0, 		"print_feaspec_rec: num_freqs < 0 or missing");	if (hd->common.tag == YES) {		(void) fprintf(file, "tag: %ld", *p->tag);		sep = ", ";	}	if (p->tot_power) {		(void) fprintf(file, "%stot_power: %g", sep, *p->tot_power);		sep = ", ";	}	if (p->frame_len)		(void) fprintf(file, "%sframe_len: %d", sep, *p->frame_len);	(void) fprintf(file, "\n");/* print any fields that are not part of the standard fea_spec file here */	xfields = get_feaspec_xfields(hd);	print_fea_recf(p->fea_rec, hd, file, xfields);	free_feaspec_xfields(xfields);/* print spectral information */	freq_format = get_genhd_s("freq_format", hd);	spsassert(freq_format, "print_feaspec_rec: freq_format undefined.")    	num_freqs = get_genhd_l("num_freqs", hd);    	spsassert(num_freqs, "print_feaspec_rec: num_freqs undefined.")	spec_type = get_genhd_s("spec_type", hd);	spsassert(spec_type, "print_feaspec_rec: spec_type undefined.")	if (*freq_format == SPFMT_ARB_VAR)		n = *p->n_frqs;	else		n = *num_freqs;	sf = get_genhd_val("sf", hd, 1.0);	(void)fprintf(file, "freq\t\tre_spec_val\tim_spec_val\n");	for (i = 0; i < n; i++) {		(void)fprintf(file, "%e", p->frqs[i]);		if (p->re_spec_val)			(void)fprintf(file, "\t%e", p->re_spec_val[i]);		else if (p->re_spec_val_b)			(void)fprintf(file, "\t%d", (int)p->re_spec_val_b[i]);		if (*spec_type == SPTYP_CPLX)			(void)fprintf(file, "\t%e", p->im_spec_val[i]);		(void)fprintf(file, "\n");	}	(void) fprintf(file, "\n");	return;}staticchar *standard_fields[] = {"tag", "tot_power", "re_spec_val", "im_spec_val",			   "n_frqs", "frqs", "frame_len", NULL};char **get_feaspec_xfields(hd)struct header *hd;{char **list;int  list_size = 1;long i,j;	spsassert(hd, "get_feaspec_xfields: hd is NULL");	spsassert(hd->common.type == FT_FEA && hd->hd.fea->fea_type == FEA_SPEC,		"get_feaspec_xfields: hd is not FEA or not FEA_SPEC");	list = (char **)malloc(sizeof(char *));	list[0] = NULL;	for (i=0; hd->hd.fea->names[i] != NULL; i++) {		for (j=0; standard_fields[j] != NULL; j++) {		   if (strcmp(hd->hd.fea->names[i], standard_fields[j]) == 0)			break;		}		if (standard_fields[j] == NULL) {		   list = (char **)realloc((char *)list, 			(list_size+1)*sizeof(char *));		   list[list_size-1] = savestring(hd->hd.fea->names[i]);		   list[list_size++] = NULL;		}	}	return list;}voidfree_feaspec_xfields(list)    char    **list;{    int	    i;    for (i = 0; list[i]; i++)	free(list[i]);    free((char *) list);}

⌨️ 快捷键说明

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