]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5996 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Jun 2023 09:23:41 +0000 (11:23 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Jun 2023 09:23:41 +0000 (11:23 +0200)
app/Console/Commands/ToolboxPrecache.php
app/Fields/FluidbookTTSVoice.php
app/Models/Base/ToolboxSettingsModel.php

index 6216840054dda8df6013da08bee319e93226f097..2d8d663d629ad28ffee95e5078e49b1a70e05afe 100644 (file)
@@ -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);
     }
 }
index 8540b9c7efb50de028d184d5432016470778bb41..51ab5166a8e96e99c8529b0b4d8203968b1a2c33 100644 (file)
@@ -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;
+        });
     }
 }
index 7c56883986a6b7cb101632247e3edd5806d92402..0545a7fd45707ca3891d812dbe4e51771457c47a 100644 (file)
@@ -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;
     }
 }