int s3gaucnt_read_full(const char *fn, vector_t ****out_wt_mean, vector_t *****out_wt_var, int32 *out_pass2var, float32 ****out_dnom, uint32 *out_n_cb, uint32 *out_n_feat, uint32 *out_n_density, uint32 **out_veclen) { uint32 rd_chksum = 0; uint32 sv_chksum; uint32 ignore; char *ver; char *do_chk; FILE *fp; uint32 swap; uint32 has_means; uint32 has_vars; uint32 pass2var; uint32 n_cb; uint32 n_feat; uint32 n_density; uint32 *veclen; float32 *buf; float32 ***dnom; uint32 n, i, b_i, j, k, l, d1, d2, d3; vector_t ***wt_mean = NULL; vector_t ****wt_var = NULL; fp = s3open(fn, "rb", &swap); if (fp == NULL) return S3_ERROR; /* check version id */ ver = s3get_gvn_fattr("version"); if (ver) { if (strcmp(ver, GAUCNT_FILE_VERSION) != 0) { E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n", fn, ver, GAUCNT_FILE_VERSION); } } else { E_FATAL("No version attribute for %s\n", fn); } /* if do_chk is non-NULL, there is a checksum after the data in the file */ do_chk = s3get_gvn_fattr("chksum0"); if (bio_fread((void *)&has_means, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) { return S3_ERROR; } if (bio_fread((void *)&has_vars, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) { return S3_ERROR; } if (bio_fread((void *)&pass2var, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) { return S3_ERROR; } if (bio_fread((void *)&n_cb, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) { return S3_ERROR; } if (bio_fread((void *)&n_density, sizeof(uint32), 1, fp, swap, &rd_chksum) != 1) { return S3_ERROR; } if (bio_fread_1d((void **)&veclen, sizeof(uint32), &n_feat, fp, swap, &rd_chksum) < 0) { return S3_ERROR; } if (has_means) { if (bio_fread_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) < 0) { return S3_ERROR; } wt_mean = (vector_t ***)ckd_calloc_3d(n_cb, n_feat, n_density, sizeof(vector_t)); for (i = 0, b_i = 0; i < n_cb; i++) { for (j = 0; j < n_feat; j++) { for (k = 0; k < n_density; k++) { wt_mean[i][j][k] = &buf[b_i]; b_i += veclen[j]; } } } } if (has_vars) { uint32 blk, maxveclen; for (i = 0, blk = 0, maxveclen = 0; i < n_feat; i++) { blk += veclen[i]; if (veclen[i] > maxveclen) maxveclen = veclen[i]; } if (bio_fread_1d((void *)&buf, sizeof(float32), &n, fp, swap, &rd_chksum) < 0) { return S3_ERROR; } assert(n == n_cb * n_density * blk * blk); wt_var = (vector_t ****)ckd_calloc_4d(n_cb, n_feat, n_density, maxveclen, sizeof(vector_t)); for (i = 0, b_i = 0; i < n_cb; i++) { for (j = 0; j < n_feat; j++) { for (k = 0; k < n_density; k++) { for (l = 0; l < veclen[j]; l++) { wt_var[i][j][k][l] = &buf[b_i]; b_i += veclen[j]; } } } } } if (bio_fread_3d((void ****)&dnom, sizeof(float32), &d1, &d2, &d3, fp, swap, &rd_chksum) < 0) { return S3_ERROR; } assert(d1 == n_cb); assert(d2 == n_feat); assert(d3 == n_density); if (do_chk) { /* See if the checksum in the file matches that which was computed from the read data */ if (bio_fread(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) { s3close(fp); return S3_ERROR; } if (sv_chksum != rd_chksum) { E_FATAL("Checksum error; read corrupt data.\n"); } } s3close(fp); *out_wt_mean = wt_mean; *out_wt_var = wt_var; *out_pass2var = pass2var; *out_dnom = dnom; *out_n_cb = n_cb; *out_n_feat = n_feat; *out_n_density = n_density; *out_veclen = veclen; E_INFO("Read %s%s%s%s [%ux%ux%u vector arrays]\n", fn, (has_means ? " with means" : ""), (has_vars ? " with vars" : ""), (has_vars && pass2var ? " (2pass)" : ""), n_cb, n_feat, n_density); return S3_SUCCESS; }
int32 feat_read_lda(feat_t *feat, const char *ldafile, int32 dim) { FILE *fh; int32 byteswap, chksum_present; uint32 chksum, i, m, n; char **argname, **argval; assert(feat); if (feat->n_stream != 1) { E_ERROR("LDA incompatible with multi-stream features (n_stream = %d)\n", feat->n_stream); return -1; } if ((fh = fopen(ldafile, "rb")) == NULL) { E_ERROR_SYSTEM("Failed to open transform file '%s' for reading", ldafile); return -1; } if (bio_readhdr(fh, &argname, &argval, &byteswap) < 0) { E_ERROR("Failed to read header from transform file '%s'\n", ldafile); fclose(fh); return -1; } chksum_present = 0; for (i = 0; argname[i]; i++) { if (strcmp(argname[i], "version") == 0) { if (strcmp(argval[i], MATRIX_FILE_VERSION) != 0) E_WARN("%s: Version mismatch: %s, expecting %s\n", ldafile, argval[i], MATRIX_FILE_VERSION); } else if (strcmp(argname[i], "chksum0") == 0) { chksum_present = 1; /* Ignore the associated value */ } } bio_hdrarg_free(argname, argval); argname = argval = NULL; chksum = 0; if (feat->lda) ckd_free_3d((void ***)feat->lda); { /* Use a temporary variable to avoid strict-aliasing problems. */ void ***outlda; if (bio_fread_3d(&outlda, sizeof(float32), &feat->n_lda, &m, &n, fh, byteswap, &chksum) < 0) { E_ERROR_SYSTEM("%s: bio_fread_3d(lda) failed\n", ldafile); fclose(fh); return -1; } feat->lda = (void *)outlda; } fclose(fh); #ifdef FIXED_POINT /* FIXME: This is a fragile hack that depends on mfcc_t and * float32 being the same size (which they are, but...) */ for (i = 0; i < feat->n_lda * m * n; ++i) { feat->lda[0][0][i] = FLOAT2MFCC(((float *)feat->lda[0][0])[i]); } #endif /* Note that SphinxTrain stores the eigenvectors as row vectors. */ if (n != feat->stream_len[0]) E_FATAL("LDA matrix dimension %d doesn't match feature stream size %d\n", n, feat->stream_len[0]); /* Override dim from file if it is 0 or greater than m. */ if (dim > m || dim <= 0) { dim = m; } feat->out_dim = dim; return 0; }
int s3mixw_read(const char *fn, float32 ****out_mixw, uint32 *out_n_mixw, uint32 *out_n_feat, uint32 *out_n_density) { uint32 rd_chksum = 0; uint32 sv_chksum; uint32 swap; uint32 ignore; char *ver; char *do_chk; FILE *fp; fp = s3open(fn, "rb", &swap); if (fp == NULL) return S3_ERROR; /* check version id */ ver = s3get_gvn_fattr("version"); if (ver) { if (strcmp(ver, MIXW_FILE_VERSION) != 0) { E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n", fn, ver, MIXW_FILE_VERSION); } } else { E_FATAL("No version attribute for %s\n", fn); } /* if do_chk is non-NULL, there is a checksum after the data in the file */ do_chk = s3get_gvn_fattr("chksum0"); /* Read the mixing weight array */ if (bio_fread_3d((void ****)out_mixw, sizeof(float32), out_n_mixw, out_n_feat, out_n_density, fp, swap, &rd_chksum) < 0) { s3close(fp); return S3_ERROR; } if (do_chk) { /* See if the checksum in the file matches that which was computed from the read data */ if (bio_fread(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) { s3close(fp); return S3_ERROR; } if (sv_chksum != rd_chksum) { E_FATAL("Checksum error; read corrupt data.\n"); } } s3close(fp); E_INFO("Read %s [%ux%ux%u array]\n", fn, *out_n_mixw, *out_n_feat, *out_n_density); return S3_SUCCESS; }