Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/speech_dispatcher.py
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@member.fsf.org>2009-03-09 00:22:18 (GMT)
committer Aleksey Lim <alsroot@member.fsf.org>2009-03-09 00:22:18 (GMT)
commitdff5fae7bd71d477e93c5c344e6de35178c68982 (patch)
tree47c0c062e5d469c3abfa3b5dc7f0fe0a38f8dbdc /speech_dispatcher.py
parent18296c8d83f9ae63c7aaf0128f091da896c1e2f7 (diff)
Run readetexts with isolated speech-dispatcher
Diffstat (limited to 'speech_dispatcher.py')
-rw-r--r--speech_dispatcher.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/speech_dispatcher.py b/speech_dispatcher.py
new file mode 100644
index 0000000..da2fb6c
--- /dev/null
+++ b/speech_dispatcher.py
@@ -0,0 +1,104 @@
+# Copyright (C) 2008 James D. Simmons
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import gtk
+import time
+import threading
+import speechd
+import logging
+
+import speech
+
+logger = logging.getLogger('readetexts')
+
+RATE_MIN = -100
+RATE_MAX = 100
+RATE_DEFAULT = 0
+
+PITCH_MIN = -100
+PITCH_MAX = 100
+PITCH_DEFAULT = 0
+
+def voices():
+ try:
+ client = speechd.SSIPClient('readetextstest')
+ voices = client.list_synthesis_voices()
+ client.close()
+ return voices
+ except Exception, e:
+ logger.warning('speech dispatcher not started: %s' % e)
+ return []
+
+def say(words):
+ try:
+ client = speechd.SSIPClient('readetextstest')
+ client.set_rate(int(speech.rate))
+ client.set_pitch(int(speech.pitch))
+ client.set_language(speech.voice[1])
+ client.speak(words)
+ client.close()
+ except Exception, e:
+ logger.warning('speech dispatcher not running: %s' % e)
+
+def play(words, highlight_cb, reset_cb):
+ global thread
+ thread = EspeakThread(words, highlight_cb, reset_cb)
+ thread.start()
+
+class EspeakThread(threading.Thread):
+ def __init__(self, words, highlight_cb, reset_cb):
+ threading.Thread.__init__(self)
+ self.words = words
+ self.highlight_cb = highlight_cb
+ self.reset_cb = reset_cb
+
+ def run(self):
+ "This is the code that is executed when the start() method is called"
+ self.client = None
+ try:
+ self.client = speechd.SSIPClient('readetexts')
+ self.client._conn.send_command('SET', speechd.Scope.SELF, 'SSML_MODE', "ON")
+ if speech.voice:
+ self.client.set_language(speech.voice[1])
+ self.client.set_rate(speech.rate)
+ self.client.set_pitch(speech.pitch)
+ self.client.speak(self.words, self.next_word_cb, (speechd.CallbackType.INDEX_MARK,
+ speechd.CallbackType.END))
+ speech.done = False
+ while not speech.done:
+ time.sleep(0.1)
+ self.cancel()
+ self.client.close()
+ except Exception, e:
+ logger.warning('speech-dispatcher client not created: %s' % e)
+
+ def cancel(self):
+ if self.client:
+ try:
+ self.client.cancel()
+ except Exception, e:
+ logger.warning('speech dispatcher cancel failed: %s' % e)
+
+ def next_word_cb(self, type, **kargs):
+ if type == speechd.CallbackType.INDEX_MARK:
+ mark = kargs['index_mark']
+ word_count = int(mark)
+ gtk.gdk.threads_enter()
+ self.highlight_cb(word_count)
+ gtk.gdk.threads_leave()
+ elif type == speechd.CallbackType.END:
+ self.reset_cb()
+ speech.done = True