]> _ Git - cubist_azuretts.git/commitdiff
wip #7977 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 5 Feb 2026 15:04:16 +0000 (16:04 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 5 Feb 2026 15:04:16 +0000 (16:04 +0100)
src/Api.php

index 3c70f7b31c706a2988e5a4726ce5d2ec6568bde4..f3fb96583b9315d5278867ae0e5096c66f2af04f 100644 (file)
@@ -6,6 +6,7 @@ use GuzzleHttp\Client;
 use GuzzleHttp\Psr7\Request;
 use GuzzleHttp\Psr7\Response;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
 
 class Api
 {
@@ -44,15 +45,24 @@ class Api
 
     public function textToSpeech($text, $locale, $gender, $voiceName, $saveToFile)
     {
+        if (stristr('|', $locale)) {
+            $e = explode('|', $locale);
+            $locale = $e[1];
+        }
+
         $ssml = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis'  xml:lang='$locale'><voice xml:lang='$locale' xml:gender='$gender' name='$voiceName'>";
         $text = self::stripNonSSMLTags($text);
         $ssml .= htmlspecialchars($text, ENT_XML1 | ENT_SUBSTITUTE | ENT_QUOTES);
         $ssml .= "</voice></speak>";
 
+        Log::info('TTS ' . mb_strlen($text) . ' chars // ' . $locale . ' // ' . $gender . ' // ' . $voiceName . ' :: ' . $ssml);
+
         $this->_call('/v1', 'post', [
             'X-Microsoft-OutputFormat' => 'audio-48khz-192kbitrate-mono-mp3',
             'Content-type' => 'application/ssml+xml'
         ], $ssml, ['sink' => $saveToFile]);
+
+
     }
 
     public static function stripNonSSMLTags($text, $additional_allowed_tags = '')
@@ -64,10 +74,12 @@ class Api
      * @param $raw
      * @return array|false
      */
-    public function listVoices($raw = false)
+    public function listVoices($raw = false, $force = false)
     {
         $cacheKey = 'azure_tts_voices_' . $raw;
-        Cache::forget($cacheKey);
+        if ($force) {
+            Cache::forget($cacheKey);
+        }
         return Cache::remember($cacheKey, 86400, function () use ($raw) {
             return $this->_listVoices($raw);
         });
@@ -89,7 +101,14 @@ class Api
         }
         $res = [];
         foreach ($j as $voice) {
-            $res[$voice['Locale'] . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = $voice['DisplayName'] . ' / ' . $voice['Gender'] . ' / ' . $voice['LocaleName'];
+            if (isset($voice['SecondaryLocaleList'])) {
+                foreach ($voice['SecondaryLocaleList'] as $secVoice) {
+                    $res[$voice['Locale'] . '|' . $secVoice . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = $voice['DisplayName'] . ' / ' . $voice['Gender'] . ' / ' . $secVoice;
+                }
+            } else {
+                $res[$voice['Locale'] . '/' . $voice['Gender'] . '/' . $voice['ShortName']] = $voice['DisplayName'] . ' / ' . $voice['Gender'] . ' / ' . $voice['LocaleName'];
+            }
+
         }
         return $res;
     }