web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

php语法可以实现语音合成和语音识别吗?

289 ℃
     

语音合成Text-To-SpeechTTS是将文本转换为语音的过程。php语法中有许多库和工具可以实现语音合成,下面web建站小编给大家简单介绍一下!

1、利用Google Text-to-Speech API实现语音合成

$text = "Hello, world.";
$url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=[API_KEY]";
$data = array(
  "input" => array(
    "text" => $text
  ),
  "voice" => array(
    "languageCode" => "en-US",
    "name" => "en-US-Wavenet-D"
  ),
  "audioConfig" => array(
    "audioEncoding" => "MP3"
  )
);
$json = json_encode($data);
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json"
));
$result = curl_exec($curl);
curl_close($curl);
 
file_put_contents("output.mp3", $result);

2、利用Google Cloud Speech-to-Text API实现语音识别

$file_name = "audio.wav";
$file_content = file_get_contents($file_name);
 
$url = "https://speech.googleapis.com/v1/speech:recognize?key=[API_KEY]";
$data = array(
  "config" => array(
    "encoding" => "LINEAR16",
    "sampleRateHertz" => 16000,
    "languageCode" => "en-US"
  ),
  "audio" => array(
    "content" => base64_encode($file_content)
  )
);
$json = json_encode($data);
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json"
));
$result = curl_exec($curl);
curl_close($curl);
 
$obj = json_decode($result);
if (isset($obj->results)) {
  $text = $obj->results[0]->alternatives[0]->transcript;
  echo $text;
}

 

html文本如何读取数据库代码示例

php表单提交报错显示适当的错误消息代码

php语法中sleep函数的基本用法

利用php语法中NOT IN排除多个字段功能

利用php语法中WHERE子句排除不等字段功能

标签: google语音合成, google语音识别, php语法, php语音合成, php语音识别, Text-To-Speech, TTS

上面是“php语法可以实现语音合成和语音识别吗?”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4684.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > php入门
本文共计1505个字,预计阅读时长11分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费、无版权的古典音乐下载的网站——Musopen
下一篇: 推荐一款免费在线图片处理工具——佐糖图片平台
x 打工人ai神器