2
0

recipewizard.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from freeswitch import *
  2. from py_modules.speechtools import Grammar, SpeechDetect
  3. from py_modules.speechtools import SpeechObtainer
  4. import os
  5. VOICE_ENGINE = "cepstral"
  6. VOICE = "William"
  7. GRAMMAR_ROOT = "/usr/src/freeswitch_trunk/scripts"
  8. """
  9. Example speech recognition application in python.
  10. How to make this work:
  11. * Get mod_openmrcp working along with an MRCP asr server
  12. * Add /usr/src/freeswitch/scripts or equivalent to your PYTHONPATH
  13. * Restart freeswitch
  14. * Create $GRAMMAR_ROOT/mainmenu.xml from contents in mainmenu() comments
  15. """
  16. class RecipeWizard:
  17. def __init__(self, session):
  18. self.session = session
  19. self.session.set_tts_parms(VOICE_ENGINE, VOICE)
  20. self.main()
  21. def main(self):
  22. console_log("debug", "recipe wizard main()\n")
  23. self.speechdetect = SpeechDetect(self.session, "openmrcp", "127.0.0.1")
  24. self.speechobtainer = SpeechObtainer(speech_detect=self.speechdetect,
  25. required_phrases=1,
  26. wait_time=5000,
  27. max_tries=3)
  28. gfile = os.path.join(GRAMMAR_ROOT, "mainmenu.xml")
  29. self.grammar = Grammar("mainmenu", gfile, "input", 80, 90)
  30. self.speechobtainer.setGrammar(self.grammar)
  31. console_log("debug", "calling speechobtainer.run()\n")
  32. self.speechobtainer.detectSpeech()
  33. self.session.speak("Hello. Welcome to the recipe wizard. Drinks or food?")
  34. result = self.speechobtainer.run()
  35. console_log("debug", "speechobtainer.run() result: %s\n" % result)
  36. if result:
  37. self.session.speak("Received result. Result is: %s" % result[0])
  38. else:
  39. self.session.speak("Sorry, I did not hear you")
  40. console_log("debug", "speechobtainer.run() finished\n")
  41. def mainmenu():
  42. """
  43. <!DOCTYPE grammar PUBLIC "-//W3C//DTD GRAMMAR 1.0//EN"
  44. "http://www.w3.org/TR/speech-grammar/grammar.dtd">
  45. <grammar xmlns="http://www.w3.org/2001/06/grammar" xml:lang="en"
  46. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  47. xsi:schemaLocation="http://www.w3.org/2001/06/grammar
  48. http://www.w3.org/TR/speech-grammar/grammar.xsd"
  49. version="1.0" mode="voice" root="root">
  50. <rule id="root" scope="public">
  51. <rule id="main">
  52. <one-of>
  53. <item weight="10">drinks</item>
  54. <item weight="2">food</item>
  55. </one-of>
  56. </rule>
  57. </rule>
  58. </grammar>
  59. """
  60. pass
  61. def handler(uuid):
  62. session = PySession(uuid)
  63. session.answer()
  64. rw = RecipeWizard(session)
  65. session.hangup("1")