1
/* ====================================================================
2
* Copyright (c) 1997-2000 Carnegie Mellon University. All rights
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in
14
* the documentation and/or other materials provided with the
17
* This work was supported in part by funding from the Defense Advanced
18
* Research Projects Agency and the National Science Foundation of the
19
* United States of America, and the CMU Sphinx Speech Consortium.
21
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
22
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
25
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
* ====================================================================
36
/*********************************************************************
44
*********************************************************************/
46
#include <s3/s3mixw_io.h>
47
#include <sphinxbase/matrix.h>
54
s3mixw_read(const char *fn,
58
uint32 *out_n_density)
68
fp = s3open(fn, "rb", &swap);
72
/* check version id */
73
ver = s3get_gvn_fattr("version");
75
if (strcmp(ver, MIXW_FILE_VERSION) != 0) {
76
E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
77
fn, ver, MIXW_FILE_VERSION);
81
E_FATAL("No version attribute for %s\n", fn);
84
/* if do_chk is non-NULL, there is a checksum after the data in the file */
85
do_chk = s3get_gvn_fattr("chksum0");
87
/* Read the mixing weight array */
88
if (s3read_3d((void ****)out_mixw,
95
&rd_chksum) != S3_SUCCESS) {
101
/* See if the checksum in the file matches that which
102
was computed from the read data */
104
if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
109
if (sv_chksum != rd_chksum) {
110
E_FATAL("Checksum error; read corrupt data.\n");
116
E_INFO("Read %s [%ux%ux%u array]\n",
117
fn, *out_n_mixw, *out_n_feat, *out_n_density);
123
s3mixw_intv_read(const char *fn,
126
float32 ****out_mixw,
129
uint32 *out_n_density)
137
fp = s3open(fn, "rb", &swap);
141
/* check version id */
142
ver = s3get_gvn_fattr("version");
144
if (strcmp(ver, MIXW_FILE_VERSION) != 0) {
145
E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
146
fn, ver, MIXW_FILE_VERSION);
150
E_FATAL("No version attribute for %s\n", fn);
153
/* if do_chk is non-NULL, there is a checksum after the data in the file */
154
do_chk = s3get_gvn_fattr("chksum0");
156
/* Read the mixing weight array */
157
if (s3read_intv_3d((void ****)out_mixw,
166
&ignore) != S3_SUCCESS) {
172
E_INFO("Read %s [%ux%ux%u array]\n",
173
fn, *out_n_mixw, *out_n_feat, *out_n_density);
179
s3mixw_write(const char *fn,
191
s3add_fattr("version", MIXW_FILE_VERSION, TRUE);
192
s3add_fattr("chksum0", "yes", TRUE);
194
fp = s3open(fn, "wb", NULL);
198
for (i = 0; i < n_mixw; i++) {
199
for (j = 0; j < n_feat; j++) {
200
for (k = 0; k < n_density; k++) {
201
if (mixw[i][j][k] < 0) {
202
E_ERROR("mixw[%u][%u][%u] < 0 (%e)\n",
203
i,j,k,mixw[i][j][k]);
209
/* floor all non-zero entries to this value to make sure
210
that results are compatible between machines */
211
floor_nz_3d(mixw, n_mixw, n_feat, n_density, MIN_POS_FLOAT32);
213
if (s3write_3d((void ***)mixw,
219
&chksum) != S3_SUCCESS) {
224
if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
232
E_INFO("Wrote %s [%ux%ux%u array]\n",
233
fn, n_mixw, n_feat, n_density);
239
* Log record. Maintained by RCS.
242
* Revision 1.6 2004/07/21 18:05:40 egouvea
243
* Changed the license terms to make it the same as sphinx2 and sphinx3.
245
* Revision 1.5 2004/07/17 08:00:23 arthchan2003
246
* deeply regretted about one function prototype, now revert to the state where multiple pronounciations code doesn't exist
248
* Revision 1.3 2001/04/05 20:02:31 awb
249
* *** empty log message ***
251
* Revision 1.2 2000/09/29 22:35:13 awb
252
* *** empty log message ***
254
* Revision 1.1 2000/09/24 21:38:31 awb
255
* *** empty log message ***
257
* Revision 1.2 97/07/16 11:36:22 eht
258
* *** empty log message ***
260
* Revision 1.1 97/03/17 15:01:49 eht