Пример #1
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);
}
Пример #2
0
/*
 * 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);
}
Пример #3
0
/*
 * 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);
}