在widora-neo上试用了一下在线百度语音合成功能,发现它的中文发音非常不错,音频返回也很快,读文章的时候基本觉察不到间隙。
现将过程记录下来供大家参考,官方的帮助文档在这里:http://yuyin.baidu.com/file/download/660
1. 申请百度语音帐号
到 http://yuyin.baidu.com/ 申请帐号,并创建一个语音合成的应用, 得到API Key。

2. 在widora-neo上安装curl
opkg update 然后 opkeg install curl
3. 获取ACCESS_TOKEN
3.1 用curl向baidu发送请求, 标示处替换成上面获得的API Key 和 Secret Key:

3.2 返回得到ACCESS_TOKE

注意:ACCESS_TOKEN每个月需要重新获取.
4. 发送中文获取语音mp3文件
现在用curl发送请求就可以返回mp3文件了,当然后面要带上你的access_token. 其中cuid我用了MAC地址.

5.制作发音脚本
5.1 建一个fifo管道,mkfifo -m 777 /home/mp3fifo
5.2 建立一个sh脚本,命名成getvoice,内容如下:

5.3 赋予权限 chmod 755 getvoice
运行一下:
./getvoice 你坐得太久了!休息一下,做做瑜珈吧。。。哈
好了,这下可以展开想象空间,在你的代码里到处加上语音吧!!!!
6.阅读文章
复习linux脚本的时候到了! 有了上面的getvoice,其实也不难了。就是用sed循环提取文章句段,然后调用getvoice而已。
如果你的文件放在/home/book1.txt, 那么可以这样写个脚本readbook:
book=${1}
j=1
pn=sed -n '$=' ${book}
while [ $j -le $pn ]
do
words=sed -n "$j,$(($j+2))p" ${book}
echo ${words}
getvoice ${words}
j=$(($j+3))
done
然后执行:readbook /home/book1.txt 就开始阅读了。
是不是很好玩啊!
11161
#4
创意非常不错!!!
就是这个没感情的声音听久了人会疯掉
用了一段时间的百度语言合成新闻播报,发现效果不错,分享给大家参考。
主要用了2个脚本文件,一个getmp3,另外一个nknews,
getmp3脚本:根据输入的中文来取得百度语音合成的mp3文件,并保存到临时文件/tmp/getmp3.mp3。XXX请应用自己的百度语音合成帐号。
#!/bin/sh
RET=fail
while [[ $RET == fail ]]
do
curl --max-time 20 --retry 0 -G “http://tsn.baidu.com/text2audio?tex=${*}lan=zh&pit=5&cuid=XX:XX:XX:XX:XX:XX&ctp=1&per=0&tok=XXXXXXXXXXXXXXXXXXXXXXX” > /tmp/getmp3.mp3 && RET=OK || RET=fail
done
nknews脚本: 主要是抓取http://cn.nikkei.com/rss.html中文新闻条目,并调用getmp3转换成mp3后合成一个文件,最后用madplay或者mplayer进行播放。
#!/bin/sh
#------- 抓取中文新闻条目 保存到 /tmp/newstitle-------------
curl -s http://cn.nikkei.com/rss.html | grep “CDATA|title” | sed ‘s//新闻提要/;s/</title>//;s/^.[CDATA[//;s/]]>.$//;s/[[:space:]]//g’ > /tmp/newstitle
book=/tmp/newstitle
j=1
#-------- 取得新闻条目总数
pn=sed -n '$=' ${book}
#-------- 创建临时文件
rm -f /tmp/nk.mp3
touch /tmp/nk.mp3
#------- 逐条用getmp3脚本百度语言合成
while [ $j -le $pn ]
do
words=sed -n "$j,$(($j))p" ${book}
echo j=$j ${words}
/home/getmp3 ${words}
#-------- 逐条合并到nk.mp3
cat /tmp/getmp3.mp3 >> /tmp/nk.mp3
wait
j=$(($j+1))
#sleep 5
done
#-------- 用madplay 或 mplayer 播放
madplay /tmp/nk.mp3
#mplayer /tmp/nk.mp3
用curl或php下载RSS新闻提要,用sed抽取中文消息。可以很方便地做成一个新闻提要播报机,定时播报。
========== 百度语音识别 =============
-
在百度语音账号中创建一个语音识别应用,得到相应的ID,API Key,Secret Key.
-
在widora-neo上安装php5:
opkg install php5 php5-cgi php5-mod-curl php5-mod-json
-
参照上面的步骤,获取语音识别应用的access_toke
-
参考官方的sample2.php文件,做一些小修改,制作一个up.php文件如下:
<?php
define('AUDIO_FILE', "/tmp/test.wav");
//put your params here
$cuid = " xxxxxxx";
$apiKey = "xxxxxxxxxxxxx";
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$token ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$url = "http://vop.baidu.com/server_api?cuid=".$cuid."&token=".$token;
//$url = $url."&lan=zh";
$audio = file_get_contents(AUDIO_FILE);
$content_len = "Content-Length: ".strlen($audio);
$header = array ($content_len,'Content-Type: audio/wav; rate=8000',);
$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, $audio);
$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);
?>
其中: cuid,apiKey,secretKey,token分别填入你自己的AppID,API Key,Secret Key,AccessToken.
-
运行alsamixer, 按F4键 将Capture 和 ADC PCM 项调到足够大。不然录音音量太小的话,也会造成无法识别。

-
录制一段语音: arecord -r 8000 -f S16_LE test.wav
注意格式和API要求一致, test.wav文件名和up.php中一致。
-
运行命令:php-cgi up.php
php会发送wav文件到百度API, 并返回识别出的语句。

想问问你发贴关于mraa控制pwm的问题,想知道利用eclipse交叉编译时总是报错如何解决,谢谢!我的QQ:6705641