#!/usr/bin/php
<?php
/******************************************************************
* Copyright © 2017 ICT Innovations Pakistan All Rights Reserved   *
* Developed By: Nasir Iqbal                                       *
*             : Tahir Almas                                       *
* Website : http://www.ictinnovations.com/                        *
* Mail : info@ictinnovations.com                                  *
******************************************************************/

/* see
   https://cloud.google.com/text-to-speech/docs/reference/libraries#client-libraries-install-php
 */

//$root_dir = dirname(dirname(__FILE__));
//include_once($root_dir.'/wwwroot/lib/lib.php');

$language = $argv[1];
$filename = $argv[2];
$message  = $argv[3];

$google_key  = '';
$google_api  = 'https://texttospeech.googleapis.com/v1beta1/text:synthesize';
$request_url = "$google_api?key=$google_key";

$requestData = <<<JSON
{
  "audioConfig": {
    "audioEncoding": "LINEAR16",
    "sampleRateHertz": "8000",
    "effectsProfileId": [
      "telephony-class-application"
    ],
    "pitch": 0,
    "speakingRate": 1
  },
  "input": {
    "text": "$message"
  },
  "voice": {
    "languageCode": "$language",
    "ssmlGender": "FEMALE"
  }
}
JSON;

$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($requestData)
  )
);

$response = curl_exec($ch);

curl_close($ch);

$responseData = json_decode($response);
$speechData   = base64_decode($responseData->audioContent);

file_put_contents($filename, $speechData);

exit(0);
