From: Vincent Vanwaelscappel Date: Thu, 15 Jun 2023 09:23:41 +0000 (+0200) Subject: wip #5996 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=cf7a3803808018ceb3f2b080274fe0f38b169577;p=fluidbook-toolbox.git wip #5996 @0.5 --- diff --git a/app/Console/Commands/ToolboxPrecache.php b/app/Console/Commands/ToolboxPrecache.php index 621684005..2d8d663d6 100644 --- a/app/Console/Commands/ToolboxPrecache.php +++ b/app/Console/Commands/ToolboxPrecache.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Fields\FluidbookTTSVoice; use App\Fluidbook\Farm; use App\Models\FluidbookTranslate; use Cubedesigners\UserDatabase\Permissions; @@ -17,5 +18,6 @@ class ToolboxPrecache extends CubistCommand // Users tree Permissions::_getData(true); FluidbookTranslate::getAllFluidbookTranslations(true); + FluidbookTTSVoice::getTTSVoices(true); } } diff --git a/app/Fields/FluidbookTTSVoice.php b/app/Fields/FluidbookTTSVoice.php index 8540b9c7e..51ab5166a 100644 --- a/app/Fields/FluidbookTTSVoice.php +++ b/app/Fields/FluidbookTTSVoice.php @@ -4,6 +4,7 @@ namespace App\Fields; use Cubist\Azure\TTS\Api; use Cubist\Backpack\Magic\Fields\SelectFromArray; +use Illuminate\Support\Facades\Cache; class FluidbookTTSVoice extends SelectFromArray { @@ -14,28 +15,41 @@ class FluidbookTTSVoice extends SelectFromArray public function getOptions() { if (null === static::$_ttsVoices) { - static::$_ttsVoices = ['' => '']; + static::$_ttsVoices = static::getTTSVoices(); + } + return static::$_ttsVoices; + } + public static function getTTSVoices($force = false) + { + $cacheKey = 'fluidbook_tts_voices'; + if ($force) { + Cache::forget($cacheKey); + } + return Cache::remember($cacheKey, 3600, function () { + start_measure('Get TTS Voices in Field'); + $res = ['' => '']; try { $api = new Api(env('AZURE_API_KEY')); $voices = $api->listVoices(true); if ($voices) { foreach ($voices as $key => $voice) { - static::$_ttsVoices['azuretts:' . $voice['Locale'] . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = 'Azure ' . $voice['DisplayName'] . ' (' . $voice['Locale'] . ')'; + $res['azuretts:' . $voice['Locale'] . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = 'Azure ' . $voice['DisplayName'] . ' (' . $voice['Locale'] . ')'; } } } catch (\Exception $e) { } - if (count(static::$_ttsVoices) === 1) { - static::$_ttsVoices['azuretts:fr-FR/Female/fr-FR-DeniseNeural'] = 'Azure Denise (fr-FR)'; - static::$_ttsVoices['azuretts:fr-FR/Male/fr-FR-HenriNeural'] = 'Azure Henri (fr-FR)'; + if (count($res) === 1) { + $res['azuretts:fr-FR/Female/fr-FR-DeniseNeural'] = 'Azure Denise (fr-FR)'; + $res['azuretts:fr-FR/Male/fr-FR-HenriNeural'] = 'Azure Henri (fr-FR)'; } - static::$_ttsVoices = array_merge(static::$_ttsVoices, ['festival:voice_cmu_us_slt_arctic_hts' => 'Festival (en-US)', + $res = array_merge($res, ['festival:voice_cmu_us_slt_arctic_hts' => 'Festival (en-US)', 'readspeaker:Sophie/en_us' => 'ReadSpeaker Sophie (en-US)', 'readspeaker:Marc/en_us' => 'ReadSpeaker Marc (en-US)']); - } - return static::$_ttsVoices; + stop_measure('Get TTS Voices in Field'); + return $res; + }); } } diff --git a/app/Models/Base/ToolboxSettingsModel.php b/app/Models/Base/ToolboxSettingsModel.php index 7c5688398..0545a7fd4 100644 --- a/app/Models/Base/ToolboxSettingsModel.php +++ b/app/Models/Base/ToolboxSettingsModel.php @@ -12,7 +12,7 @@ class ToolboxSettingsModel extends ToolboxModel */ protected $_settingsData = null; - protected static array $defaultSettingsAttributes = ['fake' => true, 'translatable' => false, 'store_in' => 'settings','default'=>'']; + protected static array $defaultSettingsAttributes = ['fake' => true, 'translatable' => false, 'store_in' => 'settings', 'default' => '']; /** @@ -37,7 +37,10 @@ class ToolboxSettingsModel extends ToolboxModel */ public function addSettingField($name, $type = 'Text', $label = '', $attributes = []) { + start_measure('add setting field ' . $name); $attributes = self::normalizeAttributes($name, $type, $label, $attributes); - return $this->addField($name, $type, $label, array_merge(self::$defaultSettingsAttributes, $attributes)); + $res = $this->addField($name, $type, $label, array_merge(self::$defaultSettingsAttributes, $attributes)); + stop_measure('add setting field ' . $name); + return $res; } }