~ubuntu-branches/ubuntu/raring/sphinxtrain/raring-proposed

« back to all changes in this revision

Viewing changes to src/programs/cp_parm/main.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: main.c
 
39
 * 
 
40
 * Description: 
 
41
 *      Copies parameters from an input file to and output file
 
42
 *
 
43
 * Author: 
 
44
 *     Eric H. Thayer (eht@cs.cmu.edu)
 
45
 *********************************************************************/
 
46
 
 
47
#include "parse_cmd_ln.h"
 
48
#include <s3/s3mixw_io.h>
 
49
#include <s3/s3gau_io.h>
 
50
#include <s3/s3tmat_io.h>
 
51
#include <s3/gauden.h>
 
52
#include <sphinxbase/ckd_alloc.h>
 
53
#include <sphinxbase/cmd_ln.h>
 
54
 
 
55
#include <s3/s3.h>
 
56
 
 
57
#include <stdio.h>
 
58
 
 
59
static int rd_parm(void);
 
60
static int cp_parm(void);
 
61
static int wr_parm(void);
 
62
 
 
63
 
 
64
static float32 ***imixw;
 
65
static uint32 n_mixw_i;
 
66
static float32 ***omixw;
 
67
static uint32 n_mixw_o;
 
68
 
 
69
static uint32 n_stream;
 
70
static uint32 n_density;
 
71
 
 
72
static vector_t ***igau;
 
73
static vector_t ****igau_full;
 
74
static uint32 n_cb_i;
 
75
static vector_t ***ogau;
 
76
static vector_t ****ogau_full;
 
77
static uint32 n_cb_o;
 
78
static uint32 *veclen;
 
79
 
 
80
static float32 ***itmat;
 
81
static uint32 n_tmat_i;
 
82
static float32 ***otmat;
 
83
static uint32 n_tmat_o;
 
84
static uint32 n_state_pm;
 
85
 
 
86
 
 
87
 
 
88
int
 
89
rd_mixw(const char *fn, uint32 n_o)
 
90
{
 
91
    if (s3mixw_read(fn,
 
92
                    &imixw,
 
93
                    &n_mixw_i,
 
94
                    &n_stream,
 
95
                    &n_density) != S3_SUCCESS)
 
96
        return S3_ERROR;
 
97
 
 
98
    n_mixw_o = n_o;
 
99
 
 
100
    omixw = (float32 ***)ckd_calloc_3d(n_mixw_o,
 
101
                                       n_stream,
 
102
                                       n_density,
 
103
                                       sizeof(float32));
 
104
 
 
105
    return S3_SUCCESS;
 
106
}
 
107
int
 
108
cp_mixw(uint32 o,
 
109
        uint32 i)
 
110
{
 
111
    uint32 j, k;
 
112
 
 
113
    printf("mixw %u <= %u\n", o, i);
 
114
 
 
115
 
 
116
    for (j = 0; j < n_stream; j++) {
 
117
        for (k = 0; k < n_density; k++) {
 
118
            omixw[o][j][k] = imixw[i][j][k];
 
119
        }
 
120
    }
 
121
    return S3_SUCCESS;
 
122
}
 
123
int
 
124
wr_mixw(const char *fn)
 
125
{
 
126
    if (s3mixw_write(fn,
 
127
                     omixw,
 
128
                     n_mixw_o,
 
129
                     n_stream,
 
130
                     n_density) != S3_SUCCESS)
 
131
        return S3_ERROR;
 
132
 
 
133
    ckd_free_3d((void ***)omixw);
 
134
    ckd_free_3d((void ***)imixw);
 
135
 
 
136
    return S3_SUCCESS;
 
137
}
 
138
 
 
139
int
 
140
rd_tmat(const char *fn, uint32 n_o)
 
141
{
 
142
    if (s3tmat_read(fn,
 
143
                    &itmat,
 
144
                    &n_tmat_i,
 
145
                    &n_state_pm) != S3_SUCCESS)
 
146
        return S3_ERROR;
 
147
 
 
148
    n_tmat_o = n_o;
 
149
 
 
150
    otmat = (float32 ***)ckd_calloc_3d(n_tmat_o,
 
151
                                       n_state_pm-1,
 
152
                                       n_state_pm,
 
153
                                       sizeof(float32));
 
154
 
 
155
    return S3_SUCCESS;
 
156
}
 
157
int
 
158
cp_tmat(uint32 o,
 
159
        uint32 i)
 
160
{
 
161
    uint32 j, k;
 
162
 
 
163
    printf("tmat %u <= %u\n", o, i);
 
164
 
 
165
    for (j = 0; j < n_state_pm-1; j++) {
 
166
        for (k = 0; k < n_state_pm; k++) {
 
167
            otmat[o][j][k] = itmat[i][j][k];
 
168
        }
 
169
    }
 
170
    return S3_SUCCESS;
 
171
}
 
172
int
 
173
wr_tmat(const char *fn)
 
174
{
 
175
    if (s3tmat_write(fn,
 
176
                     otmat,
 
177
                     n_tmat_o,
 
178
                     n_state_pm) != S3_SUCCESS)
 
179
        return S3_ERROR;
 
180
 
 
181
    ckd_free_3d((void ***)otmat);
 
182
    ckd_free_3d((void ***)itmat);
 
183
 
 
184
    return S3_SUCCESS;
 
185
}
 
186
 
 
187
int
 
188
rd_gau(const char *fn, uint32 n_o)
 
189
{
 
190
    if (s3gau_read(fn,
 
191
                   &igau,
 
192
                   &n_cb_i,
 
193
                   &n_stream,
 
194
                   &n_density,
 
195
                   &veclen) != S3_SUCCESS)
 
196
        return S3_ERROR;
 
197
 
 
198
    n_cb_o = n_o;
 
199
    
 
200
    ogau = (vector_t ***)gauden_alloc_param(n_cb_o,
 
201
                                            n_stream,
 
202
                                            n_density,
 
203
                                            veclen);
 
204
    return S3_SUCCESS;
 
205
}
 
206
int
 
207
cp_gau(uint32 o,
 
208
       uint32 i)
 
209
{
 
210
    uint32 j, k, l;
 
211
 
 
212
    printf("gau %u <= %u\n", o, i);
 
213
 
 
214
    for (j = 0; j < n_stream; j++) {
 
215
        for (k = 0; k < n_density; k++) {
 
216
            for (l = 0; l < veclen[j]; l++) {
 
217
                ogau[o][j][k][l] = igau[i][j][k][l];
 
218
            }
 
219
        }
 
220
    }
 
221
    return S3_SUCCESS;
 
222
}
 
223
int
 
224
wr_gau(const char *fn)
 
225
{
 
226
    if (s3gau_write(fn,
 
227
                    (const vector_t ***)ogau,
 
228
                    n_cb_o,
 
229
                    n_stream,
 
230
                    n_density,
 
231
                    veclen) != S3_SUCCESS)
 
232
        return S3_ERROR;
 
233
 
 
234
    gauden_free_param(ogau);
 
235
    gauden_free_param(igau);
 
236
 
 
237
    return S3_SUCCESS;
 
238
}
 
239
 
 
240
int
 
241
rd_gau_full(const char *fn, uint32 n_o)
 
242
{
 
243
    if (s3gau_read_full(fn,
 
244
                        &igau_full,
 
245
                        &n_cb_i,
 
246
                        &n_stream,
 
247
                        &n_density,
 
248
                        &veclen) != S3_SUCCESS)
 
249
        return S3_ERROR;
 
250
 
 
251
    n_cb_o = n_o;
 
252
    
 
253
    ogau_full = (vector_t ****)gauden_alloc_param_full(n_cb_o,
 
254
                                                       n_stream,
 
255
                                                       n_density,
 
256
                                                       veclen);
 
257
    return S3_SUCCESS;
 
258
}
 
259
int
 
260
cp_gau_full(uint32 o,
 
261
            uint32 i)
 
262
{
 
263
    uint32 j, k, l, ll;
 
264
 
 
265
    printf("gau %u <= %u\n", o, i);
 
266
 
 
267
    for (j = 0; j < n_stream; j++) {
 
268
        for (k = 0; k < n_density; k++) {
 
269
            for (l = 0; l < veclen[j]; l++) {
 
270
                for (ll = 0; ll < veclen[j]; ll++) {
 
271
                    ogau_full[o][j][k][l][ll] = igau_full[i][j][k][l][ll];
 
272
                }
 
273
            }
 
274
        }
 
275
    }
 
276
    return S3_SUCCESS;
 
277
}
 
278
int
 
279
wr_gau_full(const char *fn)
 
280
{
 
281
    if (s3gau_write_full(fn,
 
282
                    (const vector_t ****)ogau_full,
 
283
                    n_cb_o,
 
284
                    n_stream,
 
285
                    n_density,
 
286
                    veclen) != S3_SUCCESS)
 
287
        return S3_ERROR;
 
288
 
 
289
    gauden_free_param_full(ogau_full);
 
290
    gauden_free_param_full(igau_full);
 
291
 
 
292
    return S3_SUCCESS;
 
293
}
 
294
 
 
295
static int
 
296
rd_parm()
 
297
{
 
298
    if(cmd_ln_str("-imixwfn")   ==NULL&&
 
299
       cmd_ln_str("-igaufn")    ==NULL&&
 
300
       cmd_ln_str("-ifullgaufn")==NULL&&
 
301
       cmd_ln_str("-itmatfn")   ==NULL
 
302
       ) {
 
303
      E_INFO("Please specify one of the following: -imixwfn, -igaufn, -ifullgaufn, -itmatfn\n");
 
304
      return S3_ERROR;
 
305
    }
 
306
 
 
307
    if (cmd_ln_str("-imixwfn")) {
 
308
        if(cmd_ln_str("-nmixwout")==NULL){
 
309
            E_INFO("Please specify -nmixwout\n");
 
310
            return S3_ERROR;
 
311
        }
 
312
        rd_mixw(cmd_ln_str("-imixwfn"),
 
313
                cmd_ln_int32("-nmixwout"));
 
314
    }
 
315
    if (cmd_ln_str("-igaufn")) {
 
316
        if(cmd_ln_str("-ncbout")==NULL){
 
317
            E_INFO("Please specify -ncbout\n");
 
318
            return S3_ERROR;
 
319
        }
 
320
        rd_gau(cmd_ln_str("-igaufn"),
 
321
                cmd_ln_int32("-ncbout"));
 
322
    }
 
323
    if (cmd_ln_str("-ifullgaufn")) {
 
324
        if(cmd_ln_str("-ncbout")==NULL){
 
325
            E_INFO("Please specify -ncbout\n");
 
326
            return S3_ERROR;
 
327
        }
 
328
        rd_gau_full(cmd_ln_str("-ifullgaufn"),
 
329
                cmd_ln_int32("-ncbout"));
 
330
    }
 
331
    if (cmd_ln_str("-itmatfn")) {
 
332
        if(cmd_ln_str("-ntmatout")==NULL){
 
333
            E_INFO("Please specify -ntmatout\n");
 
334
            return S3_ERROR;
 
335
        }
 
336
        rd_tmat(cmd_ln_str("-itmatfn"),
 
337
                cmd_ln_int32("-ntmatout"));
 
338
    }
 
339
    return S3_SUCCESS;
 
340
}
 
341
 
 
342
static int
 
343
cp_parm()
 
344
{
 
345
    FILE *fp;
 
346
    uint32 i, o;
 
347
    uint32 max=0;
 
348
 
 
349
    /* Open the file first to see whether command-line parameters
 
350
       match
 
351
     */
 
352
    
 
353
    if(cmd_ln_str("-cpopsfn")==NULL) {
 
354
        E_INFO("Please specify -cpopsfn\n");
 
355
        return S3_ERROR;
 
356
    }
 
357
    fp = fopen(cmd_ln_str("-cpopsfn"), "r");
 
358
    if (fp == NULL) {
 
359
        E_INFO("Unable to open cpops file\n");
 
360
 
 
361
        return S3_ERROR;
 
362
    }
 
363
    while (fscanf(fp, "%u %u", &o, &i) == 2) {
 
364
        if(o+1>max) {
 
365
            max=o+1;
 
366
        }
 
367
    }
 
368
 
 
369
    if (omixw) {
 
370
        if(max != n_mixw_o) {
 
371
            E_INFO("Mismatch between cp operation file (max out %d) and -nmixout (%d)\n",max, n_mixw_o);
 
372
            return S3_ERROR;
 
373
        }
 
374
    }
 
375
 
 
376
    if (ogau) {
 
377
        if(max != n_cb_o) {
 
378
            E_INFO("Mismatch between cp operation file (max out %d) and -ncbout (%d)\n",max, n_cb_o);
 
379
            return S3_ERROR;
 
380
        }
 
381
    }
 
382
 
 
383
    if (ogau_full) {
 
384
        if(max != n_cb_o) {
 
385
            E_INFO("Mismatch between cp operation file (max out %d) and -ncbout (%d)\n",max, n_cb_o);
 
386
            return S3_ERROR;
 
387
        }
 
388
    }
 
389
        
 
390
    if (otmat) {
 
391
        if(max != n_tmat_o) {
 
392
            E_INFO("Mismatch between cp operation file (max out %d) and -ntmatout (%d)\n",max, n_tmat_o);
 
393
            return S3_ERROR;
 
394
        }
 
395
    }
 
396
    
 
397
    fclose(fp);
 
398
    
 
399
    fp = fopen(cmd_ln_str("-cpopsfn"), "r");
 
400
    while (fscanf(fp, "%u %u", &o, &i) == 2) {
 
401
        if (omixw) {
 
402
            cp_mixw(o, i);
 
403
        }
 
404
        if (ogau) {
 
405
            cp_gau(o, i);
 
406
        }
 
407
        if (ogau_full) {
 
408
            cp_gau_full(o, i);
 
409
        }
 
410
        if (otmat) {
 
411
            cp_tmat(o, i);
 
412
        }
 
413
    }
 
414
    fclose(fp);
 
415
 
 
416
    return S3_SUCCESS;
 
417
}
 
418
 
 
419
static int
 
420
wr_parm()
 
421
{
 
422
    if (omixw) {
 
423
        if(cmd_ln_str("-omixwfn") == NULL) {
 
424
            E_INFO("Please specify -omixwfn\n");
 
425
            return S3_ERROR;
 
426
        }
 
427
        wr_mixw(cmd_ln_str("-omixwfn"));
 
428
    }
 
429
    if (ogau) {
 
430
        if(cmd_ln_str("-ogaufn") == NULL) {
 
431
            E_INFO("Please specify -ogaufn\n");
 
432
            return S3_ERROR;
 
433
        }
 
434
        wr_gau(cmd_ln_str("-ogaufn"));
 
435
    }
 
436
    if (ogau_full) {
 
437
        if(cmd_ln_str("-ofullgaufn") == NULL) {
 
438
            E_INFO("Please specify -ofullgaufn\n");
 
439
            return S3_ERROR;
 
440
        }
 
441
        wr_gau_full(cmd_ln_str("-ofullgaufn"));
 
442
    }
 
443
    if (otmat) {
 
444
        if(cmd_ln_str("-otmatfn") == NULL) {
 
445
            E_INFO("Please specify -otmatfn\n");
 
446
            return S3_ERROR;
 
447
        }
 
448
        wr_tmat(cmd_ln_str("-otmatfn"));
 
449
    }
 
450
    return S3_SUCCESS;
 
451
}
 
452
 
 
453
int
 
454
main(int argc, char *argv[])
 
455
{
 
456
 
 
457
    parse_cmd_ln(argc, argv);
 
458
 
 
459
    if(rd_parm()==S3_ERROR) {
 
460
        E_FATAL("Problem in reading input parameters.\n");
 
461
    }
 
462
    if(cp_parm()==S3_ERROR) {
 
463
        E_FATAL("Problem in copying parameters.\n");
 
464
    }
 
465
    if(wr_parm()==S3_ERROR) {
 
466
        E_FATAL("Problem in writing output parameters.\n");
 
467
    }
 
468
    ckd_free(veclen);
 
469
 
 
470
    return 0;
 
471
}
 
472