首页 > 解决方案 > php中的谷歌云文本到语音

问题描述

我正在尝试在我的 php 网站中使用 google 的文本到语音来托管在实时 Cpanel 服务器上

我已启用文本到语音 API,在凭据部分创建 API KEY,还从创建服务帐户密钥页面下载了凭据的 json 文件。

然后我从 Github 下载了示例文件,还使用 ​​composer 构建了库

现在我不明白把钥匙放在哪里。在每个地方,它都需要在 Shell 中导出密钥,但这适用于 1 个打开的命令提示符会话,并且每次都必须导出。

由于我想在基于实时 cpanel 的主机上运行此代码,因此我认为无法导出。

代码中有没有可以传递密钥的地方?

在stackoverflow 的这篇 url文章中:第一个答案将 CURL 的响应导出到synthesize-text.txt但我们需要 mp3 输出

另一个答案表明我们应该使用jq但由于它是一个共享的 hsoting 服务器,我不确定我们是否可以安排jq

有没有办法解决这个问题?


更新

在参考@V.Tur 的答案后尝试了以下代码

$params = [
    "audioConfig"=>[
        "audioEncoding"=>"MP3",
        "pitch"=> "1",
        "speakingRate"=> "1",
        "effectsProfileId"=> [
            "medium-bluetooth-speaker-class-device"
          ]
    ],
    "input"=>[
        "ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
                  standard <break time=\"1s\"/>is defined by the
                  <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
    ],
    "voice"=>[
        "languageCode"=> "hi-IN",
        "name" =>"hi-IN-Wavenet-B",
        'ssmlGender'=>'MALE'
    ]
];
$data_string = json_encode($params);
$speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);  
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string)
    ]                                                                       
);
$response = curl_exec($handle);              
$responseDecoded = json_decode($response, true);  
curl_close($handle);
if($responseDecoded['audioContent']){
    return $responseDecoded['audioContent'];                
} 

我下载了音频,但我在 ssml 中提到的暂停/中断不起作用。我尝试将数据传递给 $params 如下

$params = "{
    'input':{
     'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
          standard <break time=\"1s\"/>is defined by the
          <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
    },
    'voice':{
      'languageCode':'en-us',
      'name':'en-US-Standard-B',
      'ssmlGender':'MALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
}";

但我收到以下错误:

Array ( [error] => Array ( [code] => 400 [message] => 收到无效的 JSON 有效负载。未知名称 "": 根元素必须是消息。 [status] => INVALID_ARGUMENT [details] => Array ( [0] => Array ( [@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] => Array ( [0] => Array ( [description] => 收到无效的 JSON 有效负载。未知名称“”:根元素必须是消息。)))))))

如何解决这个问题?

标签: phpgoogle-cloud-platformtext-to-speechgoogle-text-to-speech

解决方案


在我的工作示例文本到语音转换下面,您可以根据需要重做:

public static function getSound($text)
        {            
            
            $text = trim($text);

            if($text == '') return false;
            
            $params = [
                "audioConfig"=>[
                    "audioEncoding"=>"LINEAR16",
                    "pitch"=> "1",
                    "speakingRate"=> "1",
                    "effectsProfileId"=> [
                        "medium-bluetooth-speaker-class-device"
                      ]
                ],
                "input"=>[
                    "text"=>$text
                ],
                "voice"=>[
                    "languageCode"=> "en-US",
                    "name" =>"en-US-Wavenet-F"
                ]
            ];

            $data_string = json_encode($params);

            $url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
            $handle = curl_init($url);
            
            curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); 
            curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);  
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($handle, CURLOPT_HTTPHEADER, [                                                                          
                'Content-Type: application/json',                                                                                
                'Content-Length: ' . strlen($data_string)
                ]                                                                       
            );
            $response = curl_exec($handle);              
            $responseDecoded = json_decode($response, true);  
            curl_close($handle);
            if($responseDecoded['audioContent']){
                return $responseDecoded['audioContent'];                
            } 

            return false;  
        }

using:
public static function saveSound($text)
   {
      $speech_data = SpeechAPI::getSound($text);//see method upper

      if($speech_data) {                
         $file_name = strtolower(md5(uniqid($text)) . '.mp3');
         $path = FileUpload::getFolder();//just return directory path
         if(file_put_contents($path.$file_name, base64_decode($speech_data))){
             return $file_name;
             }
         }

        return null;
   }

对于 SSML 标准,需要更改输入参数:

$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
            standard <break time=\"1s\"/>is defined by the
            <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>";
$params = [
    "audioConfig"=>[
    "audioEncoding"=>"LINEAR16",
    "pitch"=> "1",
    "speakingRate"=> "1",
    "effectsProfileId"=> [
        "medium-bluetooth-speaker-class-device"
       ]
     ],
     "input"=>[
         //"text"=>$text
         "ssml" => $text
          ],
          "voice"=>[
              "languageCode"=> "en-US",
              "name" =>"en-US-Wavenet-F"
            ]
         ];

关于选择 audioEncoding - https://cloud.google.com/speech-to-text/docs/encoding


推荐阅读