2017-10-04 06:28:44 +00:00
|
|
|
# Copyright 2017 Mycroft AI Inc.
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2016-05-20 14:16:01 +00:00
|
|
|
import subprocess
|
2017-09-18 19:14:21 +00:00
|
|
|
from time import time
|
|
|
|
|
2017-03-30 07:40:56 +00:00
|
|
|
import os.path
|
2016-09-16 18:12:43 +00:00
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
from mycroft import MYCROFT_ROOT_PATH
|
2016-06-06 20:08:37 +00:00
|
|
|
from mycroft.configuration import ConfigurationManager
|
2016-09-17 02:08:53 +00:00
|
|
|
from mycroft.tts import TTS, TTSValidator
|
2017-09-18 18:55:58 +00:00
|
|
|
from mycroft.util.log import LOG
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
2016-09-17 02:08:53 +00:00
|
|
|
config = ConfigurationManager.get().get("tts").get("mimic")
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2017-03-30 07:40:56 +00:00
|
|
|
BIN = config.get("path", os.path.join(MYCROFT_ROOT_PATH, 'mimic', 'bin',
|
|
|
|
'mimic'))
|
2017-06-07 10:00:29 +00:00
|
|
|
if not os.path.isfile(BIN):
|
|
|
|
# Search for mimic on the path
|
|
|
|
import distutils.spawn
|
|
|
|
BIN = distutils.spawn.find_executable("mimic")
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Mimic(TTS):
|
|
|
|
def __init__(self, lang, voice):
|
2016-09-16 18:12:43 +00:00
|
|
|
super(Mimic, self).__init__(lang, voice, MimicValidator(self))
|
2016-09-17 02:08:53 +00:00
|
|
|
self.init_args()
|
2017-05-08 06:56:56 +00:00
|
|
|
self.clear_cache()
|
2017-06-05 08:31:31 +00:00
|
|
|
self.type = 'wav'
|
2016-09-17 02:08:53 +00:00
|
|
|
|
|
|
|
def init_args(self):
|
2017-03-30 07:40:56 +00:00
|
|
|
self.args = [BIN, '-voice', self.voice, '-psdur']
|
2016-09-22 14:18:27 +00:00
|
|
|
stretch = config.get('duration_stretch', None)
|
|
|
|
if stretch:
|
|
|
|
self.args += ['--setf', 'duration_stretch=' + stretch]
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
def get_tts(self, sentence, wav_file):
|
2017-03-30 07:40:56 +00:00
|
|
|
# Generate WAV and phonemes
|
|
|
|
phonemes = subprocess.check_output(self.args + ['-o', wav_file,
|
|
|
|
'-t', sentence])
|
|
|
|
return wav_file, phonemes
|
|
|
|
|
2016-09-17 02:08:53 +00:00
|
|
|
def visime(self, output):
|
2017-06-04 06:07:37 +00:00
|
|
|
visimes = []
|
2016-09-26 22:11:18 +00:00
|
|
|
start = time()
|
2016-09-17 02:08:53 +00:00
|
|
|
pairs = output.split(" ")
|
|
|
|
for pair in pairs:
|
|
|
|
pho_dur = pair.split(":") # phoneme:duration
|
|
|
|
if len(pho_dur) == 2:
|
2017-06-05 08:31:31 +00:00
|
|
|
visimes.append((VISIMES.get(pho_dur[0], '4'),
|
|
|
|
float(pho_dur[1])))
|
2017-06-04 06:07:37 +00:00
|
|
|
return visimes
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MimicValidator(TTSValidator):
|
2016-09-16 18:12:43 +00:00
|
|
|
def __init__(self, tts):
|
|
|
|
super(MimicValidator, self).__init__(tts)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def validate_lang(self):
|
2017-06-07 10:00:29 +00:00
|
|
|
# TODO: Verify version of mimic can handle the requested language
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def validate_connection(self):
|
2016-05-20 14:16:01 +00:00
|
|
|
try:
|
|
|
|
subprocess.call([BIN, '--version'])
|
|
|
|
except:
|
2017-09-18 18:55:58 +00:00
|
|
|
LOG.info("Failed to find mimic at: " + BIN)
|
2016-05-20 22:15:53 +00:00
|
|
|
raise Exception(
|
2017-03-30 18:05:46 +00:00
|
|
|
'Mimic was not found. Run install-mimic.sh to install it.')
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def get_tts_class(self):
|
2016-05-20 14:16:01 +00:00
|
|
|
return Mimic
|
2016-09-17 02:08:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Mapping based on Jeffers phoneme to viseme map, seen in table 1 from:
|
|
|
|
# http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.221.6377&rep=rep1&type=pdf
|
|
|
|
#
|
|
|
|
# Mycroft unit visemes based on images found at:
|
|
|
|
# http://www.web3.lu/wp-content/uploads/2014/09/visemes.jpg
|
|
|
|
#
|
|
|
|
# Mapping was created partially based on the "12 mouth shapes visuals seen at:
|
|
|
|
# https://wolfpaulus.com/journal/software/lipsynchronization/
|
|
|
|
|
|
|
|
VISIMES = {
|
|
|
|
# /A group
|
|
|
|
'v': '5',
|
|
|
|
'f': '5',
|
|
|
|
# /B group
|
|
|
|
'uh': '2',
|
|
|
|
'w': '2',
|
|
|
|
'uw': '2',
|
|
|
|
'er': '2',
|
|
|
|
'r': '2',
|
|
|
|
'ow': '2',
|
|
|
|
# /C group
|
|
|
|
'b': '4',
|
|
|
|
'p': '4',
|
|
|
|
'm': '4',
|
|
|
|
# /D group
|
|
|
|
'aw': '1',
|
|
|
|
# /E group
|
|
|
|
'th': '3',
|
|
|
|
'dh': '3',
|
|
|
|
# /F group
|
|
|
|
'zh': '3',
|
|
|
|
'ch': '3',
|
|
|
|
'sh': '3',
|
|
|
|
'jh': '3',
|
|
|
|
# /G group
|
|
|
|
'oy': '6',
|
|
|
|
'ao': '6',
|
|
|
|
# /Hgroup
|
|
|
|
'z': '3',
|
|
|
|
's': '3',
|
|
|
|
# /I group
|
|
|
|
'ae': '0',
|
|
|
|
'eh': '0',
|
|
|
|
'ey': '0',
|
|
|
|
'ah': '0',
|
|
|
|
'ih': '0',
|
|
|
|
'y': '0',
|
|
|
|
'iy': '0',
|
|
|
|
'aa': '0',
|
|
|
|
'ay': '0',
|
|
|
|
'ax': '0',
|
|
|
|
'hh': '0',
|
|
|
|
# /J group
|
|
|
|
'n': '3',
|
|
|
|
't': '3',
|
|
|
|
'd': '3',
|
|
|
|
'l': '3',
|
|
|
|
# /K group
|
|
|
|
'g': '3',
|
|
|
|
'ng': '3',
|
|
|
|
'k': '3',
|
|
|
|
# blank mouth
|
|
|
|
'pau': '4',
|
|
|
|
}
|