Mon Jun 27 16:51:00 2011

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"

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: 284610 $")
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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .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 630 of file app_speech_utils.c.

00630      {
00631    SB_OPT_NOANSWER = (1 << 0),
00632 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 962 of file app_speech_utils.c.

static void __unreg_module ( void   )  [static]

Definition at line 962 of file app_speech_utils.c.

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

static void destroy_callback ( void *  data  )  [static]

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

Definition at line 249 of file app_speech_utils.c.

References ast_speech_destroy().

00250 {
00251    struct ast_speech *speech = (struct ast_speech*)data;
00252 
00253    if (speech == NULL) {
00254       return;
00255    }
00256 
00257    /* Deallocate now */
00258    ast_speech_destroy(speech);
00259 
00260    return;
00261 }

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

Definition at line 285 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().

00286 {
00287    struct ast_speech_result *result = results;
00288    char *tmp = NULL;
00289    int nbest_num = 0, wanted_num = 0, i = 0;
00290 
00291    if (!result) {
00292       return NULL;
00293    }
00294 
00295    if ((tmp = strchr(result_num, '/'))) {
00296       *tmp++ = '\0';
00297       nbest_num = atoi(result_num);
00298       wanted_num = atoi(tmp);
00299    } else {
00300       wanted_num = atoi(result_num);
00301    }
00302 
00303    do {
00304       if (result->nbest_num != nbest_num)
00305          continue;
00306       if (i == wanted_num)
00307          break;
00308       i++;
00309    } while ((result = AST_LIST_NEXT(result, list)));
00310 
00311    return result;
00312 }

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

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

Definition at line 270 of file app_speech_utils.c.

References ast_channel_datastore_find(), ast_datastore::data, and speech_datastore.

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().

00271 {
00272    struct ast_speech *speech = NULL;
00273    struct ast_datastore *datastore = NULL;
00274    
00275    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00276    if (datastore == NULL) {
00277       return NULL;
00278    }
00279    speech = datastore->data;
00280 
00281    return speech;
00282 }

static int load_module ( void   )  [static]

Definition at line 935 of file app_speech_utils.c.

References ast_custom_function_register, ast_register_application_xml, speech_activate(), speech_background(), speech_create(), speech_deactivate(), speech_destroy(), speech_engine_function, speech_function, speech_grammar_function, speech_load(), speech_processing_sound(), speech_results_type_function, speech_score_function, speech_start(), speech_text_function, and speech_unload().

00936 {
00937    int res = 0;
00938 
00939    res = ast_register_application_xml("SpeechCreate", speech_create);
00940    res |= ast_register_application_xml("SpeechLoadGrammar", speech_load);
00941    res |= ast_register_application_xml("SpeechUnloadGrammar", speech_unload);
00942    res |= ast_register_application_xml("SpeechActivateGrammar", speech_activate);
00943    res |= ast_register_application_xml("SpeechDeactivateGrammar", speech_deactivate);
00944    res |= ast_register_application_xml("SpeechStart", speech_start);
00945    res |= ast_register_application_xml("SpeechBackground", speech_background);
00946    res |= ast_register_application_xml("SpeechDestroy", speech_destroy);
00947    res |= ast_register_application_xml("SpeechProcessingSound", speech_processing_sound);
00948    res |= ast_custom_function_register(&speech_function);
00949    res |= ast_custom_function_register(&speech_score_function);
00950    res |= ast_custom_function_register(&speech_text_function);
00951    res |= ast_custom_function_register(&speech_grammar_function);
00952    res |= ast_custom_function_register(&speech_engine_function);
00953    res |= ast_custom_function_register(&speech_results_type_function);
00954 
00955    return res;
00956 }

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

SpeechActivateGrammar(Grammar Name) Dialplan Application.

Definition at line 567 of file app_speech_utils.c.

References ast_speech_grammar_activate(), and find_speech().

Referenced by load_module().

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

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

SpeechBackground(Sound File,Timeout) Dialplan Application.

Definition at line 639 of file app_speech_utils.c.

References ast_channel::_state, args, ast_answer(), AST_APP_ARG, ast_app_parse_options(), ast_calloc, ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_lock, ast_channel_unlock, ast_clear_flag, AST_CONTROL_HANGUP, AST_DECLARE_APP_ARGS, AST_FORMAT_SLINEAR, 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_pbx::dtimeoutms, f, find_speech(), ast_speech::format, ast_channel::language, ast_speech::lock, parse(), ast_channel::pbx, pbx_builtin_getvar_helper(), ast_speech::processing_sound, ast_channel::readformat, ast_speech::results, SB_OPT_NOANSWER, ast_channel::sched, speech_background_options, speech_datastore, speech_streamfile(), ast_speech::state, ast_channel::stream, ast_channel::streamid, strsep(), and ast_channel::timingfunc.

Referenced by load_module().

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

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

SpeechCreate() Dialplan Application.

Definition at line 484 of file app_speech_utils.c.

References ast_channel_datastore_add(), ast_datastore_alloc, ast_speech_destroy(), ast_speech_new(), ast_datastore::data, ast_channel::nativeformats, pbx_builtin_setvar_helper(), and speech_datastore.

Referenced by load_module().

00485 {
00486    struct ast_speech *speech = NULL;
00487    struct ast_datastore *datastore = NULL;
00488 
00489    /* Request a speech object */
00490    speech = ast_speech_new(data, chan->nativeformats);
00491    if (speech == NULL) {
00492       /* Not available */
00493       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00494       return 0;
00495    }
00496 
00497    datastore = ast_datastore_alloc(&speech_datastore, NULL);
00498    if (datastore == NULL) {
00499       ast_speech_destroy(speech);
00500       pbx_builtin_setvar_helper(chan, "ERROR", "1");
00501       return 0;
00502    }
00503    pbx_builtin_setvar_helper(chan, "ERROR", NULL);
00504    datastore->data = speech;
00505    ast_channel_datastore_add(chan, datastore);
00506 
00507    return 0;
00508 }

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

SpeechDeactivateGrammar(Grammar Name) Dialplan Application.

Definition at line 552 of file app_speech_utils.c.

References ast_speech_grammar_deactivate(), and find_speech().

Referenced by load_module().

00553 {
00554    int res = 0;
00555    struct ast_speech *speech = find_speech(chan);
00556 
00557    if (speech == NULL)
00558       return -1;
00559 
00560    /* Deactivate the grammar on the speech object */
00561    res = ast_speech_grammar_deactivate(speech, data);
00562 
00563    return res;
00564 }

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

SpeechDestroy() Dialplan Application.

Definition at line 892 of file app_speech_utils.c.

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

Referenced by load_module().

00893 {
00894    int res = 0;
00895    struct ast_speech *speech = find_speech(chan);
00896    struct ast_datastore *datastore = NULL;
00897 
00898    if (speech == NULL)
00899       return -1;
00900 
00901    /* Destroy speech structure */
00902    ast_speech_destroy(speech);
00903 
00904    datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
00905    if (datastore != NULL) {
00906       ast_channel_datastore_remove(chan, datastore);
00907    }
00908 
00909    return res;
00910 }

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

References ast_speech_change(), and find_speech().

00393 {
00394    struct ast_speech *speech = find_speech(chan);
00395 
00396    if (data == NULL || speech == NULL) {
00397       return -1;
00398    }
00399 
00400    ast_speech_change(speech, data, value);
00401 
00402    return 0;
00403 }

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

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

00368 {
00369    struct ast_speech_result *result = NULL;
00370    struct ast_speech *speech = find_speech(chan);
00371 
00372    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00373       return -1;
00374    }
00375 
00376    if (result->grammar != NULL) {
00377       ast_copy_string(buf, result->grammar, len);
00378    } else {
00379       buf[0] = '\0';
00380    }
00381 
00382    return 0;
00383 }

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

SpeechLoadGrammar(Grammar Name,Path) Dialplan Application.

Definition at line 511 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().

00512 {
00513    int res = 0;
00514    struct ast_speech *speech = find_speech(chan);
00515    char *data;
00516    AST_DECLARE_APP_ARGS(args,
00517       AST_APP_ARG(grammar);
00518       AST_APP_ARG(path);
00519    );
00520 
00521    data = ast_strdupa(vdata);
00522    AST_STANDARD_APP_ARGS(args, data);
00523 
00524    if (speech == NULL)
00525       return -1;
00526 
00527    if (args.argc != 2)
00528       return -1;
00529 
00530    /* Load the grammar locally on the object */
00531    res = ast_speech_grammar_load(speech, args.grammar, args.path);
00532 
00533    return res;
00534 }

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

SpeechProcessingSound(Sound File) Dialplan Application.

Definition at line 596 of file app_speech_utils.c.

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

Referenced by load_module().

00597 {
00598    int res = 0;
00599    struct ast_speech *speech = find_speech(chan);
00600 
00601    if (speech == NULL)
00602       return -1;
00603 
00604    if (speech->processing_sound != NULL) {
00605       ast_free(speech->processing_sound);
00606       speech->processing_sound = NULL;
00607    }
00608 
00609    speech->processing_sound = ast_strdup(data);
00610 
00611    return res;
00612 }

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 434 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.

00436 {
00437    int results = 0;
00438    struct ast_speech_result *result = NULL;
00439    struct ast_speech *speech = find_speech(chan);
00440    char tmp[128] = "";
00441 
00442    /* Now go for the various options */
00443    if (!strcasecmp(data, "status")) {
00444       if (speech != NULL)
00445          ast_copy_string(buf, "1", len);
00446       else
00447          ast_copy_string(buf, "0", len);
00448       return 0;
00449    }
00450 
00451    /* Make sure we have a speech structure for everything else */
00452    if (speech == NULL) {
00453       return -1;
00454    }
00455 
00456    /* Check to see if they are checking for silence */
00457    if (!strcasecmp(data, "spoke")) {
00458       if (ast_test_flag(speech, AST_SPEECH_SPOKE))
00459          ast_copy_string(buf, "1", len);
00460       else
00461          ast_copy_string(buf, "0", len);
00462    } else if (!strcasecmp(data, "results")) {
00463       /* Count number of results */
00464       for (result = speech->results; result; result = AST_LIST_NEXT(result, list))
00465          results++;
00466       snprintf(tmp, sizeof(tmp), "%d", results);
00467       ast_copy_string(buf, tmp, len);
00468    } else {
00469       buf[0] = '\0';
00470    }
00471 
00472    return 0;
00473 }

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 412 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().

