12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- const childProcess = require('child_process')
- const SayPlatformBase = require('./base.js')
- const BASE_SPEED = 0 // Unsupported
- const COMMAND = 'powershell'
- class SayPlatformWin32 extends SayPlatformBase {
- constructor () {
- super()
- this.baseSpeed = BASE_SPEED
- }
- buildSpeakCommand ({ text, voice, speed }) {
- let args = []
- let pipedData = ''
- let options = {}
- let psCommand = `Add-Type -AssemblyName System.speech;$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;`
- if (voice) {
- psCommand += `$speak.SelectVoice('${voice}');`
- }
- if (speed) {
- let adjustedSpeed = this.convertSpeed(speed || 1)
- psCommand += `$speak.Rate = ${adjustedSpeed};`
- }
- psCommand += `$speak.Speak([Console]::In.ReadToEnd())`
- pipedData += text
- args.push(psCommand)
- options.shell = true
- return { command: COMMAND, args, pipedData, options }
- }
- buildExportCommand ({ text, voice, speed, filename }) {
- let args = []
- let pipedData = ''
- let options = {}
- let psCommand = `Add-Type -AssemblyName System.speech;$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;`
- if (voice) {
- psCommand += `$speak.SelectVoice('${voice}');`
- }
- if (speed) {
- let adjustedSpeed = this.convertSpeed(speed || 1)
- psCommand += `$speak.Rate = ${adjustedSpeed};`
- }
- if (!filename) throw new Error('Filename must be provided in export();')
- else {
- psCommand += `$speak.SetOutputToWaveFile('${filename}');`
- }
- psCommand += `$speak.Speak([Console]::In.ReadToEnd());$speak.Dispose()`
- pipedData += text
- args.push(psCommand)
- options.shell = true
- return { command: COMMAND, args, pipedData, options }
- }
- runStopCommand () {
- this.child.stdin.pause()
- childProcess.exec(`taskkill /pid ${this.child.pid} /T /F`)
- }
- convertSpeed (speed) {
- // Overriden to map playback speed (as a ratio) to Window's values (-10 to 10, zero meaning x1.0)
- return Math.max(-10, Math.min(Math.round((9.0686 * Math.log(speed)) - 0.1806), 10))
- }
- getVoices () {
- let args = []
- let psCommand = 'Add-Type -AssemblyName System.speech;$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;$speak.GetInstalledVoices() | % {$_.VoiceInfo.Name}'
- args.push(psCommand)
- return { command: COMMAND, args }
- }
- }
- module.exports = SayPlatformWin32
|