有迹象表明,已经发现了一些可能的解决方法,能够以从文本创建音频的能力;其中两个需要请求外部资源,另一个使用@masswerk使用meSpeak.js。
使用在Download the Audio Pronunciation of Words from Google中描述的方法,该方法不能够预先确定哪些字实际上存在为资源上的文件而没有writing a shell script或执行HEAD
请求以检查是否发生网络错误。例如,“do”这个词在下面使用的资源中不可用。
window.addEventListener("load",() => {
const textarea = document.querySelector("textarea");
const audio = document.createElement("audio");
const mimecodec = "audio/webm; codecs=opus";
audio.controls = "controls";
document.body.appendChild(audio);
audio.addEventListener("canplay", e => {
audio.play();
});
let words = textarea.value.trim().match(/\w+/g);
const url = "https://ssl.gstatic.com/dictionary/static/sounds/de/0/";
const mediatype = ".mp3";
Promise.all(
words.map(word =>
fetch(`https://query.yahooapis.com/v1/public/yql?q=select * from data.uri where url="${url}${word}${mediatype}"&format=json&callback=`)
.then(response => response.json())
.then(({query: {results: {url}}}) =>
fetch(url).then(response => response.blob())
.then(blob => blob)
)
)
)
.then(blobs => {
// const a = document.createElement("a");
audio.src = URL.createObjectURL(new Blob(blobs, {
type: mimecodec
}));
// a.download = words.join("-") + ".webm";
// a.click()
})
.catch(err => console.log(err));
});
<textarea>what it does my ninja?</textarea>
资源在Wikimedia Commons Category:Public domain是没有必要从同一个目录服务,看How to retrieve Wiktionary word content?,wikionary API - meaning of words。
如果资源的精确位置是已知的,声音可以请求,虽然URL可包括比这个词本身其他前缀。
fetch("https://upload.wikimedia.org/wikipedia/commons/c/c5/En-uk-hello-1.ogg")
.then(response => response.blob())
.then(blob => new Audio(URL.createObjectURL(blob)).play());
不能完全确定如何使用Wikipedia API,How to get Wikipedia content using Wikipedia's API?,Is there a clean wikipedia API just for retrieve content summary?只得到的音频文件。对于结尾为.ogg
的文本,需要解析JSON
响应,然后需要为资源本身提供第二个请求。
fetch("https://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text&callback=?&page=hello")
.then(response => response.text())
.then(data => {
new Audio(location.protocol + data.match(/\/\/upload\.wikimedia\.org\/wikipedia\/commons\/[\d-/]+[\w-]+\.ogg/).pop()).play()
})
// "//upload.wikimedia.org/wikipedia/commons/5/52/En-us-hello.ogg\"
它记录
Fetch API cannot load https://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text&callback=?&page=hello. No 'Access-Control-Allow-Origin' header is present on the requested resource
如果不是从同一产地要求。我们需要再次尝试使用YQL
,但不确定如何制定查询以避免错误。
第三种方法使用的meSpeak.js
略加修改以生成音频而不使外部请求。该修改是建立一个适当的回调.loadConfig()
方法上述方法是,它需要大约14个半秒钟的三个文件加载之前的音频是
fetch("https://gist.githubusercontent.com/guest271314/f48ee0658bc9b948766c67126ba9104c/raw/958dd72d317a6087df6b7297d4fee91173e0844d/mespeak.js")
.then(response => response.text())
.then(text => {
const script = document.createElement("script");
script.textContent = text;
document.body.appendChild(script);
return Promise.all([
new Promise(resolve => {
meSpeak.loadConfig("https://gist.githubusercontent.com/guest271314/8421b50dfa0e5e7e5012da132567776a/raw/501fece4fd1fbb4e73f3f0dc133b64be86dae068/mespeak_config.json", resolve)
}),
new Promise(resolve => {
meSpeak.loadVoice("https://gist.githubusercontent.com/guest271314/fa0650d0e0159ac96b21beaf60766bcc/raw/82414d646a7a7ef11bb04ddffe4091f78ef121d3/en.json", resolve)
})
])
})
.then(() => {
// takes approximately 14 seconds to get here
console.log(meSpeak.isConfigLoaded());
meSpeak.speak("what it do my ninja", {
amplitude: 100,
pitch: 5,
speed: 150,
wordgap: 1,
variant: "m7"
});
})
.catch(err => console.log(err));
一个警告回放。但是,避免了外部请求。
这将是一个正的之一或两者1)创建FOSS,显影剂保持数据库,或两者兼有常见的和不常见的话声音的目录; 2)进一步开发meSpeak.js
以减少三个必需文件的加载时间;并使用基于Promise
的方法来提供文件加载进度和应用程序准备情况的通知。
在这个用户的估计,这将是一个有用的资源,如果自己创造的,并有助于文件的在线数据库,与特定词的音频文件回应开发商。不完全确定github是否是主持音频文件的适当场所?如果对此类项目感兴趣,则必须考虑可能的选项。