2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <murphy/common/debug.h>
31 #include <murphy/common/mainloop.h>
33 #include <pulse/pulseaudio.h>
34 #include <pulse/mainloop.h>
37 #include "decoder-set.h"
38 #include "utterance.h"
39 #include "filter-buffer.h"
40 #include "input-buffer.h"
41 #include "pulse-interface.h"
44 #define SPHINX_NAME "sphinx-speech"
45 #define SPHINX_DESCRIPTION "A CMU Sphinx-based speech engine backend plugin."
46 #define SPHINX_AUTHORS "Janos Kovacs <janos.kovacs@intel.com>"
47 #define SPHINX_VERSION "0.0.1"
50 srs_plugin_t *self; /* us, the backend plugin */
52 srs_srec_notify_t callback; /* recognition notification callback */
53 void *data; /* notifiation callback data */
58 mrp_mainloop_t *plugin_get_mainloop(plugin_t *plugin)
60 return plugin->self->srs->ml;
64 int32_t plugin_utterance_handler(context_t *ctx, srs_srec_utterance_t *utt)
67 srs_srec_notify_t notify;
70 if (!(pl = ctx->plugin) || !(notify = pl->notify.callback))
73 length = notify(utt, pl->notify.data);
74 mrp_log_info("buffer processed till %d", length);
80 static int activate(void *user_data)
82 context_t *ctx = (context_t *)user_data;
86 mrp_log_info("Activating CMU Sphinx backend.");
88 pulse_interface_cork_input_stream(ctx, false);
94 static void deactivate(void *user_data)
96 context_t *ctx = (context_t *)user_data;
100 mrp_log_info("Deactivating CMU Sphinx backend.");
102 pulse_interface_cork_input_stream(ctx, true);
103 filter_buffer_purge(ctx, -1);
104 input_buffer_purge(ctx);
108 static int flush(uint32_t start, uint32_t end, void *user_data)
110 context_t *ctx = (context_t *)user_data;
114 mrp_log_info("flushing CMU Sphinx backend buffer (%u - %u)", start, end);
120 static int rescan(uint32_t start, uint32_t end, void *user_data)
122 context_t *ctx = (context_t *)user_data;
126 mrp_log_info("scheduling CMU Sphinx backend buffer rescan (%u - %u)",
133 static srs_audiobuf_t *sampledup(uint32_t start, uint32_t end, void *user_data)
135 context_t *ctx = (context_t *)user_data;
137 srs_audioformat_t format;
143 if (!ctx || !(opts = ctx->opts))
146 mrp_debug("duplicating CMU Sphinx backend sample (%u - %u)", start, end);
148 format = SRS_AUDIO_S16LE;
151 buf = filter_buffer_dup(ctx, start, end, &samples);
153 return srs_create_audiobuf(format, rate, channels, samples, buf);
157 static int check_decoder(const char *decoder, void *user_data)
159 context_t *ctx = (context_t *)user_data;
162 mrp_log_info("checking availability of decoder '%s' for CMU Sphinx backend",
165 available = decoder_set_contains(ctx, decoder);
167 mrp_debug("decoder %s %savailable", decoder, available ? "" : "un");
173 static int select_decoder(const char *decoder, void *user_data)
175 context_t *ctx = (context_t *)user_data;
177 mrp_log_info("selecting decoder '%s' for CMU Sphinx backend", decoder);
179 if (decoder_set_use(ctx, decoder) < 0)
186 static const char *active_decoder(void *user_data)
188 context_t *ctx = (context_t *)user_data;
191 mrp_log_info("querying active CMU Sphinx backend decoder");
193 decoder = decoder_set_name(ctx);
195 mrp_debug("active decoder is '%s'", decoder);
201 static int create_sphinx(srs_plugin_t *plugin)
203 srs_srec_api_t api = {
205 deactivate: deactivate,
208 sampledup: sampledup,
209 check_decoder: check_decoder,
210 select_decoder: select_decoder,
211 active_decoder: active_decoder,
214 srs_context_t *srs = plugin->srs;
215 context_t *ctx = NULL;
219 mrp_debug("creating CMU Sphinx speech recognition backend plugin");
221 if ((ctx = mrp_allocz(sizeof(context_t))) &&
222 (pl = mrp_allocz(sizeof(plugin_t))) )
228 sts = srs_register_srec(srs, SPHINX_NAME, &api, ctx,
229 &pl->notify.callback,
232 plugin->plugin_data = ctx;
240 mrp_log_error("Failed to create CMU Sphinx plugin.");
246 static int config_sphinx(srs_plugin_t *plugin, srs_cfg_t *settings)
248 context_t *ctx = (context_t *)plugin->plugin_data;
252 mrp_debug("configuring CMU Sphinx speech recognition backend plugin");
254 n = srs_config_collect(settings, SPHINX_PREFIX, &cfg);
256 mrp_log_info("Found %d CMU Sphinx plugin configuration keys.", n);
258 if (options_create(ctx, n, cfg) < 0 ||
259 decoder_set_create(ctx) < 0 ||
260 filter_buffer_create(ctx) < 0 ||
261 input_buffer_create(ctx) < 0 )
263 mrp_log_error("Failed to configure CMU Sphinx plugin.");
267 srs_config_free(cfg);
273 static int start_sphinx(srs_plugin_t *plugin)
275 srs_context_t *srs = plugin->srs;
276 context_t *ctx = (context_t *)plugin->plugin_data;
278 mrp_debug("start CMU Sphinx speech recognition backend plugin");
280 if (pulse_interface_create(ctx, srs->pa) < 0) {
281 mrp_log_error("Failed to start CMU Sphinx plugin: can't create "
282 "pulseaudio interface");
289 static void stop_sphinx(srs_plugin_t *plugin)
291 context_t *ctx = (context_t *)plugin->plugin_data;
293 mrp_debug("stop CMU Sphinx speech recognition backend plugin");
295 pulse_interface_destroy(ctx);
299 static void destroy_sphinx(srs_plugin_t *plugin)
301 srs_context_t *srs = plugin->srs;
302 context_t *ctx = (context_t *)plugin->plugin_data;
304 mrp_debug("destroy CMU Sphinx speech recognition backend plugin");
307 srs_unregister_srec(srs, SPHINX_NAME);
308 mrp_free(ctx->plugin);
310 input_buffer_destroy(ctx);
311 filter_buffer_destroy(ctx);
312 decoder_set_destroy(ctx);
313 options_destroy(ctx);
320 SRS_DECLARE_PLUGIN(SPHINX_NAME, SPHINX_DESCRIPTION, SPHINX_AUTHORS,
321 SPHINX_VERSION, create_sphinx, config_sphinx,
322 start_sphinx, stop_sphinx, destroy_sphinx)
328 * indent-tabs-mode: nil