SphinxBase  5prealpha
fe_prespch_buf.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 2013 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 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 
42 #include <stdio.h>
43 #include <string.h>
44 #include <assert.h>
45 
46 #include "sphinxbase/ckd_alloc.h"
47 #include "sphinxbase/err.h"
48 
49 #include "fe_prespch_buf.h"
50 
51 struct prespch_buf_s {
52  /* saved mfcc frames */
53  mfcc_t **cep_buf;
54  /* saved pcm audio */
55  int16 *pcm_buf;
56 
57  /* flag for pcm buffer initialization */
58  int16 cep_write_ptr;
59  /* read pointer for cep buffer */
60  int16 cep_read_ptr;
61  /* Count */
62  int16 ncep;
63 
64 
65  /* flag for pcm buffer initialization */
66  int16 pcm_write_ptr;
67  /* read pointer for cep buffer */
68  int16 pcm_read_ptr;
69  /* Count */
70  int16 npcm;
71 
72  /* frames amount in cep buffer */
73  int16 num_frames_cep;
74  /* frames amount in pcm buffer */
75  int16 num_frames_pcm;
76  /* filters amount */
77  int16 num_cepstra;
78  /* amount of fresh samples in frame */
79  int16 num_samples;
80 };
81 
83 fe_prespch_init(int num_frames, int num_cepstra, int num_samples)
84 {
85  prespch_buf_t *prespch_buf;
86 
87  prespch_buf = (prespch_buf_t *) ckd_calloc(1, sizeof(prespch_buf_t));
88 
89  prespch_buf->num_cepstra = num_cepstra;
90  prespch_buf->num_frames_cep = num_frames;
91  prespch_buf->num_samples = num_samples;
92  prespch_buf->num_frames_pcm = 0;
93 
94  prespch_buf->cep_write_ptr = 0;
95  prespch_buf->cep_read_ptr = 0;
96  prespch_buf->ncep = 0;
97 
98  prespch_buf->pcm_write_ptr = 0;
99  prespch_buf->pcm_read_ptr = 0;
100  prespch_buf->npcm = 0;
101 
102  prespch_buf->cep_buf = (mfcc_t **)
103  ckd_calloc_2d(num_frames, num_cepstra,
104  sizeof(**prespch_buf->cep_buf));
105 
106  prespch_buf->pcm_buf = (int16 *)
107  ckd_calloc(prespch_buf->num_frames_pcm * prespch_buf->num_samples,
108  sizeof(int16));
109 
110  return prespch_buf;
111 }
112 
113 
114 int
115 fe_prespch_read_cep(prespch_buf_t * prespch_buf, mfcc_t * feat)
116 {
117  if (prespch_buf->ncep == 0)
118  return 0;
119  memcpy(feat, prespch_buf->cep_buf[prespch_buf->cep_read_ptr],
120  sizeof(mfcc_t) * prespch_buf->num_cepstra);
121  prespch_buf->cep_read_ptr = (prespch_buf->cep_read_ptr + 1) % prespch_buf->num_frames_cep;
122  prespch_buf->ncep--;
123  return 1;
124 }
125 
126 void
127 fe_prespch_write_cep(prespch_buf_t * prespch_buf, mfcc_t * feat)
128 {
129  memcpy(prespch_buf->cep_buf[prespch_buf->cep_write_ptr], feat,
130  sizeof(mfcc_t) * prespch_buf->num_cepstra);
131  prespch_buf->cep_write_ptr = (prespch_buf->cep_write_ptr + 1) % prespch_buf->num_frames_cep;
132  if (prespch_buf->ncep < prespch_buf->num_frames_cep) {
133  prespch_buf->ncep++;
134  } else {
135  prespch_buf->cep_read_ptr = (prespch_buf->cep_read_ptr + 1) % prespch_buf->num_frames_cep;
136  }
137 }
138 
139 void
140 fe_prespch_read_pcm(prespch_buf_t * prespch_buf, int16 *samples,
141  int32 *samples_num)
142 {
143  int i;
144  int16 *cursample = samples;
145  *samples_num = prespch_buf->npcm * prespch_buf->num_samples;
146  for (i = 0; i < prespch_buf->npcm; i++) {
147  memcpy(cursample, &prespch_buf->pcm_buf[prespch_buf->pcm_read_ptr * prespch_buf->num_samples],
148  prespch_buf->num_samples * sizeof(int16));
149  prespch_buf->pcm_read_ptr = (prespch_buf->pcm_read_ptr + 1) % prespch_buf->num_frames_pcm;
150  }
151  prespch_buf->pcm_read_ptr = 0;
152  prespch_buf->pcm_write_ptr = 0;
153  prespch_buf->npcm = 0;
154  return;
155 }
156 
157 void
158 fe_prespch_write_pcm(prespch_buf_t * prespch_buf, int16 * samples)
159 {
160  int32 sample_ptr;
161 
162  sample_ptr = prespch_buf->pcm_write_ptr * prespch_buf->num_samples;
163  memcpy(&prespch_buf->pcm_buf[sample_ptr], samples,
164  prespch_buf->num_samples * sizeof(int16));
165 
166  prespch_buf->pcm_write_ptr = (prespch_buf->pcm_write_ptr + 1) % prespch_buf->num_frames_pcm;
167  if (prespch_buf->npcm < prespch_buf->num_frames_pcm) {
168  prespch_buf->npcm++;
169  } else {
170  prespch_buf->pcm_read_ptr = (prespch_buf->pcm_read_ptr + 1) % prespch_buf->num_frames_pcm;
171  }
172 }
173 
174 void
175 fe_prespch_reset_cep(prespch_buf_t * prespch_buf)
176 {
177  prespch_buf->cep_read_ptr = 0;
178  prespch_buf->cep_write_ptr = 0;
179  prespch_buf->ncep = 0;
180 }
181 
182 void
183 fe_prespch_reset_pcm(prespch_buf_t * prespch_buf)
184 {
185  prespch_buf->pcm_read_ptr = 0;
186  prespch_buf->pcm_write_ptr = 0;
187  prespch_buf->npcm = 0;
188 }
189 
190 void
191 fe_prespch_free(prespch_buf_t * prespch_buf)
192 {
193  if (!prespch_buf)
194  return;
195  if (prespch_buf->cep_buf)
196  ckd_free_2d((void **) prespch_buf->cep_buf);
197  if (prespch_buf->pcm_buf)
198  ckd_free(prespch_buf->pcm_buf);
199  ckd_free(prespch_buf);
200 }
201 
202 int32
203 fe_prespch_ncep(prespch_buf_t * prespch_buf)
204 {
205  return prespch_buf->ncep;
206 }
#define ckd_calloc_2d(d1, d2, sz)
Macro for ckd_calloc_2d
Definition: ckd_alloc.h:270
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
Definition: ckd_alloc.h:248
Sphinx&#39;s memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Definition: ckd_alloc.c:244
Implementation of logging routines.
SPHINXBASE_EXPORT void ckd_free_2d(void *ptr)
Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.
Definition: ckd_alloc.c:255