I'm using an exact copy of the code from here. Every time I run the code, the UI appears and things seem to be doing what they should. Except when I click the "Speak" button, it closes and the terminal prints out the following:
Process returned -11 (0x-b)
No errors appear. It closes as if it was meant to (I know it wasn't). Any idea on how to fix this?
(edit) Here's the complete code that I'm trying to execute
from gi import pygtkcompat
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
GObject.threads_init()
Gst.init(None)
gst = Gst
print("Using pygtkcompat and Gst from gi")
pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0')
import gtk
class DemoApp(object):
"""GStreamer/PocketSphinx Demo Application"""
def __init__(self):
"""Initialize a DemoApp object"""
self.init_gui()
self.init_gst()
def init_gui(self):
"""Initialize the GUI components"""
self.window = gtk.Window()
self.window.connect("delete-event", gtk.main_quit)
self.window.set_default_size(400,200)
self.window.set_border_width(10)
vbox = gtk.VBox()
self.textbuf = gtk.TextBuffer()
self.text = gtk.TextView(buffer=self.textbuf)
self.text.set_wrap_mode(gtk.WRAP_WORD)
vbox.pack_start(self.text)
self.button = gtk.ToggleButton("Speak")
self.button.connect('clicked', self.button_clicked)
vbox.pack_start(self.button, False, False, 5)
self.window.add(vbox)
self.window.show_all()
def init_gst(self):
"""Initialize the speech components"""
self.pipeline = gst.parse_launch('autoaudiosrc ! audioconvert ! audioresample ! pocketsphinx ! fakesink')
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::element', self.element_message)
self.pipeline.set_state(gst.State.PAUSED)
def element_message(self, bus, msg):
"""Receive element messages from the bus."""
msgtype = msg.get_structure().get_name()
if msgtype != 'pocketsphinx':
return
if msg.get_structure().get_value('final'):
self.final_result(msg.get_structure().get_value('hypothesis'),
msg.get_structure().get_value('confidence'))
self.pipeline.set_state(gst.State.PAUSED)
self.button.set_active(False)
elif msg.get_structure().get_value('hypothesis'):
self.partial_result(msg.get_structure().get_value('hypothesis'))
def partial_result(self, hyp):
"""Delete any previous selection, insert text and select it."""
# All this stuff appears as one single action
self.textbuf.begin_user_action()
self.textbuf.delete_selection(True, self.text.get_editable())
self.textbuf.insert_at_cursor(hyp)
ins = self.textbuf.get_insert()
iter = self.textbuf.get_iter_at_mark(ins)
iter.backward_chars(len(hyp))
self.textbuf.move_mark(ins, iter)
self.textbuf.end_user_action()
def final_result(self, hyp, confidence):
"""Insert the final result."""
# All this stuff appears as one single action
self.textbuf.begin_user_action()
self.textbuf.delete_selection(True, self.text.get_editable())
self.textbuf.insert_at_cursor(hyp)
self.textbuf.end_user_action()
def button_clicked(self, button):
"""Handle button presses."""
if button.get_active():
button.set_label("Stop")
self.pipeline.set_state(gst.State.PLAYING)
else:
button.set_label("Speak")
self.pipeline.set_state(gst.State.PAUSED)
app = DemoApp()
gtk.main()
I am posting this as an answer to get proper formatting. There is no guarantee this will work for you (or anyone else) !!!
I also upgraded it to full Gtk3.
import gi
gi.require_version('Gst', '1.0')
#gi.require_version('GES', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, Gst#, GES
Gst.init(None)
class DemoApp(object):
"""GStreamer/PocketSphinx Demo Application"""
def __init__(self):
"""Initialize a DemoApp object"""
self.init_gui()
self.init_gst()
def init_gui(self):
"""Initialize the GUI components"""
self.window = Gtk.Window()
self.window.connect("delete-event", Gtk.main_quit)
self.window.set_default_size(400,200)
self.window.set_border_width(10)
vbox = Gtk.Box(expand = True)
vbox.set_orientation(Gtk.Orientation.VERTICAL)
self.textbuf = Gtk.TextBuffer()
self.text = Gtk.TextView(buffer=self.textbuf)
self.text.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
vbox.pack_start(self.text, True, True, 0)
self.button = Gtk.ToggleButton("Speak")
self.button.connect('clicked', self.button_clicked)
vbox.pack_start(self.button, False, False, 5)
self.window.add(vbox)
self.window.show_all()
def init_gst(self):
"""Initialize the speech components"""
#self.pipeline = Gst.parse_launch('autoaudiosrc ! audioconvert ! audioresample ! pocketsphinx ! fakesink')
self.pipeline = Gst.Pipeline()
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::element', self.element_message)
self.pipeline.set_state(Gst.State.PAUSED)
def element_message(self, bus, msg):
"""Receive element messages from the bus."""
msgtype = msg.get_structure().get_name()
if msgtype != 'pocketsphinx':
return
if msg.get_structure().get_value('final'):
self.final_result(msg.get_structure().get_value('hypothesis'),
msg.get_structure().get_value('confidence'))
self.pipeline.set_state(Gst.State.PAUSED)
self.button.set_active(False)
elif msg.get_structure().get_value('hypothesis'):
self.partial_result(msg.get_structure().get_value('hypothesis'))
def partial_result(self, hyp):
"""Delete any previous selection, insert text and select it."""
# All this stuff appears as one single action
self.textbuf.begin_user_action()
self.textbuf.delete_selection(True, self.text.get_editable())
self.textbuf.insert_at_cursor(hyp)
ins = self.textbuf.get_insert()
iter = self.textbuf.get_iter_at_mark(ins)
iter.backward_chars(len(hyp))
self.textbuf.move_mark(ins, iter)
self.textbuf.end_user_action()
def final_result(self, hyp, confidence):
"""Insert the final result."""
# All this stuff appears as one single action
self.textbuf.begin_user_action()
self.textbuf.delete_selection(True, self.text.get_editable())
self.textbuf.insert_at_cursor(hyp)
self.textbuf.end_user_action()
def button_clicked(self, button):
"""Handle button presses."""
if button.get_active():
button.set_label("Stop")
#self.pipeline.set_mode(GES.PipelineFlags.RENDER)
self.pipeline.set_state(Gst.State.PLAYING)
else:
button.set_label("Speak")
self.pipeline.set_state(Gst.State.PAUSED)
app = DemoApp()
Gtk.main()
Other notes:
(1 when I run your original code I get a Segmentation Fault. I suspect your OS is returning a signal -11 instead of segfaulting. Searching Google for Gst.State.PLAYING Segmentation Fault gets a lot of interesting results.
(2 I have no experience in GStreamer, so I can't help you a lot on this problem. I just thought I would post my results and my 2 cents.
(3 Some of this code comes from https://github.com/pitivi/gst-editing-services/issues/39