]> _ Git - cubist_azuretts.git/commitdiff
wait #5065 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 31 Jan 2022 18:34:38 +0000 (19:34 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 31 Jan 2022 18:34:38 +0000 (19:34 +0100)
src/Api.php

index 04a1037137d4a4344c0f4f61a39f433da1e08aaa..ba038f65304493c8bea41d146254e5d6ff406d32 100644 (file)
@@ -41,22 +41,21 @@ class Api
         $this->client = new Client();
     }
 
-    public function textToSpeech($text, $locale, $gender, $voiceName)
+    public function textToSpeech($text, $locale, $gender, $voiceName, $saveToFile)
     {
-        $this->checkToken();
+        $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'>";
+        $ssml .= htmlspecialchars($text, ENT_XML1 | ENT_SUBSTITUTE | ENT_QUOTES);
+        $ssml .= "</voice></speak>";
 
-        $ssml = "<speak version='1.0' xml:lang='$locale'><voice xml:lang='$locale' xml:gender='$gender' name='$voiceName'>" . htmlspecialchars($text, ENT_XML1) . "</voice></speak>";
-        $res = $this->_call('/v1', 'post', [
+        $this->_call('/v1', 'post', [
             'X-Microsoft-OutputFormat' => 'audio-48khz-192kbitrate-mono-mp3',
             'Content-type' => 'application/ssml+xml'
-        ], $ssml);
-        return $res->getBody()->getContents();
+        ], $ssml, ['sink' => $saveToFile]);
     }
 
     public function listVoices()
     {
         $res = $this->_call('/voices/list');
-
         return json_decode($res->getBody()->getContents());
     }
 
@@ -70,11 +69,13 @@ class Api
      */
     protected function _call($url, $method = 'get', $headers = [], $body = null, $options = [])
     {
+
         $this->checkToken();
         $baseURL = 'https://' . $this->region . '.tts.speech.microsoft.com/cognitiveservices/';
         $headers = array_merge([
             'Authorization' => 'Bearer ' . $this->token,
             'User-Agent' => 'Cubist TTS Client'], $headers);
+
         return $this->client->send(new Request($method, $baseURL . ltrim($url, '/'), $headers, $body), $options);
     }