百度TTS封装&字符数统计

百度TTS调用封装,字符数统计方法

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://greasyfork.org/scripts/402986-%E7%99%BE%E5%BA%A6tts%E5%B0%81%E8%A3%85-%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%9F%E8%AE%A1/code/%E7%99%BE%E5%BA%A6TTS%E5%B0%81%E8%A3%85%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%9F%E8%AE%A1.js?version=803398

//返回值:isError
//0(false):操作成功
//-1:TXT文本过长
//-2:速度错误
//-3:音调错误
//-4:音量错误
//-5:发音人错误
//-6:自动删除和自动循环矛盾
//-7:请求串超过url最大值(414)

window.T2S = function(
    TXT = "TTS测试", //需要转语音的文本
    IsKillChrome = true, //是否克服新版(18年以后)Chrome部分页面禁止自动播放的问题
    Volce = 9, //音量,范围:[0~15],5为正常
    Speed = 7, //语速,范围:[0~9],5为正常
    tone = 5, //音调,范围:[0~9],5为正常
    Per = 4, //发音人, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为度丫丫
    //精品语音库:106:度博文,110:度小童,111:度小萌,103:度米朵,5:度小娇
    IsLoop = false, //是否循环
    IsDelate = true, //是否自动删除TTS生成的audio(与上一参数无法同时为真)
    AudioID = "taoSignAudio" //新建的audio标签的id(如果需要删除,则无所谓)
) {
    if (TXT.length > 900) {
        return -1;
    }
    if (Speed > 15 || Speed < 0) {
        return -2;
    }
    if (Tone > 9 || tone < 0) {
        return -3;
    }
    if (Volce > 9 || Volce < 0) {
        return -4;
    }
    if (Per < 0 || (Per > 6 && Per !== 106 && Per !== 110 && Per !== 111 && Per !== 103)) {
        return -5;
    }
    if (IsDelate && IsLoop) {
        return -6;
    }

    var code = "cuid=baiduid&per=" + Per + "&vol=" + Volce + "&pit=" + Tone + "&lan=zh&spd=" + Speed + "&ctp=1&pdt=311&ie=UTF-8&tex=" + TXT;
    if (CharactersCount(code) > 2047) {
        return -7;
    }
    var tmp;
    if (IsKillChrome) {
        tmp = document.createElement("div");
        tmp.innerHTML = '<iframe src="data:audio/mpeg;base64," allow="autoplay" style="position:absolute;z-index:-999999999" autoplay></iframe>';
        tmp.id = "taoSignAudioTEST";
        document.getElementsByTagName("body")[0].append(tmp);
        setTimeout(function() {
            document.getElementById('taoSignAudioTEST').remove();
        }, 1000);
    }
    tmp = document.getElementById(AudioID);
    if (tmp) {
        if (IsDelate) {
            document.getElementById(AudioID).remove();
        } else {
            tmp.src = "http://tts.baidu.com/text2audio?" + code;
        }
    } else {
        tmp = document.createElement("audio");
        tmp.id = AudioID;
        tmp.src = "http://tts.baidu.com/text2audio?" + code;
        document.getElementsByTagName("body")[0].append(tmp);
    }
    document.getElementById(AudioID).play();

    if (IsDelate) {
        document.getElementById(AudioID).addEventListener('ended', function() {
            document.getElementById(AudioID).remove();
        });
    }
    return 0;
};


//Words:文本
//RetType:返回类型
//1:汉字数
//2:字数
//3:字符数
//4:标点数(全角)
//5:标点数(英文)
//6:英文字母
//7:数字

//返回值:
//正常:>=0
//错误值:
//-1:文本参数错误
//-2:请求类型错误
window.CharactersCount = function(Words, RetType) {
    if (Words === "" || Words === undefined) {
        return -1;
    }
    if (RetType > 7 || RetType <= 0) {
        return -2;
    }
    var W = {};
    var Result = [];
    var iNumwords = 0;
    var sNumwords = 0;
    var sTotal = 0;
    var iTotal = 0;
    var eTotal = 0;
    var otherTotal = 0;
    var bTotal = 0;
    var inum = 0;
    var e = 0;
    for (i = 0; i < Words.length; i++) {
        var c = Words.charAt(i);
        if (c.match(/[\u4e00-\u9fa5]/)) {
            if (isNaN(W[c])) {
                iNumwords++;
                W[c] = 1;
            }
            iTotal++;
        }
        if (c.match(/[^\x00-\xff]/)) {
            if (isNaN(W[c])) {
                sNumwords++;
            }
            sTotal++;
        } else {
            eTotal++;
        }
        if (c.match(/[0-9]/)) {
            inum++;
        }
        if (c.match(/[\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]/)) {
            e++;
        }
    }
    if (Number(RetType) == 1) {
        return iTotal;
    }
    if (Number(RetType) == 2) {
        return inum + iTotal;
    }
    if (Number(RetType) == 3) {
        return iTotal * 2 + (sTotal - iTotal) * 2 + eTotal;
    }
    if (Number(RetType) == 4) {
        return sTotal - iTotal;
    }
    if (Number(RetType) == 5) {
        return e;
    }
    if (Number(RetType) == 6) {
        return eTotal - inum;
    }
    if (Number(RetType) == 7) {
        return inum;
    }
};