-
Notifications
You must be signed in to change notification settings - Fork 3
/
tts.js
116 lines (103 loc) · 3.76 KB
/
tts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict'
const fs = require('fs')
var child_process = require('child_process')
var Ivona = require('ivona-node/')
var config = require('./config')
var Logger = require('./logger')
class TTS {
constructor(musicPlayer){
this.ivona = new Ivona({
accessKey: config.ivona.accessKey,
secretKey: config.ivona.secretKey
})
if (musicPlayer) this.musicPlayer = musicPlayer
this.speaking = false
this.processing = false
this.voiceOptions = {
body: {
voice: {
gender: 'Female',
language: 'en-GB',
name: 'Amy'
},
parameters: {
volume: 'x-loud',
rate: 'medium',
sentenceBreak : 600,
paragraphBreak : 1200
},
input: {
type: 'application/ssml+xml'
}
}
}
}
speak(string, options) {
if (!options) {
options = {
alert: true,
bgm: true,
volume: 7,
}
}
if (this.processing || this.speaking) {
Logger.log("Attempted to speak... I'm already speaking or processing. Abandoning.")
} else {
Logger.log("TTS processing: " + string.split("\n").join(''))
this.processing = true
this.ivona.createVoice(`<prosody rate="+10%">` + string + `</prosody>`, JSON.parse(JSON.stringify(this.voiceOptions)))
.pipe(fs.createWriteStream('/tmp/text.mp3'))
.on('finish', () => {
var commands = []
if (options.bgm || options.alert) {
if (options.bgm) {
commands.push('sox sounds/silence/silence1000.wav sounds/silence/silence1000.wav /tmp/text.mp3 sounds/silence/silence1500.wav sounds/silence/silence1500.wav sounds/silence/silence5000.wav /tmp/textdelay.mp3')
} else {
commands.push('sox sounds/silence/silence1000.wav sounds/silence/silence1000.wav /tmp/text.mp3 /tmp/textdelay.mp3')
}
if (options.bgm && options.alert) {
commands.push('sox -m sounds/event/cchord.wav /tmp/textdelay.mp3 sounds/bgm/happygit.wav /tmp/new.mp3 trim 0 `soxi -D /tmp/textdelay.mp3`')
} else if (options.alert) {
commands.push('sox -m sounds/event/cchord.wav /tmp/textdelay.mp3 /tmp/new.mp3 trim 0 `soxi -D /tmp/textdelay.mp3`')
} else {
commands.push('sox -m /tmp/textdelay.mp3 sounds/bgm/happygit.wav /tmp/new.mp3 trim 0 `soxi -D /tmp/textdelay.mp3`')
}
if (options.bgm) {
commands.push('sox /tmp/new.mp3 /tmp/new2.mp3 fade t 0 `soxi -D /tmp/new.mp3` 0:05')
} else {
commands.push('mv /tmp/new.mp3 /tmp/new2.mp3')
}
} else {
commands.push('mv /tmp/text.mp3 /tmp/new2.mp3')
}
child_process.exec(commands.join(' && '), () => {
this.processing = false
Logger.log("TTS speaking: " + string.split("\n").join(''))
this.speaking = true
if (this.musicPlayer.playing) {
this.musicPlayer.fadeDown().then(()=>{
child_process.exec(`play /tmp/new2.mp3`, () => {
Logger.log("TTS finished playing")
if (this.musicPlayer.playing) this.musicPlayer.fadeUp()
this.speaking = false
if (options.playnews) {
this.musicPlayer.playPodcast(config.newsPodcastUri)
}
})
})
} else {
child_process.exec(`play /tmp/new2.mp3`, () => {
Logger.log("TTS finished playing")
if (this.musicPlayer.playing) this.musicPlayer.fadeUp()
this.speaking = false
if (options.playnews) {
this.musicPlayer.playPodcast(config.newsPodcastUri)
}
})
}
})
})
}
}
}
module.exports = TTS