forked from googlearchive/appengine-vm-fortunespeak-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synth.py
36 lines (33 loc) · 1.01 KB
/
synth.py
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
from pyttsx.drivers import _espeak
import ctypes
import wave
import time
import threading
import StringIO
class Synth(object):
_done = False
def __init__(self):
self.rate = _espeak.Initialize(_espeak.AUDIO_OUTPUT_RETRIEVAL, 1000)
assert self.rate != -1, 'could not initialize espeak'
_espeak.SetSynthCallback(self)
self.lock = threading.Lock()
def __call__(self, wav, numsamples, events):
if self._done:
return 0
data = ctypes.string_at(wav, numsamples*2)
if len(data) == 0:
self._done = True
return 0
self.wav.writeframes(data)
return 0
def say(self, say, out):
with self.lock:
self.wav = wave.open(out, 'w')
self.wav.setnchannels(1)
self.wav.setsampwidth(2)
self.wav.setframerate(self.rate)
self._done = False
_espeak.Synth(say)
while not self._done:
time.sleep(0)
self.wav.close()