diff options
author | Gonzalo Odiard <godiard@sugarlabs.org> | 2011-02-24 20:30:58 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@sugarlabs.org> | 2011-02-24 20:30:58 (GMT) |
commit | 2123bbd08ca8a9f3852832570b205a5a972a3055 (patch) | |
tree | 2019e84561769109385209ee345fc33f7c471cd0 /epubadapter.py | |
parent | 9202daa7d92ada1f1536d812b0e1a62af9366192 (diff) |
Initial implementation of text to speech in epub backend
No highlight of spoken word yet.
Diffstat (limited to 'epubadapter.py')
-rw-r--r-- | epubadapter.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/epubadapter.py b/epubadapter.py index 2b1b597..ab6eb01 100644 --- a/epubadapter.py +++ b/epubadapter.py @@ -2,6 +2,9 @@ import gobject import logging import epubview +import speech + +from cStringIO import StringIO _logger = logging.getLogger('read-activity') @@ -18,9 +21,14 @@ class EpubViewer(epubview.EpubView): activity._hbox.pack_start(self, expand=True, fill=True) self.show_all() + # text to speech initialization + self.current_word = 0 + self.word_tuples = [] def load_document(self, file_path): self.set_document(EpubDocument(self, file_path.replace('file://', ''))) + speech.highlight_cb = self.highlight_next_word + speech.end_text_cb = self.get_more_text def load_metadata(self, activity): @@ -50,7 +58,32 @@ class EpubViewer(epubview.EpubView): return False def can_do_text_to_speech(self): - return False + return True + + def get_marked_words(self): + "Adds a mark between each word of text." + i = self.current_word + file_str = StringIO() + file_str.write('<speak> ') + end_range = i + 40 + if end_range > len(self.word_tuples): + end_range = len(self.word_tuples) + for word_tuple in self.word_tuples[self.current_word:end_range]: + file_str.write('<mark name="' + str(i) + '"/>' + word_tuple[2]) + i = i + 1 + file_str.write('</speak>') + return file_str.getvalue() + + def get_more_text(self): + self.current_word = self.current_word + 1 + if self.current_word < len(self.word_tuples): + speech.stop() + more_text = self.get_marked_words() + speech.play(more_text) + + def highlight_next_word(self, word_count): + self.current_word = word_count + return True def connect_zoom_handler(self, handler): self._zoom_handler = handler |