我正在使用Electron创建一个应用程序。 因此,我想将SpeechSynthesis语音输出到wav文件。
这只是日本人的声音。 (所以我不能像meSpeak一样使用库。)
我想使用安装在本地PC上的语音。 (如呗咏,Softalk等......)
如果是以下代码,则无法正常输出。
虽然您可以输出文件本身, 内容是无意的。
测试环境是macOS 10.13.1。
我该怎么做?
主要流程:
// This code is a part.
// Actually there are many different properties,
// This code shows only the minimum necessary part.
let obj = {
speech: "コンニチワ", // it was converted from sentence to reading katakana
cast: {
voice: {
pitch: 100,
speed: 100,
volume: 50.0,
name: ""
}
}
}
// mainWindow is BrowserWindow
mainWindow.webContents.send('record-speech', obj, "example.wav")
渲染程序:
ipcRenderer.on('record-speech', (event, obj, path) => {
// Setup utterance
let voice = null
speechSynthesis.onvoiceschanged = () => {
voice = speechSynthesis.getVoices().find(({
name: _name
}) => _name === obj.cast.voice.name)
}
let utter = new SpeechSynthesisUtterance()
utter.lang = "ja-JP"
utter.text = obj.speech
utter.pitch = obj.cast.voice.pitch / 100
utter.rate = obj.cast.voice.speed / 20
utter.voice = voice
utter.volume = obj.cast.voice.volume / 100
let audioContext = new AudioContext()
navigator.getUserMedia({audio: true}, (stream) => {
let audioInput = audioContext.createMediaStreamSource(stream)
// Using 'wave-recorder' package of npm
let recorder = WaveRecorder(audioContext, {
channels: 2,
bitDepth: 32,
silenceDuration: 0.1
})
audioInput.connect(recorder.input)
let fileStream = fs.createWriteStream(path)
recorder.pipe(fileStream)
recorder.on('header', (header) => {
let headerStream = fs.createWriteStream(path, {
start: 0,
flags: 'w'
})
headerStream.write(header)
headerStream.end()
})
utter.onend = ((event) => {
console.log("Speak ended");
})
speechSynthesis.speak(utter)
}, (e) => { console.log(e); })
})