app_speech_utils.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. .nonoptreq = "res_speech",
  880. );