|
| 1 | +import { NodeCG } from "nodecg-types/types/server"; |
| 2 | +import { OpenTTSClient } from "nodecg-io-opentts"; |
| 3 | +import { requireService } from "nodecg-io-core"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Plays a "hello world" tts message inside the graphic of this sample bundle. |
| 7 | + */ |
| 8 | +async function playTTSInGraphic(client: OpenTTSClient, nodecg: NodeCG) { |
| 9 | + const voices = await client.getVoices("en"); |
| 10 | + |
| 11 | + // Get random voice |
| 12 | + const voiceName = Object.keys(voices)[Math.floor(Math.random() * Object.keys(voices).length)]; |
| 13 | + if (voiceName === undefined) throw new Error("no voice available"); |
| 14 | + |
| 15 | + const helloWorldUrl = client.generateWavUrl("Hello World", voiceName); |
| 16 | + await nodecg.sendMessage("setSrc", helloWorldUrl); |
| 17 | +} |
| 18 | + |
| 19 | +module.exports = function (nodecg: NodeCG) { |
| 20 | + nodecg.log.info("Sample bundle for the OpenTTS service started."); |
| 21 | + |
| 22 | + const opentts = requireService<OpenTTSClient>(nodecg, "opentts"); |
| 23 | + |
| 24 | + nodecg.listenFor("ready", () => { |
| 25 | + const client = opentts?.getClient(); |
| 26 | + if (client !== undefined){ |
| 27 | + playTTSInGraphic(client, nodecg) |
| 28 | + .catch(err => nodecg.log.error(`Error while trying to play tts message: ${err.messages}`)); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + opentts?.onAvailable(async (client) => { |
| 33 | + nodecg.log.info("OpenTTS service available."); |
| 34 | + |
| 35 | + const voices = await client.getVoices(); |
| 36 | + const languages = await client.getLanguages(); |
| 37 | + |
| 38 | + nodecg.log.info( |
| 39 | + `OpenTTS server supports ${Object.entries(voices).length} voices in ${languages.length} languages.`, |
| 40 | + ); |
| 41 | + |
| 42 | + await playTTSInGraphic(client, nodecg); |
| 43 | + }); |
| 44 | + |
| 45 | + opentts?.onUnavailable(() => { |
| 46 | + nodecg.log.info("OpenTTS service unavailable."); |
| 47 | + }); |
| 48 | +}; |
0 commit comments