/* * process input file. */ int process(char *ifn, char *ofn) { sigstream_t *is; spfstream_t *os; float frate; unsigned short dim; int status; int lpc_analysis(sigstream_t *, spfstream_t *); /* ----- show what was asked to do ----- */ if (trace) { fprintf(stdout, "%s --> %s\n", ifn, ofn); fflush(stdout); } /* ----- open input stream ----- */ if ((is = sig_stream_open(ifn, format, Fs, ibs, swap)) == NULL) { fprintf(stderr, "slpc error -- cannot open input stream %s\n", ifn); return(SPRO_STREAM_OPEN_ERR); } /* ----- open output stream ----- */ frate = (unsigned long)(fm_d * is->Fs / 1000.0) / is->Fs; /* real frame period */ dim = (flag & WITHE) ? ncoeff + 1 : ncoeff; if ((os = spf_output_stream_open(ofn, dim, flag & WITHE, flag, 1.0 / frate, NULL, obs)) == NULL) { fprintf(stderr, "slpc error -- cannot open output stream %s\n", ofn); sig_stream_close(is); return(SPRO_STREAM_OPEN_ERR); } if (winlen) set_stream_seg_length(os, winlen); if (escale != 0.0) set_stream_energy_scale(os, escale); /* ----- run LPC analysis ----- */ if ((status = lpc_analysis(is, os)) != 0) { fprintf(stderr, "slpc error -- error processing stream %s\n", (ifn) ? (ifn) : "stdin"); sig_stream_close(is); spf_stream_close(os); return(status); } /* ----- clean ----- */ sig_stream_close(is); spf_stream_close(os); return(0); }
/* ---------------------------------- */ int main(int argc, char **argv) { char *ifn, *ofn; /* I/O filenames */ sigstream_t *is; spfstream_t *os; float frate; unsigned short dim; struct spf_header_field * vh = NULL; int status; /* error status */ int read_args(int, char **); struct spf_header_field * set_header(const char *); void free_header(struct spf_header_field *); int cepstral_analysis(sigstream_t *, spfstream_t *); /* ----- process command line ----- */ if (read_args(argc, argv)) return(1); if (optind < argc) ifn = argv[optind++]; else { fprintf(stderr, "slpcep error -- no input filename specified (use --help to get usage)\n"); return(1); } if (optind < argc) ofn = argv[optind++]; else { fprintf(stderr, "slpcep error -- no output filename specified (use --help to get usage)\n"); return(1); } if (optind < argc) { fprintf(stderr, "slpcep error -- invalid number of arguments (use --help to get usage)\n"); return(1); } /* ----- show what was asked to do ----- */ if (trace) { fprintf(stdout, "%s --> %s\n", ifn, ofn); fflush(stdout); } /* ----- open input stream ----- */ if ((is = sig_stream_open(ifn, format, Fs, ibs, swap)) == NULL) { fprintf(stderr, "slpcep error -- cannot open input stream %s\n", ifn); return(SPRO_STREAM_OPEN_ERR); } /* ----- open output stream ----- */ frate = (unsigned long)(fm_d * is->Fs / 1000.0) / is->Fs; /* real frame period */ dim = (flag & WITHE) ? numceps + 1 : numceps; if (with_header) if ((vh = set_header(ifn)) == NULL) { fprintf(stderr, "slpcep error -- cannot allocate memory\n"); sig_stream_close(is); return(SPRO_ALLOC_ERR); } if ((os = spf_output_stream_open(ofn, dim, flag & WITHE, flag, 1.0 / frate, vh, obs)) == NULL) { fprintf(stderr, "slpcep error -- cannot open output stream %s\n", ofn); sig_stream_close(is); free_header(vh); return(SPRO_STREAM_OPEN_ERR); } free_header(vh); if (winlen) set_stream_seg_length(os, winlen); if (escale != 0.0) set_stream_energy_scale(os, escale); /* ----- run LPCC analysis ----- */ if ((status = cepstral_analysis(is, os)) != 0) { fprintf(stderr, "slpcep error -- error processing stream %s\n", ifn); sig_stream_close(is); spf_stream_close(os); return(status); } /* ----- clean ----- */ sig_stream_close(is); spf_stream_close(os); return(0); }
/* * Open output feature stream. Return a pointer to the stream or NULL * in case of error. */ spfstream_t *spf_output_stream_open(const char *fn, unsigned short idim, long iflag, long cflag, float frate, const spfield_t *vh, size_t nbytes) { spfstream_t *s; unsigned short dim, idx[9]; long flag; float rate; if ((s = (spfstream_t *)malloc(sizeof(spfstream_t))) == NULL) { fprintf(stderr, "spf_output_stream_open(): cannot allocate memory\n"); return(NULL); } s->name = NULL; s->f = NULL; s->iomode = SPRO_STREAM_WRITE_MODE; s->Fs = frate; s->idim = idim; s->iflag = iflag; s->cflag = cflag; s->escale = 0.0; s->winlen = 0; s->header = NULL; s->buf = NULL; s->start = 0; s->idx = 0; /* determine output flag and dim */ s->oflag = iflag | cflag; dim = s->idim; if (s->oflag != s->iflag) { spf_indexes(idx, s->idim, s->iflag); s->odim = spf_tot_dim(idx[1] + 1, s->oflag); if (s->odim > dim) dim = s->odim; } else s->odim = s->idim; /* allocate buffer */ if ((s->buf = spf_buf_alloc(dim, nbytes)) == NULL) { fprintf(stderr, "spf_output_stream_open(): cannot create output buffer for %s\n", (fn) ? (fn) : "stdout"); spf_stream_close(s); return(NULL); } s->buf->dim = s->idim; /* set the actual size */ /* create stream header */ if ((s->header = spf_header_init(vh)) == NULL) { fprintf(stderr, "spf_output_stream_open(): cannot initialize variable length header\n"); spf_stream_close(s); return(NULL); } /* set stream filename */ if (fn && strcmp(fn, "-") != 0) { if ((s->name = strdup(fn)) == NULL) { fprintf(stderr, "spf_output_stream_open(): cannot set stream name %s\n", fn); spf_stream_close(s); return(NULL); } if ((s->f = fopen(fn, "w")) == NULL) { fprintf(stderr, "spf_output_stream_open(): cannot open input file %s\n", fn); spf_stream_close(s); return(NULL); } } else s->f = stdout; /* write header to stream */ if (spf_header_write(s->header, s->f)) { fprintf(stderr, "spf_output_stream_open(): cannot write header to %s\n", (fn) ? (fn) : "stdout"); spf_stream_close(s); return(NULL); } dim = s->odim; flag = s->oflag; rate = s->Fs; #ifdef WORDS_BIGENDIAN sp_swap(&dim, SIZEOF_SHORT); sp_swap(&flag, SIZEOF_LONG); sp_swap(&rate, sizeof(float)); #endif if (fwrite(&dim, SIZEOF_SHORT, 1, s->f) != 1 || fwrite(&flag, SIZEOF_LONG, 1, s->f) != 1 || fwrite(&rate, sizeof(float), 1, s->f) != 1) { fprintf(stderr, "spf_output_stream_open() -- cannot write fixed header to %s\n", (fn) ? (fn) : "stdout"); return(NULL); } return(s); }
/* * Open input feature stream. Return a pointer to the stream or NULL * in case of error. */ spfstream_t *spf_input_stream_open(const char *fn, long flag, size_t nbytes) { spfstream_t *s; unsigned short dim, idx[9]; if ((s = (spfstream_t *)malloc(sizeof(spfstream_t))) == NULL) { fprintf(stderr, "spf_input_stream_open(): cannot allocate memory\n"); return(NULL); } s->name = NULL; s->f = NULL; s->iomode = SPRO_STREAM_READ_MODE; s->header = NULL; s->cflag = flag; s->escale = 0.0; s->winlen = 0; s->buf = NULL; s->start = 0; s->idx = 0; /* set stream filename */ if (fn && strcmp(fn, "-") != 0) { if ((s->name = strdup(fn)) == NULL) { fprintf(stderr, "spf_input_stream_open(): cannot set stream name %s\n", fn); spf_stream_close(s); return(NULL); } if ((s->f = fopen(fn, "r")) == NULL) { fprintf(stderr, "spf_input_stream_open(): cannot open input file %s\n", fn); spf_stream_close(s); return(NULL); } } else s->f = stdin; /* read header */ if ((s->header = spf_header_read(s->f)) == NULL) { fprintf(stderr, "spf_input_stream_open(): cannot initialize header from %s\n", (fn) ? (fn) : "stdin"); spf_stream_close(s); return(NULL); } if (fread(&(s->idim), SIZEOF_SHORT, 1, s->f) != 1 || fread(&(s->iflag), SIZEOF_LONG, 1, s->f) != 1 || fread(&(s->Fs), sizeof(float), 1, s->f) != 1) { fprintf(stderr, "spf_header_read(): cannot read fixed header\n"); spf_stream_close(s); return(NULL); } #ifdef WORDS_BIGENDIAN sp_swap(&(s->idim), SIZEOF_SHORT); sp_swap(&(s->iflag), SIZEOF_LONG); sp_swap(&(s->Fs), sizeof(float)); #endif /* initialize output dimension */ s->oflag = s->iflag | flag; /* flag is the flag we want *added* */ if (s->oflag != s->iflag) { spf_indexes(idx, s->idim, s->iflag); s->odim = spf_tot_dim(idx[1] + 1, s->oflag); } else s->odim = s->idim; /* allocate buffer */ /* if a convertion is necessary, allocate a buffer for the highest of the input and output dimensions so that we can use this buffer for both feature set. */ dim = s->idim; if (s->odim > dim) dim = s->odim; if ((s->buf = spf_buf_alloc(dim, nbytes)) == NULL) { fprintf(stderr, "spf_input_stream_open(): cannot create input buffer for %s\n", (fn) ? (fn) : "stdin"); spf_stream_close(s); return(NULL); } return(s); }
/* --------------------------------- */ int main(int argc,char **argv) { char *fn1, *fn2; spfstream_t *f1, *f2; spf_t *yt1, *yt2; unsigned short dim, j; unsigned long t = 0; int status = 0; int read_args(int, char **); /* ----- process command line ----- */ if (read_args(argc, argv)) return(1); if (optind < argc) fn1 = argv[optind++]; else { fprintf(stderr, "compare error -- no input filename specified (use --help to get usage)\n"); return(1); } if (optind < argc) fn2 = argv[optind++]; else { fprintf(stderr, "compare error -- no output filename specified (use --help to get usage)\n"); return(1); } if (optind < argc) { fprintf(stderr, "compare error -- invalid number of arguments (use --help to get usage)\n"); return(1); } /* ----- open input streams ----- */ if ((f1 = spf_input_stream_open(fn1, 0, BUFSIZE)) == NULL) { fprintf(stderr, "compare error -- cannot open input stream %s\n", fn1); return(SPRO_STREAM_OPEN_ERR); } if ((f2 = spf_input_stream_open(fn2, 0, BUFSIZE)) == NULL) { fprintf(stderr, "compare error -- cannot open input stream %s\n", fn2); spf_stream_close(f1); return(SPRO_STREAM_OPEN_ERR); } if (trace > 0) { fprintf(stdout, "comparing %s and %s:\n", fn1, fn2); fflush(stdout); } /* ----- check headers ----- */ if (spf_stream_dim(f1) != spf_stream_dim(f2)) { if (trace >= 0) fprintf(stdout, "stream dimensions differ!\n"); spf_stream_close(f1); spf_stream_close(f2); return(1); } dim = spf_stream_dim(f1); if (spf_stream_rate(f1) != spf_stream_rate(f2)) { if (trace >= 0) fprintf(stdout, "stream rates differ!\n"); spf_stream_close(f1); spf_stream_close(f2); return(1); } if (spf_stream_flag(f1) != spf_stream_flag(f2)) { if (trace >= 0) fprintf(stdout, "stream descriptions differ!\n"); spf_stream_close(f1); spf_stream_close(f2); return(1); } /* ----- check features ----- */ yt1 = get_next_spf_frame(f1); yt2 = get_next_spf_frame(f2); while (yt1 != NULL && yt2 != NULL) { for (j = 0; j < dim; j++) { double x = *(yt1+j) - *(yt2+j); if (x < -epsilon || x > epsilon) { if (trace > 0) fprintf(stdout, "vectors differ at time %lu", t); status = 3; break; } } if (status && trace <= 0) break; yt1 = get_next_spf_frame(f1); yt2 = get_next_spf_frame(f2); t += 1; } if (t == 0) fprintf(stderr, ">>>>> warning >>>>> empty streams!\n"); if (status && trace != -1) fprintf(stdout, "stream differs!\n"); else if ((yt1 == NULL && yt2 != NULL) || (yt2 == NULL && yt1 != NULL)) { if (trace != -1) fprintf(stdout, "stream lengths differ\n"); status = 2; } spf_stream_close(f1); spf_stream_close(f2); return(status); }