|
| 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 | + |
0 commit comments