00413 {
00414    struct ast_speech *speech = find_speech(chan);
00415 
00416    if (data == NULL || speech == NULL)
00417       return -1;
00418 
00419    if (!strcasecmp(value, "normal"))
00420       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL);
00421    else if (!strcasecmp(value, "nbest"))
00422       ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST);
00423 
00424    return 0;
00425 }

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

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

00317 {
00318    struct ast_speech_result *result = NULL;
00319    struct ast_speech *speech = find_speech(chan);
00320    char tmp[128] = "";
00321 
00322    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00323       return -1;
00324    }
00325    
00326    snprintf(tmp, sizeof(tmp), "%d", result->score);
00327    
00328    ast_copy_string(buf, tmp, len);
00329 
00330    return 0;
00331 }

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

SpeechStart() Dialplan Application.

Definition at line 582 of file app_speech_utils.c.

References ast_speech_start(), and find_speech().

Referenced by load_module().

00583 {
00584    int res = 0;
00585    struct ast_speech *speech = find_speech(chan);
00586 
00587    if (speech == NULL)
00588       return -1;
00589 
00590    ast_speech_start(speech);
00591 
00592    return res;
00593 }

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

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

Referenced by handle_speechrecognize(), and speech_background().

00616 {
00617    struct ast_filestream *fs = NULL;
00618 
00619    if (!(fs = ast_openstream(chan, filename, preflang)))
00620       return -1;
00621    
00622    if (ast_applystream(chan, fs))
00623       return -1;
00624    
00625    ast_playstream(fs);
00626 
00627    return 0;
00628 }

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

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

00342 {
00343    struct ast_speech_result *result = NULL;
00344    struct ast_speech *speech = find_speech(chan);
00345 
00346    if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
00347       return -1;
00348    }
00349 
00350    if (result->text != NULL) {
00351       ast_copy_string(buf, result->text, len);
00352    } else {
00353       buf[0] = '\0';
00354    }
00355 
00356    return 0;
00357 }

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

SpeechUnloadGrammar(Grammar Name) Dialplan Application.

Definition at line 537 of file app_speech_utils.c.

References ast_speech_grammar_unload(), and find_speech().

Referenced by load_module().

00538 {
00539    int res = 0;
00540    struct ast_speech *speech = find_speech(chan);
00541 
00542    if (speech == NULL)
00543       return -1;
00544 
00545    /* Unload the grammar */
00546    res = ast_speech_grammar_unload(speech, data);
00547 
00548    return res;
00549 }

static int unload_module ( void   )  [static]

Definition at line 912 of file app_speech_utils.c.

References ast_custom_function_unregister(), ast_unregister_application(), speech_engine_function, speech_function, speech_grammar_function, speech_results_type_function, speech_score_function, and speech_text_function.

00913 {
00914    int res = 0;
00915 
00916    res = ast_unregister_application("SpeechCreate");
00917    res |= ast_unregister_application("SpeechLoadGrammar");
00918    res |= ast_unregister_application("SpeechUnloadGrammar");
00919    res |= ast_unregister_application("SpeechActivateGrammar");
00920    res |= ast_unregister_application("SpeechDeactivateGrammar");
00921    res |= ast_unregister_application("SpeechStart");
00922    res |= ast_unregister_application("SpeechBackground");
00923    res |= ast_unregister_application("SpeechDestroy");
00924    res |= ast_unregister_application("SpeechProcessingSound");
00925    res |= ast_custom_function_unregister(&speech_function);
00926    res |= ast_custom_function_unregister(&speech_score_function);
00927    res |= ast_custom_function_unregister(&speech_text_function);
00928    res |= ast_custom_function_unregister(&speech_grammar_function);
00929    res |= ast_custom_function_unregister(&speech_engine_function);
00930    res |= ast_custom_function_unregister(&speech_results_type_function);
00931 
00932    return res; 
00933 }


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

Definition at line 962 of file app_speech_utils.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 962 of file app_speech_utils.c.

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

Definition at line 636 of file app_speech_utils.c.

Referenced by speech_background().

struct ast_datastore_info speech_datastore [static]

Initial value:

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

Definition at line 264 of file app_speech_utils.c.

Referenced by find_speech(), speech_background(), speech_create(), and speech_destroy().

struct ast_custom_function speech_engine_function [static]

Initial value:

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

Definition at line 405 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function speech_function [static]

Initial value:

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

Definition at line 475 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function speech_grammar_function [static]

Initial value:

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

Definition at line 385 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function speech_results_type_function [static]

Initial value:

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

Definition at line 427 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function speech_score_function [static]

Initial value:

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

Definition at line 333 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function speech_text_function [static]

Initial value:

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

Definition at line 359 of file app_speech_utils.c.

Referenced by load_module(), and unload_module().


Generated on Mon Jun 27 16:51:00 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7