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 int32_t plugin_utterance_handler(context_t *ctx, srs_srec_utterance_t *utt)
61 srs_srec_notify_t notify;
64 if (!(pl = ctx->plugin) || !(notify = pl->notify.callback))
67 length = notify(utt, pl->notify.data);
69 if (length < 0 && utt->length)
76 static int activate(void *user_data)
78 context_t *ctx = (context_t *)user_data;
82 mrp_log_info("Activating CMU Sphinx backend.");
88 static void deactivate(void *user_data)
90 context_t *ctx = (context_t *)user_data;
94 mrp_log_info("Deactivating CMU Sphinx backend.");
98 static int flush(uint32_t start, uint32_t end, void *user_data)
100 context_t *ctx = (context_t *)user_data;
104 mrp_debug("flushing CMU Sphinx backend buffer (%u - %u)", start, end);
110 static int rescan(uint32_t start, uint32_t end, void *user_data)
112 context_t *ctx = (context_t *)user_data;
116 mrp_debug("scheduling CMU Sphinx backend buffer rescan (%u - %u)",
123 static void *sampledup(uint32_t start, uint32_t end, void *user_data)
125 context_t *ctx = (context_t *)user_data;
129 mrp_debug("duplicating CMU Sphinx backend samples (%u - %u)", start, end);
136 static int check_decoder(const char *decoder, void *user_data)
138 context_t *ctx = (context_t *)user_data;
142 mrp_debug("checking availability of decoder '%s' for CMU Sphinx backend",
149 static int select_decoder(const char *decoder, void *user_data)
151 context_t *ctx = (context_t *)user_data;
155 mrp_debug("selecting decoder '%s' for CMU Sphinx backend", decoder);
160 static int create_sphinx(srs_plugin_t *plugin)
162 srs_srec_api_t api = {
164 deactivate: deactivate,
167 sampledup: sampledup,
168 check_decoder: check_decoder,
169 select_decoder: select_decoder,
172 srs_context_t *srs = plugin->srs;
173 context_t *ctx = NULL;
177 mrp_debug("creating CMU Sphinx speech recognition backend plugin");
179 if ((ctx = mrp_allocz(sizeof(context_t))) &&
180 (pl = mrp_allocz(sizeof(plugin_t))) )
186 sts = srs_register_srec(srs, SPHINX_NAME, &api, ctx,
187 &pl->notify.callback,
190 plugin->plugin_data = ctx;
198 mrp_log_error("Failed to create CMU Sphinx plugin.");
204 static int config_sphinx(srs_plugin_t *plugin, srs_cfg_t *settings)
206 context_t *ctx = (context_t *)plugin->plugin_data;
210 mrp_debug("configuring CMU Sphinx speech recognition backend plugin");
212 n = srs_collect_config(settings, SPHINX_PREFIX, &cfg);
214 mrp_log_info("Found %d CMU Sphinx plugin configuration keys.", n);
216 if (options_create(ctx, n, cfg) < 0 ||
217 decoder_set_create(ctx) < 0 ||
218 filter_buffer_create(ctx) < 0 ||
219 input_buffer_create(ctx) < 0 )
221 mrp_log_error("Failed to configure CMU Sphinx plugin.");
225 srs_free_config(cfg);
231 static int start_sphinx(srs_plugin_t *plugin)
233 srs_context_t *srs = plugin->srs;
234 context_t *ctx = (context_t *)plugin->plugin_data;
236 mrp_debug("start CMU Sphinx speech recognition backend plugin");
238 if (pulse_interface_create(ctx, srs->pa) < 0) {
239 mrp_log_error("Failed to start CMU Sphinx plugin: can't create "
240 "pulseaudio interface");
247 static void stop_sphinx(srs_plugin_t *plugin)
249 context_t *ctx = (context_t *)plugin->plugin_data;
251 mrp_debug("stop CMU Sphinx speech recognition backend plugin");
253 pulse_interface_destroy(ctx);
257 static void destroy_sphinx(srs_plugin_t *plugin)
259 srs_context_t *srs = plugin->srs;
260 context_t *ctx = (context_t *)plugin->plugin_data;
262 mrp_debug("destroy CMU Sphinx speech recognition backend plugin");
265 srs_unregister_srec(srs, SPHINX_NAME);
266 mrp_free(ctx->plugin);
268 input_buffer_destroy(ctx);
269 filter_buffer_destroy(ctx);
270 decoder_set_destroy(ctx);
271 options_destroy(ctx);
278 SRS_DECLARE_PLUGIN(SPHINX_NAME, SPHINX_DESCRIPTION, SPHINX_AUTHORS,
279 SPHINX_VERSION, create_sphinx, config_sphinx,
280 start_sphinx, stop_sphinx, destroy_sphinx)
286 * indent-tabs-mode: nil