PocketSphinx  5prealpha
ps_mllr.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2009 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
42 /* System headers. */
43 #include <stdio.h>
44 
45 /* SphinxBase headers. */
46 #include <sphinxbase/ckd_alloc.h>
47 
48 /* Local headers. */
49 #include "acmod.h"
50 
51 ps_mllr_t *
52 ps_mllr_read(char const *regmatfile)
53 {
54  ps_mllr_t *mllr;
55  FILE *fp;
56  int n, i, m, j, k;
57 
58  mllr = ckd_calloc(1, sizeof(*mllr));
59  mllr->refcnt = 1;
60 
61  if ((fp = fopen(regmatfile, "r")) == NULL) {
62  E_ERROR_SYSTEM("Failed to open MLLR file '%s' for reading", regmatfile);
63  goto error_out;
64  }
65  else
66  E_INFO("Reading MLLR transformation file '%s'\n", regmatfile);
67 
68  if ((fscanf(fp, "%d", &n) != 1) || (n < 1)) {
69  E_ERROR("Failed to read number of MLLR classes\n");
70  goto error_out;
71  }
72  mllr->n_class = n;
73 
74  if ((fscanf(fp, "%d", &n) != 1)) {
75  E_ERROR("Failed to read number of feature streams\n");
76  goto error_out;
77  }
78  mllr->n_feat = n;
79  mllr->veclen = ckd_calloc(mllr->n_feat, sizeof(*mllr->veclen));
80 
81  mllr->A = (float32 ****) ckd_calloc(mllr->n_feat, sizeof(float32 **));
82  mllr->b = (float32 ***) ckd_calloc(mllr->n_feat, sizeof(float32 *));
83  mllr->h = (float32 ***) ckd_calloc(mllr->n_feat, sizeof(float32 *));
84 
85  for (i = 0; i < mllr->n_feat; ++i) {
86  if (fscanf(fp, "%d", &n) != 1) {
87  E_ERROR("Failed to read stream length for feature %d\n", i);
88  goto error_out;
89  }
90  mllr->veclen[i] = n;
91  mllr->A[i] =
92  (float32 ***) ckd_calloc_3d(mllr->n_class, mllr->veclen[i],
93  mllr->veclen[i], sizeof(float32));
94  mllr->b[i] =
95  (float32 **) ckd_calloc_2d(mllr->n_class, mllr->veclen[i],
96  sizeof(float32));
97  mllr->h[i] =
98  (float32 **) ckd_calloc_2d(mllr->n_class, mllr->veclen[i],
99  sizeof(float32));
100 
101  for (m = 0; m < mllr->n_class; ++m) {
102  for (j = 0; j < mllr->veclen[i]; ++j) {
103  for (k = 0; k < mllr->veclen[i]; ++k) {
104  if (fscanf(fp, "%f ", &mllr->A[i][m][j][k]) != 1) {
105  E_ERROR("Failed reading MLLR rotation (%d,%d,%d,%d)\n",
106  i, m, j, k);
107  goto error_out;
108  }
109  }
110  }
111  for (j = 0; j < mllr->veclen[i]; ++j) {
112  if (fscanf(fp, "%f ", &mllr->b[i][m][j]) != 1) {
113  E_ERROR("Failed reading MLLR bias (%d,%d,%d)\n",
114  i, m, j);
115  goto error_out;
116  }
117  }
118  for (j = 0; j < mllr->veclen[i]; ++j) {
119  if (fscanf(fp, "%f ", &mllr->h[i][m][j]) != 1) {
120  E_ERROR("Failed reading MLLR variance scale (%d,%d,%d)\n",
121  i, m, j);
122  goto error_out;
123  }
124  }
125  }
126  }
127  fclose(fp);
128  return mllr;
129 
130 error_out:
131  if (fp)
132  fclose(fp);
133  ps_mllr_free(mllr);
134  return NULL;
135 }
136 
137 ps_mllr_t *
139 {
140  ++mllr->refcnt;
141  return mllr;
142 }
143 
144 int
146 {
147  int i;
148 
149  if (mllr == NULL)
150  return 0;
151  if (--mllr->refcnt > 0)
152  return mllr->refcnt;
153 
154  for (i = 0; i < mllr->n_feat; ++i) {
155  if (mllr->A)
156  ckd_free_3d(mllr->A[i]);
157  if (mllr->b)
158  ckd_free_2d(mllr->b[i]);
159  if (mllr->h)
160  ckd_free_2d(mllr->h[i]);
161  }
162  ckd_free(mllr->veclen);
163  ckd_free(mllr->A);
164  ckd_free(mllr->b);
165  ckd_free(mllr->h);
166  ckd_free(mllr);
167 
168  return 0;
169 }
POCKETSPHINX_EXPORT ps_mllr_t * ps_mllr_retain(ps_mllr_t *mllr)
Retain a pointer to a linear transform.
Definition: ps_mllr.c:138
float32 *** h
Diagonal transformation of variances.
Definition: acmod.h:89
int * veclen
Length of input vectors for each stream.
Definition: acmod.h:86
float32 *** b
Bias part of mean transformations.
Definition: acmod.h:88
int n_class
Number of MLLR classes.
Definition: acmod.h:84
POCKETSPHINX_EXPORT ps_mllr_t * ps_mllr_read(char const *file)
Read a speaker-adaptive linear transform from a file.
Definition: ps_mllr.c:52
int n_feat
Number of feature streams.
Definition: acmod.h:85
POCKETSPHINX_EXPORT int ps_mllr_free(ps_mllr_t *mllr)
Release a pointer to a linear transform.
Definition: ps_mllr.c:145
Feature space linear transform structure.
Definition: acmod.h:82
float32 **** A
Rotation part of mean transformations.
Definition: acmod.h:87
int refcnt
Reference count.
Definition: acmod.h:83
Acoustic model structures for PocketSphinx.