Skip to content

Commit 6980261

Browse files
committed
Add Google TTS Provider
Adds a Google Translate TTS Provider, allowing users to use TTS without an API key from an existing provider.
1 parent f5e5475 commit 6980261

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Experimental support for TTS. Today the following providers are available:
365365
* voicerss
366366
* Microsoft Cognitive Services (Bing Text to Speech API)
367367
* AWS Polly
368+
* Google
368369

369370
It will use the one you configure in settings.json. If you define settings for multiple TTS services, it will not be guaranteed which one it will choose!
370371

@@ -539,6 +540,70 @@ This is the current list of voice names and their corresponding language and acc
539540
| Welsh | cy-GB | Female | Gwyneth |
540541
| Welsh | English | en-GB-WLS | Male | Geraint |
541542

543+
#### Google
544+
545+
Does not require any API keys. Please note that Google has been known in the past to change the requirements for its Text-to-Speech API, and this may stop working in the future.
546+
547+
The following language codes are supported
548+
549+
| Language code | Language |
550+
| ------------- | -------- |
551+
| af | Afrikaans |
552+
| sq | Albanian |
553+
| ar | Arabic |
554+
| hy | Armenian |
555+
| bn | Bengali |
556+
| ca | Catalan |
557+
| zh | Chinese |
558+
| zh-cn | Chinese (Mandarin/China) |
559+
| zh-tw | Chinese (Mandarin/Taiwan) |
560+
| zh-yue | Chinese (Cantonese) |
561+
| hr | Croatian |
562+
| cs | Czech |
563+
| da | Danish |
564+
| nl | Dutch |
565+
| en | English |
566+
| en-au | English (Australia) |
567+
| en-gb | English (Great Britain) |
568+
| en-us | English (United States) |
569+
| eo | Esperanto |
570+
| fi | Finnish |
571+
| fr | Franch |
572+
| de | German |
573+
| el | Greek |
574+
| hi | Hindi |
575+
| hu | Hungarian |
576+
| is | Icelandic |
577+
| id | Indonesian |
578+
| it | Italian |
579+
| ja | Japanese |
580+
| ko | Korean |
581+
| la | Latin |
582+
| lv | Latvian |
583+
| mk | Macedonian |
584+
| no | Norwegian |
585+
| pl | Polish |
586+
| pt | Portuguese |
587+
| pt-br | Portuguese (Brazil) |
588+
| ro | Romanian |
589+
| ru | Russian |
590+
| sr | Serbian |
591+
| sk | Slovak |
592+
| es | Spanish |
593+
| es-es | Spanish (Spain) |
594+
| es-us | Spanish (United States) |
595+
| sw | Swahili |
596+
| sv | Swedish |
597+
| ta | Tamil |
598+
| th | Thai |
599+
| tr | Turkish |
600+
| vi | Vietnamese |
601+
| cy | Welsh |
602+
603+
Action is:
604+
605+
/[Room name]/say/[phrase][/[language_code]][/[announce volume]]
606+
/sayall/[phrase][/[language_code]][/[announce volume]]
542607

543608
Line-in
544609
-------

lib/tts-providers/google.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const crypto = require('crypto');
3+
const fs = require('fs');
4+
const http = require('http');
5+
const path = require('path');
6+
const settings = require('../../settings');
7+
8+
function google(phrase, language) {
9+
// Use Google tts translation service to create a mp3 file
10+
const ttsRequestUrl = 'http://translate.google.com/translate_tts?client=tw-ob&tl=' + language + '&q=' + phrase;
11+
12+
// Construct a filesystem neutral filename
13+
const phraseHash = crypto.createHash('sha1').update(phrase).digest('hex');
14+
const filename = `google-${phraseHash}-${language}.mp3`;
15+
const filepath = path.resolve(settings.webroot, 'tts', filename);
16+
17+
const expectedUri = `/tts/${filename}`;
18+
try {
19+
fs.accessSync(filepath, fs.R_OK);
20+
return Promise.resolve(expectedUri);
21+
} catch (err) {
22+
console.log(`announce file for phrase "${phrase}" does not seem to exist, downloading`);
23+
}
24+
25+
return new Promise((resolve, reject) => {
26+
var file = fs.createWriteStream(filepath);
27+
var options = {"headers": {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}, "host": "translate.google.com", "path": "/translate_tts?client=tw-ob&tl=" + language + "&q=" + encodeURIComponent(phrase) }
28+
var callback = function (response) {
29+
if (response.statusCode < 300 && response.statusCode >= 200) {
30+
response.pipe(file);
31+
file.on('finish', function () {
32+
file.end();
33+
resolve(expectedUri);
34+
});
35+
} else {
36+
reject(new Error(`Download from google TTS failed with status ${response.statusCode}, ${response.message}`));
37+
38+
}
39+
}
40+
41+
http.request(options, callback).on('error', function (err) {
42+
fs.unlink(dest);
43+
reject(err);
44+
}).end();
45+
});
46+
}
47+
48+
module.exports = google;

0 commit comments

Comments
 (0)