JS调用微软TTS DEMO朗读

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script LANGUAGE="JavaScript">
        // Create the Sapi SpVoice object
        var VoiceObj = new ActiveXObject("Sapi.SpVoice");
        // ChangeVoice() function:
        //      This function sets the newly selected voice choice from the Voice
        //      Select box on the Voice object.
        function ChangeVoice() {
            var i = parseInt(idsVoices.value);
            VoiceObj.Voice = VoiceObj.GetVoices().Item(i);
        }
        // ChangeAudioOutput() function:
        //      This function sets the newly selected audio output choice from the
        //      Audio Output Select box on the Voice object.
        function ChangeAudioOutput() {
            var i = parseInt(idsAudioOutputs.value);
            VoiceObj.AudioOutput = VoiceObj.GetAudioOutputs().Item(i);
        }
        // IncRate() function:
        //      This function increases the speaking rate by 1 up to a maximum
        //      of 10.
        function IncRate() {
            if(VoiceObj.Rate < 10) {
                VoiceObj.Rate = VoiceObj.Rate + 1;
            }
        }
        // DecRate() function:
        //      This function decreases the speaking rate by -1 down to a minimum
        //      of -10.
        function DecRate() {
            if(VoiceObj.Rate > -10) {
                VoiceObj.Rate = VoiceObj.Rate - 1;
            }
        }
        // IncVol() function:
        //      This function increases the speaking volume by 10 up to a maximum
        //      of 100.
        function IncVol() {
            if(VoiceObj.Volume < 200) {
                VoiceObj.Volume = VoiceObj.Volume + 10;
            }
        }
        // DecVol() function:
        //      This function decreases the speaking volume by -10 down to a minimum
        //      of 0.
        function DecVol() {
            if(VoiceObj.Volume > 9) {
                VoiceObj.Volume = VoiceObj.Volume - 10;
            }
        }
        // SpeakText() function:
        //      This function gets the text from the textbox and sends it to the
        //      Voice object's Speak() function. The value "1" for the second
        //              parameter corresponds to the SVSFlagsAsync value in the SpeechVoiceSpeakFlags
        //      enumerated type.
        function SpeakText() {
            if(idbSpeakText.value == "SpeakText") {
                // Speak the string in the edit box
                try {
                    VoiceObj.Speak(idTextBox.value, 2);
                }
                catch(exception) {
                    alert("Speak error");
                }
            }
            else if(idbSpeakText.value == "Stop") {
                // Speak empty string to Stop current speaking. The value "2" for
                // the second parameter corresponds to the SVSFPurgeBeforeSpeak
                // value in the SpeechVoiceSpeakFlags enumerated type.
                VoiceObj.Speak("", 2);
            }
        }
    </script>
    <script for="window" EVENT="OnQuit()" LANGUAGE="JavaScript">
        delete VoiceObj;
    </script>
</head>
<body>
<h1 align=center>TTS Demo</h1>
<img alt="" border="0" hspace="0" id="idImage" src="mouthclo.bmp" style="width: 50px;margin-left: -25px;left: 50%;position: relative">
<h1 align=center>
    <textarea id="idTextBox" cols="50" rows="10" wrap="VIRTUAL">请输入内容在此朗读</textarea>
</h1>
<P align=center>
    Rate
    <input id="idbIncRate" name="button1" type="button" onclick="IncRate()" value="+">
    <input id="idbDecRate" name="button2" type="button" onclick="DecRate()" value="-" style="LEFT: 237px; TOP: 292px">
    Volume
    <input id="idbIncVol" name="button3" onclick="IncVol()" style="left:67px;top:318px;" type="button" value="+">
    <input id="idbDecVol" name="button4" onclick="DecVol()" type=button value="-" style="LEFT: 134px; TOP: 377px">
</P>
<P align="center">
    <button id="idbSpeakText" onclick="SpeakText()" value="SpeakText" style="height:25px;left:363px; top: 332px; width: 178px;">
        SpeakText
    </button>
</P>
<P align="center">
    <label>Voice</label>
    <label>Audio Output</label>
</P>
<P align="center">
    <select id="idsVoices" name="Voices" onchange="ChangeVoice()"></select>
    <select id="idsAudioOutputs" name="AudioOutputs" onchange="ChangeAudioOutput()"></select>
</P>
<script LANGUAGE="JavaScript">
    // Code in the BODY of the webpage is used to initialize controls and
    // to handle SAPI events
    /***** Initializer code *****/
    InitializeControls();
    function InitializeControls() {
        // Initialize the Voices and AudioOutput Select boxes
        var VoicesToken = VoiceObj.GetVoices();
        var AudioOutputsToken = VoiceObj.GetAudioOutputs();
        // Add correct strings to Voice Select box
        for(var i = 0; i < VoicesToken.Count; i++) {
            var oOption = document.createElement("OPTION");
            idsVoices.options.add(oOption);
            oOption.innerText = VoicesToken.Item(i).GetDescription();
            oOption.value = i;
        }
        // Add correct strings to Audio Output Select box
        for(var i = 0; i < AudioOutputsToken.Count; i++) {
            var oOption = document.createElement("OPTION");
            idsAudioOutputs.options.add(oOption);
            oOption.innerText = AudioOutputsToken.Item(i).GetDescription();
            oOption.value = i;
        }
    }
    /***** Event handling code *****/
    // These functions are used to handle the SAPI events
    // Handle StartStream event
    function VoiceObj::StartStream()
    {
        idbSpeakText.value = "Stop";
    }
    // Handle EndStream event
    function VoiceObj::EndStream()
    {
        idbSpeakText.value = "SpeakText";
        idImage.src = "mouthclo.bmp";
    }
    // Handle Viseme event
    function VoiceObj::Viseme(StreamNum, StreamPos, Duration, VisemeType, Feature, VisemeId)
    {
        // Map the VisemeId to the appropriate .bmp
        if(VisemeId == 15 || VisemeId == 17 || VisemeId == 18 || VisemeId == 21) {
            idImage.src = "mouthop1.bmp";
        }
        else if(VisemeId == 14 || VisemeId == 16 || VisemeId == 19 || VisemeId == 20) {
            idImage.src = "mouthop2.bmp";
        }
        else if(VisemeId == 4 || VisemeId == 6 || VisemeId == 9 || VisemeId == 12) {
            idImage.src = "mouthop3.bmp";
        }
        else if(VisemeId == 1 || VisemeId == 2 || VisemeId == 3 || VisemeId == 11) {
            idImage.src = "mouthop4.bmp";
        }
        else if(VisemeId == 7 || VisemeId == 8) {
            idImage.src = "mouthnar.bmp";
        }
        else if(VisemeId == 5 || VisemeId == 10 || VisemeId == 13) {
            idImage.src = "mouthmed.bmp";
        }
        else {
            idImage.src = "mouthclo.bmp";
        }
    }
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值