app_speech_utils.c File Reference

Speech Recognition Utility Applications. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/speech.h"

Include dependency graph for app_speech_utils.c:

Go to the source code of this file.

Enumerations

enum  { SB_OPT_NOANSWER = (1 << 0) }

Functions

static void __reg_module (void)
static void __unreg_module (void)
 ASTERISK_FILE_VERSION (__FILE__,"$Revision: 419688 $")
static void destroy_callback (void *data)
 Helper function used by datastores to destroy the speech structure upon hangup.
static struct ast_speech_resultfind_result (struct ast_speech_result *results, char *result_num)
static struct ast_speechfind_speech (struct ast_channel *chan)
 Helper function used to find the speech structure attached to a channel.
static int load_module (void)
static int speech_activate (struct ast_channel *chan, const char *data)
 SpeechActivateGrammar(Grammar Name) Dialplan Application.
static int speech_background (struct ast_channel *chan, const char *data)
 SpeechBackground(Sound File,Timeout) Dialplan Application.
static int speech_create (struct ast_channel *chan, const char *data)
 SpeechCreate() Dialplan Application.
static int speech_datastore_destroy (struct ast_channel *chan)
static int speech_deactivate (struct ast_channel *chan, const char *data)
 SpeechDeactivateGrammar(Grammar Name) Dialplan Application.
static int speech_destroy (struct ast_channel *chan, const char *data)
 SpeechDestroy() Dialplan Application.
static int speech_engine_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 SPEECH_ENGINE() Dialplan Get Function.
static int speech_engine_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 SPEECH_ENGINE() Dialplan Set Function.
static int speech_grammar (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 SPEECH_GRAMMAR() Dialplan Function.
static int speech_load (struct ast_channel *chan, const char *vdata)
 SpeechLoadGrammar(Grammar Name,Path) Dialplan Application.
static int speech_processing_sound (struct ast_channel *chan, const char *data)
 SpeechProcessingSound(Sound File) Dialplan Application.
static int speech_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 SPEECH() Dialplan Function.
static int speech_results_type_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 SPEECH_RESULTS_TYPE() Dialplan Function.
static int speech_score (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 SPEECH_SCORE() Dialplan Function.
static int speech_start (struct ast_channel *chan, const char *data)
 SpeechStart() Dialplan Application.
static int speech_streamfile (struct ast_channel *chan, const char *filename, const char *preflang)
 Helper function used by speech_background to playback a soundfile.
static int speech_text (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 SPEECH_TEXT() Dialplan Function.
static int speech_unload (struct ast_channel *chan, const char *data)
 SpeechUnloadGrammar(Grammar Name) Dialplan Application.
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Dialplan Speech Applications" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .nonoptreq = "res_speech", }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_app_option speech_background_options [128] = { [ 'n' ] = { .flag = SB_OPT_NOANSWER }, }
static struct ast_datastore_info speech_datastore
 Static structure for datastore information.
static struct ast_custom_function speech_engine_function
static struct ast_custom_function speech_function
static struct ast_custom_function speech_grammar_function
static struct ast_custom_function speech_results_type_function
static struct ast_custom_function speech_score_function
static struct ast_custom_function speech_text_function


Detailed Description

Speech Recognition Utility Applications.

Author:
Joshua Colp <jcolp@digium.com>

Definition in file app_speech_utils.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
SB_OPT_NOANSWER 

Definition at line 693 of file app_speech_utils.c.

00693      {
00694    SB_OPT_NOANSWER = (1 << 0),
00695 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 1009 of file app_speech_utils.c.

static void __unreg_module ( void   )  [static]

Definition at line 1009 of file app_speech_utils.c.

ASTERISK_FILE_VERSION ( __FILE__  ,
"$Revision: 419688 $"   
)

static void destroy_callback ( void *  data  )  [static]

Helper function used by datastores to destroy the speech structure upon hangup.

Definition at line 263 of file app_speech_utils.c.

References ast_speech_destroy(), and NULL.

00264 {
00265    struct ast_speech *speech = (struct ast_speech*)data;
00266 
00267    if (speech == NULL) {
00268       return;
00269    }
00270 
00271    /* Deallocate now */
00272    ast_speech_destroy(speech);
00273 
00274    return;
00275 }

static struct ast_speech_result* find_result ( struct ast_speech_result results,
char *  result_num 
) [static, read]

Definition at line 334 of file app_speech_utils.c.

References AST_LIST_NEXT, ast_speech_result::nbest_num, NULL, result, and tmp().

Referenced by speech_grammar(), speech_score(), and speech_text().

00335 {
00336    struct ast_speech_result *result = results;
00337    char *tmp = NULL;
00338    int nbest_num = 0, wanted_num = 0, i = 0;
00339 
00340    if (!result) {
00341       return NULL;
00342    }
00343 
00344    if ((tmp = strchr(result_num, '/'))) {
00345       *tmp++ = '\0';
00346       nbest_num = atoi(result_num);
00347       wanted_num = atoi(tmp);
00348    } else {
00349       wanted_num = atoi(result_num);
00350    }
00351 
00352    do {
00353       if (result->nbest_num != nbest_num)
00354          continue;
00355       if (i == wanted_num)
00356          break;
00357       i++;
00358    } while ((result = AST_LIST_NEXT(result, list)));
00359 
00360    return result;
00361 }

static struct ast_speech* find_speech ( struct ast_channel chan  )  [static, read]

Helper function used to find the speech structure attached to a channel.

Definition at line 284 of file app_speech_utils.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore::data, and NULL.

Referenced by speech_activate(), speech_background(), speech_deactivate(), speech_engine_read(), speech_engine_write(), speech_grammar(), speech_load(), speech_processing_sound(), speech_read(), speech_results_type_write(), speech_score(), speech_start(), speech_text(), and speech_unload().

00285 {
00286    struct ast_speech *speech = NULL;
00287    struct ast_datastore *datastore = NULL;
00288 
00289    if (!chan) {
00290       return NULL;
00291    }
00292 
00293    ast_channel_lock(chan);
00294    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00295    ast_channel_unlock(chan);
00296    if (datastore == NULL) {
00297       return NULL;
00298    }
00299    speech = datastore->data;
00300 
00301    return speech;
00302 }

static int load_module ( void   )  [static]

static int speech_activate ( struct ast_channel chan,
const char *  data 
) [static]

SpeechActivateGrammar(Grammar Name) Dialplan Application.

Definition at line 630 of file app_speech_utils.c.

References ast_speech_grammar_activate(), find_speech(), and NULL.

Referenced by load_module().

00631 {
00632    int res = 0;
00633    struct ast_speech *speech = find_speech(chan);
00634 
00635    if (speech == NULL)
00636       return -1;
00637 
00638    /* Activate the grammar on the speech object */
00639    res = ast_speech_grammar_activate(speech, data);
00640 
00641    return res;
00642 }

static int speech_background ( struct ast_channel chan,
const char *  data 
) [static]

SpeechBackground(Sound File,Timeout) Dialplan Application.

Definition at line 702 of file app_speech_utils.c.

References ao2_bump, ao2_cleanup, args, ast_answer(), AST_APP_ARG, ast_app_parse_options(), ast_calloc, ast_channel_language(), ast_channel_lock, ast_channel_pbx(), ast_channel_readformat(), ast_channel_sched(), ast_channel_stream(), ast_channel_streamid(), ast_channel_timingfunc(), ast_channel_unlock, ast_clear_flag, AST_CONTROL_HANGUP, AST_DECLARE_APP_ARGS, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, AST_MAX_EXTENSION, ast_mutex_lock, ast_mutex_unlock, ast_read(), ast_sched_runq(), ast_sched_wait(), ast_set_read_format(), ast_speech_change_state(), ast_speech_dtmf(), AST_SPEECH_QUIET, ast_speech_results_get(), ast_speech_start(), AST_SPEECH_STATE_DONE, AST_SPEECH_STATE_NOT_READY, AST_SPEECH_STATE_READY, AST_SPEECH_STATE_WAIT, ast_speech_write(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdup, ast_strdupa, ast_strlen_zero, ast_test_flag, ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), ast_frame::data, ast_frame::datalen, ast_pbx::dtimeoutms, f, find_speech(), ast_speech::format, ast_frame::frametype, ast_speech_result::grammar, ast_frame_subclass::integer, ast_speech::lock, NULL, parse(), pbx_builtin_getvar_helper(), ast_speech::processing_sound, ast_frame::ptr, RAII_VAR, ast_speech::results, SB_OPT_NOANSWER, ast_speech_result::score, speech_background_options, speech_datastore_destroy(), speech_streamfile(), ast_speech::state, strsep(), ast_frame::subclass, ast_speech_result::text, timeout, and tmp().

Referenced by load_module().

00703 {
00704    unsigned int timeout = 0;
00705    int res = 0, done = 0, started = 0, quieted = 0, max_dtmf_len = 0;
00706    struct ast_speech *speech = find_speech(chan);
00707    struct ast_frame *f = NULL;
00708    RAII_VAR(struct ast_format *, oldreadformat, NULL, ao2_cleanup);
00709    char dtmf[AST_MAX_EXTENSION] = "";
00710    struct timeval start = { 0, 0 }, current;
00711    char *parse, *filename_tmp = NULL, *filename = NULL, tmp[2] = "", dtmf_terminator = '#';
00712    const char *tmp2 = NULL;
00713    struct ast_flags options = { 0 };
00714    AST_DECLARE_APP_ARGS(args,
00715       AST_APP_ARG(soundfile);
00716       AST_APP_ARG(timeout);
00717       AST_APP_ARG(options);
00718    );
00719 
00720    parse = ast_strdupa(data);
00721    AST_STANDARD_APP_ARGS(args, parse);
00722 
00723    if (speech == NULL)
00724       return -1;
00725 
00726    if (!ast_strlen_zero(args.options)) {
00727       char *options_buf = ast_strdupa(args.options);
00728       ast_app_parse_options(speech_background_options, &options, NULL, options_buf);
00729    }
00730 
00731    /* If channel is not already answered, then answer it */
00732    if (ast_channel_state(chan) != AST_STATE_UP && !ast_test_flag(&options, SB_OPT_NOANSWER)
00733       && ast_answer(chan)) {
00734          return -1;
00735    }
00736 
00737    /* Record old read format */
00738    oldreadformat = ao2_bump(ast_channel_readformat(chan));
00739 
00740    /* Change read format to be signed linear */
00741    if (ast_set_read_format(chan, speech->format))
00742       return -1;
00743 
00744    if (!ast_strlen_zero(args.soundfile)) {
00745       /* Yay sound file */
00746       filename_tmp = ast_strdupa(args.soundfile);
00747       if (!ast_strlen_zero(args.timeout)) {
00748          if ((timeout = atof(args.timeout) * 1000.0) == 0)
00749             timeout = -1;
00750       } else
00751          timeout = 0;
00752    }
00753 
00754    /* See if the maximum DTMF length variable is set... we use a variable in case they want to carry it through their entire dialplan */
00755    ast_channel_lock(chan);
00756    if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_MAXLEN")) && !ast_strlen_zero(tmp2)) {
00757       max_dtmf_len = atoi(tmp2);
00758    }
00759    
00760    /* See if a terminator is specified */
00761    if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_TERMINATOR"))) {
00762       if (ast_strlen_zero(tmp2))
00763          dtmf_terminator = '\0';
00764       else
00765          dtmf_terminator = tmp2[0];
00766    }
00767    ast_channel_unlock(chan);
00768 
00769    /* Before we go into waiting for stuff... make sure the structure is ready, if not - start it again */
00770    if (speech->state == AST_SPEECH_STATE_NOT_READY || speech->state == AST_SPEECH_STATE_DONE) {
00771       ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
00772       ast_speech_start(speech);
00773    }
00774 
00775    /* Ensure no streams are currently running */
00776    ast_stopstream(chan);
00777 
00778    /* Okay it's streaming so go into a loop grabbing frames! */
00779    while (done == 0) {
00780       /* If the filename is null and stream is not running, start up a new sound file */
00781       if (!quieted && (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) && (filename = strsep(&filename_tmp, "&"))) {
00782          /* Discard old stream information */
00783          ast_stopstream(chan);
00784          /* Start new stream */
00785          speech_streamfile(chan, filename, ast_channel_language(chan));
00786       }
00787 
00788       /* Run scheduled stuff */
00789       ast_sched_runq(ast_channel_sched(chan));
00790 
00791       /* Yay scheduling */
00792       res = ast_sched_wait(ast_channel_sched(chan));
00793       if (res < 0)
00794          res = 1000;
00795 
00796       /* If there is a frame waiting, get it - if not - oh well */
00797       if (ast_waitfor(chan, res) > 0) {
00798          f = ast_read(chan);
00799          if (f == NULL) {
00800             /* The channel has hung up most likely */
00801             done = 3;
00802             break;
00803          }
00804       }
00805 
00806       /* Do timeout check (shared between audio/dtmf) */
00807       if ((!quieted || strlen(dtmf)) && started == 1) {
00808          current = ast_tvnow();
00809          if ((ast_tvdiff_ms(current, start)) >= timeout) {
00810             done = 1;
00811             if (f)
00812                ast_frfree(f);
00813             break;
00814          }
00815       }
00816 
00817       /* Do checks on speech structure to see if it's changed */
00818       ast_mutex_lock(&speech->lock);
00819       if (ast_test_flag(speech, AST_SPEECH_QUIET)) {
00820          if (ast_channel_stream(chan))
00821             ast_stopstream(chan);
00822          ast_clear_flag(speech, AST_SPEECH_QUIET);
00823          quieted = 1;
00824       }
00825       /* Check state so we can see what to do */
00826       switch (speech->state) {
00827       case AST_SPEECH_STATE_READY:
00828          /* If audio playback has stopped do a check for timeout purposes */
00829          if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL)
00830             ast_stopstream(chan);
00831          if (!quieted && ast_channel_stream(chan) == NULL && timeout && started == 0 && !filename_tmp) {
00832             if (timeout == -1) {
00833                done = 1;
00834                if (f)
00835                   ast_frfree(f);
00836                break;
00837             }
00838             start = ast_tvnow();
00839             started = 1;
00840          }
00841          /* Write audio frame out to speech engine if no DTMF has been received */
00842          if (!strlen(dtmf) && f != NULL && f->frametype == AST_FRAME_VOICE) {
00843             ast_speech_write(speech, f->data.ptr, f->datalen);
00844          }
00845          break;
00846       case AST_SPEECH_STATE_WAIT:
00847          /* Cue up waiting sound if not already playing */
00848          if (!strlen(dtmf)) {
00849             if (ast_channel_stream(chan) == NULL) {
00850                if (speech->processing_sound != NULL) {
00851                   if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) {
00852                      speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan));
00853                   }
00854                }
00855             } else if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) {
00856                ast_stopstream(chan);
00857                if (speech->processing_sound != NULL) {
00858                   if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) {
00859                      speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan));
00860                   }
00861                }
00862             }
00863          }
00864          break;
00865       case AST_SPEECH_STATE_DONE:
00866          /* Now that we are done... let's switch back to not ready state */
00867          ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
00868          if (!strlen(dtmf)) {
00869             /* Copy to speech structure the results, if available */
00870             speech->results = ast_speech_results_get(speech);
00871             /* Break out of our background too */
00872             done = 1;
00873             /* Stop audio playback */
00874             if (ast_channel_stream(chan) != NULL) {
00875                ast_stopstream(chan);
00876             }
00877          }
00878          break;
00879       default:
00880          break;
00881       }
00882       ast_mutex_unlock(&speech->lock);
00883 
00884       /* Deal with other frame types */
00885       if (f != NULL) {
00886          /* Free the frame we received */
00887          switch (f->frametype) {
00888          case AST_FRAME_DTMF:
00889             if (dtmf_terminator != '\0' && f->subclass.integer == dtmf_terminator) {
00890                done = 1;
00891             } else {
00892                quieted = 1;
00893                if (ast_channel_stream(chan) != NULL) {
00894                   ast_stopstream(chan);
00895                }
00896                if (!started) {
00897                   /* Change timeout to be 5 seconds for DTMF input */
00898                   timeout = (ast_channel_pbx(chan) && ast_channel_pbx(chan)->dtimeoutms) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
00899                   started = 1;
00900                }
00901                start = ast_tvnow();
00902                snprintf(tmp, sizeof(tmp), "%c", f->subclass.integer);
00903                strncat(dtmf, tmp, sizeof(dtmf) - strlen(dtmf) - 1);
00904                /* If the maximum length of the DTMF has been reached, stop now */
00905                if (max_dtmf_len && strlen(dtmf) == max_dtmf_len)
00906                   done = 1;
00907             }
00908             break;
00909          case AST_FRAME_CONTROL:
00910             switch (f->subclass.integer) {
00911             case AST_CONTROL_HANGUP:
00912                /* Since they hung up we should destroy the speech structure */
00913                done = 3;
00914             default:
00915                break;
00916             }
00917          default:
00918             break;
00919          }
00920          ast_frfree(f);
00921          f = NULL;
00922       }
00923    }
00924 
00925    if (!ast_strlen_zero(dtmf)) {
00926       /* We sort of make a results entry */
00927       speech->results = ast_calloc(1, sizeof(*speech->results));
00928       if (speech->results != NULL) {
00929          ast_speech_dtmf(speech, dtmf);
00930          speech->results->score = 1000;
00931          speech->results->text = ast_strdup(dtmf);
00932          speech->results->grammar = ast_strdup("dtmf");
00933       }
00934       ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
00935    }
00936 
00937    /* See if it was because they hung up */
00938    if (done == 3) {
00939       speech_datastore_destroy(chan);
00940    } else {
00941       /* Channel is okay so restore read format */
00942       ast_set_read_format(chan, oldreadformat);
00943    }
00944 
00945    return 0;
00946 }

static int speech_create ( struct ast_channel chan,
const char *  data 
) [static]

SpeechCreate() Dialplan Application.

Definition at line 545 of file app_speech_utils.c.

References ast_channel_datastore_add(), ast_channel_lock, ast_channel_nativeformats(), ast_channel_unlock, ast_datastore_alloc, ast_speech_destroy(), ast_speech_new(), ast_datastore::data, NULL, and pbx_builtin_setvar_helper().

Referenced by load_module().

00546 {
00547    struct ast_speech *speech = NULL;
00548    struct ast_datastore *datastore = NULL;
00549 
00550    /* Request a speech object */
00551    speech = ast_speech_new(data, ast_channel_nativeformats(chan));
00552    if (speech == NULL) {
00553       /* Not available */
00554       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00555       return 0;
00556    }
00557 
00558    datastore = ast_datastore_alloc(&speech_datastore, NULL);
00559    if (datastore == NULL) {
00560       ast_speech_destroy(speech);
00561       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00562       return 0;
00563    }
00564    pbx_builtin_setvar_helper(chan, "ERROR", NULL);
00565    datastore->data = speech;
00566    ast_channel_lock(chan);
00567    ast_channel_datastore_add(chan, datastore);
00568    ast_channel_unlock(chan);
00569 
00570    return 0;
00571 }

static int speech_datastore_destroy ( struct ast_channel chan  )  [static]

Definition at line 313 of file app_speech_utils.c.

References ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_lock, ast_channel_unlock, ast_datastore_free(), and NULL.

Referenced by speech_background(), and speech_destroy().

00314 {
00315    struct ast_datastore *datastore;
00316    int res;
00317 
00318    ast_channel_lock(chan);
00319    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00320    if (datastore) {
00321       ast_channel_datastore_remove(chan, datastore);
00322    }
00323    ast_channel_unlock(chan);
00324    if (datastore) {
00325       ast_datastore_free(datastore);
00326       res = 0;
00327    } else {
00328       res = -1;
00329    }
00330    return res;
00331 }

static int speech_deactivate ( struct ast_channel chan,
const char *  data 
) [static]

SpeechDeactivateGrammar(Grammar Name) Dialplan Application.

Definition at line 615 of file app_speech_utils.c.

References ast_speech_grammar_deactivate(), find_speech(), and NULL.

Referenced by load_module().

00616 {
00617    int res = 0;
00618    struct ast_speech *speech = find_speech(chan);
00619 
00620    if (speech == NULL)
00621       return -1;
00622 
00623    /* Deactivate the grammar on the speech object */
00624    res = ast_speech_grammar_deactivate(speech, data);
00625 
00626    return res;
00627 }

static int speech_destroy ( struct ast_channel chan,
const char *  data 
) [static]

SpeechDestroy() Dialplan Application.

Definition at line 950 of file app_speech_utils.c.

References speech_datastore_destroy().

Referenced by load_module().

00951 {
00952    if (!chan) {
00953       return -1;
00954    }
00955    return speech_datastore_destroy(chan);
00956 }

static int speech_engine_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

SPEECH_ENGINE() Dialplan Get Function.

Definition at line 455 of file app_speech_utils.c.

References ast_speech_get_setting(), and find_speech().

00456 {
00457    struct ast_speech *speech = find_speech(chan);
00458 
00459    if (!data || !speech) {
00460       return -1;
00461    }
00462 
00463    return ast_speech_get_setting(speech, data, buf, len);
00464 }

static int speech_engine_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

SPEECH_ENGINE() Dialplan Set Function.

Definition at line 441 of file app_speech_utils.c.

References ast_speech_change(), find_speech(), and NULL.

00442 {
00443    struct ast_speech *speech = find_speech(chan);
00444 
00445    if (data == NULL || speech == NULL) {
00446       return -1;
00447    }
00448 
00449    ast_speech_change(speech, data, value);
00450 
00451    return 0;
00452 }

static int speech_grammar ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

SPEECH_GRAMMAR() Dialplan Function.

Definition at line 415 of file app_speech_utils.c.

References ast_copy_string(), find_result(), find_speech(), ast_speech_result::grammar, NULL, result, and ast_speech::results.

00417 {
00418    struct ast_speech_result *result = NULL;
00419    struct ast_speech *speech = find_speech(chan);
00420 
00421    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00422       return -1;
00423    }
00424 
00425    if (result->grammar != NULL) {
00426       ast_copy_string(buf, result->grammar, len);
00427    } else {
00428       buf[0] = '\0';
00429    }
00430 
00431    return 0;
00432 }

static int speech_load ( struct ast_channel chan,
const char *  vdata 
) [static]

SpeechLoadGrammar(Grammar Name,Path) Dialplan Application.

Definition at line 574 of file app_speech_utils.c.

References args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_speech_grammar_load(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_speech::data, find_speech(), and NULL.

Referenced by load_module().

00575 {
00576    int res = 0;
00577    struct ast_speech *speech = find_speech(chan);
00578    char *data;
00579    AST_DECLARE_APP_ARGS(args,
00580       AST_APP_ARG(grammar);
00581       AST_APP_ARG(path);
00582    );
00583 
00584    data = ast_strdupa(vdata);
00585    AST_STANDARD_APP_ARGS(args, data);
00586 
00587    if (speech == NULL)
00588       return -1;
00589 
00590    if (args.argc != 2)
00591       return -1;
00592 
00593    /* Load the grammar locally on the object */
00594    res = ast_speech_grammar_load(speech, args.grammar, args.path);
00595 
00596    return res;
00597 }

static int speech_processing_sound ( struct ast_channel chan,
const char *  data 
) [static]

SpeechProcessingSound(Sound File) Dialplan Application.

Definition at line 659 of file app_speech_utils.c.

References ast_free, ast_strdup, find_speech(), NULL, and ast_speech::processing_sound.

Referenced by load_module().

00660 {
00661    int res = 0;
00662    struct ast_speech *speech = find_speech(chan);
00663 
00664    if (speech == NULL)
00665       return -1;
00666 
00667    if (speech->processing_sound != NULL) {
00668       ast_free(speech->processing_sound);
00669       speech->processing_sound = NULL;
00670    }
00671 
00672    speech->processing_sound = ast_strdup(data);
00673 
00674    return res;
00675 }

static int speech_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

SPEECH() Dialplan Function.

Definition at line 495 of file app_speech_utils.c.

References ast_copy_string(), AST_LIST_NEXT, AST_SPEECH_SPOKE, ast_test_flag, find_speech(), NULL, result, ast_speech::results, and tmp().

00497 {
00498    int results = 0;
00499    struct ast_speech_result *result = NULL;
00500    struct ast_speech *speech = find_speech(chan);
00501    char tmp[128] = "";
00502 
00503    /* Now go for the various options */
00504    if (!strcasecmp(data, "status")) {
00505       if (speech != NULL)
00506          ast_copy_string(buf, "1", len);
00507       else
00508          ast_copy_string(buf, "0", len);
00509       return 0;
00510    }
00511 
00512    /* Make sure we have a speech structure for everything else */
00513    if (speech == NULL) {
00514       return -1;
00515    }
00516 
00517    /* Check to see if they are checking for silence */
00518    if (!strcasecmp(data, "spoke")) {
00519       if (ast_test_flag(speech, AST_SPEECH_SPOKE))
00520          ast_copy_string(buf, "1", len);
00521       else
00522          ast_copy_string(buf, "0", len);
00523    } else if (!strcasecmp(data, "results")) {
00524       /* Count number of results */
00525       for (result = speech->results; result; result = AST_LIST_NEXT(result, list))
00526          results++;
00527       snprintf(tmp, sizeof(tmp), "%d", results);
00528       ast_copy_string(buf, tmp, len);
00529    } else {
00530       buf[0] = '\0';
00531    }
00532 
00533    return 0;
00534 }

static int speech_results_type_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

SPEECH_RESULTS_TYPE() Dialplan Function.

Definition at line 473 of file app_speech_utils.c.

References ast_speech_change_results_type(), AST_SPEECH_RESULTS_TYPE_NBEST, AST_SPEECH_RESULTS_TYPE_NORMAL, find_speech(), and NULL.

00474 {
00475    struct ast_speech *speech = find_speech(chan);
00476 
00477    if (data == NULL || speech == NULL)
00478       return -1;
00479 
00480    if (!strcasecmp(value, "normal"))
00481       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL);
00482    else if (!strcasecmp(value, "nbest"))
00483       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST);
00484 
00485    return 0;
00486 }

