diff options
author | James Simmons <jim@olpc2.simmons> | 2010-01-22 02:23:09 (GMT) |
---|---|---|
committer | James Simmons <jim@olpc2.simmons> | 2010-01-22 02:23:09 (GMT) |
commit | 2d31ebd3d24ebedacbe6dd57fd0acc2040651d34 (patch) | |
tree | f7ff4d7f2ae5cd9542de671222ddbf289432e80a | |
parent | 11e92f1347cc0be369704df45827d7d16c4b1a90 (diff) |
Clean up speak.py
-rwxr-xr-x | ReadEtextsTTS.py | 2 | ||||
-rw-r--r-- | speech.py | 103 |
2 files changed, 48 insertions, 57 deletions
diff --git a/ReadEtextsTTS.py b/ReadEtextsTTS.py index 7d8fcfe..1ae4399 100755 --- a/ReadEtextsTTS.py +++ b/ReadEtextsTTS.py @@ -1,6 +1,6 @@ #! /usr/bin/env python -# Copyright (C) 2008, 2009 James D. Simmons +# Copyright (C) 2010 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 @@ -1,4 +1,4 @@ -# Copyright (C) 2009 Aleksey S. Lim +# Copyright (C) 2010 Aleksey Lim and 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 @@ -18,96 +18,86 @@ import gst voice = 'default' pitch = 0 -rate = -20 +rate = -20 highlight_cb = None -def _message_cb(bus, message, pipe): - if message.type in (gst.MESSAGE_EOS, gst.MESSAGE_ERROR): - pipe.set_state(gst.STATE_NULL) - elif message.type == gst.MESSAGE_ELEMENT and \ - message.structure.get_name() == 'espeak-mark': - mark = message.structure['mark'] - highlight_cb(int(mark)) - def _create_pipe(): - pipe = gst.Pipeline('pipeline') + pipeline = 'espeak name=source ! autoaudiosink' + pipe = gst.parse_launch(pipeline) - source = gst.element_factory_make('espeak', 'source') - pipe.add(source) + def stop_cb(bus, message): + pipe.set_state(gst.STATE_NULL) - sink = gst.element_factory_make('autoaudiosink', 'sink') - pipe.add(sink) - source.link(sink) + def mark_cb(bus, message): + if message.structure.get_name() == 'espeak-mark': + mark = message.structure['mark'] + highlight_cb(int(mark)) bus = pipe.get_bus() bus.add_signal_watch() - bus.connect('message', _message_cb, pipe) + bus.connect('message::eos', stop_cb) + bus.connect('message::error', stop_cb) + bus.connect('message::element', mark_cb) + + return (pipe.get_by_name('source'), pipe) - return (source, pipe) +def _speech(source, pipe, words): + source.props.pitch = pitch + source.props.rate = rate + source.props.voice = voice + source.props.text = words; + pipe.set_state(gst.STATE_PLAYING) -def _speech(speaker, words): - speaker[0].props.pitch = pitch - speaker[0].props.rate = rate - speaker[0].props.voice = voice[1] - speaker[0].props.text = words; - speaker[1].set_state(gst.STATE_PLAYING) +info_source, info_pipe = _create_pipe() +play_source, play_pipe = _create_pipe() -info_speaker = _create_pipe() -play_speaker = _create_pipe() -play_speaker[0].props.track = 2 +# track for marks +play_source.props.track = 2 def voices(): - return info_speaker[0].props.voices + return info_source.props.voices def say(words): - _speech(info_speaker, words) + _speech(info_source, info_pipe, words) print words def play(words): - _speech(play_speaker, words) + _speech(play_source, play_pipe, words) def is_stopped(): - for i in play_speaker[1].get_state(): + for i in play_pipe.get_state(): if isinstance(i, gst.State) and i == gst.STATE_NULL: - return True + return True return False def stop(): - play_speaker[1].set_state(gst.STATE_NULL) + play_pipe.set_state(gst.STATE_NULL) def is_paused(): - for i in play_speaker[1].get_state(): + for i in play_pipe.get_state(): if isinstance(i, gst.State) and i == gst.STATE_PAUSED: - return True + return True return False def pause(): - play_speaker[1].set_state(gst.STATE_PAUSED) + play_pipe.set_state(gst.STATE_PAUSED) def rate_up(): global rate - rate = rate + 10 - if rate > 99: - rate = 99 + rate = min(99, rate + 10) def rate_down(): global rate - rate = rate - 10 - if rate < -99: - rate = -99 + rate = max(-99, rate - 10) def pitch_up(): global pitch - pitch = pitch + 10 - if pitch > 99: - pitch = 99 + pitch = min(99, pitch + 10) -def pitch_down(): +def pitch_down(): global pitch - pitch = pitch - 10 - if pitch < -99: - pitch = -99 + pitch = max(-99, pitch - 10) def prepare_highlighting(label_text): i = 0 @@ -116,26 +106,27 @@ def prepare_highlighting(label_text): word_end = 0 current_word = 0 word_tuples = [] - omitted = [' ', '\n', u'\r', '_', '[', '{', ']', '}', '|', '<', '>', '*', '+', '/', '\\' ] + omitted = [' ', '\n', u'\r', '_', '[', '{', ']', '}', '|', '<',\ + '>', '*', '+', '/', '\\' ] omitted_chars = set(omitted) while i < len(label_text): if label_text[i] not in omitted_chars: word_begin = i j = i - while j < len(label_text) and label_text[j] not in omitted_chars: - j = j + 1 - word_end = j - i = j + while j < len(label_text) and label_text[j] not in omitted_chars: + j = j + 1 + word_end = j + i = j word_t = (word_begin, word_end, label_text[word_begin: word_end].strip()) if word_t[2] != u'\r': - word_tuples.append(word_t) + word_tuples.append(word_t) i = i + 1 return word_tuples def add_word_marks(word_tuples): "Adds a mark between each word of text." i = 0 - marked_up_text = '<speak> ' + marked_up_text = '<speak> ' while i < len(word_tuples): word_t = word_tuples[i] marked_up_text = marked_up_text + '<mark name="' + str(i) + '"/>' + word_t[2] |