-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
79 lines (74 loc) · 3.16 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
//@ts-nocheck
/**
* @typedef {(
* "en"
* "af-ZA"|"am-ET"|"hy-AM"|"az-AZ"|"id-ID"|"ms-MY"|"bn-BD"|"bn-IN"|"ca-ES"|"cs-CZ"|"da-DK"|"de-DE"|
* "en-AU"|"en-CA"|"en-GH"|"en-GB"|"en-IN"|"en-IE"|"en-KE"|"en-NZ"|"en-NG"|"en-PH"|"en-SG"|"en-ZA"|
* "en-TZ"|"en-US"|"es-AR"|"es-BO"|"es-CL"|"es-CO"|"es-CR"|"es-EC"|"es-SV"|"es-ES"|"es-US"|"es-GT"|
* "es-HN"|"es-MX"|"es-NI"|"es-PA"|"es-PY"|"es-PE"|"es-PR"|"es-DO"|"es-UY"|"es-VE"|"eu-ES"|"fil-PH"|
* "fr-CA"|"fr-FR"|"gl-ES"|"ka-GE"|"gu-IN"|"hr-HR"|"zu-ZA"|"is-IS"|"it-IT"|"jv-ID"|"kn-IN"|"km-KH"|
* "lo-LA"|"lv-LV"|"lt-LT"|"hu-HU"|"ml-IN"|"mr-IN"|"nl-NL"|"ne-NP"|"nb-NO"|"pl-PL"|"pt-BR"|"pt-PT"|
* "ro-RO"|"si-LK"|"sk-SK"|"sl-SI"|"su-ID"|"sw-TZ"|"sw-KE"|"fi-FI"|"sv-SE"|"ta-IN"|"ta-SG"|"ta-LK"|
* "ta-MY"|"te-IN"|"vi-VN"|"tr-TR"|"ur-PK"|"ur-IN"|"el-GR"|"bg-BG"|"ru-RU"|"sr-RS"|"uk-UA"|"he-IL"|
* "ar-IL"|"ar-JO"|"ar-AE"|"ar-BH"|"ar-DZ"|"ar-SA"|"ar-IQ"|"ar-KW"|"ar-MA"|"ar-TN"|"ar-OM"|"ar-PS"|
* "ar-QA"|"ar-LB"|"ar-EG"|"fa-IR"|"hi-IN"|"th-TH"|"ko-KR"|"zh-TW"|"yue-Hant-HK"|"ja-JP"|"zh-HK"|"zh"
* )} Language
*/
const googleTTS = require('google-tts-api'); // CommonJS
const fs = require('fs');
const Stream = require('stream');
function base64toBinaryStream(base64Text){
// Convert base64 stream to binary stream
const audioBinaryStream = new Stream.Readable();
audioBinaryStream.push(Buffer.from(base64Text, 'base64'));
// Indicate end of stream
audioBinaryStream.push(null);
return audioBinaryStream;
}
/**
* @param {string} text
* @param {PlainObject} cfg
* @param {Language} cfg.lang
* @param {boolean} cfg.slow
* @param {string} cfg.host
* @param {number} cfg.timeout
* @param {string} cfg.splitPunct
*/
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);
}
/**
* @param {string} text
* @param {Language} [lang="en-GB"]
* @param {boolean} [slow=false]
* @param {string} cfg.host
* @param {number} cfg.timeout
* @param {string} cfg.splitPunct
*/
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;
}
/**
* @param {string} filePath
* @param {string} text
* @param {PlainObject} cfg
* @param {Language} [cfg.lang="en-GB"]
* @param {number} [cfg.slow=false]
* @param {string} cfg.host
* @param {number} cfg.timeout
* @param {string} cfg.splitPunct
*/
function saveToFile(filePath, text, {lang = 'en-GB', slow = false, host, timeout, splitPunct} = {}) {
const stream = new Stream.PassThrough();
const writeStream = fs.createWriteStream(filePath);
downloadFromInfoCallback(stream, text, {lang, slow, host, timeout, splitPunct });
stream.pipe(writeStream);
stream.on('end', () => writeStream.close());
}
module.exports.getVoiceStream = getVoiceStream;
module.exports.saveToFile = saveToFile;