📄 psps.c
字号:
/* psps - prints ESPS data files * * 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) 1987 Entropic Speech, Inc.; All rights reserved" * * psps - dumps an ESPS data file on standard output. This verison by * Alan Parker, based on the SDS version by Joe Buck. */#include <stdio.h>#include <esps/esps.h>#include <esps/sd.h>#include <esps/spec.h>#include <esps/fea.h>#include <esps/filt.h>#include <esps/scbk.h>#ifdef ESI#include <esps/ana.h>#include <esps/pitch.h>#include <esps/ros.h>#endif ESI#define HUGE 2000000000#ifdef SCCS static char *sccs_id = "@(#)psps.c 1.24 8/12/87 ESI";#endif#define SYNTAX USAGE("psps [-hlnrxvg] [-o start:end] [-t tag] [-f field_name] file")int getopt();int atoi();int skiprec();int size_rec();char *file_typeX();short get_rec_len();char *calloc(), *realloc();void lrange_switch();void pr_full_header();void pr_history();long pr_sd_data();void print_ana();void print_common();void print_fea();void print_filt(); void print_generic();void print_pitch();void print_ros();void print_scbk();void print_sd();void print_spec();void tab();int vflag = 0;extern char *optarg;extern int optind; /* array of strings containing FEA fields */char **field_name = NULL;int fflag = 0; /* FEA field name given if fflag set */int nfield = 0; /* number of field names given */main (argc, argv)int argc;char **argv;{ int c; long i; int history = 0; /* if true, print a history */ int recursive = 0; /* if true, recursively print all headers */ int full = 0; /* if true, print full header(s) */ int nohead = 0; /* if true, don't print header ' */ int xflag = 0; /* -x given? */ char *otext = NULL; /* -o argument */ char *inp_file; /* input file name */ FILE *istrm = stdin; struct header *h; struct spec_data *spec_rec; struct filt_data *filt_rec; struct scbk_data *scbk_rec; struct fea_data *fea_rec;#ifdef ESI struct ana_data *ana_rec; struct pitch *pit_rec; struct ros_data *ros_rec;#endif ESI long srec = 1; /* first record to print */ long erec = HUGE; /* last record to print */ double dbuf[500]; double *data; long tag; long rec_size;#define MAX_TAGS 200 int tags[MAX_TAGS]; int num_tags = 0; int k = 0; int nfound = 0; int tflag = 0; int oflag = 0; int gflag = 0; /* print any ESPS file in generic format */ short type; /* ESPS filt type */ while ((c = getopt (argc, argv, "hlnrxo:t:vgf:")) != EOF) { switch (c) { case 'h': history++; break; case 'r': recursive++; /* fall through to case 'f' intentional */ case 'l': full++; break; case 'n': nohead++; break; case 'x': xflag++; break; case 'o': otext = optarg; oflag++; break; case 't': tags[num_tags++] = atoi (optarg); tflag++; break; case 'v': vflag++; full++; break; case 'g': gflag++; break; case 'f': nfield++; field_name = (char **) realloc ((char *) field_name, (nfield+1) * sizeof (char *)); assert (field_name); field_name[nfield-1] = optarg; field_name[nfield] = NULL; assert (field_name[nfield-1]); fflag++; break; default: SYNTAX; } }/* check for conflicting switches */ if (xflag && otext || nohead && full || history && full) { (void)fprintf (stderr, "psps: conflicting switches\n"); exit (1); } if (tflag && oflag) { (void) fprintf (stderr, "psps: conflicting switches: -t and -o options.\n"); exit (1); } if (optind == argc) SYNTAX; inp_file = argv[optind]; if(strcmp(inp_file,"-") != 0) { if ((istrm = fopen (inp_file, "r")) == NULL) CANTOPEN ("psps", inp_file); } else inp_file = "<stdin>"; if ((h = read_header (istrm)) == NULL) NOTSPS ("psps", inp_file); if (tflag && (h->common.tag == NO)) { (void) fprintf (stderr, "%s: Data is not tagged, cannot use -t option.\n", argv[0]); exit (1); } if (fflag && (h->common.type != FT_FEA)) { (void) fprintf (stderr, "%s: Use the -f option with ESPS Feature files only.\n", argv[0]); exit (1); } if (!fflag) nfield = 1; if (full) pr_full_header (inp_file, h, recursive, 0); else if (history) pr_history (inp_file, h, 0); else if (!nohead) { (void) printf("File: %s\n",inp_file); print_common(0,h); } if (xflag) exit (0); srec = 1; erec = h->common.ndrec; lrange_switch (otext, &srec, &erec, 1); if (srec == 0) (void)fprintf(stderr, "psps: records start at 1, using range %ld:%ld\n",srec++,erec++); if(srec > h->common.ndrec) (void)fprintf(stderr,"psps: warning, starting point > than h->ndrec\n"); if (gflag) type = -1; else type = h->common.type; switch (type) { case FT_SD: i = srec; while(srec-- > 1 && (get_sd_recd(dbuf,1,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } i = pr_sd_data (istrm, i, erec, h); break;#ifdef ESI case FT_ANA: ana_rec = allo_ana_rec(h); i = srec; while(srec-- > 1 && (get_ana_rec(ana_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_ana_rec(ana_rec,h,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (ana_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_ana_rec(ana_rec,h,stdout); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_ana_rec(ana_rec,h,stdout); } } break; case FT_PIT: pit_rec = allo_pitch_rec(); i = srec; while(srec-- > 1 && (get_pitch_rec(pit_rec,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_pitch_rec(pit_rec,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (pit_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_pitch_rec(pit_rec,stdout); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_pitch_rec(pit_rec,stdout); } } break;#endif ESI case FT_SPEC: spec_rec = allo_spec_rec(h); i = srec; while(srec-- > 1 && (get_spec_rec(spec_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_spec_rec(spec_rec,h,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (spec_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_spec_rec(spec_rec,h,stdout); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_spec_rec(spec_rec,h,stdout); } } break;#ifdef ESI case FT_ROS: ros_rec = allo_ros_rec(h); i = srec; while(srec-- > 1 && (get_ros_rec(ros_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_ros_rec(ros_rec,h,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (ros_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_ros_rec(ros_rec,h,stdout); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_ros_rec(ros_rec,h,stdout); } } break;#endif ESI case FT_FILT: filt_rec = allo_filt_rec(h); i = srec; while(srec-- > 1 && (get_filt_rec(filt_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_filt_rec(filt_rec,h,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (filt_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_filt_rec(filt_rec,h,stdout); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_filt_rec(filt_rec,h,stdout); } } break; case FT_SCBK: scbk_rec = allo_scbk_rec(h); i = srec; while(srec-- > 1 && (get_scbk_rec(scbk_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_scbk_rec(scbk_rec,h,istrm) != EOF)) { if (tflag) { (void) fprintf(stdout, "psps: No tags exist in Scalar Quantization Codebook.\n"); exit (1); } else { (void) printf("Record %d: ",i-1); (void) print_scbk_rec(scbk_rec,h,stdout); } } break; case FT_FEA: fea_rec = allo_fea_rec(h); i = srec; while(srec-- >1 && (get_fea_rec(fea_rec,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_fea_rec(fea_rec,h,istrm) != EOF)) { if (tflag) { for (k = 0; k < num_tags; k++) if (fea_rec->tag == tags[k]) { (void) printf("Record %d: ",i-1); (void) print_fea_recf(fea_rec,h,stdout,field_name); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf("Record %d: ",i-1); (void) print_fea_recf(fea_rec,h,stdout,field_name); } } break; default: if (!gflag) (void) printf("Unknown file type, code: %d\n",h->common.type); rec_size = get_rec_len (h); if ((data = (double *) calloc((unsigned)rec_size, sizeof(double))) == NULL) { (void) fprintf (stderr, "%s: calloc: could not allocate memory for data.\n", argv[0]); exit (1); } i = srec; while(srec-- >1 && (get_gen_recd (data, &tag,h,istrm) != EOF)); if (srec > 1) { (void) printf ("%s: not enough records in %s\n", argv[0], inp_file); break; } while (i++ <= erec && (get_gen_recd(data,&tag,h,istrm) != EOF)) { if (tflag) { int k1; for (k1=0; k1 < num_tags; k1++) if (tag == tags[k1]) { (void) printf ("Record: %d Tag: %ld\n", i-1,tag); for (k = 0; k < rec_size; k++) (void) printf (" element%d: %g\n",k+1,data[k]); nfound++; break; } if (nfound == num_tags) break; } else { (void) printf ("Record: %d", i-1); if (h->common.tag == YES) (void) printf (" Tag: %ld\n", tag); else (void) printf ("\n"); for (k = 0; k < rec_size; k++) (void) printf (" element%d: %g\n",k+1,data[k]); } } } if (i-2 > h->common.ndrec) printf("\nThere are %ld records, the header claims there are %ld.\n", i-2, h->common.ndrec); exit (0); return 0;}voidpr_history(name,h,level)char *name;struct header *h;int level;{ int i; tab(level); (void) printf("File: %s\n",name); print_common(level,h); level++; for (i = 0; h->variable.srchead[i] && i < h->variable.nheads; i++) { if (i > 0) { tab (level); (void) printf ("----------------\n"); } pr_history (h->variable.source[i], h->variable.srchead[i], level); } return;}char *file_typeX(type)short type;{ switch (type) {#ifdef ESI case FT_ANA: return "Analysis File"; case FT_PIT: return "Pitch Data File"; case FT_ROS: return "Rosetta Speech Frame File";#endif case FT_SD: return "Sampled Data File"; case FT_SPEC: return "Spectral Records"; case FT_FILT: return "Filter Coefficient File"; case FT_SCBK: return "Scaler Codebook File"; case FT_FEA: return "Feature File"; case FT_SPECT_DB: return "Spectrum Records, Decibels"; default: return "Unknown type"; }}/* print_header, prints the type specfic part of the header pointed to by h*//* pr_sampled_data - print a range of data from a ESPS sampled data file | * for integer types, 10/line. For floating types, 5/line. |*/longpr_sd_data (istrm, rec, erec, h)FILE *istrm;struct header *h;long rec, erec;{ int itype; double data[10]; long count = rec-1; long i, npl, nleft = erec - rec + 1; itype = (h->common.nlong || h->common.nshort || h->common.nchar); if (rec > erec) return(0); npl = (itype ? 10 : 5); while (nleft > 0) { (void) printf ("%6d: ", rec); if (npl > nleft) npl = nleft; if(get_sd_recd (data, (int)npl, h, istrm) == EOF) break; if (itype) for (i = 0; i < npl; i++) { (void) printf ("%7d", (int) data[i]); count++; } else for (i = 0; i < npl; i++) { (void) printf (" %10.4f", data[i]); count++; } putchar ('\n'); nleft -= npl; rec += npl; } return count;}voidpr_full_header(name,h,recursive,level)char *name;struct header *h;int recursive,level;{ tab (level); (void) printf("File: %s\n",name); print_common(level,h); if (h->variable.refhd) { (void) printf("Reference Header:\n"); pr_full_header("reference header",h->variable.refhd,0,level+1); }#ifdef ESI if (h->common.type == FT_ANA) print_ana(level,h); if (h->common.type == FT_PIT) print_pitch(level,h); if (h->common.type == FT_ROS) print_ros(level,h);#endif ESI if (h->common.type == FT_SD) print_sd(level,h); if (h->common.type == FT_SPEC) print_spec(level,h); if (h->common.type == FT_FILT) print_filt(level,h); if (h->common.type == FT_SCBK) print_scbk(level,h); if (h->common.type == FT_FEA)/* if (vflag) */ print_fea(level,h); print_generic(level,h); if (recursive) { int i = 0; level++; while (h->variable.srchead[i] && i < h->variable.nheads) { if (i > 0) { tab (level); (void) printf ("----------------\n"); } pr_full_header (h->variable.source[i], h->variable.srchead[i], recursive, level); i++; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -