Wed May 16 06:34:16 2012

Asterisk developer's documentation


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: 362817 $")
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_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_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 SPEECH_ENGINE() Dialplan 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, .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 643 of file app_speech_utils.c.

00643      {
00644    SB_OPT_NOANSWER = (1 << 0),
00645 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 976 of file app_speech_utils.c.

static void __unreg_module ( void   )  [static]

Definition at line 976 of file app_speech_utils.c.

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

static void destroy_callback ( void *  data  )  [static]

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

Definition at line 262 of file app_speech_utils.c.

References ast_speech_destroy().

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

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

Definition at line 298 of file app_speech_utils.c.

References AST_LIST_NEXT, and ast_speech_result::nbest_num.

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

00299 {
00300    struct ast_speech_result *result = results;
00301    char *tmp = NULL;
00302    int nbest_num = 0, wanted_num = 0, i = 0;
00303 
00304    if (!result) {
00305       return NULL;
00306    }
00307 
00308    if ((tmp = strchr(result_num, '/'))) {
00309       *tmp++ = '\0';
00310       nbest_num = atoi(result_num);
00311       wanted_num = atoi(tmp);
00312    } else {
00313       wanted_num = atoi(result_num);
00314    }
00315 
00316    do {
00317       if (result->nbest_num != nbest_num)
00318          continue;
00319       if (i == wanted_num)
00320          break;
00321       i++;
00322    } while ((result = AST_LIST_NEXT(result, list)));
00323 
00324    return result;
00325 }

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 283 of file app_speech_utils.c.

References ast_channel_datastore_find(), and ast_datastore::data.

Referenced by speech_activate(), speech_background(), speech_deactivate(), speech_destroy(), 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().

00284 {
00285    struct ast_speech *speech = NULL;
00286    struct ast_datastore *datastore = NULL;
00287    
00288    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00289    if (datastore == NULL) {
00290       return NULL;
00291    }
00292    speech = datastore->data;
00293 
00294    return speech;
00295 }

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 580 of file app_speech_utils.c.

References ast_speech_grammar_activate(), and find_speech().

Referenced by load_module().

00581 {
00582    int res = 0;
00583    struct ast_speech *speech = find_speech(chan);
00584 
00585    if (speech == NULL)
00586       return -1;
00587 
00588    /* Activate the grammar on the speech object */
00589    res = ast_speech_grammar_activate(speech, data);
00590 
00591    return res;
00592 }

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

SpeechBackground(Sound File,Timeout) Dialplan Application.

Definition at line 652 of file app_speech_utils.c.

References args, ast_answer(), AST_APP_ARG, ast_app_parse_options(), ast_calloc, ast_channel_datastore_find(), ast_channel_datastore_remove(), 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_format_clear(), ast_format_copy(), 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_destroy(), 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, parse(), pbx_builtin_getvar_helper(), ast_speech::processing_sound, ast_frame::ptr, ast_speech::results, SB_OPT_NOANSWER, ast_speech_result::score, speech_background_options, speech_streamfile(), ast_speech::state, strsep(), ast_frame::subclass, and ast_speech_result::text.

Referenced by load_module().

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

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

SpeechCreate() Dialplan Application.

Definition at line 497 of file app_speech_utils.c.

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

Referenced by load_module().

00498 {
00499    struct ast_speech *speech = NULL;
00500    struct ast_datastore *datastore = NULL;
00501 
00502    /* Request a speech object */
00503    speech = ast_speech_new(data, ast_channel_nativeformats(chan));
00504    if (speech == NULL) {
00505       /* Not available */
00506       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00507       return 0;
00508    }
00509 
00510    datastore = ast_datastore_alloc(&speech_datastore, NULL);
00511    if (datastore == NULL) {
00512       ast_speech_destroy(speech);
00513       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00514       return 0;
00515    }
00516    pbx_builtin_setvar_helper(chan, "ERROR", NULL);
00517    datastore->data = speech;
00518    ast_channel_datastore_add(chan, datastore);
00519 
00520    return 0;
00521 }

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

SpeechDeactivateGrammar(Grammar Name) Dialplan Application.

Definition at line 565 of file app_speech_utils.c.

References ast_speech_grammar_deactivate(), and find_speech().

Referenced by load_module().

00566 {
00567    int res = 0;
00568    struct ast_speech *speech = find_speech(chan);
00569 
00570    if (speech == NULL)
00571       return -1;
00572 
00573    /* Deactivate the grammar on the speech object */
00574    res = ast_speech_grammar_deactivate(speech, data);
00575 
00576    return res;
00577 }

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

SpeechDestroy() Dialplan Application.

Definition at line 906 of file app_speech_utils.c.

References ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_speech_destroy(), and find_speech().

Referenced by load_module().

00907 {
00908    int res = 0;
00909    struct ast_speech *speech = find_speech(chan);
00910    struct ast_datastore *datastore = NULL;
00911 
00912    if (speech == NULL)
00913       return -1;
00914 
00915    /* Destroy speech structure */
00916    ast_speech_destroy(speech);
00917 
00918    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00919    if (datastore != NULL) {
00920       ast_channel_datastore_remove(chan, datastore);
00921    }
00922 
00923    return res;
00924 }

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

SPEECH_ENGINE() Dialplan Function.

Definition at line 405 of file app_speech_utils.c.

References ast_speech_change(), and find_speech().

00406 {
00407    struct ast_speech *speech = find_speech(chan);
00408 
00409    if (data == NULL || speech == NULL) {
00410       return -1;
00411    }
00412 
00413    ast_speech_change(speech, data, value);
00414 
00415    return 0;
00416 }

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 379 of file app_speech_utils.c.

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

00381 {
00382    struct ast_speech_result *result = NULL;
00383    struct ast_speech *speech = find_speech(chan);
00384 
00385    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00386       return -1;
00387    }
00388 
00389    if (result->grammar != NULL) {
00390       ast_copy_string(buf, result->grammar, len);
00391    } else {
00392       buf[0] = '\0';
00393    }
00394 
00395    return 0;
00396 }

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

SpeechLoadGrammar(Grammar Name,Path) Dialplan Application.

Definition at line 524 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, and find_speech().

Referenced by load_module().

00525 {
00526    int res = 0;
00527    struct ast_speech *speech = find_speech(chan);
00528    char *data;
00529    AST_DECLARE_APP_ARGS(args,
00530       AST_APP_ARG(grammar);
00531       AST_APP_ARG(path);
00532    );
00533 
00534    data = ast_strdupa(vdata);
00535    AST_STANDARD_APP_ARGS(args, data);
00536 
00537    if (speech == NULL)
00538       return -1;
00539 
00540    if (args.argc != 2)
00541       return -1;
00542 
00543    /* Load the grammar locally on the object */
00544    res = ast_speech_grammar_load(speech, args.grammar, args.path);
00545 
00546    return res;
00547 }

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

SpeechProcessingSound(Sound File) Dialplan Application.

Definition at line 609 of file app_speech_utils.c.

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

Referenced by load_module().

00610 {
00611    int res = 0;
00612    struct ast_speech *speech = find_speech(chan);
00613 
00614    if (speech == NULL)
00615       return -1;
00616 
00617    if (speech->processing_sound != NULL) {
00618       ast_free(speech->processing_sound);
00619       speech->processing_sound = NULL;
00620    }
00621 
00622    speech->processing_sound = ast_strdup(data);
00623 
00624    return res;
00625 }

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 447 of file app_speech_utils.c.

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

00449 {
00450    int results = 0;
00451    struct ast_speech_result *result = NULL;
00452    struct ast_speech *speech = find_speech(chan);
00453    char tmp[128] = "";
00454 
00455    /* Now go for the various options */
00456    if (!strcasecmp(data, "status")) {
00457       if (speech != NULL)
00458          ast_copy_string(buf, "1", len);
00459       else
00460          ast_copy_string(buf, "0", len);
00461       return 0;
00462    }
00463 
00464    /* Make sure we have a speech structure for everything else */
00465    if (speech == NULL) {
00466       return -1;
00467    }
00468 
00469    /* Check to see if they are checking for silence */
00470    if (!strcasecmp(data, "spoke")) {
00471       if (ast_test_flag(speech, AST_SPEECH_SPOKE))
00472          ast_copy_string(buf, "1", len);
00473       else
00474          ast_copy_string(buf, "0", len);
00475    } else if (!strcasecmp(data, "results")) {
00476       /* Count number of results */
00477       for (result = speech->results; result; result = AST_LIST_NEXT(result, list))
00478          results++;
00479       snprintf(tmp, sizeof(tmp), "%d", results);
00480       ast_copy_string(buf, tmp, len);
00481    } else {
00482       buf[0] = '\0';
00483    }
00484 
00485    return 0;
00486 }

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 425 of file app_speech_utils.c.

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

