use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
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 = '')
* @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);
});
}
$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;
}