2
0

pizza.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Anthony Minessale II <anthm@freeswitch.org>
  27. *
  28. *
  29. * pizza.js ASR Demonstration Application
  30. *
  31. */
  32. include("js_modules/SpeechTools.jm");
  33. function on_dtmf(a, b, c) {}
  34. var dft_min = 100;
  35. var dft_confirm = 600;
  36. /***************** Initialize The Speech Detector *****************/
  37. var asr = new SpeechDetect(session, "lumenvox", "127.0.0.1");
  38. /***************** Be more verbose *****************/
  39. asr.debug = 1;
  40. /***************** Set audio params *****************/
  41. asr.setAudioBase("/root/pizza/");
  42. asr.setAudioExt(".wav");
  43. /***************** Unload the last grammar whenever we activate a new one *****************/
  44. asr.AutoUnload = true;
  45. /***************** Create And Configure The Pizza *****************/
  46. var pizza = new Object();
  47. /***************** Delivery Or Take-Out? *****************/
  48. pizza.orderObtainer = new SpeechObtainer(asr, 1, 5000);
  49. pizza.orderObtainer.setGrammar("order", "pizza/order.gram", "result", dft_min, dft_confirm, true);
  50. pizza.orderObtainer.setTopSound("GP-DeliveryorTakeout");
  51. pizza.orderObtainer.setBadSound("GP-NoDeliveryorTake-out");
  52. pizza.orderObtainer.addItem("Delivery,Pickup");
  53. /***************** What Size? *****************/
  54. pizza.sizeObtainer = new SpeechObtainer(asr, 1, 5000);
  55. pizza.sizeObtainer.setGrammar("size", "pizza/size.gram", "result", dft_min, dft_confirm, true);
  56. pizza.sizeObtainer.setTopSound("GP-Size");
  57. pizza.sizeObtainer.setBadSound("GP-NI");
  58. pizza.sizeObtainer.addItem("ExtraLarge,Large,Medium,Small,TotallyHumongous");
  59. /***************** What Type Of Crust? *****************/
  60. pizza.crustObtainer = new SpeechObtainer(asr, 1, 5000);
  61. pizza.crustObtainer.setGrammar("crust", "pizza/crust.gram", "result", dft_min, dft_confirm, true);
  62. pizza.crustObtainer.setTopSound("GP-Crust");
  63. pizza.crustObtainer.setBadSound("GP-NI");
  64. pizza.crustObtainer.addItem("HandTossed,Pan,Thin");
  65. /***************** Specialty Or Custom? *****************/
  66. pizza.specialtyObtainer = new SpeechObtainer(asr, 1, 5000);
  67. pizza.specialtyObtainer.setGrammar("specialty", "pizza/specialty.gram", "result", dft_min, dft_confirm, true);
  68. pizza.specialtyObtainer.setTopSound("GP-SpecialtyList");
  69. pizza.specialtyObtainer.setBadSound("GP-NI");
  70. pizza.specialtyObtainer.addItem("Hawaiian,MeatLovers,Pickle,Dali,Vegetarian");
  71. /***************** Which Specialty? *****************/
  72. pizza.typeObtainer = new SpeechObtainer(asr, 1, 5000);
  73. pizza.typeObtainer.setGrammar("type", "pizza/specialtyorcustom.gram", "result", dft_min, dft_confirm, true);
  74. pizza.typeObtainer.setTopSound("GP-SpecialtyorCustom");
  75. pizza.typeObtainer.setBadSound("GP-NI");
  76. pizza.typeObtainer.addItem("Specialty,Custom");
  77. /***************** What Toppings? *****************/
  78. pizza.toppingsObtainer = new SpeechObtainer(asr, 1, 5000);
  79. pizza.toppingsObtainer.setGrammar("toppings", "pizza/pizza.gram", "result.toppinglist.ingredient", dft_min, 400, true);
  80. pizza.toppingsObtainer.setTopSound("GP-Toppings");
  81. pizza.toppingsObtainer.setBadSound("GP-NI");
  82. pizza.toppingsObtainer.addItem("anchovie,artichoke,canadianbacon,everything,extracheese,garlic,goatcheese,bellpepper");
  83. pizza.toppingsObtainer.addItem("mango,mushroom,olives,onions,pepperoni,pickle,pineapple,salami,sausage,shrimp,spinach,ham");
  84. /***************** Change Delivery Or Size Or Crust, Add/Rem Toppings Or Start Over *****************/
  85. pizza.arsoObtainer = new SpeechObtainer(asr, 1, 5000);
  86. pizza.arsoObtainer.setGrammar("arso", "pizza/arso.gram", "result", 500, 250, true);
  87. pizza.arsoObtainer.setTopSound("GP-ARSO");
  88. pizza.arsoObtainer.setBadSound("GP-NI");
  89. pizza.arsoObtainer.addItem("delivery,size,crust,startover,add_topping,rem_topping");
  90. /***************** Yes? No? Maybe So? *****************/
  91. pizza.yesnoObtainer = new SpeechObtainer(asr, 1, 5000);
  92. pizza.yesnoObtainer.setGrammar("yesno", "pizza/yesno.gram", "result", 500, 250, true);
  93. pizza.yesnoObtainer.setBadSound("GP-NI");
  94. pizza.yesnoObtainer.addItem("yes,no");
  95. /***************** Get Some Information *****************/
  96. pizza.get = function(params, confirm) {
  97. for(;;) {
  98. if (!session.ready()) {
  99. return false;
  100. }
  101. var main_items = params.run();
  102. if (confirm && params.needConfirm) {
  103. pizza.yesnoObtainer.setTopSound("Confirm" + main_items[0]);
  104. var items = pizza.yesnoObtainer.run();
  105. if (items[0] == "yes") {
  106. break;
  107. }
  108. } else {
  109. break;
  110. }
  111. }
  112. return main_items;
  113. };
  114. /***************** Is This Right? *****************/
  115. pizza.check = function () {
  116. if (!session.ready()) {
  117. return false;
  118. }
  119. asr.streamFile("GP-You_ordered_a");
  120. asr.streamFile(pizza.size);
  121. asr.streamFile(pizza.crust);
  122. if (pizza.type == "Specialty") {
  123. asr.streamFile(pizza.specialty);
  124. asr.streamFile("pizza");
  125. } else {
  126. asr.streamFile("pizza");
  127. asr.streamFile("GP-With");
  128. for (key in pizza.toppings) {
  129. if (pizza.toppings[key] == "add") {
  130. asr.streamFile(key);
  131. }
  132. }
  133. }
  134. pizza.yesnoObtainer.setTopSound("GP-WasThisRight");
  135. items = pizza.yesnoObtainer.run();
  136. return items[0] == "yes" ? true : false;
  137. };
  138. /***************** Let's Remove The Toppings *****************/
  139. pizza.clearToppings = function() {
  140. if (!session.ready()) {
  141. return false;
  142. }
  143. if (pizza.toppings) {
  144. delete pizza.toppings;
  145. }
  146. pizza.have_toppings = false;
  147. pizza.toppings = new Array();
  148. }
  149. /***************** Clean Slate *****************/
  150. pizza.init = function() {
  151. if (!session.ready()) {
  152. return false;
  153. }
  154. pizza.add_rem = "add";
  155. pizza.order = pizza.size = pizza.crust = pizza.type = false;
  156. pizza.toppingsObtainer.setTopSound("GP-Toppings");
  157. pizza.specialty = false;
  158. pizza.clearToppings();
  159. pizza.said_greet = false;
  160. }
  161. /***************** Welcome! *****************/
  162. pizza.greet = function () {
  163. if (!session.ready()) {
  164. return false;
  165. }
  166. if (!pizza.said_greet) {
  167. asr.streamFile("GP-Greeting");
  168. pizza.said_greet = true;
  169. }
  170. };
  171. /***************** Collect Order Type *****************/
  172. pizza.getOrder = function() {
  173. if (!session.ready()) {
  174. return false;
  175. }
  176. if (!pizza.order) {
  177. var items = pizza.get(pizza.orderObtainer, true);
  178. pizza.order = items[0];
  179. }
  180. };
  181. /***************** Collect Size *****************/
  182. pizza.getSize = function() {
  183. if (!session.ready()) {
  184. return false;
  185. }
  186. if (!pizza.size) {
  187. var items = pizza.get(pizza.sizeObtainer, true);
  188. pizza.size = items[0];
  189. }
  190. };
  191. /***************** Collect Crust *****************/
  192. pizza.getCrust = function() {
  193. if (!session.ready()) {
  194. return false;
  195. }
  196. if (!pizza.crust) {
  197. var items = pizza.get(pizza.crustObtainer, true);
  198. pizza.crust = items[0];
  199. }
  200. };
  201. /***************** Collect Pizza Type *****************/
  202. pizza.getType = function() {
  203. if (!session.ready()) {
  204. return false;
  205. }
  206. if (!pizza.type) {
  207. var items = pizza.get(pizza.typeObtainer, true);
  208. pizza.type = items[0];
  209. }
  210. };
  211. /***************** Collect Toppings *****************/
  212. pizza.getToppings = function() {
  213. if (!session.ready()) {
  214. return false;
  215. }
  216. if (pizza.type == "Specialty" && !pizza.specialty) {
  217. var items = pizza.get(pizza.specialtyObtainer, true);
  218. pizza.specialty = items[0];
  219. pizza.have_toppings = true;
  220. } else if (!pizza.have_toppings) {
  221. toppings = pizza.get(pizza.toppingsObtainer, false);
  222. for(x = 0; x < toppings.length; x++) {
  223. pizza.toppings[toppings[x]] = pizza.add_rem;
  224. }
  225. pizza.have_toppings = true;
  226. }
  227. };
  228. /***************** Modify Pizza If You Don't Like It *****************/
  229. pizza.fix = function() {
  230. if (!session.ready()) {
  231. return false;
  232. }
  233. asr.streamFile("GP-Wanted-No");
  234. arso = pizza.get(pizza.arsoObtainer, false);
  235. for (x = 0; x < arso.length; x++) {
  236. if (arso[x] == "delivery") {
  237. pizza.order = false;
  238. } else if (arso[x] == "size") {
  239. pizza.size = false;
  240. } else if (arso[x] == "crust") {
  241. pizza.crust = false;
  242. } else if (arso[x] == "startover") {
  243. pizza.init();
  244. } else {
  245. if (pizza.type == "Specialty") {
  246. asr.streamFile("GP-ChangeSpec");
  247. pizza.type = false;
  248. pizza.clearToppings();
  249. } else {
  250. pizza.have_toppings = false;
  251. if (arso[x] == "add_topping") {
  252. pizza.add_rem = "add";
  253. pizza.toppingsObtainer.setTopSound("GP-Adding");
  254. } else {
  255. pizza.add_rem = "rem";
  256. pizza.toppingsObtainer.setTopSound("GP-Remove");
  257. }
  258. }
  259. }
  260. }
  261. };
  262. /***************** Tie It All Together *****************/
  263. pizza.run = function() {
  264. pizza.init();
  265. for(;;) {
  266. if (!session.ready()) {
  267. break;
  268. }
  269. pizza.greet();
  270. pizza.getOrder();
  271. pizza.getSize();
  272. pizza.getCrust();
  273. pizza.getType();
  274. pizza.getToppings();
  275. if (pizza.check()) {
  276. asr.streamFile(pizza.order);
  277. break;
  278. } else {
  279. pizza.fix();
  280. }
  281. }
  282. };
  283. /***************** Begin Program *****************/
  284. session.answer();
  285. pizza.run();
  286. asr.stop();