app_speech_utils.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Speech Recognition Utility Applications
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. <depend>res_speech</depend>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/speech.h"
  39. /*** DOCUMENTATION
  40. <application name="SpeechCreate" language="en_US">
  41. <synopsis>
  42. Create a Speech Structure.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="engine_name" required="true" />
  46. </syntax>
  47. <description>
  48. <para>This application creates information to be used by all the other applications.
  49. It must be called before doing any speech recognition activities such as activating a grammar.
  50. It takes the engine name to use as the argument, if not specified the default engine will be used.</para>
  51. <para>Sets the ERROR channel variable to 1 if the engine cannot be used.</para>
  52. </description>
  53. </application>
  54. <application name="SpeechActivateGrammar" language="en_US">
  55. <synopsis>
  56. Activate a grammar.
  57. </synopsis>
  58. <syntax>
  59. <parameter name="grammar_name" required="true" />
  60. </syntax>
  61. <description>
  62. <para>This activates the specified grammar to be recognized by the engine.
  63. A grammar tells the speech recognition engine what to recognize, and how to portray it back to you
  64. in the dialplan. The grammar name is the only argument to this application.</para>
  65. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  66. </description>
  67. </application>
  68. <application name="SpeechStart" language="en_US">
  69. <synopsis>
  70. Start recognizing voice in the audio stream.
  71. </synopsis>
  72. <syntax />
  73. <description>
  74. <para>Tell the speech recognition engine that it should start trying to get results from audio being
  75. fed to it.</para>
  76. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  77. </description>
  78. </application>
  79. <application name="SpeechBackground" language="en_US">
  80. <synopsis>
  81. Play a sound file and wait for speech to be recognized.
  82. </synopsis>
  83. <syntax>
  84. <parameter name="sound_file" required="true" />
  85. <parameter name="timeout">
  86. <para>Timeout integer in seconds. Note the timeout will only start
  87. once the sound file has stopped playing.</para>
  88. </parameter>
  89. <parameter name="options">
  90. <optionlist>
  91. <option name="n">
  92. <para>Don't answer the channel if it has not already been answered.</para>
  93. </option>
  94. </optionlist>
  95. </parameter>
  96. </syntax>
  97. <description>
  98. <para>This application plays a sound file and waits for the person to speak. Once they start speaking playback
  99. of the file stops, and silence is heard. Once they stop talking the processing sound is played to indicate
  100. the speech recognition engine is working. Once results are available the application returns and results
  101. (score and text) are available using dialplan functions.</para>
  102. <para>The first text and score are ${SPEECH_TEXT(0)} AND ${SPEECH_SCORE(0)} while the second are ${SPEECH_TEXT(1)}
  103. and ${SPEECH_SCORE(1)}.</para>
  104. <para>The first argument is the sound file and the second is the timeout integer in seconds.</para>
  105. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  106. </description>
  107. </application>
  108. <application name="SpeechDeactivateGrammar" language="en_US">
  109. <synopsis>
  110. Deactivate a grammar.
  111. </synopsis>
  112. <syntax>
  113. <parameter name="grammar_name" required="true">
  114. <para>The grammar name to deactivate</para>
  115. </parameter>
  116. </syntax>
  117. <description>
  118. <para>This deactivates the specified grammar so that it is no longer recognized.</para>
  119. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  120. </description>
  121. </application>
  122. <application name="SpeechProcessingSound" language="en_US">
  123. <synopsis>
  124. Change background processing sound.
  125. </synopsis>
  126. <syntax>
  127. <parameter name="sound_file" required="true" />
  128. </syntax>
  129. <description>
  130. <para>This changes the processing sound that SpeechBackground plays back when the speech recognition engine is
  131. processing and working to get results.</para>
  132. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  133. </description>
  134. </application>
  135. <application name="SpeechDestroy" language="en_US">
  136. <synopsis>
  137. End speech recognition.
  138. </synopsis>
  139. <syntax />
  140. <description>
  141. <para>This destroys the information used by all the other speech recognition applications.
  142. If you call this application but end up wanting to recognize more speech, you must call SpeechCreate()
  143. again before calling any other application.</para>
  144. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  145. </description>
  146. </application>
  147. <application name="SpeechLoadGrammar" language="en_US">
  148. <synopsis>
  149. Load a grammar.
  150. </synopsis>
  151. <syntax>
  152. <parameter name="grammar_name" required="true" />
  153. <parameter name="path" required="true" />
  154. </syntax>
  155. <description>
  156. <para>Load a grammar only on the channel, not globally.</para>
  157. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  158. </description>
  159. </application>
  160. <application name="SpeechUnloadGrammar" language="en_US">
  161. <synopsis>
  162. Unload a grammar.
  163. </synopsis>
  164. <syntax>
  165. <parameter name="grammar_name" required="true" />
  166. </syntax>
  167. <description>
  168. <para>Unload a grammar.</para>
  169. <para>Hangs up the channel on failure. If this is not desired, use TryExec.</para>
  170. </description>
  171. </application>
  172. <function name="SPEECH_SCORE" language="en_US">
  173. <synopsis>
  174. Gets the confidence score of a result.
  175. </synopsis>
  176. <syntax argsep="/">
  177. <parameter name="nbest_number" />
  178. <parameter name="result_number" required="true" />
  179. </syntax>
  180. <description>
  181. <para>Gets the confidence score of a result.</para>
  182. </description>
  183. </function>
  184. <function name="SPEECH_TEXT" language="en_US">
  185. <synopsis>
  186. Gets the recognized text of a result.
  187. </synopsis>
  188. <syntax argsep="/">
  189. <parameter name="nbest_number" />
  190. <parameter name="result_number" required="true" />
  191. </syntax>
  192. <description>
  193. <para>Gets the recognized text of a result.</para>
  194. </description>
  195. </function>
  196. <function name="SPEECH_GRAMMAR" language="en_US">
  197. <synopsis>
  198. Gets the matched grammar of a result if available.
  199. </synopsis>
  200. <syntax argsep="/">
  201. <parameter name="nbest_number" />
  202. <parameter name="result_number" required="true" />
  203. </syntax>
  204. <description>
  205. <para>Gets the matched grammar of a result if available.</para>
  206. </description>
  207. </function>
  208. <function name="SPEECH_ENGINE" language="en_US">
  209. <synopsis>
  210. Get or change a speech engine specific attribute.
  211. </synopsis>
  212. <syntax>
  213. <parameter name="name" required="true" />
  214. </syntax>
  215. <description>
  216. <para>Changes a speech engine specific attribute.</para>
  217. </description>
  218. </function>
  219. <function name="SPEECH_RESULTS_TYPE" language="en_US">
  220. <synopsis>
  221. Sets the type of results that will be returned.
  222. </synopsis>
  223. <syntax />
  224. <description>
  225. <para>Sets the type of results that will be returned. Valid options are normal or nbest.</para>
  226. </description>
  227. </function>
  228. <function name="SPEECH" language="en_US">
  229. <synopsis>
  230. Gets information about speech recognition results.
  231. </synopsis>
  232. <syntax>
  233. <parameter name="argument" required="true">
  234. <enumlist>
  235. <enum name="status">
  236. <para>Returns <literal>1</literal> upon speech object existing,
  237. or <literal>0</literal> if not</para>
  238. </enum>
  239. <enum name="spoke">
  240. <para>Returns <literal>1</literal> if spoker spoke,
  241. or <literal>0</literal> if not</para>
  242. </enum>
  243. <enum name="results">
  244. <para>Returns number of results that were recognized.</para>
  245. </enum>
  246. </enumlist>
  247. </parameter>
  248. </syntax>
  249. <description>
  250. <para>Gets information about speech recognition results.</para>
  251. </description>
  252. </function>
  253. ***/
  254. /*! \brief Helper function used by datastores to destroy the speech structure upon hangup */
  255. static void destroy_callback(void *data)
  256. {
  257. struct ast_speech *speech = (struct ast_speech*)data;
  258. if (speech == NULL) {
  259. return;
  260. }
  261. /* Deallocate now */
  262. ast_speech_destroy(speech);
  263. return;
  264. }
  265. /*! \brief Static structure for datastore information */
  266. static const struct ast_datastore_info speech_datastore = {
  267. .type = "speech",
  268. .destroy = destroy_callback
  269. };
  270. /*! \brief Helper function used to find the speech structure attached to a channel */
  271. static struct ast_speech *find_speech(struct ast_channel *chan)
  272. {
  273. struct ast_speech *speech = NULL;
  274. struct ast_datastore *datastore = NULL;
  275. if (!chan) {
  276. return NULL;
  277. }
  278. ast_channel_lock(chan);
  279. datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
  280. ast_channel_unlock(chan);
  281. if (datastore == NULL) {
  282. return NULL;
  283. }
  284. speech = datastore->data;
  285. return speech;
  286. }
  287. /*!
  288. * \internal
  289. * \brief Destroy the speech datastore on the given channel.
  290. *
  291. * \param chan Channel to destroy speech datastore.
  292. *
  293. * \retval 0 on success.
  294. * \retval -1 not found.
  295. */
  296. static int speech_datastore_destroy(struct ast_channel *chan)
  297. {
  298. struct ast_datastore *datastore;
  299. int res;
  300. ast_channel_lock(chan);
  301. datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL);
  302. if (datastore) {
  303. ast_channel_datastore_remove(chan, datastore);
  304. }
  305. ast_channel_unlock(chan);
  306. if (datastore) {
  307. ast_datastore_free(datastore);
  308. res = 0;
  309. } else {
  310. res = -1;
  311. }
  312. return res;
  313. }
  314. /* Helper function to find a specific speech recognition result by number and nbest alternative */
  315. static struct ast_speech_result *find_result(struct ast_speech_result *results, char *result_num)
  316. {
  317. struct ast_speech_result *result = results;
  318. char *tmp = NULL;
  319. int nbest_num = 0, wanted_num = 0, i = 0;
  320. if (!result) {
  321. return NULL;
  322. }
  323. if ((tmp = strchr(result_num, '/'))) {
  324. *tmp++ = '\0';
  325. nbest_num = atoi(result_num);
  326. wanted_num = atoi(tmp);
  327. } else {
  328. wanted_num = atoi(result_num);
  329. }
  330. do {
  331. if (result->nbest_num != nbest_num)
  332. continue;
  333. if (i == wanted_num)
  334. break;
  335. i++;
  336. } while ((result = AST_LIST_NEXT(result, list)));
  337. return result;
  338. }
  339. /*! \brief SPEECH_SCORE() Dialplan Function */
  340. static int speech_score(struct ast_channel *chan, const char *cmd, char *data,
  341. char *buf, size_t len)
  342. {
  343. struct ast_speech_result *result = NULL;
  344. struct ast_speech *speech = find_speech(chan);
  345. char tmp[128] = "";
  346. if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
  347. return -1;
  348. }
  349. snprintf(tmp, sizeof(tmp), "%d", result->score);
  350. ast_copy_string(buf, tmp, len);
  351. return 0;
  352. }
  353. static struct ast_custom_function speech_score_function = {
  354. .name = "SPEECH_SCORE",
  355. .read = speech_score,
  356. .write = NULL,
  357. };
  358. /*! \brief SPEECH_TEXT() Dialplan Function */
  359. static int speech_text(struct ast_channel *chan, const char *cmd, char *data,
  360. char *buf, size_t len)
  361. {
  362. struct ast_speech_result *result = NULL;
  363. struct ast_speech *speech = find_speech(chan);
  364. if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
  365. return -1;
  366. }
  367. if (result->text != NULL) {
  368. ast_copy_string(buf, result->text, len);
  369. } else {
  370. buf[0] = '\0';
  371. }
  372. return 0;
  373. }
  374. static struct ast_custom_function speech_text_function = {
  375. .name = "SPEECH_TEXT",
  376. .read = speech_text,
  377. .write = NULL,
  378. };
  379. /*! \brief SPEECH_GRAMMAR() Dialplan Function */
  380. static int speech_grammar(struct ast_channel *chan, const char *cmd, char *data,
  381. char *buf, size_t len)
  382. {
  383. struct ast_speech_result *result = NULL;
  384. struct ast_speech *speech = find_speech(chan);
  385. if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) {
  386. return -1;
  387. }
  388. if (result->grammar != NULL) {
  389. ast_copy_string(buf, result->grammar, len);
  390. } else {
  391. buf[0] = '\0';
  392. }
  393. return 0;
  394. }
  395. static struct ast_custom_function speech_grammar_function = {
  396. .name = "SPEECH_GRAMMAR",
  397. .read = speech_grammar,
  398. .write = NULL,
  399. };
  400. /*! \brief SPEECH_ENGINE() Dialplan Set Function */
  401. static int speech_engine_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  402. {
  403. struct ast_speech *speech = find_speech(chan);
  404. if (data == NULL || speech == NULL) {
  405. return -1;
  406. }
  407. ast_speech_change(speech, data, value);
  408. return 0;
  409. }
  410. /*! \brief SPEECH_ENGINE() Dialplan Get Function */
  411. static int speech_engine_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  412. {
  413. struct ast_speech *speech = find_speech(chan);
  414. if (!data || !speech) {
  415. return -1;
  416. }
  417. return ast_speech_get_setting(speech, data, buf, len);
  418. }
  419. static struct ast_custom_function speech_engine_function = {
  420. .name = "SPEECH_ENGINE",
  421. .read = speech_engine_read,
  422. .write = speech_engine_write,
  423. };
  424. /*! \brief SPEECH_RESULTS_TYPE() Dialplan Function */
  425. static int speech_results_type_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  426. {
  427. struct ast_speech *speech = find_speech(chan);
  428. if (data == NULL || speech == NULL)
  429. return -1;
  430. if (!strcasecmp(value, "normal"))
  431. ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL);
  432. else if (!strcasecmp(value, "nbest"))
  433. ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST);
  434. return 0;
  435. }
  436. static struct ast_custom_function speech_results_type_function = {
  437. .name = "SPEECH_RESULTS_TYPE",
  438. .read = NULL,
  439. .write = speech_results_type_write,
  440. };
  441. /*! \brief SPEECH() Dialplan Function */
  442. static int speech_read(struct ast_channel *chan, const char *cmd, char *data,
  443. char *buf, size_t len)
  444. {
  445. int results = 0;
  446. struct ast_speech_result *result = NULL;
  447. struct ast_speech *speech = find_speech(chan);
  448. char tmp[128] = "";
  449. /* Now go for the various options */
  450. if (!strcasecmp(data, "status")) {
  451. if (speech != NULL)
  452. ast_copy_string(buf, "1", len);
  453. else
  454. ast_copy_string(buf, "0", len);
  455. return 0;
  456. }
  457. /* Make sure we have a speech structure for everything else */
  458. if (speech == NULL) {
  459. return -1;
  460. }
  461. /* Check to see if they are checking for silence */
  462. if (!strcasecmp(data, "spoke")) {
  463. if (ast_test_flag(speech, AST_SPEECH_SPOKE))
  464. ast_copy_string(buf, "1", len);
  465. else
  466. ast_copy_string(buf, "0", len);
  467. } else if (!strcasecmp(data, "results")) {
  468. /* Count number of results */
  469. for (result = speech->results; result; result = AST_LIST_NEXT(result, list))
  470. results++;
  471. snprintf(tmp, sizeof(tmp), "%d", results);
  472. ast_copy_string(buf, tmp, len);
  473. } else {
  474. buf[0] = '\0';
  475. }
  476. return 0;
  477. }
  478. static struct ast_custom_function speech_function = {
  479. .name = "SPEECH",
  480. .read = speech_read,
  481. .write = NULL,
  482. };
  483. /*! \brief SpeechCreate() Dialplan Application */
  484. static int speech_create(struct ast_channel *chan, const char *data)
  485. {
  486. struct ast_speech *speech = NULL;
  487. struct ast_datastore *datastore = NULL;
  488. /* Request a speech object */
  489. speech = ast_speech_new(data, ast_channel_nativeformats(chan));
  490. if (speech == NULL) {
  491. /* Not available */
  492. pbx_builtin_setvar_helper(chan, "ERROR", "1");
  493. return 0;
  494. }
  495. datastore = ast_datastore_alloc(&speech_datastore, NULL);
  496. if (datastore == NULL) {
  497. ast_speech_destroy(speech);
  498. pbx_builtin_setvar_helper(chan, "ERROR", "1");
  499. return 0;
  500. }
  501. pbx_builtin_setvar_helper(chan, "ERROR", NULL);
  502. datastore->data = speech;
  503. ast_channel_lock(chan);
  504. ast_channel_datastore_add(chan, datastore);
  505. ast_channel_unlock(chan);
  506. return 0;
  507. }
  508. /*! \brief SpeechLoadGrammar(Grammar Name,Path) Dialplan Application */
  509. static int speech_load(struct ast_channel *chan, const char *vdata)
  510. {
  511. int res = 0;
  512. struct ast_speech *speech = find_speech(chan);
  513. char *data;
  514. AST_DECLARE_APP_ARGS(args,
  515. AST_APP_ARG(grammar);
  516. AST_APP_ARG(path);
  517. );
  518. data = ast_strdupa(vdata);
  519. AST_STANDARD_APP_ARGS(args, data);
  520. if (speech == NULL)
  521. return -1;
  522. if (args.argc != 2)
  523. return -1;
  524. /* Load the grammar locally on the object */
  525. res = ast_speech_grammar_load(speech, args.grammar, args.path);
  526. return res;
  527. }
  528. /*! \brief SpeechUnloadGrammar(Grammar Name) Dialplan Application */
  529. static int speech_unload(struct ast_channel *chan, const char *data)
  530. {
  531. int res = 0;
  532. struct ast_speech *speech = find_speech(chan);
  533. if (speech == NULL)
  534. return -1;
  535. /* Unload the grammar */
  536. res = ast_speech_grammar_unload(speech, data);
  537. return res;
  538. }
  539. /*! \brief SpeechDeactivateGrammar(Grammar Name) Dialplan Application */
  540. static int speech_deactivate(struct ast_channel *chan, const char *data)
  541. {
  542. int res = 0;
  543. struct ast_speech *speech = find_speech(chan);
  544. if (speech == NULL)
  545. return -1;
  546. /* Deactivate the grammar on the speech object */
  547. res = ast_speech_grammar_deactivate(speech, data);
  548. return res;
  549. }
  550. /*! \brief SpeechActivateGrammar(Grammar Name) Dialplan Application */
  551. static int speech_activate(struct ast_channel *chan, const char *data)
  552. {
  553. int res = 0;
  554. struct ast_speech *speech = find_speech(chan);
  555. if (speech == NULL)
  556. return -1;
  557. /* Activate the grammar on the speech object */
  558. res = ast_speech_grammar_activate(speech, data);
  559. return res;
  560. }
  561. /*! \brief SpeechStart() Dialplan Application */
  562. static int speech_start(struct ast_channel *chan, const char *data)
  563. {
  564. int res = 0;
  565. struct ast_speech *speech = find_speech(chan);
  566. if (speech == NULL)
  567. return -1;
  568. ast_speech_start(speech);
  569. return res;
  570. }
  571. /*! \brief SpeechProcessingSound(Sound File) Dialplan Application */
  572. static int speech_processing_sound(struct ast_channel *chan, const char *data)
  573. {
  574. int res = 0;
  575. struct ast_speech *speech = find_speech(chan);
  576. if (speech == NULL)
  577. return -1;
  578. if (speech->processing_sound != NULL) {
  579. ast_free(speech->processing_sound);
  580. speech->processing_sound = NULL;
  581. }
  582. speech->processing_sound = ast_strdup(data);
  583. return res;
  584. }
  585. /*! \brief Helper function used by speech_background to playback a soundfile */
  586. static int speech_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
  587. {
  588. struct ast_filestream *fs = NULL;
  589. if (!(fs = ast_openstream(chan, filename, preflang)))
  590. return -1;
  591. if (ast_applystream(chan, fs))
  592. return -1;
  593. ast_playstream(fs);
  594. return 0;
  595. }
  596. enum {
  597. SB_OPT_NOANSWER = (1 << 0),
  598. };
  599. AST_APP_OPTIONS(speech_background_options, BEGIN_OPTIONS
  600. AST_APP_OPTION('n', SB_OPT_NOANSWER),
  601. END_OPTIONS );
  602. /*! \brief SpeechBackground(Sound File,Timeout) Dialplan Application */
  603. static int speech_background(struct ast_channel *chan, const char *data)
  604. {
  605. unsigned int timeout = 0;
  606. int res = 0, done = 0, started = 0, quieted = 0, max_dtmf_len = 0;
  607. struct ast_speech *speech = find_speech(chan);
  608. struct ast_frame *f = NULL;
  609. RAII_VAR(struct ast_format *, oldreadformat, NULL, ao2_cleanup);
  610. char dtmf[AST_MAX_EXTENSION] = "";
  611. struct timeval start = { 0, 0 }, current;
  612. char *parse, *filename_tmp = NULL, *filename = NULL, tmp[2] = "", dtmf_terminator = '#';
  613. const char *tmp2 = NULL;
  614. struct ast_flags options = { 0 };
  615. AST_DECLARE_APP_ARGS(args,
  616. AST_APP_ARG(soundfile);
  617. AST_APP_ARG(timeout);
  618. AST_APP_ARG(options);
  619. );
  620. parse = ast_strdupa(data);
  621. AST_STANDARD_APP_ARGS(args, parse);
  622. if (speech == NULL)
  623. return -1;
  624. if (!ast_strlen_zero(args.options)) {
  625. char *options_buf = ast_strdupa(args.options);
  626. ast_app_parse_options(speech_background_options, &options, NULL, options_buf);
  627. }
  628. /* If channel is not already answered, then answer it */
  629. if (ast_channel_state(chan) != AST_STATE_UP && !ast_test_flag(&options, SB_OPT_NOANSWER)
  630. && ast_answer(chan)) {
  631. return -1;
  632. }
  633. /* Record old read format */
  634. oldreadformat = ao2_bump(ast_channel_readformat(chan));
  635. /* Change read format to be signed linear */
  636. if (ast_set_read_format(chan, speech->format))
  637. return -1;
  638. if (!ast_strlen_zero(args.soundfile)) {
  639. /* Yay sound file */
  640. filename_tmp = ast_strdupa(args.soundfile);
  641. if (!ast_strlen_zero(args.timeout)) {
  642. if ((timeout = atof(args.timeout) * 1000.0) == 0)
  643. timeout = -1;
  644. } else
  645. timeout = 0;
  646. }
  647. /* See if the maximum DTMF length variable is set... we use a variable in case they want to carry it through their entire dialplan */
  648. ast_channel_lock(chan);
  649. if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_MAXLEN")) && !ast_strlen_zero(tmp2)) {
  650. max_dtmf_len = atoi(tmp2);
  651. }
  652. /* See if a terminator is specified */
  653. if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_TERMINATOR"))) {
  654. if (ast_strlen_zero(tmp2))
  655. dtmf_terminator = '\0';
  656. else
  657. dtmf_terminator = tmp2[0];
  658. }
  659. ast_channel_unlock(chan);
  660. /* Before we go into waiting for stuff... make sure the structure is ready, if not - start it again */
  661. if (speech->state == AST_SPEECH_STATE_NOT_READY || speech->state == AST_SPEECH_STATE_DONE) {
  662. ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
  663. ast_speech_start(speech);
  664. }
  665. /* Ensure no streams are currently running */
  666. ast_stopstream(chan);
  667. /* Okay it's streaming so go into a loop grabbing frames! */
  668. while (done == 0) {
  669. /* If the filename is null and stream is not running, start up a new sound file */
  670. if (!quieted && (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) && (filename = strsep(&filename_tmp, "&"))) {
  671. /* Discard old stream information */
  672. ast_stopstream(chan);
  673. /* Start new stream */
  674. speech_streamfile(chan, filename, ast_channel_language(chan));
  675. }
  676. /* Run scheduled stuff */
  677. ast_sched_runq(ast_channel_sched(chan));
  678. /* Yay scheduling */
  679. res = ast_sched_wait(ast_channel_sched(chan));
  680. if (res < 0)
  681. res = 1000;
  682. /* If there is a frame waiting, get it - if not - oh well */
  683. if (ast_waitfor(chan, res) > 0) {
  684. f = ast_read(chan);
  685. if (f == NULL) {
  686. /* The channel has hung up most likely */
  687. done = 3;
  688. break;
  689. }
  690. }
  691. /* Do timeout check (shared between audio/dtmf) */
  692. if ((!quieted || strlen(dtmf)) && started == 1) {
  693. current = ast_tvnow();
  694. if ((ast_tvdiff_ms(current, start)) >= timeout) {
  695. done = 1;
  696. if (f)
  697. ast_frfree(f);
  698. break;
  699. }
  700. }
  701. /* Do checks on speech structure to see if it's changed */
  702. ast_mutex_lock(&speech->lock);
  703. if (ast_test_flag(speech, AST_SPEECH_QUIET)) {
  704. if (ast_channel_stream(chan))
  705. ast_stopstream(chan);
  706. ast_clear_flag(speech, AST_SPEECH_QUIET);
  707. quieted = 1;
  708. }
  709. /* Check state so we can see what to do */
  710. switch (speech->state) {
  711. case AST_SPEECH_STATE_READY:
  712. /* If audio playback has stopped do a check for timeout purposes */
  713. if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL)
  714. ast_stopstream(chan);
  715. if (!quieted && ast_channel_stream(chan) == NULL && timeout && started == 0 && !filename_tmp) {
  716. if (timeout == -1) {
  717. done = 1;
  718. if (f)
  719. ast_frfree(f);
  720. break;
  721. }
  722. start = ast_tvnow();
  723. started = 1;
  724. }
  725. /* Write audio frame out to speech engine if no DTMF has been received */
  726. if (!strlen(dtmf) && f != NULL && f->frametype == AST_FRAME_VOICE) {
  727. ast_speech_write(speech, f->data.ptr, f->datalen);
  728. }
  729. break;
  730. case AST_SPEECH_STATE_WAIT:
  731. /* Cue up waiting sound if not already playing */
  732. if (!strlen(dtmf)) {
  733. if (ast_channel_stream(chan) == NULL) {
  734. if (speech->processing_sound != NULL) {
  735. if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) {
  736. speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan));
  737. }
  738. }
  739. } else if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) {
  740. ast_stopstream(chan);
  741. if (speech->processing_sound != NULL) {
  742. if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) {
  743. speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan));
  744. }
  745. }
  746. }
  747. }
  748. break;
  749. case AST_SPEECH_STATE_DONE:
  750. /* Now that we are done... let's switch back to not ready state */
  751. ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
  752. if (!strlen(dtmf)) {
  753. /* Copy to speech structure the results, if available */
  754. speech->results = ast_speech_results_get(speech);
  755. /* Break out of our background too */
  756. done = 1;
  757. /* Stop audio playback */
  758. if (ast_channel_stream(chan) != NULL) {
  759. ast_stopstream(chan);
  760. }
  761. }
  762. break;
  763. default:
  764. break;
  765. }
  766. ast_mutex_unlock(&speech->lock);
  767. /* Deal with other frame types */
  768. if (f != NULL) {
  769. /* Free the frame we received */
  770. switch (f->frametype) {
  771. case AST_FRAME_DTMF:
  772. if (dtmf_terminator != '\0' && f->subclass.integer == dtmf_terminator) {
  773. done = 1;
  774. } else {
  775. quieted = 1;
  776. if (ast_channel_stream(chan) != NULL) {
  777. ast_stopstream(chan);
  778. }
  779. if (!started) {
  780. /* Change timeout to be 5 seconds for DTMF input */
  781. timeout = (ast_channel_pbx(chan) && ast_channel_pbx(chan)->dtimeoutms) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
  782. started = 1;
  783. }
  784. start = ast_tvnow();
  785. snprintf(tmp, sizeof(tmp), "%c", f->subclass.integer);
  786. strncat(dtmf, tmp, sizeof(dtmf) - strlen(dtmf) - 1);
  787. /* If the maximum length of the DTMF has been reached, stop now */
  788. if (max_dtmf_len && strlen(dtmf) == max_dtmf_len)
  789. done = 1;
  790. }
  791. break;
  792. case AST_FRAME_CONTROL:
  793. switch (f->subclass.integer) {
  794. case AST_CONTROL_HANGUP:
  795. /* Since they hung up we should destroy the speech structure */
  796. done = 3;
  797. default:
  798. break;
  799. }
  800. default:
  801. break;
  802. }
  803. ast_frfree(f);
  804. f = NULL;
  805. }
  806. }
  807. if (!ast_strlen_zero(dtmf)) {
  808. /* We sort of make a results entry */
  809. speech->results = ast_calloc(1, sizeof(*speech->results));
  810. if (speech->results != NULL) {
  811. ast_speech_dtmf(speech, dtmf);
  812. speech->results->score = 1000;
  813. speech->results->text = ast_strdup(dtmf);
  814. speech->results->grammar = ast_strdup("dtmf");
  815. }
  816. ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY);
  817. }
  818. /* See if it was because they hung up */
  819. if (done == 3) {
  820. speech_datastore_destroy(chan);
  821. } else {
  822. /* Channel is okay so restore read format */
  823. ast_set_read_format(chan, oldreadformat);
  824. }
  825. return 0;
  826. }
  827. /*! \brief SpeechDestroy() Dialplan Application */
  828. static int speech_destroy(struct ast_channel *chan, const char *data)
  829. {
  830. if (!chan) {
  831. return -1;
  832. }
  833. return speech_datastore_destroy(chan);
  834. }
  835. static int unload_module(void)
  836. {
  837. int res = 0;
  838. res = ast_unregister_application("SpeechCreate");
  839. res |= ast_unregister_application("SpeechLoadGrammar");
  840. res |= ast_unregister_application("SpeechUnloadGrammar");
  841. res |= ast_unregister_application("SpeechActivateGrammar");
  842. res |= ast_unregister_application("SpeechDeactivateGrammar");
  843. res |= ast_unregister_application("SpeechStart");
  844. res |= ast_unregister_application("SpeechBackground");
  845. res |= ast_unregister_application("SpeechDestroy");
  846. res |= ast_unregister_application("SpeechProcessingSound");
  847. res |= ast_custom_function_unregister(&speech_function);
  848. res |= ast_custom_function_unregister(&speech_score_function);
  849. res |= ast_custom_function_unregister(&speech_text_function);
  850. res |= ast_custom_function_unregister(&speech_grammar_function);
  851. res |= ast_custom_function_unregister(&speech_engine_function);
  852. res |= ast_custom_function_unregister(&speech_results_type_function);
  853. return res;
  854. }
  855. static int load_module(void)
  856. {
  857. int res = 0;
  858. res = ast_register_application_xml("SpeechCreate", speech_create);
  859. res |= ast_register_application_xml("SpeechLoadGrammar", speech_load);
  860. res |= ast_register_application_xml("SpeechUnloadGrammar", speech_unload);
  861. res |= ast_register_application_xml("SpeechActivateGrammar", speech_activate);
  862. res |= ast_register_application_xml("SpeechDeactivateGrammar", speech_deactivate);
  863. res |= ast_register_application_xml("SpeechStart", speech_start);
  864. res |= ast_register_application_xml("SpeechBackground", speech_background);
  865. res |= ast_register_application_xml("SpeechDestroy", speech_destroy);
  866. res |= ast_register_application_xml("SpeechProcessingSound", speech_processing_sound);
  867. res |= ast_custom_function_register(&speech_function);
  868. res |= ast_custom_function_register(&speech_score_function);
  869. res |= ast_custom_function_register(&speech_text_function);
  870. res |= ast_custom_function_register(&speech_grammar_function);
  871. res |= ast_custom_function_register(&speech_engine_function);
  872. res |= ast_custom_function_register(&speech_results_type_function);
  873. return res;
  874. }
  875. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Dialplan Speech Applications",
  876. .support_level = AST_MODULE_SUPPORT_CORE,
  877. .load = load_module,
  878. .unload = unload_module,
  879. );