Skip to content

Commit 2dabd0c

Browse files
committed
Sanskrit TTS
0 parents  commit 2dabd0c

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Sanskrit Text-to-Speech 🕉️
2+
### Generate text-to-speech for Sanskrit.
3+
#### संस्कृत वाङ्निर्मिति
4+
5+
## Installation ⬇️
6+
```
7+
$ npm install sanskrit-tts
8+
```
9+
10+
## Usage
11+
#### Import the `sanskrit-tts` module:
12+
```ts
13+
import tts from "sanskrit-tts"
14+
```
15+
```js
16+
const tts = require("sanskrit-tts") // CommonJS
17+
```
18+
19+
### Methods
20+
#### 💾 Save to disk:
21+
```js
22+
tts.saveFile("aham vanam gacchāmi", {
23+
script: "iast",
24+
fileName: "audio.mp3"
25+
});
26+
```
27+
You can make it ***slower*** if you need:
28+
```js
29+
tts.saveFile("अहं वक्तुं शक्नोमि", {
30+
script: "devanagari",
31+
slow: true,
32+
fileName: "audio.mp3"
33+
});
34+
```
35+
36+
#### 🔗 Get URL:
37+
```js
38+
let url = tts.getURL("कथम् अस्ति भवान्", { script: "devanagari" })
39+
console.log(url);
40+
41+
// Returns: https://translate.google.com/translate_tts...
42+
```
43+
44+
#### 🔢 Get Audio in Base64 encoding:
45+
```js
46+
let base64 = tts.getBase64("ओम् नमः शिवाय", { script: "devanagari" })
47+
console.log(base64);
48+
```
49+
50+
#### 🔗 Get All URLs (For queries more than 200 characters): 🔗
51+
```js
52+
let urls = tts.getAllURLs("गणितम् पठामः", { script: "devanagari" })
53+
console.log(urls);
54+
55+
// Returns [Array]
56+
```
57+
> You can also do the same to get Base64 encodings for queries more than 200 characters using `tts.getAllBase64()`.
58+
59+
### 🆎 Supported Scripts
60+
Sanskrit TTS uses the [SanscriptJS](https://npmjs.org/package/sanscriptjs) module to transliterate your Sanskrit input. The following scripts are supported:
61+
- bengali
62+
- devanagari
63+
- gujarati
64+
- gurmukhi
65+
- kannada
66+
- malayalam
67+
- oriya
68+
- tamil
69+
- telugu
70+
- grantha
71+
- grantamil
72+
- hk (Harvard-Kyoto)
73+
- iast (International Alphabet of Sanskrit Transliteration)
74+
- iso15919 (ISO 15919)
75+
- itrans (ITRANS)
76+
- slp1 (Sanskrit Library Phonetic Basic)
77+
- velthuis (Velthuis)
78+
- wx (WX)
79+
80+
### ⚖️ License
81+
MIT

index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const sanscript = require("sanscriptjs");
2+
const googleTTS = require("google-tts-api");
3+
const fs = require("fs");
4+
5+
let tts = {};
6+
7+
let getQuery = (text, script) => {
8+
if (script) {
9+
return (script != "kannada" ? sanscript.t(text, script, "kannada") : text)
10+
} else {
11+
throw console.error("ERROR: Must specify the script of the query.");
12+
}
13+
}
14+
15+
tts.saveFile = (text, options) => {
16+
googleTTS.getAudioBase64(getQuery(text, options.script), {
17+
lang: 'kn',
18+
slow: options.slow ?? false,
19+
host: 'https://translate.google.com',
20+
timeout: 10000,
21+
}).then(response => {
22+
fs.writeFileSync(options.fileName ?? "audio.mp3", Buffer.from(response, 'base64'))
23+
}).catch(console.error);
24+
}
25+
26+
tts.getBase64 = (text, options) => {
27+
googleTTS.getAudioBase64(getQuery(text, options.script), {
28+
lang: 'kn',
29+
slow: options.slow ?? false,
30+
host: 'https://translate.google.com',
31+
timeout: 10000,
32+
}).then(response => {
33+
return response;
34+
}).catch(console.error);
35+
}
36+
37+
tts.getURL = (text, options) => {
38+
return googleTTS.getAudioUrl(getQuery(text, options.script), {
39+
lang: 'kn',
40+
slow: options.slow ?? false,
41+
host: 'https://translate.google.com',
42+
})
43+
}
44+
45+
tts.getAllBase64 = (text, options) => {
46+
googleTTS.getAllAudioBase64(getQuery(text, options.script), {
47+
lang: 'kn',
48+
slow: options.slow ?? false,
49+
host: 'https://translate.google.com',
50+
timeout: 10000,
51+
splitPunct: options.splitPunct ?? "॥.?"
52+
}).then(response => {
53+
return response;
54+
}).catch(console.error);
55+
}
56+
57+
tts.getAllURLs = (text, options) => {
58+
return googleTTS.getAllAudioUrls(getQuery(text, options.script), {
59+
lang: 'kn',
60+
slow: options.slow ?? false,
61+
host: 'https://translate.google.com',
62+
splitPunct: options.splitPunct ?? "॥.?"
63+
})
64+
}
65+
66+
module.exports = tts;
67+

package-lock.json

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "sanskrit-tts",
3+
"version": "1.0.0",
4+
"description": "Generate text-to-speech for Sanskrit.",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/SameeraMurthy/sanskrit-tts.git"
9+
},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"keywords": [
14+
"sanskrit",
15+
"text-to-speech",
16+
"tts",
17+
"voice"
18+
],
19+
"author": "Sameer Murthy",
20+
"license": "MIT",
21+
"dependencies": {
22+
"google-tts-api": "^2.0.2",
23+
"sanscriptjs": "^0.0.1"
24+
}
25+
}

0 commit comments

Comments
 (0)