~ubuntu-branches/ubuntu/vivid/cicero/vivid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8
# This file is part of Cicero TTS.
#   Cicero TTS: A Small, Fast and Free Text-To-Speech Engine.
#   Copyright (C) 2003-2008 Nicolas Pitre  <nico@cam.org>
#   Copyright (C) 2003-2008 Stéphane Doyon <s.doyon@videotron.ca>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2.
#   See the accompanying COPYING file for more details.
#
#   This program comes with ABSOLUTELY NO WARRANTY.

# This module handles a simplisticshell-like interface to TTS.
# Text received on stdin is spoken out. Each word is written back 
# to stdout after it has been spoken.
# An empty line causes a shutup/mute command.
# EOF causes the tts program to exit.
# This interface is meant to be simple, not feature-full.

import os, sys, fcntl, errno

import setencoding
import tracing

def trace(msg):
    mod = 'shell'
    tracing.trace(mod+': '+msg)

class AppFeed:
    def __init__(self):
        sys.stdin = os.fdopen(sys.stdin.fileno(), 'r', 0)
        fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NONBLOCK)
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
        sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
        setencoding.stdfilesEncoding()
        self.pendingTxt = ''
        self.processedTxt = ''
        self.lastInx = 0
    def selectfd(self):
        return sys.stdin
    def sendIndex(self, inx):
        n = inx-self.lastInx
        if n>0 and self.processedTxt:
            self.lastInx = inx
            sys.stdout.write(self.processedTxt[:n])
            sys.stdout.flush()
            self.processedTxt = self.processedTxt[n:]
            if not self.processedTxt:
                self.lastInx = 0
                sys.stdout.write('\n')
    def handle(self):
        if self.pendingTxt:
            input = self.pendingTxt
            self.pendingTxt = ''
        else:
            try:
                input = sys.stdin.read()
            except IOError, x:
                if x.errno == errno.EAGAIN:
                    return 'M',None
                raise
            if input == '':
                trace('Exiting on EOF')
                sys.exit(0)
            if input[0] == '\n':
                trace('Got empty line: muting')
                self.pendingTxt = input[1:]
                self.processedTxt = ''
                self.lastInx = 0
                sys.stdout.write('\n')
                return 'M',None
        self.processedTxt += input
        return 'S',input
    def gotMore(self):
        return not not self.pendingTxt