Skip to content

Instantly share code, notes, and snippets.

@castella-cake
Last active September 2, 2023 05:00
Show Gist options
  • Save castella-cake/d56aca799b17686d921165a447614391 to your computer and use it in GitHub Desktop.
Save castella-cake/d56aca799b17686d921165a447614391 to your computer and use it in GitHub Desktop.
VOICEVOXエンジンの連続処理テスト
// VOICEVOXエンジンが必要。VOICEVOX: 中国うさぎ です。
const voicevox_host = "127.0.0.1:50021"
const fs = require("fs");
const startDate = Date.now();
const speakerId = 61 // 中国うさぎ / ノーマル
function Synthesis(text,filename) {
return new Promise((resolve,reject) => {
console.log(`VOICEVOXエンジンから 話者ID${speakerId} テキスト "${text}" でクエリを取得します`)
fetch(`http://${voicevox_host}/audio_query?text=${text}&speaker=${speakerId}`, {
method: "POST"
}).then(response => {
if (response.status === 200) {
response.text().then(text => {
const parsedquery = JSON.parse(text)
console.log(`VOICEVOXエンジンからクエリを取得しました +${(Date.now() - startDate) / 1000}s`)
fetch(`http://${voicevox_host}/synthesis?speaker=${speakerId}`, {
method: "POST",
headers: {"Content-Type": "application/json", "accept": "audio/wav"},
body: JSON.stringify(parsedquery)
}).then(response => {
if (response.status === 200) {
response.arrayBuffer().then(res => {
const buffer = Buffer.from(res)
fs.writeFile(`temp/${filename}`, buffer, (err) => {
if (err) {
console.log(`ファイルの書き込みに失敗: ${err}`)
resolve(false)
} else {
console.log(`ファイルを書き込みました +${(Date.now() - startDate) / 1000}s`)
resolve(true)
}
})
})
} else {
console.log(`VOICEVOXの呼び出しに失敗: ${response.status}`)
response.text().then(res => {
console.log(res)
})
reject(false)
}
})
})
} else {
reject(false)
}
})
})
}
Promise.all([
Synthesis("メロスは激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。","test1.wav"),
Synthesis("メロスには政治がわからぬ。メロスは、村の牧人である。","test2.wav"),
Synthesis("あのイーハトーヴォのすきとおった風、郊外のきらきら光る草の波。","test3.wav")
]).then(res => {
console.log(res)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment