/* * Close feature stream. Close opened files, free memory and, * if stream is an output stream, flush buffer. No error code * returned, even if flushing generates an error. */ void spf_stream_close(spfstream_t *s) { if (s) { spf_stream_flush(s); if (s->name) { free(s->name); if (s->f) fclose(s->f); } spf_header_free(s->header); spf_buf_free(s->buf); free(s); } }
/* * Convert the input buffer so that the output stream description matches * the target stream description. This somewhat complex function has three * different modes: */ spfbuf_t *spf_buf_convert(spfbuf_t *ibuf, long iflag, long oflag, unsigned long wl, int mode) { unsigned long i; unsigned short sdim, odim, ibins[9], obins[9]; spfbuf_t *obuf; spf_t *ip, *op; /* sanity checks */ if ((oflag & WITHE) && !(iflag & WITHE)) { fprintf(stderr, "spf_buf_convert(): cannot invent missing energy\n"); return(NULL); } if ((oflag & WITHN) && !(oflag & WITHE)) { fprintf(stderr, "spf_buf_convert(): cannot suppress static energy without energy\n"); return(NULL); } if ((oflag & WITHA) && !(oflag & WITHD)) { fprintf(stderr, "spf_buf_convert(): cannot have accelerations without delta\n"); return(NULL); } if ((oflag & WITHR) && !(oflag & WITHZ)) { fprintf(stderr, "spf_buf_convert(): cannot have variance normalization without mean removal\n"); return(NULL); } /* find out an output buffer */ spf_indexes(ibins, ibuf->dim, iflag); sdim = ibins[1] + 1; /* base static feature dimension (regardless of energy) */ odim = spf_tot_dim(sdim, oflag); spf_indexes(obins, odim, oflag); if (mode == SPRO_CONV_UPDATE) { if (odim > ibuf->adim) { fprintf(stderr, "spf_buf_convert(): buffer max dimension (%d) too small (odim=%d)\n", ibuf->adim, odim); return(NULL); } obuf = ibuf; obuf->dim = odim; } else { if ((obuf = spf_buf_alloc(odim, ibuf->n * odim * sizeof(spf_t))) == NULL) { fprintf(stderr, "spf_buf_convert(): cannot allocate memory\n"); return(NULL); } obuf->n = ibuf->n; /* copy static coefficients */ ip = ibuf->s; op = obuf->s; for (i = 0; i < ibuf->n; i++) { memcpy(op, ip, sdim * sizeof(spf_t)); if ((oflag & WITHE) && ! (oflag & WITHN)) *(op+obins[2]) = *(ip+ibins[2]); ip += ibuf->adim; op += obuf->adim; } } /* global normalizations */ if (((oflag & WITHZ) && ! (iflag & WITHZ)) || ((oflag & WITHR) && ! (iflag & WITHR))) if (spf_buf_normalize(obuf, 0, sdim - 1, wl, oflag & WITHR) != 0) { fprintf(stderr, "spf_buf_convert(): cannot normalize output buffer\n"); if (mode != SPRO_CONV_UPDATE) spf_buf_free(obuf); return(NULL); } /* add deltas */ if (oflag & WITHD) { /* delta energy *must* be computed first since, in update mode, the static energy can be overwritten by the deltas -- check out if deltas already exist in which case they may not be recomputed. */ if (oflag & WITHE) spf_delta_set(ibuf, ibins[2], 1, obuf, obins[5]); spf_delta_set(obuf, 0, sdim, obuf, obins[3]); } if (oflag & WITHA) spf_delta_set(obuf, obins[3], (oflag & WITHE) ? (sdim + 1): (sdim), obuf, obins[6]); if (mode == SPRO_CONV_REPLACE) spf_buf_free(ibuf); return(obuf); }