00426 {
00427    struct ast_speech *speech = find_speech(chan);
00428 
00429    if (data == NULL || speech == NULL)
00430       return -1;
00431 
00432    if (!strcasecmp(value, "normal"))
00433       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL);
00434    else if (!strcasecmp(value, "nbest"))
00435       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST);
00436 
00437    return 0;
00438 }

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 328 of file app_speech_utils.c.

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

00330 {
00331    struct ast_speech_result *result = NULL;
00332    struct ast_speech *speech = find_speech(chan);
00333    char tmp[128] = "";
00334 
00335    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00336       return -1;
00337    }
00338    
00339    snprintf(tmp, sizeof(tmp), "%d", result->score);
00340    
00341    ast_copy_string(buf, tmp, len);
00342 
00343    return 0;
00344 }

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

SpeechStart() Dialplan Application.

Definition at line 595 of file app_speech_utils.c.

References ast_speech_start(), and find_speech().

Referenced by load_module().

00596 {
00597    int res = 0;
00598    struct ast_speech *speech = find_speech(chan);
00599 
00600    if (speech == NULL)
00601       return -1;
00602 
00603    ast_speech_start(speech);
00604 
00605    return res;
00606 }

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 628 of file app_speech_utils.c.

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

Referenced by handle_speechrecognize(), and speech_background().

00629 {
00630    struct ast_filestream *fs = NULL;
00631 
00632    if (!(fs = ast_openstream(chan, filename, preflang)))
00633       return -1;
00634    
00635    if (ast_applystream(chan, fs))
00636       return -1;
00637    
00638    ast_playstream(fs);
00639 
00640    return 0;
00641 }

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 353 of file app_speech_utils.c.

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

00355 {
00356    struct ast_speech_result *result = NULL;
00357    struct ast_speech *speech = find_speech(chan);
00358 
00359    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00360       return -1;
00361    }
00362 
00363    if (result->text != NULL) {
00364       ast_copy_string(buf, result->text, len);
00365    } else {
00366       buf[0] = '\0';
00367    }
00368 
00369    return 0;
00370 }

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

SpeechUnloadGrammar(Grammar Name) Dialplan Application.

Definition at line 550 of file app_speech_utils.c.

References ast_speech_grammar_unload(), and find_speech().

Referenced by load_module().

00551 {
00552    int res = 0;
00553    struct ast_speech *speech = find_speech(chan);
00554 
00555    if (speech == NULL)
00556       return -1;
00557 
00558    /* Unload the grammar */
00559    res = ast_speech_grammar_unload(speech, data);
00560 
00561    return res;
00562 }

static int unload_module ( void   )  [static]

Definition at line 926 of file app_speech_utils.c.

References ast_custom_function_unregister(), and ast_unregister_application().

00927 {
00928    int res = 0;
00929 
00930    res = ast_unregister_application("SpeechCreate");
00931    res |= ast_unregister_application("SpeechLoadGrammar");
00932    res |= ast_unregister_application("SpeechUnloadGrammar");
00933    res |= ast_unregister_application("SpeechActivateGrammar");
00934    res |= ast_unregister_application("SpeechDeactivateGrammar");
00935    res |= ast_unregister_application("SpeechStart");
00936    res |= ast_unregister_application("SpeechBackground");
00937    res |= ast_unregister_application("SpeechDestroy");
00938    res |= ast_unregister_application("SpeechProcessingSound");
00939    res |= ast_custom_function_unregister(&speech_function);
00940    res |= ast_custom_function_unregister(&speech_score_function);
00941    res |= ast_custom_function_unregister(&speech_text_function);
00942    res |= ast_custom_function_unregister(&speech_grammar_function);
00943    res |= ast_custom_function_unregister(&speech_engine_function);
00944    res |= ast_custom_function_unregister(&speech_results_type_function);
00945 
00946    return res; 
00947 }


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, .load = load_module, .unload = unload_module, .nonoptreq = "res_speech", } [static]

Definition at line 976 of file app_speech_utils.c.

Definition at line 976 of file app_speech_utils.c.

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

Definition at line 649 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 277 of file app_speech_utils.c.

Initial value:

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

Definition at line 418 of file app_speech_utils.c.

Initial value:

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

Definition at line 488 of file app_speech_utils.c.

Initial value:

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

Definition at line 398 of file app_speech_utils.c.

Initial value:

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

Definition at line 440 of file app_speech_utils.c.

Initial value:

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

Definition at line 346 of file app_speech_utils.c.

Initial value:

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

Definition at line 372 of file app_speech_utils.c.


Generated on Wed May 16 06:34:16 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6