1/*---------------------------------------------------------------------------*
2 *  swimodel.h  *
3 *                                                                           *
4 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5 *                                                                           *
6 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7 *  you may not use this file except in compliance with the License.         *
8 *                                                                           *
9 *  You may obtain a copy of the License at                                  *
10 *      http://www.apache.org/licenses/LICENSE-2.0                           *
11 *                                                                           *
12 *  Unless required by applicable law or agreed to in writing, software      *
13 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 *  See the License for the specific language governing permissions and      *
16 *  limitations under the License.                                           *
17 *                                                                           *
18 *---------------------------------------------------------------------------*/
19
20#ifndef __SWIMODEL_H__
21#define __SWIMODEL_H__
22
23#include "hmm_type.h"
24#include "pre_desc.h"
25#include "srec_sizes.h"
26#include "PortExport.h"
27
28
29/**
30 * @todo document
31 */
32typedef struct
33{
34  short num_pdfs;           /* number of pdfs for this state */
35  /* featdata avg_durn;           average state duration, belongs here but stored
36     elsewhere to avoid paging back to memory of acoustic models, c54!! */
37  const featdata *means;            /* pointer to block of means for the set
38       of pdfs (points into the allmeans array)*/
39  const wtdata *weights;            /*pointer to weights*/
40}
41SWIhmmState;
42
43/**
44 * Model loading storage structures.
45 */
46typedef struct
47{
48  void* mmap_zip_data;          /* mmap file in one chunk */
49  size_t mmap_zip_size;         /* size of above */
50  modelID num_hmmstates;        /* number of hmm states ~ 800 */
51  short num_dims;               /* feature vector dimensions ~ 36 or 28 */
52  modelID num_pdfs;             /* total number of pdfs ~ 4800 */
53  const SWIhmmState *hmmstates;       /* size num_hmmstates ~ 800*/
54  const featdata *allmeans;        /* size num_dims*num_pdfs ~ 36*4800 */
55  const wtdata *allweights;        /* size num_pdfs ~ 4800 */
56  const featdata *avg_state_durations; /* average duration of this acoustic model state */
57}
58SWIModel;
59
60#ifdef __cplusplus
61extern "C"
62{
63#endif
64
65/* SpeechWorks compact acoustic models */
66const SWIModel *load_swimodel(const char *filename);
67void free_swimodel(const SWIModel* swimodel);
68scodata mixture_diagonal_gaussian_swimodel(const preprocessed *prep, const SWIhmmState *spd, short num_dims);
69
70extern const char loop_cost_table [128][6];
71extern const char trans_cost_table [128][6];
72
73#ifdef __cplusplus
74}
75#endif
76
77
78/* the looping cost for the new duration model. In this new duration model,
79   the looping probability is multiplied by a sigmoid function having the
80   following form: sigm(-scale(duration_so_far-offset))  so that the looping
81   cost increases as the duration_so_far increases and encouraging to
82   stay within a given state for a duration approx. equal to the average state
83   duration. The looping cost values are implemented as a lookup table.*/
84
85static PINLINE costdata duration_penalty_loop(frameID average_duration, frameID duration_so_far)
86{
87 if (average_duration > 127)  average_duration =  127;
88 if(duration_so_far> 6) duration_so_far = 6;
89 return (costdata)loop_cost_table[average_duration][duration_so_far-1];
90}
91
92/* the transition cost for the new duration model. In this new duration model,
93   the transition probability is multiplied by a sigmoid function having the
94   following form: sigm(scale(duration_so_far-offset)) so that the  transition
95   cost decreases as the duration_so_far increases thus encouraging to leave
96   a given state when the duration exceeds the average state duration. The transition
97   cost values are implemented as a lookup table*/
98
99static PINLINE costdata duration_penalty_depart(frameID average_duration, frameID duration_so_far)
100{
101  if (average_duration > 127)     average_duration = 127;
102  if(duration_so_far> 6)     duration_so_far = 6;
103  return (costdata) trans_cost_table[average_duration][duration_so_far-1];
104}
105
106#endif /* __SWIMODEL_H__ */
107
108