简体   繁体   English

Javascript本地语言文本转语音

[英]Javascript Native Language Text-To-Speech

I'm making a project for school where we'll teach children to calculate for the first time. 我正在为学校做一个项目,我们将教孩子们第一次计算。 Unfortunately these children can't read so the task will be spoken to them in there native language, which in this case is Dutch. 不幸的是,这些孩子无法阅读,所以他们会用母语向他们讲述任务,在这种情况下是荷兰语。

I've been looking around and most text-to-speech javascript libs provide great support for English, but not for any other language. 我一直在环顾四周,大多数文本到语音的javascript库都为英语提供了很好的支持,但对于任何其他语言都没有。 Also the HTML5 speechSynthesis doesn't support Dutch: 此外,HTML5 speechSynthesis不支持Dutch:

Chrome 35目前支持语音合成语音

In further research I've come across an article where you can use Google Translate Voice to generate any text into speech. 在进一步的研究中,我遇到了一篇文章,您可以使用Google Translate Voice将任何文本生成语音。

You take this as a base URL: http://translate.google.com/translate_tts 您可以将其作为基本网址: http//translate.google.com/translate_tts

Attach your language you want so in my case Dutch 在我的案例荷兰语中附上您想要的语言

http://translate.google.com/translate_tts?tl=nl http://translate.google.com/translate_tts?tl=nl

and attach your text: 并附上你的文字:

"This is a test" translated to Dutch “这是一个测试”翻译成荷兰语

You would expect this to work with longer text but it doesn't. 您希望这可以使用更长的文本,但事实并非如此。

Text to speech which is not working 文本到语音不起作用

Are there any other solutions? 还有其他解决方案吗? The Google Voice speaks Dutch pretty well so I would like the link to somehow work. 谷歌语音很好地说荷兰语,所以我希望链接以某种方式工作。

DISCLAIMER: This is probably not allowed by Google. 免责声明:谷歌可能不允许这样做。 Don't use this without permission from Google. 未经Google许可,请勿使用此功能。

It's possible to use Google's text to speech with texts of any size. 可以使用谷歌的文本到任何规模的文本语音。 It requires a little tweaking though. 它需要稍微调整一下。

Here's the script: 这是脚本:

function textToSpeech(text, language) {
    if(text === undefined || text === null || text.length === 0)
        return

    var vid = document.createElement("video")
    var textParts = []

    text.split(" ").forEach(function(word) {
        var textPartsIndex = textParts.length - 1
        var currentStr = textParts[textPartsIndex]
        if(currentStr !== undefined && currentStr.length + word.length < 100)
            textParts[textPartsIndex] += " " + word
        else
            textParts.push(word)
    })

    function play(txt) {
        vid.src = "http://translate.google.com/translate_tts?tl=" + language + "&q=" + txt
        vid.load()
        vid.play()
    }

    var partIndex = 0
    play(textParts[partIndex])

    vid.addEventListener("ended", function() {
        var part = textParts[++partIndex]
        if(part !== undefined)
            play(part)
        else
            vid.removeEventListener("ended")
    })
}

BEFORE using it you must close all Chrome browser instances and, on a windows machine, open "Run" (WIN+R) and type "chrome --no-referrers" without " 在使用之前,您必须关闭所有Chrome浏览器实例,然后在Windows计算机上打开“运行”(WIN + R)并输入“chrome --no-referrers”,而不是“

On your page, you can now write (I don't understand dutch, so it's just some sample text I found) 在你的页面上,你现在可以写(我不懂荷兰语,所以它只是我发现的一些示例文本)

textToSpeech("Ik ga ervan uit dat de mens een groot vermogen bezit om externe informatie op te slorpen ,\" legt professor Georges uit . Hij kan zelfs 5 tot 6 maal méér informatie verwerken dan het debiet van het gesproken woord , 120 woorden per minuut . Teamgeest is voor mij geen ijdel woord . In de tweede plaats komen puur betekenisloze verhaspelingen niet in aanmerking , want die zijn niet ontstaan door verwarring met een ander woord .", "nl")

If your application is only run locally, ie not hosted, you don't have to run chrome with the --no-referrers command. 如果您的应用程序仅在本地运行,即未托管,则不必使用--no-referrers命令运行chrome。

The reason for disabling referrers is that Google cancels your request if there's a referrer-header. 禁用引荐来源的原因是,如果有引荐来源标头,则Google会取消您的请求。

IFRAME VERSION WHICH DOESN'T REQUIRE --no-referrers 不需要的IFRAME版本 - 不需要推荐人

http://jsfiddle.net/9tTeg/ http://jsfiddle.net/9tTeg/

<body>
<script>
textToSpeech("Ik ga ervan uit dat de mens een groot vermogen bezit om externe informatie op te slorpen ,\" legt professor Georges uit . Hij kan zelfs 5 tot 6 maal méér informatie verwerken dan het debiet van het gesproken woord , 120 woorden per minuut . Teamgeest is voor mij geen ijdel woord . In de tweede plaats komen puur betekenisloze verhaspelingen niet in aanmerking , want die zijn niet ontstaan door verwarring met een ander woord .", "nl")

function textToSpeech(text, language) {
    if(text === undefined || text === null || text.length === 0)
        return

    var vid = document.createElement("iframe")
    document.body.appendChild(vid)
    var textParts = []

    text.split(" ").forEach(function(word) {
        var textPartsIndex = textParts.length - 1
        var currentStr = textParts[textPartsIndex]
        if(currentStr !== undefined && currentStr.length + word.length < 100)
            textParts[textPartsIndex] += " " + word
        else
            textParts.push(word)
    })

    function play(txt) {
        vid.src = "http://translate.google.com/translate_tts?tl=" + language + "&q=" + txt
    }

    var partIndex = 0
    play(textParts[partIndex])

    var intervalId = setInterval(function() {
        var part = textParts[++partIndex]
        if(part !== undefined)
            play(part)
        else
            clearInterval(intervalId)       
    }, 9000)
}
</script>
</body>

Improvements: Instead of separating the string by words, use dot and comma, so the pauses are more natural. 改进:不使用单词分隔字符串,而是使用点和逗号,因此暂停更自然。 Each part is currently being played with 9 seconds each, but if the words are grouped by dots and commas, you could instead calculate the amount of time each part has by looking at the length of the part, since 9 seconds might be too much then. 每个部分当前每个播放9秒,但如果单词按点和逗号分组,您可以通过查看部分的长度来计算每个部分的时间,因为9秒可能太多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM