📜 ⬆️ ⬇️

SpeechKit Cloud API application for sounding weather and news from Yandex

Good day! In this article I would like to talk about how you can do on the site "Voice weather and news informer", which, for example, when choosing a city, would not only show, but also tell the actual weather forecast, as well as arbitrary news. In fact, this script was developed to inform the “Smart Home” system, but can be easily converted to any site that has PHP support on the hosting, which was done for this article. In the example for speech synthesis, the SpeechKit Cloud API technology from Yandex and the XML weather and news Yandex parser written in PHP will be used.

What is needed for implementation:

This material is provided for informational purposes only, so the installation process and the addition of cities will not be considered here.

Principle of operation:

Script with a form for selecting cities index.html
<!DOCTYPE html> <html lang="ru"> <head> <title> SpeechKit Cloud API   Yandex.</title> <meta charset="utf-8"> </head> <body> <script language="javascript" type="text/javascript"> function Get_Weather() { var tts_text=document.getElementById('weather').value; console.log(tts_text); var response="weather.php?&q="+encodeURI(tts_text); //  mp3  var audio = document.getElementById("gameMusic"); if (audio) { audio.parentNode.removeChild(audio); } //    audio audio = document.createElement("audio"); audio.setAttribute("id", "gameMusic"); audio.setAttribute("autoplay", "true"); //audio.setAttribute("loop", "false"); var mp3 = document.createElement("source"); mp3.setAttribute("src", response); mp3.setAttribute("type", "audio/mpeg"); audio.appendChild(mp3); audio.play(); document.body.appendChild(audio); } function Get_News() { var tts_text=document.getElementById('news').value; console.log(tts_text); var response="news.php?&q="+encodeURI(tts_text); //  mp3  var audio = document.getElementById("gameMusic"); if (audio) { audio.parentNode.removeChild(audio); } //    audio audio = document.createElement("audio"); audio.setAttribute("id", "gameMusic"); audio.setAttribute("autoplay", "true"); //audio.setAttribute("loop", "false"); var mp3 = document.createElement("source"); mp3.setAttribute("src", response); mp3.setAttribute("type", "audio/mpeg"); audio.appendChild(mp3); audio.play(); document.body.appendChild(audio); } </script> <h1><span> SpeechKit Cloud API   Yandex.</span></h1> <h2></h2> <select size="1" onclick="Get_Weather();" id="weather"> <option disabled> </option> <option value="27612"></option> <option value="34880"></option> <option value="26063">-</option> <option value="25913"></option> </select> <h2></h2> <select size="1" onclick="Get_News();" id="news"> <option disabled> </option> <option selected value="Astrakhan"></option> <option value="Volgograd"></option> <option value="Krasnodar"></option> <option value="Moscow"></option> <option value="Saratov"></option> </select> </body> </html> 


Proud to add by setting new option parameters in the select element.
')
Script for generating mp3 file for weather.php weather
 <?php $id=$_GET["q"]; // id  $url="http://export.yandex.ru/weather-ng/forecasts/".$id.".xml"; // url xml  $xml = simplexml_load_file($url); //  XML-   //: $city=$xml['city']; // $temp=$xml->fact->temperature; //  $weather_type=$xml->fact->weather_type; //   $humidity=$xml->fact->humidity; //  $wind_direction=$xml->fact->wind_direction; //   $wind_speed=$xml->fact->wind_speed; //   $pressure=$xml->fact->pressure; //  $znak=""; if($wind_direction=="e") { $wind_direction_text="";} if($wind_direction=="w") { $wind_direction_text="";} if($wind_direction=="s") { $wind_direction_text="";} if($wind_direction=="n") { $wind_direction_text="";} if($wind_direction=="se") { $wind_direction_text="-";} if($wind_direction=="ne") { $wind_direction_text="-";} if($wind_direction=="sw") { $wind_direction_text="-";} if($wind_direction=="nw") { $wind_direction_text="-";} $minus_arry=preg_match("/(-)/", $temp, $minus_out); if (!empty($minus_out[1])) { $znak=""; $temp=str_replace("-","",$temp); } // if( $temp=="1" or $temp=="21" or $temp=="31" or $temp=="41" or $temp=="51" or $temp=="61" or $temp=="71" or $temp=="81" or $temp=="91" or $temp=="101") { $text=""; } else { // if( $temp=="2" or $temp=="3" or $temp=="4" or $temp=="22" or $temp=="23" or $temp=="24" or $temp=="32" or $temp=="33" or $temp=="34" or $temp=="42" or $temp=="43" or $temp=="44" or $temp=="52" or $temp=="53" or $temp=="54" or $temp=="62" or $temp=="63" or $temp=="64" or $temp=="72" or $temp=="73" or $temp=="74" or $temp=="82" or $temp=="83" or $temp=="84" or $temp=="92" or $temp=="93" or $temp=="94" or $temp=="102" or $temp=="103" ) {$text="";} else {$text="";} } // if( $humidity=="1" or $humidity=="21" or $humidity=="31" or $humidity=="41" or $humidity=="51" or $humidity=="61" or $humidity=="71" or $humidity=="81" or $humidity=="91" or $humidity=="101") { $humidity_text=""; } else { // if( $humidity=="2" or $humidity=="3" or $humidity=="4" or $humidity=="22" or $humidity=="23" or $humidity=="24" or $humidity=="32" or $humidity=="33" or $humidity=="34" or $humidity=="42" or $humidity=="43" or $humidity=="44" or $humidity=="52" or $humidity=="53" or $humidity=="54" or $humidity=="62" or $humidity=="63" or $humidity=="64" or $humidity=="72" or $humidity=="73" or $humidity=="74" or $humidity=="82" or $humidity=="83" or $humidity=="84" or $humidity=="92" or $humidity=="93" or $humidity=="94" or $humidity=="102" or $humidity=="103" ) {$humidity_text="";} else {$humidity_text="";} } // if( $pressure=="701" or $pressure=="721" or $pressure=="731" or $pressure=="741" or $pressure=="751" or $pressure=="761" or $pressure=="771" or $pressure=="781" or $pressure=="791" or $pressure=="801") { $pressure_text="  "; } else { // if( $pressure=="702" or $pressure=="703" or $pressure=="704" or $pressure=="722" or $pressure=="723" or $pressure=="724" or $pressure=="732" or $pressure=="733" or $pressure=="734" or $pressure=="742" or $pressure=="743" or $pressure=="744" or $pressure=="752" or $pressure=="753" or $pressure=="754" or $pressure=="762" or $pressure=="763" or $pressure=="764" or $pressure=="772" or $pressure=="773" or $pressure=="774" or $pressure=="782" or $pressure=="783" or $pressure=="784" or $pressure=="792" or $pressure=="793" or $pressure=="794" or $pressure=="802" or $pressure=="803" ) {$pressure_text="  ";} else {$pressure_text="  ";} } $say_text="   ".$city." ".$weather_type.". T  ".$znak." ".$temp." ".$text.".  ".$humidity." ".$humidity_text.".  ".$wind_direction_text." ".$wind_speed."   .   ".$pressure." ".$pressure_text." !"; $qs = http_build_query(array("format" => "mp3","lang" => "ru-RU","speaker" => "jane","key" => "SpeechKit_Cloud_API_Key","emotion" => "good", "text" => $say_text)); //   $ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n"))); $soundfile = file_get_contents("https://tts.voicetech.yandex.net/generate?".$qs, false, $ctx); //    mp3  echo($soundfile); ?> 


In the script you need to change the SpeechKit_Cloud_API_Key to the key you received.
Script generating mp3 file for news news.php
 <?php $city=$_GET['q']; $data_file="http://news.yandex.ru/".$city."/index.rss"; //  xml  $xml = simplexml_load_file($data_file); //  xml   $number= rand(1,13); //    $news=$xml->channel->item[$number]->description; // $title=$xml->channel->item[$number]->title; // $content_news = trim(preg_replace('/\s{2,}/', ' ', $news));//   $content_title = trim(preg_replace('/\s{2,}/', ' ', $title));//   $text=$content_title." - ".$content_news; $search = array('"','"',' ',')','('); $replace = array(''); $text = str_replace($search, $replace, $text); $qs = http_build_query(array("format" => "mp3","lang" => "ru-RU","speaker" => "jane","key" => "SpeechKit_Cloud_API_Key","emotion" => "good", "text" => $text)); //   $ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n"))); $soundfile = file_get_contents("https://tts.voicetech.yandex.net/generate?".$qs, false, $ctx); //    mp3  echo($soundfile); ?> 



In the script you need to change the SpeechKit_Cloud_API_Key to the key you received. This technology can also be used to sound the date and time. It is possible to change the language, timbre, voices (male and female), a description of all speech synthesis parameters can be viewed at the link to SpeechKit Cloud API which is located below.
The above scripts were tested in Google Chrome and Iceweasel browsers, but should work correctly in other browsers that support HTML 5 Web Audio.

PS Used literature


Publishing Yandeks.TvoyaPogoda
Weather from Yandex
News from Yandex
SpeechKit Cloud API
Check the operation of the SpeechKit Cloud API here . See on Youtube what happened (example at the beginning of the 7th minute - speech synthesis using RHVoice).

The written code does not claim to be ideal and can always be improved.

Source: https://habr.com/ru/post/271081/


All Articles