功能: 调用百度云语音接口api,实现文字转语音
import win.ui;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=253;bottom=381)
mainForm.add(
button={cls="button";text="合 成";left=27;top=290;right=158;bottom=329;z=3};
button2={cls="button";text="播放";left=183;top=295;right=242;bottom=324;z=8};
edit={cls="edit";text="1";left=142;top=346;right=188;bottom=371;edge=1;multiline=1;z=11};
edtTxt={cls="edit";left=9;top=8;right=244;bottom=181;autohscroll=false;edge=1;multiline=1;vscroll=1;z=2};
radiobutton={cls="radiobutton";text="标准男声";left=24;top=225;right=98;bottom=248;z=4};
radiobutton2={cls="radiobutton";text="标准女声";left=25;top=254;right=99;bottom=277;checked=1;z=5};
radiobutton3={cls="radiobutton";text="度逍遥";left=131;top=226;right=205;bottom=249;z=6};
radiobutton4={cls="radiobutton";text="度丫丫";left=132;top=256;right=206;bottom=279;z=7};
static={cls="static";text="语速";left=29;top=200;right=64;bottom=217;transparent=1;z=9};
static2={cls="static";text="输出文件名(*.mp3):";left=19;top=350;right=136;bottom=367;transparent=1;z=10};
trackbar={cls="trackbar";left=55;top=189;right=180;bottom=219;max=9;min=1;z=1}
)
/*}}*/
import inet.whttp
import web.json
import crypt.bin
var whttp = inet.whttp()
mainForm.button.oncommand = function(id,event){
if(!#mainForm.edtTxt.text) return ;
var text = mainForm.edtTxt.text
var per = 3
if(mainForm.radiobutton.checked) per = 2
if(mainForm.radiobutton2.checked) per = 0
if(mainForm.radiobutton4.checked) per = 4
var rtn = whttp.post("https://cloud.baidu.com/aidemo","type=tns&spd="+mainForm.trackbar.pos+"&vol=9&per="+per+"&tex="+inet.url.encode(text))
if(rtn){
if(string.indexOf(rtn,'{"errno":0')){
var tab = web.json.parse(rtn)
if(tab && tab.data){
tab = string.split(tab.data,",")
if(#tab == 2){
string.save(#mainForm.edit.text?mainForm.edit.text+".mp3":"\temp.mp3",crypt.bin.decodeBase64(tab[2]) )
mainForm.msgbox("转换成功")
return ;
}
}
}
}
mainForm.msgbox("转换失败")
}
mainForm.edit.threadCallable()
mainForm.button2.oncommand = function(id,event){
if(!io.exist(#mainForm.edit.text?mainForm.edit.text+".mp3":"\temp.mp3")) return ;
thread.invoke(
function(mainForm){
import fsys.media
var md = fsys.media(#mainForm.edit.text?mainForm.edit.text+".mp3":"\temp.mp3")
md.play("wait")
md.close()
},
mainForm
)
}
mainForm.trackbar.setFrequency(2)
mainForm.trackbar.pos = 4
mainForm.enableDpiScaling();
mainForm.show();
return win.loopMessage();