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  * ascr.c -- Acoustic (senone) scores
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.5  2006/02/23  05:19:57  arthchan2003
49  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Added acoustic scores. 1, change names of sen to senscr. Add functions to clean up ascr's buffers.
50  *
51  * Revision 1.4.4.4  2005/10/17 04:51:03  arthchan2003
52  * Fixed resource in ascr correctly.
53  *
54  * Revision 1.4.4.3  2005/08/02 21:11:33  arthchan2003
55  * Changed sen to senscr, this avoid confusion in align,allphone, decode_anytopo.
56  *
57  * Revision 1.4.4.2  2005/07/24 01:31:03  arthchan2003
58  * add interface to set temporary active array correctly.
59  *
60  * Revision 1.4.4.1  2005/07/17 05:44:30  arthchan2003
61  * Added dag_write_header so that DAG header writer could be shared between 3.x and 3.0. However, because the backtrack pointer structure is different in 3.x and 3.0. The DAG writer still can't be shared yet.
62  *
63  * Revision 1.4  2005/06/21 22:32:25  arthchan2003
64  * Log. Significant expansion of ascr_t to be the container of all types
65  * of acoustic scores.  Implementations of init, report, free functions
66  * are now provided. ascr_shift_one_cache_frame is used to provide a
67  * wrapper for shifting one frame of cache frame in phoneme lookahead.
68  * It is expected to be used later more if more complicated scheme of
69  * lookahead is used.
70  *
71  * Revision 1.6  2005/04/21 23:50:26  archan
72  * Some more refactoring on the how reporting of structures inside kbcore_t is done, it is now 50% nice. Also added class-based LM test case into test-decode.sh.in.  At this moment, everything in search mode 5 is already done.  It is time to test the idea whether the search can really be used.
73  *
74  * Revision 1.5  2005/04/20 03:31:57  archan
75  * Part of refactoring: add ascr_shift_one_cache_frame which could shift one cache frame.
76  *
77  * Revision 1.4  2005/03/30 01:22:46  archan
78  * Fixed mistakes in last updates. Add
79  *
80  *
81  * 09-Feb-2000	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
82  * 		Started.
83  */
84 
85 
86 #include <string.h>
87 
88 #include "ascr.h"
89 
90 
91 ascr_t *
ascr_init(int32 n_sen,int32 n_comsen,int32 n_sseq,int32 n_comsseq,int32 pl_win,int32 n_cisen)92 ascr_init(int32 n_sen, int32 n_comsen, int32 n_sseq, int32 n_comsseq,
93           int32 pl_win, int32 n_cisen)
94 {
95     ascr_t *ascr;
96 
97     ascr = (ascr_t *) ckd_calloc(1, sizeof(ascr_t));
98     ascr->senscr = (int32 *) ckd_calloc(n_sen + n_comsen, sizeof(int32));
99     ascr->comsen = ascr->senscr + n_sen;
100 
101 
102     /* MEMORY ALLOCATION : Active senones */
103     ascr->sen_active = (uint8 *) ckd_calloc(n_sen, sizeof(uint8));
104     ascr->rec_sen_active = (uint8 *) ckd_calloc(n_sen, sizeof(uint8));
105     ascr->ssid_active = (uint8 *) ckd_calloc(n_sseq, sizeof(uint8));
106     if (n_comsseq > 0)
107         ascr->comssid_active =
108             (uint8 *) ckd_calloc(n_comsseq, sizeof(uint8));
109 
110     /* MEMORY ALLOCATION : CI senones */
111     ascr->cache_ci_senscr =
112         (int32 **) ckd_calloc_2d(pl_win, n_cisen, sizeof(int32));
113     ascr->cache_best_list = (int32 *) ckd_calloc(pl_win, sizeof(int32));
114 
115     ascr->n_sen = n_sen;
116     ascr->n_comsen = n_comsen;
117     ascr->n_sseq = n_sseq;
118     ascr->n_comsseq = n_comsseq;
119     ascr->pl_win = pl_win;
120     ascr->n_cisen = n_cisen;
121     return ascr;
122 }
123 
124 void
ascr_report(ascr_t * a)125 ascr_report(ascr_t * a)
126 {
127     E_INFO_NOFN("Initialization of ascr_t, report:\n");
128     E_INFO_NOFN("No. of CI senone =%d \n", a->n_cisen);
129     E_INFO_NOFN("No. of senone = %d\n", a->n_sen);
130     E_INFO_NOFN("No. of composite senone = %d\n", a->n_comsen);
131     E_INFO_NOFN("No. of senone sequence = %d\n", a->n_sseq);
132     E_INFO_NOFN("No. of composite senone sequence=%d \n", a->n_comsseq);
133     E_INFO_NOFN("Parameters used in phoneme lookahead:\n");
134     E_INFO_NOFN("Phoneme lookahead window = %d\n", a->pl_win);
135     E_INFO_NOFN("\n");
136 }
137 
138 /* Shift one frame. */
139 /* get the CI sen scores for the t+pl_win'th frame (a slice) */
140 
141 void
ascr_shift_one_cache_frame(ascr_t * a,int32 win_efv)142 ascr_shift_one_cache_frame(ascr_t * a, int32 win_efv)
143 {
144     int32 i, j;
145     for (i = 0; i < win_efv - 1; i++) {
146         a->cache_best_list[i] = a->cache_best_list[i + 1];
147         for (j = 0; j < a->n_cisen; j++) {
148             a->cache_ci_senscr[i][j] = a->cache_ci_senscr[i + 1][j];
149         }
150     }
151 }
152 
153 /* Print the senscr now.
154  */
155 void
ascr_print_senscr(ascr_t * a)156 ascr_print_senscr(ascr_t * a)
157 {
158     int32 i;
159 
160     for (i = 0; i < a->n_sen; i++) {
161         if (a->sen_active[i]) {
162             E_INFO("ascr->senscr[%d], %d\n", i, a->senscr[i]);
163             if (a->senscr[i] > 0)
164                 E_WARN("Score of %d >0\n", i);
165         }
166     }
167 
168     for (i = 0; i < a->n_comsen; i++) {
169         if (a->comssid_active[i]) {
170             E_INFO("ascr->comsen[%d], %d\n", i, a->comsen[i]);
171             if (a->comsen[i] > 0)
172                 E_WARN("Score of %d >0\n", i);
173         }
174     }
175 }
176 
177 
178 void
ascr_free(ascr_t * a)179 ascr_free(ascr_t * a)
180 {
181     if (a) {
182         if (a->senscr)
183             ckd_free(a->senscr);
184 
185         if (a->sen_active)
186             ckd_free((void *) a->sen_active);
187 
188         if (a->rec_sen_active)
189             ckd_free((void *) a->rec_sen_active);
190 
191         if (a->ssid_active)
192             ckd_free((void *) a->ssid_active);
193 
194         if (a->comssid_active)
195             ckd_free((void *) a->comssid_active);
196 
197         if (a->cache_ci_senscr)
198             ckd_free_2d((void **) a->cache_ci_senscr);
199         if (a->cache_best_list)
200             ckd_free((void *) a->cache_best_list);
201 
202         ckd_free(a);
203     }
204 
205 }
206 
207 void
ascr_clear_sen_active(ascr_t * a)208 ascr_clear_sen_active(ascr_t * a)
209 {
210     assert(a);
211     assert(a->sen_active);
212     memset(a->sen_active, 0, a->n_sen * sizeof(*a->sen_active));
213 }
214 
215 
216 void
ascr_clear_ssid_active(ascr_t * a)217 ascr_clear_ssid_active(ascr_t * a)
218 {
219     assert(a);
220     assert(a->ssid_active);
221     memset(a->ssid_active, 0, a->n_sseq * sizeof(*a->ssid_active));
222 }
223 
224 void
ascr_clear_comssid_active(ascr_t * a)225 ascr_clear_comssid_active(ascr_t * a)
226 {
227     assert(a);
228     if (a->n_comsseq > 0) {
229         assert(a->comssid_active);
230         memset(a->comssid_active, 0, a->n_comsseq * sizeof(*a->comssid_active));
231     }
232 }
233