forked from DreamBoy65/soundBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
70 lines (49 loc) · 1.7 KB
/
functions.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
const googleTTS = require('google-tts-api');
const Stream = require('stream');
const langs = require("./langs")
function base64ToBinary(base64Text){
const binary = Buffer.from(base64Text, "base64").toString("binary");
const buffer = new ArrayBuffer(binary.length);
let bytes = new Uint8Array(buffer);
let i = 0;
const bytesLength = buffer.byteLength;
for (i; i < bytesLength; i++) {
bytes[i]=binary.charCodeAt(i) & 0xFF;
}
return bytes;
}
function base64toBinaryStream(base64Text){
const binary = base64ToBinary(base64Text);
const stream = new Stream.PassThrough();
stream.write(binary, "binary");
return stream;
}
function downloadFromInfoCallback(stream, text, {lang, slow, host, timeout, splitPunct}) {
googleTTS.getAudioBase64(text, { lang, slow, host, timeout, splitPunct })
.then(base64Audio => base64toBinaryStream(base64Audio))
.then(audioStream => audioStream.pipe(stream))
.catch(console.error);
}
function getVoiceStream(text, { lang = 'en', slow = false, host = 'https://translate.google.com', timeout = 10000, splitPunct } = {}) {
const stream = new Stream.PassThrough();
downloadFromInfoCallback(stream, text, {lang, slow, host, timeout, splitPunct });
return stream;
}
function findLocale(locale) {
let loc = langs[locale]
if(loc) {
return true;
} else {
return false
}
}
function getAllLocales() {
let array = []
Object.keys(langs).map(c => {
array.push(`${c} => ${langs[c]}`)
})
return array;
}
module.exports.getVoiceStream = getVoiceStream
module.exports.findLocale = findLocale
module.exports.getAllLocales = getAllLocales