1 /* ====================================================================
2  * Copyright (c) 1999-2004 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  * adaptor.c -- Wrapper for structures of adaptation.
38  *
39  * **********************************************
40  * CMU ARPA Speech Project
41  *
42  * Copyright (c) 1999 Carnegie Mellon University.
43  * ALL RIGHTS RESERVED.
44  * **********************************************
45  *
46  * HISTORY
47  * $Log$
48  * Revision 1.2  2006/02/22  14:48:13  arthchan2003
49  * Merged from SPHINX3_5_2_RCI_IRII_BRANCH: Added adapt_set_mllr (Moved from kb)
50  *
51  * Revision 1.1.4.1  2005/09/18 01:12:31  arthchan2003
52  * Add adapt_set_mllr which is responsible for single stream MLLR switching.
53  *
54  * Revision 1.1  2005/06/21 17:59:44  arthchan2003
55  * Log: Implementation and Interface of adapt_am_t, a wrapper of
56  * adaptation capabability of Sphinx 3.  It takes the responsibility from
57  * kb_t to manage regA, regB and mllr_nclass.  Interfaces are not fully
58  * completed. So "pointer" symtom code still appears in kb.c
59  *
60  * Revision 1.5  2005/06/19 19:41:21  archan
61  * Sphinx3 to s3.generic: Added multiple regression class for single stream MLLR. Enabled MLLR for livepretend and decode.
62  *
63  * Revision 1.4  2005/05/11 06:10:37  archan
64  * Code for lattice and back track pointer table dumping is now wrapped in reg_result_dump.  The function is shared across mode 4 and mode 5.  Possibly later for mode 3 and mode 6 as well.
65  *
66  * Revision 1.3  2005/03/30 01:22:46  archan
67  * Fixed mistakes in last updates. Add
68  *
69  *
70  * 25-Mar-2005  Arthur Chan (archan@cs.cmu.edu) at Carnegie Mellon Unversity
71  *              First started.
72  */
73 
74 #include <string.h>
75 
76 #include "adaptor.h"
77 #include "mllr.h"
78 #include "cb2mllr_io.h"
79 
80 #define ADAPT_FILE_NAME_LENGTH 1024
81 
82 adapt_am_t *
adapt_am_init()83 adapt_am_init()
84 {
85     adapt_am_t *ad = (adapt_am_t *) ckd_calloc(1, sizeof(adapt_am_t));
86 
87     ad->prevmllrfn =
88         (char *) ckd_calloc(ADAPT_FILE_NAME_LENGTH, sizeof(char));
89     ad->prevmllrfn[0] = '\0';
90 
91     return ad;
92 }
93 
94 void
adapt_am_free(adapt_am_t * ad)95 adapt_am_free(adapt_am_t * ad)
96 {
97     if (ad) {
98         if (ad->prevmllrfn) {
99             ckd_free((void *) ad->prevmllrfn);
100         }
101         ckd_free((void *) ad);
102     }
103 }
104 
105 void
adapt_set_mllr(adapt_am_t * ad,mgau_model_t * g,const char * mllrfile,const char * cb2mllrname,mdef_t * mdef,cmd_ln_t * config)106 adapt_set_mllr(adapt_am_t * ad, mgau_model_t * g, const char *mllrfile,
107 	       const char *cb2mllrname, mdef_t * mdef, cmd_ln_t *config)
108 {
109     int32 *cb2mllr;
110 
111     /* Reread the gaussian mean from the file again */
112     E_INFO("Reloading mean\n");
113 
114     /* Read in the mllr matrix */
115     mgau_mean_reload(g, cmd_ln_str_r(config, "-mean"));
116 
117 #if MLLR_DEBUG
118     /*This generates huge amount of information */
119     /*    mgau_dump(g,1); */
120 #endif
121 
122     mllr_read_regmat(mllrfile,
123                      &(ad->regA),
124                      &(ad->regB), &(ad->mllr_nclass), mgau_veclen(g));
125 
126     if (cb2mllrname && strcmp(cb2mllrname, ".1cls.") != 0) {
127         int32 ncb, nmllr;
128 
129         cb2mllr_read(cb2mllrname, &cb2mllr, &ncb, &nmllr);
130         if (nmllr != ad->mllr_nclass)
131             E_FATAL
132                 ("Number of classes in cb2mllr does not match mllr (%d != %d)\n",
133                  ncb, ad->mllr_nclass);
134         if (ncb != mdef->n_sen)
135             E_FATAL
136                 ("Number of senones in cb2mllr does not match mdef (%d != %d)\n",
137                  ncb, mdef->n_sen);
138     }
139     else
140         cb2mllr = NULL;
141 
142     /* Transform all the mean vectors */
143 
144     mllr_norm_mgau(g, ad->regA, ad->regB, ad->mllr_nclass, cb2mllr);
145     ckd_free(cb2mllr);
146 
147 #if MLLR_DEBUG
148     /*#if 1 */
149     mllr_dump(ad->regA, ad->regB, mgau_veclen(g), g->mllr_class, cb2mllr);
150     /*This generates huge amount of information */
151     /*mgau_dump(kbcore_mgau(kb->kbcore),1); */
152 #endif
153 
154 }
155