def __init__(self): super(PrayerTimeSkill, self).__init__(name="PrayerTimeSkill") self.prayer_times = None self.STREAM = '{0}/stream.mp3'.format( get_cache_directory("PrayerTimeSkill")) self.first_time_event_flag = True
def __init__(self): super(NPROneSkill, self).__init__(name="NPROneSkill") self.curl = None self.now_playing = None self.last_message = None self.STREAM = '{}/stream'.format(get_cache_directory('NPROneSkill')) # read api key for NPR One from settings and pass to api managing cnstructor self.apiKey = self.settings.get('api_key') self.nprone = NPROne(self.apiKey) # change setting if modified on web self.settings_change_callback = self.websettings_callback
def save_phonemes(self, key, phonemes): """Cache phonemes Arguments: key: Hash key for the sentence phonemes: phoneme string to save """ cache_dir = get_cache_directory("tts/" + self.tts_name) pho_file = os.path.join(cache_dir, key + ".pho") try: with open(pho_file, "w") as cachefile: cachefile.write(json.dumps(phonemes)) except Exception: LOG.exception("Failed to write {} to cache".format(pho_file))
def test_curate_cache_both_low(self, mock_psutil, mock_conf): """Test Free space and percentage low.""" mock_conf.get.return_value = test_config space = mock.Mock(name='diskspace') mock_psutil.disk_usage.return_value = space cache_dir = get_cache_directory('braveNewWorld') # Create files in the cache directory huxley_path, aldous_path = create_cache_files(cache_dir) space.percent = 96.0 space.free = 2 * 1024 * 1024 # 2MB space.total = 20 * 1024 * 1024 * 1024 # 20GB self.assertEqual(curate_cache(cache_dir), [aldous_path, huxley_path]) self.assertFalse(exists(aldous_path)) self.assertFalse(exists(huxley_path))
def save_phonemes(self, key, phonemes): """ Cache phonemes Args: key: Hash key for the sentence phonemes: phoneme string to save """ cache_dir = get_cache_directory("tts/" + self.tts_name) pho_file = os.path.join(cache_dir, key + ".pho") try: with open(pho_file, "w") as cachefile: cachefile.write(json.dumps(phonemes)) except Exception: LOG.exception("Failed to write {} to cache".format(pho_file))
def load_phonemes(self, key): """ Load phonemes from cache file. Args: Key: Key identifying phoneme cache """ pho_file = os.path.join(get_cache_directory("tts"), key + ".pho") if os.path.exists(pho_file): try: with open(pho_file, "r") as cachefile: phonemes = json.load(cachefile) return phonemes except Exception as e: LOG.error("Failed to read .PHO from cache ({})".format(e)) return None
def test_curate_cache_plenty_space(self, mock_psutil, mock_conf): """Test plenty of space free.""" mock_conf.get.return_value = test_config space = mock.Mock(name='diskspace') mock_psutil.disk_usage.return_value = space cache_dir = get_cache_directory('braveNewWorld') # Create files in the cache directory huxley_path, aldous_path = create_cache_files(cache_dir) space.percent = 5.0 space.free = 2 * 1024 * 1024 * 1024 # 2GB space.total = 20 * 1024 * 1024 * 1024 # 20GB self.assertEqual(curate_cache(cache_dir), []) self.assertTrue(exists(aldous_path)) self.assertTrue(exists(huxley_path))
def load_phonemes(self, key): """ Load phonemes from cache file. Args: Key: Key identifying phoneme cache """ pho_file = os.path.join(get_cache_directory("tts"), key + ".pho") if os.path.exists(pho_file): try: with open(pho_file, "r") as cachefile: phonemes = json.load(cachefile) return phonemes except Exception as e: LOG.error("Failed to read .PHO from cache ({})".format(e)) return None
def copy_cache(cache_audio_dir): """ This method copies the cache from 'cache_audio_dir' to TTS specific cache directory given by get_cache_directory() Args: cache_audio_dir (path): path containing .wav files """ if os.path.exists(cache_audio_dir): # get tmp directory where tts cache is stored dest = util.get_cache_directory('tts/' + 'Mimic2') files = os.listdir(cache_audio_dir) for f in files: shutil.copy2(os.path.join(cache_audio_dir, f), dest) LOG.debug("Copied all pre-loaded cache for {} to {}".format(TTS, dest)) else: LOG.debug("No Source directory for {} pre-loaded cache".format(TTS))
def execute(self, sentence, ident=None): """request and play mimic2 wav audio Args: sentence (str): sentence to synthesize from mimic2 ident (optional): Defaults to None. """ chunks = sentence_chunker(sentence, self.chunk_size) for idx, req in enumerate(self._requests(chunks)): results = req.result().json() audio = base64.b64decode(results['audio_base64']) vis = self.visime(results['visimes']) key = str( hashlib.md5(chunks[idx].encode('utf-8', 'ignore')).hexdigest()) wav_file = os.path.join(get_cache_directory("tts"), key + '.' + self.audio_ext) with open(wav_file, 'wb') as f: f.write(audio) self.queue.put((self.audio_ext, wav_file, vis, ident))
def test_curate_cache(self, mock_psutil, mock_conf): """Test removal of cache files when disk space is running low.""" mock_conf.get.return_value = test_config space = mock.Mock(name='diskspace') mock_psutil.disk_usage.return_value = space cache_dir = get_cache_directory('braveNewWorld') huxley_path, aldous_path = create_cache_files(cache_dir) # Create files in the cache directory # Test plenty of space free space.percent = 5.0 space.free = 2 * 1024 * 1024 * 1024 # 2GB space.total = 20 * 1024 * 1024 * 1024 # 20GB curate_cache(cache_dir) self.assertTrue(exists(aldous_path)) self.assertTrue(exists(huxley_path)) # Free Percentage low but not free space space.percent = 96.0 space.free = 2 * 1024 * 1024 * 1024 # 2GB curate_cache(cache_dir) self.assertTrue(exists(aldous_path)) self.assertTrue(exists(huxley_path)) # Free space low, but not percentage space.percent = 95.0 space.free = 2 * 1024 * 1024 # 2MB curate_cache(cache_dir) self.assertTrue(exists(aldous_path)) self.assertTrue(exists(huxley_path)) # Free space and percentage low space.percent = 96.0 space.free = 2 * 1024 * 1024 # 2MB curate_cache(cache_dir) self.assertFalse(exists(aldous_path)) self.assertFalse(exists(huxley_path))
def execute(self, sentence, ident=None): """request and play mimic2 wav audio Args: sentence (str): sentence to synthesize from mimic2 ident (optional): Defaults to None. """ create_signal("isSpeaking") sentence = self._normalized_numbers(sentence) # Use the phonetic_spelling mechanism from the TTS base class if self.phonetic_spelling: for word in re.findall(r"[\w']+", sentence): if word.lower() in self.spellings: sentence = sentence.replace(word, self.spellings[word.lower()]) chunks = sentence_chunker(sentence, self.chunk_size) try: for idx, req in enumerate(self._requests(chunks)): results = req.result().json() audio = base64.b64decode(results['audio_base64']) vis = self.visime(results['visimes']) key = str(hashlib.md5( chunks[idx].encode('utf-8', 'ignore')).hexdigest()) wav_file = os.path.join( get_cache_directory("tts"), key + '.' + self.audio_ext ) with open(wav_file, 'wb') as f: f.write(audio) self.queue.put((self.audio_ext, wav_file, vis, ident)) except (ReadTimeout, ConnectionError, ConnectTimeout, HTTPError): raise RemoteTTSTimeoutException( "Mimic 2 remote server request timedout. falling back to mimic" )
def execute(self, sentence, ident=None): """request and play mimic2 wav audio Args: sentence (str): sentence to synthesize from mimic2 ident (optional): Defaults to None. """ create_signal("isSpeaking") sentence = self._normalized_numbers(sentence) # Use the phonetic_spelling mechanism from the TTS base class if self.phonetic_spelling: for word in re.findall(r"[\w']+", sentence): if word.lower() in self.spellings: sentence = sentence.replace(word, self.spellings[word.lower()]) chunks = sentence_chunker(sentence, self.chunk_size) try: for idx, req in enumerate(self._requests(chunks)): results = req.result().json() audio = base64.b64decode(results['audio_base64']) vis = self.visime(results['visimes']) key = str(hashlib.md5( chunks[idx].encode('utf-8', 'ignore')).hexdigest()) wav_file = os.path.join( get_cache_directory("tts"), key + '.' + self.audio_ext ) with open(wav_file, 'wb') as f: f.write(audio) self.queue.put((self.audio_ext, wav_file, vis, ident)) except (ReadTimeout, ConnectionError, ConnectTimeout, HTTPError): raise RemoteTTSTimeoutException( "Mimic 2 remote server request timedout. falling back to mimic" )
def test_get_cache_directory(self, mock_conf): mock_conf.get.return_value = test_config expected_path = join(test_config['cache_dir'], 'mycroft', 'cache') self.assertEqual(get_cache_directory(), expected_path) self.assertTrue(isdir(expected_path))
def __init__(self): super().__init__(name="NewsSkill") self.curl = None self.now_playing = None self.last_message = None self.STREAM = '{}/stream'.format(get_cache_directory('NewsSkill'))
def __init__(self): super().__init__(name="Radio") self.curl = None self.regexes = {} self.STREAM = '{}/stream'.format(get_cache_directory('RadioSkill'))