-
Notifications
You must be signed in to change notification settings - Fork 3
/
recog~.c.fixed
324 lines (237 loc) · 7.94 KB
/
recog~.c.fixed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
[recog~] is a speech recognizer external for Pure Data written with the pocketsphinx API:
http://cmusphinx.sourceforge.net/
here is the copyright info for this excellent software:
* ====================================================================
* Copyright (c) 1999-2010 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
my own copyright:
This code is entirely free to use for any reasonable purpose.
Copyright(c) David Medine February 3, 2011
http://imusic1.ucsd.edu/~dmedine
****/
#include "m_pd.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <pocketsphinx.h>
//#include <sndfile.h>
#include <string.h>
#include "dlfcn.h"
typedef cmd_ln_t *(*fn_cl_init)(cmd_ln_t *inout_cmdln,
arg_t const *defn,
int32 strict,
...);
typedef ps_decoder_t *(*fn_ps_init)(cmd_ln_t *config);
typedef int (*fn_ps_start_utt)(ps_decoder_t *ps, char const *uttid);
typedef int (*fn_ps_process_raw)(ps_decoder_t *ps, int16 const *data, size_t n_samples, int no_search, int full_utt);
typedef int (*fn_ps_end_utt)(ps_decoder_t *ps);
typedef char const *(*fn_ps_get_hyp)(ps_decoder_t *ps, int32 *out_best_score, char const **out_uttid);
typedef int(*fn_ps_free)(ps_decoder_t *ps);
typedef struct _dl_sphinx{
void *handle;
fn_cl_init r_ps_cl_init;
fn_ps_init r_ps_init;
fn_ps_start_utt r_ps_start_utt;
fn_ps_process_raw r_ps_process_raw;
fn_ps_end_utt r_ps_end_utt;
fn_ps_get_hyp r_ps_get_hyp;
fn_ps_free r_ps_free;
ps_decoder_t *ps;
cmd_ln_t *config;
char const *hyp, *uttid;
int16 buf[512];
int rv;
int32 score;
}t_dl_sphinx;
static t_class *recog_tilde_class;
typedef struct histogram{
t_int isGood;
}t_histogram;
typedef struct _recog_tilde{
t_object x_obj;
float f;
t_dl_sphinx y;
}t_recog_tilde;
//structs from sphinx don't put these in the object struct??
ps_decoder_t *ps; //decoder
cmd_ln_t *config; //configuration
t_symbol *hmm;
t_symbol *lm;
t_symbol *dict;
//----------------------------------
//----------------------------------
void recog_tilde_decode(t_recog_tilde *x);
void recog_tilde_reinit(t_recog_tilde *x);
void output(t_recog_tilde *x);
//----------------------------------
//----------------------------------
static int recog_tilde_go(t_recog_tilde *x)
{
int rv;
int i;
/* char const *hyp, *uttid; */
/* int32 score; */
/* char buf[256]; */
/* int hyplen; */
FILE *fh;
x->y.handle = dlopen("/usr/local/lib/libpocketsphinx.so", RTLD_LAZY | RTLD_GLOBAL);
if(!x->y.handle)
printf("shared object load ERROR:\n\t%s\n",dlerror());
fh = fopen("./goforward.raw", "rb");
x->y.r_ps_cl_init = (fn_cl_init)dlsym(x->y.handle, "cmd_ln_init");
x->y.config = (*x->y.r_ps_cl_init)(NULL, ps_args(), TRUE,
"-hmm", "./model/hmm/en_US/hub4wsj_sc_8k",
"-lm", "./model/lm/en/turtle.DMP",
"-dict", "./model/lm/en/turtle.dic",
NULL);
x->y.r_ps_init = (fn_ps_init)dlsym(x->y.handle, "ps_init");
x->y.ps = (*x->y.r_ps_init)(x->y.config);
if (x->y.ps == NULL)
return 1;
if (fh == NULL) {
perror("Failed to open goforward.raw");
return 1;
}
fseek(fh, 0, SEEK_SET);
x->y.r_ps_start_utt = (fn_ps_start_utt)dlsym(x->y.handle, "ps_start_utt");
rv = (*x->y.r_ps_start_utt)(x->y.ps, "goforward");
if (rv < 0)
return 1;
x->y.r_ps_process_raw = (fn_ps_process_raw)dlsym(x->y.handle, "ps_process_raw");
while (!feof(fh)) {
size_t nsamp;
nsamp = fread(x->y.buf, 2, 512, fh);
rv = x->y.r_ps_process_raw(x->y.ps, x->y.buf, nsamp, FALSE, FALSE);
}
x->y.r_ps_end_utt = (fn_ps_end_utt)dlsym(x->y.handle, "ps_end_utt");
rv = x->y.r_ps_end_utt(x->y.ps);
if (rv < 0)
return 1;
x->y.r_ps_get_hyp = (fn_ps_get_hyp)dlsym(x->y.handle, "ps_get_hyp");
x->y.hyp = x->y.r_ps_get_hyp(x->y.ps, &x->y.score, &x->y.uttid);
if (x->y.hyp == NULL)
return 1;
printf("Recognized: %s\n", x->y.hyp);
x->y.r_ps_free = (fn_ps_free)dlsym(x->y.handle, "ps_free");
x->y.r_ps_free(x->y.ps);
fclose(fh);
/* config = cmd_ln_init(NULL, ps_args(), TRUE, */
/* "-hmm", "./model/hmm/en_US/hub4wsj_sc_8k", */
/* "-lm", "./model/lm/en/turtle.DMP", */
/* "-dict", "./model/lm/en/turtle.dic", */
/* NULL); */
/* if(config == NULL) */
/* return 1; */
/* ps = ps_init(config); */
/* if (ps == NULL) */
/* return 1; */
/* fh = fopen("./goforward.raw", "rb"); */
/* if (fh == NULL) { */
/* perror("Failed to open goforward.raw"); */
/* return 1; */
/* } */
/* rv = ps_decode_raw(ps, fh, "gofor", -1); */
/* if (rv < 0) */
/* return 1; */
/* hyp = ps_get_hyp(ps, &score, &uttid); */
/* if (hyp == NULL) */
/* return 1; */
/* printf("Recognized the following: %s\n\n\n\n\n\n\n\n", hyp); */
/* fseek(fh, 0, SEEK_SET); */
/* rv = ps_start_utt(ps, "goforward"); */
/* if (rv < 0) */
/* return 1; */
/* while (!feof(fh)) { */
/* size_t nsamp; */
/* nsamp = fread(buf, 2, 512, fh); */
/* rv = ps_process_raw(ps, buf, nsamp, FALSE, FALSE); */
/* } */
/* rv = ps_end_utt(ps); */
/* if (rv < 0) */
/* return 1; */
/* hyp = ps_get_hyp(ps, &score, &uttid); */
/* if (hyp == NULL) */
/* return 1; */
/* printf("Recognized: %s\n", hyp); */
/* fclose(fh); */
/* ps_free(ps); */
return 0;
}
//----------------------------------
//----------------------------------
//pd glue
//perform routine
t_int *recog_tilde_perform(t_int *w)
{
t_recog_tilde *x = (t_recog_tilde *)(w[1]);
t_sample *in = (t_sample *)(w[2]);
t_sample *out = (t_sample *)(w[3]);
t_int n = (t_int)(w[4]);
int i;
return (w+5);
}
void recog_tilde_dsp(t_recog_tilde *x, t_signal **sp)
{
dsp_add(recog_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}
void *recog_tilde_new(int argc, t_atom *argv)
{
t_recog_tilde *x = (t_recog_tilde *)pd_new(recog_tilde_class);
outlet_new(&x->x_obj, &s_signal);
return (void*)x;
}
void recog_tilde_free(t_recog_tilde *x)
{
}
void recog_tilde_setup(void)
{
recog_tilde_class = class_new(gensym("recog~"),
(t_newmethod)recog_tilde_new,
(t_method)recog_tilde_free,
sizeof(t_recog_tilde),
0,
A_GIMME,
0);
class_addmethod(recog_tilde_class,
(t_method)recog_tilde_dsp,
gensym("dsp"),
0);
CLASS_MAINSIGNALIN(recog_tilde_class, t_recog_tilde, f);
class_addmethod(recog_tilde_class,
(t_method)recog_tilde_go,
gensym("go"),
0);
}