static int speech_score ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

SPEECH_SCORE() Dialplan Function.

Definition at line 364 of file app_speech_utils.c.

References ast_copy_string(), find_result(), find_speech(), NULL, result, ast_speech::results, ast_speech_result::score, and tmp().

00366 {
00367    struct ast_speech_result *result = NULL;
00368    struct ast_speech *speech = find_speech(chan);
00369    char tmp[128] = "";
00370 
00371    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00372       return -1;
00373    }
00374    
00375    snprintf(tmp, sizeof(tmp), "%d", result->score);
00376    
00377    ast_copy_string(buf, tmp, len);
00378 
00379    return 0;
00380 }

static int speech_start ( struct ast_channel chan,
const char *  data 
) [static]

SpeechStart() Dialplan Application.

Definition at line 645 of file app_speech_utils.c.

References ast_speech_start(), find_speech(), and NULL.

Referenced by load_module().

00646 {
00647    int res = 0;
00648    struct ast_speech *speech = find_speech(chan);
00649 
00650    if (speech == NULL)
00651       return -1;
00652 
00653    ast_speech_start(speech);
00654 
00655    return res;
00656 }

static int speech_streamfile ( struct ast_channel chan,
const char *  filename,
const char *  preflang 
) [static]

Helper function used by speech_background to playback a soundfile.

Definition at line 678 of file app_speech_utils.c.

References ast_applystream(), ast_openstream(), ast_playstream(), and NULL.

Referenced by handle_speechrecognize(), and speech_background().

00679 {
00680    struct ast_filestream *fs = NULL;
00681 
00682    if (!(fs = ast_openstream(chan, filename, preflang)))
00683       return -1;
00684    
00685    if (ast_applystream(chan, fs))
00686       return -1;
00687    
00688    ast_playstream(fs);
00689 
00690    return 0;
00691 }

static int speech_text ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

SPEECH_TEXT() Dialplan Function.

Definition at line 389 of file app_speech_utils.c.

References ast_copy_string(), find_result(), find_speech(), NULL, result, ast_speech::results, and ast_speech_result::text.

00391 {
00392    struct ast_speech_result *result = NULL;
00393    struct ast_speech *speech = find_speech(chan);
00394 
00395    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00396       return -1;
00397    }
00398 
00399    if (result->text != NULL) {
00400       ast_copy_string(buf, result->text, len);
00401    } else {
00402       buf[0] = '\0';
00403    }
00404 
00405    return 0;
00406 }

static int speech_unload ( struct ast_channel chan,
const char *  data 
) [static]

SpeechUnloadGrammar(Grammar Name) Dialplan Application.

Definition at line 600 of file app_speech_utils.c.

References ast_speech_grammar_unload(), find_speech(), and NULL.

Referenced by load_module().

00601 {
00602    int res = 0;
00603    struct ast_speech *speech = find_speech(chan);
00604 
00605    if (speech == NULL)
00606       return -1;
00607 
00608    /* Unload the grammar */
00609    res = ast_speech_grammar_unload(speech, data);
00610 
00611    return res;
00612 }

static int unload_module ( void   )  [static]

Definition at line 958 of file app_speech_utils.c.

References ast_custom_function_unregister(), and ast_unregister_application().

00959 {
00960    int res = 0;
00961 
00962    res = ast_unregister_application("SpeechCreate");
00963    res |= ast_unregister_application("SpeechLoadGrammar");
00964    res |= ast_unregister_application("SpeechUnloadGrammar");
00965    res |= ast_unregister_application("SpeechActivateGrammar");
00966    res |= ast_unregister_application("SpeechDeactivateGrammar");
00967    res |= ast_unregister_application("SpeechStart");
00968    res |= ast_unregister_application("SpeechBackground");
00969    res |= ast_unregister_application("SpeechDestroy");
00970    res |= ast_unregister_application("SpeechProcessingSound");
00971    res |= ast_custom_function_unregister(&speech_function);
00972    res |= ast_custom_function_unregister(&speech_score_function);
00973    res |= ast_custom_function_unregister(&speech_text_function);
00974    res |= ast_custom_function_unregister(&speech_grammar_function);
00975    res |= ast_custom_function_unregister(&speech_engine_function);
00976    res |= ast_custom_function_unregister(&speech_results_type_function);
00977 
00978    return res; 
00979 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Dialplan Speech Applications" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .nonoptreq = "res_speech", } [static]

Definition at line 1009 of file app_speech_utils.c.

Definition at line 1009 of file app_speech_utils.c.

struct ast_app_option speech_background_options[128] = { [ 'n' ] = { .flag = SB_OPT_NOANSWER }, } [static]

Definition at line 699 of file app_speech_utils.c.

Referenced by speech_background().

Initial value:

 {
   .type = "speech",
   .destroy = destroy_callback
}
Static structure for datastore information.

Definition at line 278 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH_ENGINE",
   .read = speech_engine_read,
   .write = speech_engine_write,
}

Definition at line 466 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH",
   .read = speech_read,
   .write = NULL,
}

Definition at line 536 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH_GRAMMAR",
   .read = speech_grammar,
   .write = NULL,
}

Definition at line 434 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH_RESULTS_TYPE",
   .read = NULL,
   .write = speech_results_type_write,
}

Definition at line 488 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH_SCORE",
   .read = speech_score,
   .write = NULL,
}

Definition at line 382 of file app_speech_utils.c.

Initial value:

 {
   .name = "SPEECH_TEXT",
   .read = speech_text,
   .write = NULL,
}

Definition at line 408 of file app_speech_utils.c.


Generated on Thu Apr 16 06:28:44 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6