From 5f2f404164cfdb9547d26e16dcab34d04e876f82 Mon Sep 17 00:00:00 2001 From: Luis Daniel Lucio Quiroz Date: Sun, 5 Nov 2017 17:46:46 -0500 Subject: [PATCH] Azure TTS (#2891) * Azure TTS This is the first of a set of pull that will give FusionPBX TTS capabilities in many places. This is the main class that will interact with the other modifications. Please suggest if this is the best place to put this file or if a /app/azure/resources/class is best. I am not sure about this. This class is sponsored by my friend since he paid some Indian guys for a development and I discovered they were offering/selling that private development to other companies. He took the call to make this capability free. * Moving to another place * New place --- app/azure/resources/classes/azure.php | 198 ++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 app/azure/resources/classes/azure.php diff --git a/app/azure/resources/classes/azure.php b/app/azure/resources/classes/azure.php new file mode 100644 index 0000000000..393d73faba --- /dev/null +++ b/app/azure/resources/classes/azure.php @@ -0,0 +1,198 @@ + + array ( + 'lang' => 'en-US', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)' + ), + 'English-Jessa' => + array ( + 'lang' => 'en-US', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)' + ), + 'English-Benjamin' => + array ( + 'lang' => 'en-US', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)' + ), + 'British-Susan' => + array ( + 'lang' => 'en-GB', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)' + ), + 'British-Hazel' => + array ( + 'lang' => 'en-GB', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-GB, HazelRUS)' + ), + 'British-George' => + array ( + 'lang' => 'en-GB', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)' + ), + 'Australian-Catherine' => + array ( + 'lang' => 'en-AU', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)' + ), + 'Spanish-Helena' => + array ( + 'lang' => 'es-ES', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (es-ES, HelenaRUS)' + ), + 'Spanish-Laura' => + array ( + 'lang' => 'es-ES', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)' + ), + 'Spanish-Pablo' => + array ( + 'lang' => 'es-ES', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)' + ), + 'French-Julie' => + array ( + 'lang' => 'fr-FR', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (fr-FR, Julie, Apollo)' + ), + 'French-Hortense' => + array ( + 'lang' => 'fr-FR', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)' + ), + 'French-Paul' => + array ( + 'lang' => 'fr-FR', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (fr-FR, Paul, Apollo)' + ), + 'German-Hedda' => + array ( + 'lang' => 'de-DE', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)' + ), + 'Russian-Irina' => + array ( + 'lang' => 'ru-RU', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (ru-RU, Irina, Apollo)' + ), + 'Russian-Pavel' => + array ( + 'lang' => 'ru-RU', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (ru-RU, Pavel, Apollo)' + ), + 'Chinese-Huihui' => + array ( + 'lang' => 'zh-CN', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)' + ), + 'Chinese-Yaoyao' => + array ( + 'lang' => 'zh-CN', + 'gender' => 'Female', + 'name' => 'Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)' + ), + 'Chinese-Kangkang' => + array ( + 'lang' => 'zh-CN', + 'gender' => 'Male', + 'name' => 'Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)' + ) + ); + + private static function getTokenUrl(){ + return "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; + } + + private static function getApiUrl(){ + return "https://speech.platform.bing.com/synthesize"; + } + + private static function getSubscriptionKey(){ + return $_SESSION['azure]['key']['text']; + } + + private static function _getToken(){ + $url = self::getTokenUrl(); + $subscriptionKey = self::getSubscriptionKey(); + + $headers = array(); + $headers[] = 'Ocp-Apim-Subscription-Key: '. $subscriptionKey; + $headers[] = 'Content-Length: 0'; + + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt ( $ch, CURLOPT_URL, $url ); + //curl_setopt($ch, CURLOPT_SSLVERSION, 1); + curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 ); + curl_setopt ( $ch, CURLOPT_TIMEOUT, 300 ); + curl_setopt ( $ch, CURLOPT_VERBOSE, false); + curl_setopt($ch, CURLOPT_POST, true); + //curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); + + $response = curl_exec($ch); + + curl_close($ch); + return $response; + } + + public static function synthesize($data,$formate_key){ + + $lang = self::$formats[$formate_key]['lang']; + $gender = self::$formats[$formate_key]['gender']; + $name = self::$formats[$formate_key]['name']; + $token = self::_getToken(); + + $url = self::getApiUrl(); + + $headers = array(); + $headers[] = 'Authorization: Bearer '. $token; + $headers[] = 'Content-Type: application/ssml+xml'; + $headers[] = 'X-Microsoft-OutputFormat: riff-16khz-16bit-mono-pcm'; + $headers[] = 'User-Agent: TTS'; + + $xml_post_string = " + "; + $xml_post_string .= $data; + $xml_post_string .= " + "; + + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt ( $ch, CURLOPT_URL, $url ); + //curl_setopt($ch, CURLOPT_SSLVERSION, 1); + curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 ); + curl_setopt ( $ch, CURLOPT_TIMEOUT, 300 ); + curl_setopt ( $ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); + + $response = curl_exec($ch); + $filename = "tts_".time().".wav"; + file_put_contents("/var/www/html/fusionpbx/app/voiplyrecording/tts_record/".$filename, $response); + + curl_close($ch); + return $filename; + } +}