public function speak($lang = 'en', $text = 'text not provided') { // Create temp path $uxmsTtsTempPath = temp_path(Configs::get('temp_name')); if (!File::isDirectory($uxmsTtsTempPath)) { if (!File::makeDirectory($uxmsTtsTempPath)) { throw new ApplicationException('Unable to create temp directory: ' . $uxmsTtsTempPath); } } // Create storage path $uxmsTtsStoragePath = storage_path() . '/app/' . Configs::get('final_audio_folder_name'); if (!File::isDirectory($uxmsTtsStoragePath)) { if (!File::makeDirectory($uxmsTtsStoragePath)) { throw new ApplicationException('Unable to create storage directory: ' . $uxmsTtsStoragePath); } } // Check if saved audios will be using and if it exists in stor dir if (Configs::get('use_saved_files')) { if (File::exists($uxmsTtsStoragePath . '/' . md5($text) . '.mp3')) { return $this->tts->getAudioElementOrUri(md5($text)); } } // Lets create the speech $this->tts->text($lang, $text)->saveFile(false); // Do not forget to activate puging from settings.. if (Configs::get('purge_temp')) { $this->tts->clearTemp(); } return $this->tts->getAudioElementOrUri(); }
/** * The boot() method is called right before a request is routed */ public function boot() { if (Configs::get('use_saved_files') == null) { Configs::set('use_saved_files', '1'); } if (Configs::get('purge_temp') == null) { Configs::set('purge_temp', '1'); } if (Configs::get('return_element_or_uri') == null) { Configs::set('return_element_or_uri', '1'); } if (Configs::get('translate_base_url') == null) { Configs::set('translate_base_url', 'http://translate.google.com/translate_tts'); } if (Configs::get('ip_for_header') == null) { Configs::set('ip_for_header', '74.125.239.35'); } if (Configs::get('temp_name') == null) { Configs::set('temp_name', 'uxms_tts'); } if (Configs::get('final_audio_folder_name') == null) { Configs::set('final_audio_folder_name', 'tts'); } }
/** * Clears the temp dir * * @param string $which * @return Tts */ public function clearTemp($which = '*') { $uxmsTtsTempPath = temp_path(Configs::get('temp_name')); try { if ($which == '*') { array_map('unlink', glob($uxmsTtsTempPath . "/{$which}")); } else { array_map('unlink', glob($uxmsTtsTempPath . "/{$which}_*")); } } catch (\Exception $e) { } return $this; }