簡體   English   中英

如何在 Web Speech API 中獲取特定的女性語音語音合成?

[英]How to get a specific female voice speech synthesis in Web Speech API?

我試圖讓女性聲音接受來自 Web Speech API 的“美國英語”。

下面是我的代碼:

var synth = window.speechSynthesis;
var voiceOptioins = synth.getVoices();
var voiceChoice = voiceOptioins[5];

var utterThis = new SpeechSynthesisUtterance(textToSpeech);
utterThis.voice = voiceChoice;

synth.speak(utterThis);

使用此代碼,當前可以發出帶有超低音的男聲。 我嘗試更改voiceOptioins[<index>]中的索引:嘗試使用 0、5、10。使用所有這些索引,只會發出相同的男性聲音。

如何選擇特定的聲音?

試試這個代碼以獲得特定的聲音

const synth = window.speechSynthesis;

const inputForm = document.querySelector("form");
const inputTxt = document.querySelector(".txt");
const voiceSelect = document.querySelector("select");

const pitch = document.querySelector("#pitch");
const pitchValue = document.querySelector(".pitch-value");
const rate = document.querySelector("#rate");
const rateValue = document.querySelector(".rate-value");

let voices = [];

function populateVoiceList() {
    voices = synth.getVoices().sort(function (a, b) {
        const aname = a.name.toUpperCase();
        const bname = b.name.toUpperCase();
        // console.log(aname);
        // console.log(bname);
        // console.log(a)
        if (aname < bname) {
            return -1;
        } else if (aname == bname) {
            return 0;
        } else {
            return +1;
        }
    });
    const selectedIndex =
        voiceSelect.selectedIndex < 0 ? 0 : voiceSelect.selectedIndex;
    voiceSelect.innerHTML = "";

    for (let i = 0; i < voices.length; i++) {
        console.log(voices.length);
        console.log(voices);
        if (i == 6) {
            const option = document.createElement("option");
            console.log(voices[i])
            option.textContent = `${voices[i].name} (${voices[i].lang})`;


            // if (voices[0].default) {
            //     option.textContent += `${voices[0].name} (${voices[0].lang})`;
            // }
            option.setAttribute("data-lang", voices[6].lang);
            option.setAttribute("data-name", voices[6].name);
            voiceSelect.appendChild(option); 
        }
        voiceSelect.selectedIndex = selectedIndex;
    }
}

populateVoiceList();

if (speechSynthesis.onvoiceschanged !== undefined) {
    speechSynthesis.onvoiceschanged = populateVoiceList;
}

function speak() {
    if (synth.speaking) {
        console.error("speechSynthesis.speaking");
        return;
    }

    if (inputTxt.value !== "") {
        const utterThis = new SpeechSynthesisUtterance(inputTxt.value);

        utterThis.onend = function (event) {
            console.log("SpeechSynthesisUtterance.onend");
        };

        utterThis.onerror = function (event) {
            console.error("SpeechSynthesisUtterance.onerror");
        };

        const selectedOption =
            voiceSelect.selectedOptions[0].getAttribute("data-name");

        for (let i = 0; i < voices.length; i++) {
            if (voices[i].name === selectedOption) {
                utterThis.voice = voices[i];

                break;
            }
        }
        utterThis.pitch = pitch.value;
        utterThis.rate = rate.value;
        synth.speak(utterThis);
    }
}

inputForm.onsubmit = function (event) {
    event.preventDefault();

    speak();

    inputTxt.blur();
};

pitch.onchange = function () {
    pitchValue.textContent = pitch.value;
};

rate.onchange = function () {
    rateValue.textContent = rate.value;
};

voiceSelect.onchange = function () {
    speak();
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM