~ubuntu-branches/ubuntu/saucy/sphinxtrain/saucy

« back to all changes in this revision

Viewing changes to src/libs/libio/s3mixw_io.c

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ====================================================================
 
2
 * Copyright (c) 1997-2000 Carnegie Mellon University.  All rights 
 
3
 * reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer. 
 
11
 *
 
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
 
15
 *    distribution.
 
16
 *
 
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.
 
20
 *
 
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.
 
32
 *
 
33
 * ====================================================================
 
34
 *
 
35
 */
 
36
/*********************************************************************
 
37
 *
 
38
 * File: s3mixw_io.c
 
39
 * 
 
40
 * Description: 
 
41
 * 
 
42
 * Author: 
 
43
 * 
 
44
 *********************************************************************/
 
45
 
 
46
#include <s3/s3mixw_io.h>
 
47
#include <sphinxbase/matrix.h>
 
48
#include <s3/s3io.h>
 
49
#include <s3/s3.h>
 
50
 
 
51
#include <string.h>
 
52
 
 
53
int
 
54
s3mixw_read(const char *fn,
 
55
            float32 ****out_mixw,
 
56
            uint32 *out_n_mixw,
 
57
            uint32 *out_n_feat,
 
58
            uint32 *out_n_density)
 
59
{
 
60
    uint32 rd_chksum = 0;
 
61
    uint32 sv_chksum;
 
62
    uint32 swap;
 
63
    uint32 ignore;
 
64
    char *ver;
 
65
    char *do_chk;
 
66
    FILE *fp;
 
67
 
 
68
    fp = s3open(fn, "rb", &swap);
 
69
    if (fp == NULL)
 
70
        return S3_ERROR;
 
71
 
 
72
    /* check version id */
 
73
    ver = s3get_gvn_fattr("version");
 
74
    if (ver) {
 
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);
 
78
        }
 
79
    }
 
80
    else {
 
81
        E_FATAL("No version attribute for %s\n", fn);
 
82
    }
 
83
    
 
84
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
85
    do_chk = s3get_gvn_fattr("chksum0");
 
86
 
 
87
    /* Read the mixing weight array */
 
88
    if (s3read_3d((void ****)out_mixw,
 
89
                  sizeof(float32),
 
90
                  out_n_mixw,
 
91
                  out_n_feat,
 
92
                  out_n_density,
 
93
                  fp,
 
94
                  swap,
 
95
                  &rd_chksum) != S3_SUCCESS) {
 
96
        s3close(fp);
 
97
        return S3_ERROR;
 
98
    }
 
99
 
 
100
    if (do_chk) {
 
101
        /* See if the checksum in the file matches that which
 
102
           was computed from the read data */
 
103
 
 
104
        if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
 
105
            s3close(fp);
 
106
            return S3_ERROR;
 
107
        }
 
108
 
 
109
        if (sv_chksum != rd_chksum) {
 
110
            E_FATAL("Checksum error; read corrupt data.\n");
 
111
        }
 
112
    }
 
113
 
 
114
    s3close(fp);
 
115
 
 
116
    E_INFO("Read %s [%ux%ux%u array]\n",
 
117
           fn, *out_n_mixw, *out_n_feat, *out_n_density);
 
118
 
 
119
    return S3_SUCCESS;
 
120
}
 
121
 
 
122
int
 
123
s3mixw_intv_read(const char *fn,
 
124
                 uint32  mixw_s,
 
125
                 uint32  mixw_e,
 
126
                 float32 ****out_mixw,
 
127
                 uint32 *out_n_mixw,
 
128
                 uint32 *out_n_feat,
 
129
                 uint32 *out_n_density)
 
130
{
 
131
    uint32 ignore;
 
132
    char *ver;
 
133
    char *do_chk;
 
134
    FILE *fp;
 
135
    uint32 swap;
 
136
 
 
137
    fp = s3open(fn, "rb", &swap);
 
138
    if (fp == NULL)
 
139
        return S3_ERROR;
 
140
 
 
141
    /* check version id */
 
142
    ver = s3get_gvn_fattr("version");
 
143
    if (ver) {
 
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);
 
147
        }
 
148
    }
 
149
    else {
 
150
        E_FATAL("No version attribute for %s\n", fn);
 
151
    }
 
152
    
 
153
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
154
    do_chk = s3get_gvn_fattr("chksum0");
 
155
 
 
156
    /* Read the mixing weight array */
 
157
    if (s3read_intv_3d((void ****)out_mixw,
 
158
                       sizeof(float32),
 
159
                       mixw_s,
 
160
                       mixw_e,
 
161
                       out_n_mixw,
 
162
                       out_n_feat,
 
163
                       out_n_density,
 
164
                       fp,
 
165
                       swap,
 
166
                       &ignore) != S3_SUCCESS) {
 
167
        s3close(fp);
 
168
        return S3_ERROR;
 
169
    }
 
170
    s3close(fp);
 
171
 
 
172
    E_INFO("Read %s [%ux%ux%u array]\n",
 
173
           fn, *out_n_mixw, *out_n_feat, *out_n_density);
 
174
 
 
175
    return S3_SUCCESS;
 
176
}
 
177
 
 
178
int
 
179
s3mixw_write(const char *fn,
 
180
             float32 ***mixw,
 
181
             uint32 n_mixw,
 
182
             uint32 n_feat,
 
183
             uint32 n_density)
 
184
{
 
185
    FILE *fp;
 
186
    uint32 chksum = 0;
 
187
    uint32 ignore = 0;
 
188
    uint32 i, j, k;
 
189
 
 
190
    s3clr_fattr();
 
191
    s3add_fattr("version", MIXW_FILE_VERSION, TRUE);
 
192
    s3add_fattr("chksum0", "yes", TRUE);
 
193
 
 
194
    fp = s3open(fn, "wb", NULL);
 
195
    if (fp == NULL)
 
196
        return S3_ERROR;
 
197
 
 
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]);
 
204
                }
 
205
            }
 
206
        }
 
207
    }
 
208
 
 
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);
 
212
 
 
213
    if (s3write_3d((void ***)mixw,
 
214
                   sizeof(float32),
 
215
                   n_mixw,
 
216
                   n_feat,
 
217
                   n_density,
 
218
                   fp,
 
219
                   &chksum) != S3_SUCCESS) {
 
220
        s3close(fp);
 
221
        return S3_ERROR;
 
222
    }
 
223
        
 
224
    if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
 
225
        s3close(fp);
 
226
 
 
227
        return S3_ERROR;
 
228
    }
 
229
        
 
230
    s3close(fp);
 
231
 
 
232
    E_INFO("Wrote %s [%ux%ux%u array]\n",
 
233
           fn, n_mixw, n_feat, n_density);
 
234
 
 
235
    return S3_SUCCESS;
 
236
}
 
237
 
 
238
/*
 
239
 * Log record.  Maintained by RCS.
 
240
 *
 
241
 * $Log$
 
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.
 
244
 * 
 
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
 
247
 *
 
248
 * Revision 1.3  2001/04/05 20:02:31  awb
 
249
 * *** empty log message ***
 
250
 *
 
251
 * Revision 1.2  2000/09/29 22:35:13  awb
 
252
 * *** empty log message ***
 
253
 *
 
254
 * Revision 1.1  2000/09/24 21:38:31  awb
 
255
 * *** empty log message ***
 
256
 *
 
257
 * Revision 1.2  97/07/16  11:36:22  eht
 
258
 * *** empty log message ***
 
259
 * 
 
260
 * Revision 1.1  97/03/17  15:01:49  eht
 
261
 * Initial revision
 
262
 * 
 
263
 *
 
264
 */