aar中使用百度文字转语音的api

aardio 文章 2023-01-11 22:12 375 0 全屏看文

AI助手支持GPT4.0

这个文件代码来自群里 Kio1 提供

貌似他那个阿蒂欧社区打不开了...

功能: 调用百度云语音接口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();

image.png

---------------------------------------------------------------------

第二种实现

import win.ui;
/*DSG{{*/
var winform = ..win.form( text="TTS测试";right=492;bottom=323 )
winform.add( 
button={ bottom=301;right=444;left=348;top=262;z=2;text="朗读";cls="button" };
static={ bottom=294;text="*";left=33;top=265;transparent=1;z=3;right=319;cls="static" };
edit={ right=467;bottom=249;text="audio levels change often";left=23;multiline=1;top=20;z=1;edge=1;cls="edit" }
)
/*}}*/
 
winform.edit.text = /*
God, Grant me  
the SERENITY, to accept the things I cannot change, 
COURAGE to change the things I can, and 
the WISDOM to know the difference.  
Living one day at a time; Enjoying one moment at a time; 
Accepting hardships as the pathway to peace.  
Taking as He did, this sinful world as it is, not as I would have it; 
Trusting that He will make all things right if I surrender to His Will. 
That I may be reasonably happy in this life, and 
supremely happy with Him forever in the next.  
Amen 
*/
 
import com; 
SpVoice = com.CreateObject("Sapi.SpVoice"); 
 
callEvent = {
        AudioLevel=function(StreamNumber,StreamPosition ,AudioLevel){ 
                winform.static.text = string.repeat(AudioLevel/2,"*")
        }
        EndStream=function(StreamNumber ,StreamPosition ){ 
        }
        StartStream=function(StreamNumber ,StreamPosition ){ 
        }
        Word=function(StreamNumber ,StreamPosition ,CharacterPosition ,Length ){ 
                winform.edit.setsel(CharacterPosition,CharacterPosition + Length)
        } 
}
 
//挂载事件 - TTS会自动设置为回调默认事件
com.Connect(SpVoice,callEvent);  
 
//默认不接收 AudioLevel,要在 Connect 后面 重新设置
SpVoice.setEventInterests( SpVoice.EventInterests | SpVoice.SVEAudioLevel );
 
winform.button.oncommand = function(id,event){
        winform.static.text="";
        SpVoice.Speak(winform.edit.text,1);
}
 
winform.show() 
win.loopMessage();

代码出处:http://www.htmlayout.cn/t/46

软件下载地址:

百度文字转语音.zip: https://url93.ctfile.com/f/147393-762328138-de1955?p=5043 (访问密码: 5043)

-EOF-

AI助手支持GPT4.0