-
Notifications
You must be signed in to change notification settings - Fork 990
Expand file tree
/
Copy pathplugin.py
More file actions
186 lines (148 loc) · 4.68 KB
/
Copy pathplugin.py
File metadata and controls
186 lines (148 loc) · 4.68 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
import abc
import tempfile
import wave
import mad
from . import paths
from . import vocabcompiler
from . import audioengine
from . import i18n
class GenericPlugin(object):
def __init__(self, info, config):
self._plugin_config = config
self._plugin_info = info
@property
def profile(self):
# FIXME: Remove this in favor of something better
return self._plugin_config
@property
def info(self):
return self._plugin_info
class AudioEnginePlugin(GenericPlugin, audioengine.AudioEngine):
pass
class SpeechHandlerPlugin(GenericPlugin, i18n.GettextMixin):
"""
Generic parent class for SpeechHandlingPlugins
"""
__metaclass__ = abc.ABCMeta
def __init__(self, info, config, tti_plugin, mic):
"""
Instantiates a new generic SpeechhandlerPlugin instance. Requires a tti_plugin and a mic
instance.
"""
GenericPlugin.__init__(self, info, config)
i18n.GettextMixin.__init__(
self, self.info.translations, self.profile)
self._tti_plugin = tti_plugin
#self._tti_plugin = tti_plugin_info.plugin_class(tti_plugin_info, self._plugin_config)
self._mic = mic
# @classmethod
# def init(self, *args, **kwargs):
# """
# Initiate Plugin, e.g. do some runtime preparation stuff
#
# Arguments:
# """
# self._tti_plugin.init(self, *args, **kwargs)
@classmethod
def get_phrases(self):
return self._tti_plugin.get_phrases(self)
@classmethod
@abc.abstractmethod
def handle(self, text, mic):
pass
@classmethod
def is_valid(self, text):
return self._tti_plugin.is_valid(self, text)
@classmethod
def check_phrase(self, text):
return self._tti_plugin.get_confidence(self, text)
def get_priority(self):
return 0
class TTIPlugin(GenericPlugin):
"""
Generic parent class for text-to-intent handler
"""
__metaclass__ = abc.ABCMeta
ACTIONS = []
WORDS = {}
def __init__(self, *args, **kwargs):
GenericPlugin.__init__(self, *args, **kwargs)
@classmethod
@abc.abstractmethod
def get_phrases(cls):
pass
@classmethod
@abc.abstractmethod
def get_intent(cls, phrase):
pass
@abc.abstractmethod
def is_valid(self, phrase):
pass
@classmethod
def get_confidence(self, phrase):
return self.is_valid(self, phrase)
@abc.abstractmethod
def get_actionlist(self, phrase):
pass
class STTPlugin(GenericPlugin):
def __init__(self, *args, **kwargs):
GenericPlugin.__init__(self, *args, **kwargs)
self._vocabulary_phrases = None
self._vocabulary_name = None
self._vocabulary_compiled = False
self._vocabulary_path = None
def init(self, name, phrases):
self._vocabulary_phrases = phrases
self._vocabulary_name = name
def compile_vocabulary(self, compilation_func):
if self._vocabulary_compiled:
raise RuntimeError("Vocabulary has already been compiled!")
try:
language = self.profile['language']
except KeyError:
language = None
if not language:
language = 'en-US'
vocabulary = vocabcompiler.VocabularyCompiler(
self.info.name, self._vocabulary_name,
path=paths.config('vocabularies', language))
if not vocabulary.matches_phrases(self._vocabulary_phrases):
vocabulary.compile(
self.profile, compilation_func, self._vocabulary_phrases)
self._vocabulary_path = vocabulary.path
return self._vocabulary_path
@property
def vocabulary_path(self):
return self._vocabulary_path
@classmethod
@abc.abstractmethod
def is_available(cls):
return True
@abc.abstractmethod
def transcribe(self, fp):
pass
class TTSPlugin(GenericPlugin):
"""
Generic parent class for all speakers
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def say(self, phrase, *args):
pass
def mp3_to_wave(self, filename):
mf = mad.MadFile(filename)
with tempfile.SpooledTemporaryFile() as f:
wav = wave.open(f, mode='wb')
wav.setframerate(mf.samplerate())
wav.setnchannels(1 if mf.mode() == mad.MODE_SINGLE_CHANNEL else 2)
# 4L is the sample width of 32 bit audio
wav.setsampwidth(4)
frame = mf.read()
while frame is not None:
wav.writeframes(frame)
frame = mf.read()
wav.close()
f.seek(0)
data = f.read()
return data