Advertisement
mc_teo

PocketSphinx

Apr 27th, 2011
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Copyright (c) 2008 Carnegie Mellon University.
  4. #
  5. # You may modify and redistribute this file under the same terms as
  6. # the CMU Sphinx system.  See
  7. # http://cmusphinx.sourceforge.net/html/LICENSE for more information.
  8.  
  9. import pygtk
  10. pygtk.require('2.0')
  11. import gtk
  12.  
  13. import gobject
  14. import pygst
  15. pygst.require('0.10')
  16. gobject.threads_init()
  17. import gst
  18.  
  19. class DemoApp(object):
  20.     """GStreamer/PocketSphinx Demo Application"""
  21.     def __init__(self):
  22.         """Initialize a DemoApp object"""
  23.         self.init_gui()
  24.         self.init_gst()
  25.  
  26.     def init_gui(self):
  27.         """Initialize the GUI components"""
  28.         self.window = gtk.Window()
  29.         self.window.connect("delete-event", gtk.main_quit)
  30.         self.window.set_default_size(400,200)
  31.         self.window.set_border_width(10)
  32.         vbox = gtk.VBox()
  33.         self.textbuf = gtk.TextBuffer()
  34.         self.text = gtk.TextView(self.textbuf)
  35.         self.text.set_wrap_mode(gtk.WRAP_WORD)
  36.         vbox.pack_start(self.text)
  37.         self.button = gtk.ToggleButton("Speak")
  38.         self.button.connect('clicked', self.button_clicked)
  39.         vbox.pack_start(self.button, False, False, 5)
  40.         self.window.add(vbox)
  41.         self.window.show_all()
  42.  
  43.     def init_gst(self):
  44.         """Initialize the speech components"""
  45.         self.pipeline = gst.parse_launch('pulsesrc ! audioconvert ! audioresample '
  46.                                          + '! vader name=vad auto-threshold=true '
  47.                                          + '! pocketsphinx name=asr ! fakesink')
  48.         asr = self.pipeline.get_by_name('asr')
  49.         asr.connect('partial_result', self.asr_partial_result)
  50.         asr.connect('result', self.asr_result)
  51.         asr.set_property('configured', True)
  52.  
  53.         bus = self.pipeline.get_bus()
  54.         bus.add_signal_watch()
  55.         bus.connect('message::application', self.application_message)
  56.  
  57.         self.pipeline.set_state(gst.STATE_PAUSED)
  58.  
  59.     def asr_partial_result(self, asr, text, uttid):
  60.         """Forward partial result signals on the bus to the main thread."""
  61.         struct = gst.Structure('partial_result')
  62.         struct.set_value('hyp', text)
  63.         struct.set_value('uttid', uttid)
  64.         asr.post_message(gst.message_new_application(asr, struct))
  65.  
  66.     def asr_result(self, asr, text, uttid):
  67.         """Forward result signals on the bus to the main thread."""
  68.         struct = gst.Structure('result')
  69.         struct.set_value('hyp', text)
  70.         struct.set_value('uttid', uttid)
  71.         asr.post_message(gst.message_new_application(asr, struct))
  72.  
  73.     def application_message(self, bus, msg):
  74.         """Receive application messages from the bus."""
  75.         msgtype = msg.structure.get_name()
  76.         if msgtype == 'partial_result':
  77.             self.partial_result(msg.structure['hyp'], msg.structure['uttid'])
  78.         elif msgtype == 'result':
  79.             self.final_result(msg.structure['hyp'], msg.structure['uttid'])
  80.             self.pipeline.set_state(gst.STATE_PAUSED)
  81.             self.button.set_active(False)
  82.  
  83.     def partial_result(self, hyp, uttid):
  84.         """Delete any previous selection, insert text and select it."""
  85.         # All this stuff appears as one single action
  86.         print "Partial Result:", hyp
  87.         self.textbuf.begin_user_action()
  88.         self.textbuf.delete_selection(True, self.text.get_editable())
  89.         self.textbuf.insert_at_cursor(hyp)
  90.         ins = self.textbuf.get_insert()
  91.         iter = self.textbuf.get_iter_at_mark(ins)
  92.         iter.backward_chars(len(hyp))
  93.         self.textbuf.move_mark(ins, iter)
  94.         self.textbuf.end_user_action()
  95.  
  96.     def final_result(self, hyp, uttid):
  97.         """Insert the final result."""
  98.         # All this stuff appears as one single action
  99.         print "Final Result:", hyp
  100.         self.textbuf.begin_user_action()
  101.         self.textbuf.delete_selection(True, self.text.get_editable())
  102.         self.textbuf.insert_at_cursor(hyp)
  103.         self.textbuf.end_user_action()
  104.  
  105.     def button_clicked(self, button):
  106.         """Handle button presses."""
  107.         if button.get_active():
  108.             button.set_label("Stop")
  109.             self.pipeline.set_state(gst.STATE_PLAYING)
  110.         else:
  111.             button.set_label("Speak")
  112.             vader = self.pipeline.get_by_name('vad')
  113.             vader.set_property('silent', True)
  114.  
  115. app = DemoApp()
  116. gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement