-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (121 loc) · 3.42 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const fs = require("fs");
const { join } = require("path");
const { spawn } = require("child_process");
const tempfile = require("./tempfile");
const ISO6391 = require("iso-639-1");
module.exports = class Balabolka {
/**
* @param {{
* balaPath: string,
* output: string
* }} options
*/
constructor(options = {}) {
this.options = options;
this.balaPath = this.options.balaPath || "balcon";
this.args = ["-w", "temp"];
this.setText = false;
}
set(...args) {
this.args.push(...args);
return this;
}
voice(voice) {
this.args.push("-n", voice);
return this;
}
text(text) {
this.setText = true;
this.args.push("-t", text);
return this;
}
reset() {
this.args = ["-w", "temp"];
return this;
}
/**
* @param {void | string} output
* @returns {Promise<Buffer | string>}
*/
generate(output) {
return new Promise((res, rej) => {
if (!this.setText) {
return rej(new Error("no text specified"));
}
const filepath = (
(output = output || "buffer")
) == "buffer" ?
tempfile() :
join(module.parent.path, output);
this.args[1] = filepath;
let buffers = [];
const balcon = spawn(this.balaPath, this.args);
balcon.stdout.on("data", (c) => buffers.push(c));
balcon.on("close", (code) => {
if (code != 0) {
const error = Buffer.concat(buffers).toString();
return rej(new Error(error));
}
if (output == "buffer") {
res(fs.readFileSync(filepath));
fs.unlinkSync(filepath);
} else {
res(filepath);
}
});
});
}
listVoices() {
return new Promise((res, rej) => {
let buffers = [];
const balcon = spawn(this.balaPath, ["-l"]);
balcon.stdout.on("data", (c) => buffers.push(c));
balcon.stdout.on("end", async () => {
const response = Buffer.concat(buffers).toString();
const pieces = response.split("\r\n");
let voiceList = {};
let category;
for (let x = 0; pieces.length > x; x++) {
const val = pieces[x];
if (val.startsWith(" ")) { // voices
const voiceName = val.trim();
if (voiceName.length <= 0) continue;
const voice = await new Promise((res, rej) => {
let buffers = [];
const balcon = spawn("balcon", ["-n", voiceName, "-m"]);
balcon.stdout.on("data", (b) => buffers.push(b));
balcon.stdout.on("end", () => {
const response = Buffer.concat(buffers).toString();
const pieces = response.split("\r\n");
let unparsedInfo = {};
for (let x = 0; pieces.length - 1 > x; x++) {
const val = pieces[x].trim().split(": ");
unparsedInfo[val[0]] = val[1];
}
let info = {};
info.description = unparsedInfo["Description"];
info.vendor = unparsedInfo["Vendor"];
info.age = unparsedInfo["Age"];
info.gender = unparsedInfo["Gender"] == "Male" ?
"M" : unparsedInfo["Gender"] == "Female" ?
"F" : "U";
info.lang = ISO6391.getCode(unparsedInfo["Language"]);
info.channels = +unparsedInfo["Number of channels"];
info.sampleRate = +unparsedInfo["Samples per second"];
info.bitDepth = +unparsedInfo["Bits per sample"];
res(info);
});
});
voiceList[category][voiceName] = voice;
} else { // create categories
const name = val.trim();
if (name.length <= 0) continue;
category = name;
voiceList[name] = {};
}
}
res(voiceList);
});
});
}
};