刚刚使用了一下 乐鑫 ESP-ADF 的 baidu 语音合成 demo
然后结合网上例程,
梳理一下调用过程
1. 第一步,根据 在 baidu 申请的 API Key 和 Secret Key 获取 tocken (令牌)
API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU
上面的 token 24.1d62c2210aae7e03f3987b4d4579e8d3.2592000.1527812681.282335-11175544 有效期是 一个月(30天)
2. 第二步, 填入上面获取的令牌 和 txt 文字, 生成语音文件。
http://tsn.baidu.com/text2audio?tex=%E6 … 5-11175544
上面的测试大概1个月会失效, 失效后可以用第一步产生的 token 代入 第二步的链接的 tok 参数。
2018-07-10 更新:
------------------------------------------------------
播放: 挖坑网,我的最爱
离线
哈, 搞定一个php 语音识别!
参考这个: https://blog.csdn.net/weixin_36429334/article/details/53893824
首先要安装curl:
sudo apt-get install php-curl
语音文件可以从这里下载: http://yuyin.baidu.com/docs/asr/54
http://speech-doc.gz.bcebos.com/rest-api-asr/public_audio/16k.wav
代码里面采样率改成 16000
我改的代码:
root@ubuntu:~# cat test.php
<?php
define('AUDIO_FILE', "./text2audio_1.wav"); //语音文件地址,值支持本地
$url = "http://vop.baidu.com/server_api";
//put your params here
$cuid = "addd";
$apiKey = "FvNYWABLHgYC3aMMgaO7j5Qh";
$secretKey = "uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU";
$auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];
$audio = file_get_contents(AUDIO_FILE);
$base_data = base64_encode($audio);
$array = array(
"format" => "wav",
"rate" => 16000,
"channel" => 1,
// "lan" => "zh",
"token" => $token,
"cuid"=> $cuid,
//"url" => "http://www.xxx.com/sample.pcm",
//"callback" => "http://www.xxx.com/audio/callback",
"len" => filesize(AUDIO_FILE),
"speech" => $base_data,
);
$json_array = json_encode($array);
$content_len = "Content-Length: ".strlen($json_array);
$header = array ($content_len, 'Content-Type: application/json; charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);
echo $response;
$response = json_decode($response, true);
var_dump($response);
php test.php 运行结果:
root@ubuntu:~# php test.php
{"corpus_no":"6550630004628230343","err_msg":"success.","err_no":0,"result":["北京科技馆,"],"sn":"949063508651525187400"}
array(5) {
["corpus_no"]=>
string(19) "6550630004628230343"
["err_msg"]=>
string(8) "success."
["err_no"]=>
int(0)
["result"]=>
array(1) {
[0]=>
string(18) "北京科技馆,"
}
["sn"]=>
string(21) "949063508651525187400"
}
看起来效果不错.
离线
离线
离线
刚刚有网友传了一个文件 b.wav, 结果识别效果惨不忍睹.
源码:
<?php
define('AUDIO_FILE', "./b.wav"); //语音文件地址,值支持本地
$url = "http://vop.baidu.com/server_api";
//put your params here
$cuid = "addd";
$apiKey = "FvNYWABLHgYC3aMMgaO7j5Qh";
$secretKey = "uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU";
$auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];
$audio = file_get_contents(AUDIO_FILE);
$base_data = base64_encode($audio);
$array = array(
"format" => "wav",
"rate" => 8000,
"channel" => 1,
// "lan" => "zh",
"token" => $token,
"cuid"=> $cuid,
//"url" => "http://www.xxx.com/sample.pcm",
//"callback" => "http://www.xxx.com/audio/callback",
"len" => filesize(AUDIO_FILE),
"speech" => $base_data,
);
$json_array = json_encode($array);
$content_len = "Content-Length: ".strlen($json_array);
$header = array ($content_len, 'Content-Type: application/json; charset=utf-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);
echo $response;
$response = json_decode($response, true);
var_dump($response);
执行结果:
root@ubuntu:~# php test2.php
{"corpus_no":"6550913871967495989","err_msg":"success.","err_no":0,"result":["米,米,哎你好,你好你是网上说的大扎吧,宝贝,我是那个人的爱的,哦你说唉你好吴先生联系就给他呗学生打过四天该你还的上眼点六克的实力不是特别的想你又没给他都说的不想去检查看一下,哦没有,你咋恁闲下午开家长会的时候我没那个医院为咱们学校做了一个免费的公益的感慨像不单压你倒是可以带你飞都说给你个也感慨下我看一下,嗯好的嗯还来的不打给她的人呢好的谢谢再见再减,"],"sn":"976533065391525253493"}
array(5) {
["corpus_no"]=>
string(19) "6550913871967495989"
["err_msg"]=>
string(8) "success."
["err_no"]=>
int(0)
["result"]=>
array(1) {
[0]=>
string(531) "米,米,哎你好,你好你是网上说的大扎吧,宝贝,我是那个人的爱的,哦你说唉你好吴先生联系就给他呗学生打过四天该你还的上眼点六克的实力不是特别的想你又没给他都说的不想去检查看一下,哦没有,你咋恁闲下午开家长会的时候我没那个医院为咱们学校做了一个免费的公益的感慨像不单压你倒是可以带你飞都说给你个也感慨下我看一下,嗯好的嗯还来的不打给她的人呢好的谢谢再见再减,"
}
["sn"]=>
string(21) "976533065391525253493"
}
离线
<?php
require_once 'AipSpeech.php';
const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';
$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
$result = $client->synthesis('欢迎来到挖坑网', 'zh', 1, array('vol' => 8,));
// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
file_put_contents('/var/www/html/files/audio.mp3', $result);
}
?>
参考网址: 百度语音 接口说明
百度语音合成在线体验: http://yuyin.baidu.com/#try
php sdk 下载地址: https://github.com/Baidu-AIP/php-sdk
php sdk 下载命令: git clone https://github.com/Baidu-AIP/php-sdk.git
执行: php test.php
自动生成 /var/www/html/audio.mp3
试听地址: https://whycan.cn/files/audio.mp3
离线
<?php
require_once 'AipSpeech.php';
const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';
const AUDIO_FILE = '/var/www/html/files/audio.mp3';
$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
//printf($_GET["Text"]);
unlink(AUDIO_FILE);
$result = $client->synthesis($_GET['Text'], 'zh', 1, array('vol' => 8,));
// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
file_put_contents(AUDIO_FILE, $result);
}
printf('<video width="320" height="240" controls>
<source src="/files/audio.mp3?%s" type="audio/mpeg">
Your browser does not support audio in video tag.
</video><br /><br /><br />', time());
printf('download: <a href="/files/audio.mp3">audio.mp3</a>');
?>
做了一个简单的测试: 欢迎来到挖坑网的世界
本地命令行测试:
php-cgi -f test1.php "Text=时代"
离线
<?php
require_once 'AipSpeech.php';
const APP_ID = 'whycan.cn';
const API_KEY = 'FvNYWABLHgYC3aMMgaO7j5Qh';
const SECRET_KEY = 'uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU';
const AUDIO_FILE = '/var/www/html/files/audio.mp3';
$client= new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
//printf($_GET["Text"]);
//printf($_POST["Text"]);
if(isset($_GET["Text"]))
{
$Text = $_GET["Text"];
}
else if(isset($_POST["Text"]))
{
$Text = $_POST["Text"];
}
unlink(AUDIO_FILE);
$result = $client->synthesis($Text, 'zh', 1, array('vol' => 8,));
// 识别正确返回语音二进制 错误则返回json 参照下面错误码
if(!is_array($result)){
file_put_contents(AUDIO_FILE, $result);
}
printf('<video width="320" height="240" controls>
<source src="/files/audio.mp3?%s" type="audio/mpeg">
Your browser does not support audio in video tag.
</video><br /><br /><br />', time());
printf('download: <a href="/files/audio.mp3">audio.mp3</a>');
?>
<form action="test1.php" method="post">
<p>Input your text: <input type="text" name="Text" value=<?php echo $Text; ?> /></p>
<input type="submit" name="submit" value="Submit" />
</form>
单行输入框搞定.
离线
API Key: FvNYWABLHgYC3aMMgaO7j5Qh
Secret Key: uc1IRoK4yMBqvAnmizT0ZZ7nR5OgYREU然后make flash monitor 。
启动ESP32 之后 会有什么效果呢?
连上WIFI之后会与baidu的 api 交互,根据你的文字生成mp3文件, esp32播放出来,插上耳机或者接上喇叭可以听到.
第二个问题我得想一下,一段时间没弄了,要恢复一下堆栈。
离线