From f9cac4dfaecc401669ab45a75f1cb29f16006569 Mon Sep 17 00:00:00 2001 From: Adrien ERAUD Date: Fri, 29 Nov 2024 19:35:26 +0100 Subject: [PATCH] First commit --- .editorconfig | 21 + .gitattributes | 1 + .github/renovate.json | 24 + .github/workflows/codeql-analysis.yml | 70 +++ .github/workflows/quality-control.yml | 35 ++ .gitignore | 19 + .mocharc.json | 5 + .npmignore | 11 + .npmrc | 1 + .prettierignore | 8 + .vscode/launch.json | 17 + .vscode/settings.json | 41 ++ .whitesource | 21 + .yarnrc.yml | 7 + eslint.config.mjs | 47 ++ license | 7 + package.json | 56 +++ prettier.config.mjs | 20 + readme.md | 17 + src/encoder.ts | 92 ++++ src/index.ts | 32 ++ src/synthesizer.ts | 105 +++++ src/voices.ts | 608 ++++++++++++++++++++++++++ tsconfig.build.json | 8 + tsconfig.json | 15 + update-voices.ts | 48 ++ 26 files changed, 1336 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/renovate.json create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .github/workflows/quality-control.yml create mode 100644 .gitignore create mode 100644 .mocharc.json create mode 100644 .npmignore create mode 100644 .npmrc create mode 100644 .prettierignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .whitesource create mode 100644 .yarnrc.yml create mode 100644 eslint.config.mjs create mode 100644 license create mode 100644 package.json create mode 100644 prettier.config.mjs create mode 100644 readme.md create mode 100644 src/encoder.ts create mode 100644 src/index.ts create mode 100644 src/synthesizer.ts create mode 100644 src/voices.ts create mode 100644 tsconfig.build.json create mode 100644 tsconfig.json create mode 100644 update-voices.ts diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e13c9d7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false + +[*.yml] +indent_style = space +indent_size = 2 + +[.whitesource] +indent_style = space +indent_size = 2 + +[*.{js,ts,cjs,mjs}] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 0000000..36e148f --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,24 @@ +{ + "extends": ["config:recommended", ":semanticCommits"], + "timezone": "Europe/Paris", + "schedule": ["before 4:00 am on the first day of the month"], + "rangeStrategy": "bump", + "packageRules": [ + { + "matchDatasources": ["npm"], + "minimumReleaseAge": "3 days" + }, + { + "matchDatasources": ["npm"], + "matchPackageNames": ["*"], + "matchUpdateTypes": ["minor", "patch"], + "groupName": "all non-major dependencies", + "groupSlug": "all-minor-patch", + "automerge": true, + "automergeType": "pr", + "automergeStrategy": "squash" + } + ], + "ignoreDeps": [], + "ignorePaths": [] +} diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..c7be3b4 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,70 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [main] + pull_request: + # The branches below must be a subset of the branches above + branches: [main] + schedule: + - cron: "20 18 * * 0" + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: ["javascript"] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://git.io/codeql-language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v3 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/quality-control.yml b/.github/workflows/quality-control.yml new file mode 100644 index 0000000..5546c51 --- /dev/null +++ b/.github/workflows/quality-control.yml @@ -0,0 +1,35 @@ +name: QC Checks + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + checks: + name: QC Checks + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Use Node.js 22 + uses: actions/setup-node@v4 + with: + node-version: "22" + + - name: Install + run: npm install + + - name: ESLint checks + run: npm run eslint-check + + - name: Prettier checks + run: npm run prettier-check + + - name: TSC checks + run: npm run tsc-check + + - name: Build JS + run: npm run build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b998002 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Generated files +lib + +# Yarn lockfile +yarn.lock + +# Yarn & node.js dependencies +node_modules/ +.pnp.* +.yarn/* +!.yarn/patches +!.yarn/plugins +!.yarn/releases +!.yarn/sdks +!.yarn/versions + +# Configuration & Tests folders +config +texts \ No newline at end of file diff --git a/.mocharc.json b/.mocharc.json new file mode 100644 index 0000000..1b66185 --- /dev/null +++ b/.mocharc.json @@ -0,0 +1,5 @@ +{ + "extension": ["ts"], + "spec": "tests/**/*.spec.ts", + "require": ["ts-node/register", "tests/fixtures.ts"] +} diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..531e8bc --- /dev/null +++ b/.npmignore @@ -0,0 +1,11 @@ +.github +.vscode +.yarn +node_modules +.editorconfig +eslint.config.mjs +.gitattributes +.yarnrc.yml +tsconfig.build.json +tsconfig.json +.whitesource \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..c7e546f --- /dev/null +++ b/.prettierignore @@ -0,0 +1,8 @@ +# Generated files +lib + +# Yarn & node.js dependencies +package.json +node_modules/ +.pnp.* +.yarn/* \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..508a6f7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Tests", + "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/mocha", + "runtimeArgs": [], + "outputCapture": "std", + "skipFiles": ["/**/*.js"] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6c56f76 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,41 @@ +{ + "search.exclude": { + "**/.git/**": true, + "**/node_modules/**": true, + "**/.yarn/**": true, + "**/lib/**": true + }, + "files.exclude": { + "**/.git/**": true, + "**/node_modules/**": true, + "**/.yarn/**": true, + "**/lib/**": true + }, + "files.watcherExclude": { + "**/.git/**": true, + "**/node_modules/**": true, + "**/.yarn/**": true, + "**/lib/**": true + }, + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.codeActionsOnSave": { + "source.fixAll": "explicit", + "source.organizeImports": "explicit" + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "eslint.enable": true, + "eslint.validate": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact" + ], + "prettier.configPath": "prettier.config.mjs", + "prettier.ignorePath": ".prettierignore" +} diff --git a/.whitesource b/.whitesource new file mode 100644 index 0000000..aabfb04 --- /dev/null +++ b/.whitesource @@ -0,0 +1,21 @@ +{ + "scanSettings": { + "configMode": "AUTO", + "enableLicenseViolations": false, + "baseBranches": [] + }, + "checkRunSettings": { + "vulnerableCheckRunConclusionLevel": "failure", + "displayMode": "diff", + "useMendCheckNames": true + }, + "issueSettings": { + "minSeverityLevel": "LOW", + "issueType": "DEPENDENCY" + }, + "remediateSettings": { + "workflowRules": { + "enabled": true + } + } +} diff --git a/.yarnrc.yml b/.yarnrc.yml new file mode 100644 index 0000000..e16af01 --- /dev/null +++ b/.yarnrc.yml @@ -0,0 +1,7 @@ +compressionLevel: mixed + +enableGlobalCache: true + +enableScripts: true + +nodeLinker: node-modules diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..597ae91 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,47 @@ +import eslint from "@eslint/js"; +import prettierConfig from "eslint-config-prettier"; +import globals from "globals"; +import tseslint from "typescript-eslint"; + +export default tseslint.config( + eslint.configs.recommended, + ...tseslint.configs.recommended, + { + languageOptions: { + parserOptions: { + projectService: true, + tsconfigRootDir: import.meta.dirname, + ecmaVersion: 2022, + globals: { + ...globals.es2022, + ...globals.node, + }, + }, + }, + }, + prettierConfig, + { + ignores: [ + "package.json", + "eslint.config.mjs", + "prettier.config.mjs", + "node_modules/*", + ".yarn/*", + "lib/*", + ], + }, + { + files: ["**/*.ts"], + rules: { + "@typescript-eslint/no-unused-vars": [ + "error", + { + args: "all", + argsIgnorePattern: "^_", + caughtErrors: "all", + caughtErrorsIgnorePattern: "^_", + }, + ], + }, + } +); diff --git a/license b/license new file mode 100644 index 0000000..e125673 --- /dev/null +++ b/license @@ -0,0 +1,7 @@ +Copyright 2021 Les Jours SAS + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..5edb67d --- /dev/null +++ b/package.json @@ -0,0 +1,56 @@ +{ + "name": "@lesjoursfr/gcp-tts", + "version": "0.0.0", + "description": "Wrapper around the Google Cloud TTS module.", + "keywords": [ + "tts" + ], + "author": "", + "license": "MIT", + "repository": "lesjoursfr/gcp-tts", + "homepage": "https://github.com/lesjoursfr/gcp-tts#readme", + "bugs": { + "url": "https://github.com/lesjoursfr/gcp-tts/issues" + }, + "publishConfig": { + "access": "public" + }, + "engines": { + "node": "20.x || 22.x || 24.x" + }, + "scripts": { + "freshlock": "rm -rf node_modules/ && rm .yarn/install-state.gz && rm yarn.lock && yarn", + "update-voices": "TTS_GCP_CREDENTIALS=config/tts-gcp-credentials.json ts-node update-voices.ts", + "eslint-check": "eslint", + "eslint-fix": "eslint --fix", + "prettier-check": "prettier --check .", + "prettier-fix": "prettier --write .", + "tsc-check": "tsc --noEmit", + "build": "tsc -p tsconfig.build.json", + "test": "mocha" + }, + "dependencies": { + "@google-cloud/storage": "^7.14.0", + "@google-cloud/text-to-speech": "^5.6.0", + "debug": "^4.3.7", + "fluent-ffmpeg": "^2.1.3", + "picocolors": "^1.1.1" + }, + "devDependencies": { + "@eslint/js": "^9.15.0", + "@tsconfig/node20": "^20.1.4", + "@types/debug": "^4.1.12", + "@types/fluent-ffmpeg": "^2.1.27", + "@types/mocha": "^10.0.10", + "@types/node": "^22.10.1", + "eslint": "^9.15.0", + "eslint-config-prettier": "^9.1.0", + "globals": "^15.12.0", + "mocha": "^10.8.2", + "prettier": "^3.4.1", + "ts-node": "^10.9.2", + "typescript": "^5.7.2", + "typescript-eslint": "^8.16.0" + }, + "packageManager": "yarn@4.5.3" +} diff --git a/prettier.config.mjs b/prettier.config.mjs new file mode 100644 index 0000000..1bfeb9b --- /dev/null +++ b/prettier.config.mjs @@ -0,0 +1,20 @@ +const config = { + printWidth: 120, + trailingComma: "es5", + overrides: [ + { + files: ["eslint.config.mjs", "prettier.config.mjs", "*.json", "*.md"], + options: { + printWidth: 80, + }, + }, + { + files: ["tsconfig.json", "tsconfig.build.json"], + options: { + trailingComma: "none", + }, + }, + ], +}; + +export default config; diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..0ef76f1 --- /dev/null +++ b/readme.md @@ -0,0 +1,17 @@ +[![npm version](https://badge.fury.io/js/@lesjoursfr%2Fgcp-tts.svg)](https://badge.fury.io/js/@lesjoursfr%2Fgcp-tts) +[![QC Checks](https://github.com/lesjoursfr/gcp-tts/actions/workflows/quality-control.yml/badge.svg)](https://github.com/lesjoursfr/gcp-tts/actions/workflows/quality-control.yml) + +# gcp-tts + +Generate audio files from text file using GCP. + +## Presentation + +This module convert text to sound using the Google Cloud Text-to-Speech service. +The generated audio is a WAVE file but you can ask for extra WEBA and/or M4A files (they will be generated using ffmpeg). + +```javascript +import gcpTTS from "@lesjoursfr/gcp-tts"; + +let result = synthesizeTextWithGCP("Lorem ipsum..."); +``` diff --git a/src/encoder.ts b/src/encoder.ts new file mode 100644 index 0000000..f729bcd --- /dev/null +++ b/src/encoder.ts @@ -0,0 +1,92 @@ +import debug from "debug"; +import type { FfmpegCommand } from "fluent-ffmpeg"; +import ffmpeg from "fluent-ffmpeg"; +import { EOL } from "os"; +import { extname } from "path"; +import pc from "picocolors"; + +const log = debug("gcp-tts:encoder"); + +const kDefaultAudioBitrate = 256; + +export type EncoderOptions = { + audioBitrate?: number; +}; + +export enum Codecs { + m4a = "m4a", + weba = "weba", +} + +function weba(ffmpeg: FfmpegCommand, audioBitrate: number): FfmpegCommand { + ffmpeg + .format("webm") + .noVideo() + .audioBitrate(`${audioBitrate}k`) + .audioCodec("libopus") + .audioChannels(2) + .outputOptions([ + // https://ffmpeg.org/ffmpeg-codecs.html#libopus-1 + "-vbr constrained", // Use constrained variable bit rate encoding + "-compression_level 10", // 0 gives the fastest encodes but lower quality, while 10 gives the highest quality but slowest encoding. + ]); + + return ffmpeg; +} + +function m4a(ffmpeg: FfmpegCommand, audioBitrate: number): FfmpegCommand { + ffmpeg.format("mp4").noVideo().audioBitrate(`${audioBitrate}k`).audioCodec("aac").audioChannels(2).outputOptions([ + // https://ffmpeg.org/ffmpeg-codecs.html#aac + "-aac_coder twoloop", // Two loop searching (TLS) method + "-profile:a aac_low", // The default, AAC "Low-complexity" profile + "-movflags +faststart", // AAC Progresive download : https://trac.ffmpeg.org/wiki/Encode/AAC#Progressivedownload + ]); + + return ffmpeg; +} + +function ffmpegWithCodec(ffmpeg: FfmpegCommand, codec: string, options: EncoderOptions): FfmpegCommand { + switch (codec) { + // Audio codecs + case Codecs.m4a: + return m4a(ffmpeg, options.audioBitrate || kDefaultAudioBitrate); + case Codecs.weba: + return weba(ffmpeg, options.audioBitrate || kDefaultAudioBitrate); + // Unknown codec : Throw an error + default: + throw new Error(`The codec ${codec} is not implemented !`); + } +} + +export function encode(file: string, codec: Codecs, options: EncoderOptions): Promise { + return new Promise((resolve, reject) => { + const extension = extname(file); + const destination = file.replace(new RegExp(`${extension}$`), `.${codec}`); + + ffmpegWithCodec(ffmpeg(file), codec, options) + .on("start", (commandLine) => { + log(`Spawn with the command ${pc.gray(pc.italic(commandLine))}`); + }) + .on("codecData", function (data) { + const codecData = data; + + log( + "The input is an audio file :" + + pc.gray( + `${EOL}\t- format : ${codecData.format}${EOL}\t- duration : ${codecData.duration}${EOL}\t- audio : ${codecData.audio_details.join(", ")}` + ) + ); + }) + .on("progress", function (progress) { + log(`${EOL}Conversion progress : ${pc.gray((progress.percent ?? 0) / 100)}`); + }) + .on("error", function (err) { + reject(err); + }) + .on("end", function () { + log(`${EOL}Conversion complete : ${pc.gray(destination)}`); + resolve(destination); + }) + .save(destination); + }); +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..02a25df --- /dev/null +++ b/src/index.ts @@ -0,0 +1,32 @@ +import { Codecs, encode, EncoderOptions } from "./encoder"; +import { GCPConfig, synthesize, SynthesizeDestination, SynthesizeOptions } from "./synthesizer"; + +export { Codecs, EncoderOptions } from "./encoder"; +export { GCPConfig, SynthesizeDestination, SynthesizeOptions } from "./synthesizer"; +export * from "./voices"; + +export async function synthesizeTextWithGCP( + textToRead: string, + gcpConfig: GCPConfig, + options: SynthesizeOptions, + destination: SynthesizeDestination, + extraEncodes: Array<{ codec: Codecs; options: EncoderOptions }> +): Promise<{ + sourceFile: string; + exextraEncodes: Array; +}> { + // Create the source version on the audio file + const sourceFilePath = await synthesize(textToRead, gcpConfig, options, destination); + + // Generate extra version with different codecs + const extras = [] as Array; + for (const { codec, options } of extraEncodes) { + extras.push(await encode(sourceFilePath, codec, options)); + } + + // Return the result + return { + sourceFile: sourceFilePath, + exextraEncodes: extras, + }; +} diff --git a/src/synthesizer.ts b/src/synthesizer.ts new file mode 100644 index 0000000..37c2167 --- /dev/null +++ b/src/synthesizer.ts @@ -0,0 +1,105 @@ +import { Storage } from "@google-cloud/storage"; +import { TextToSpeechClient, TextToSpeechLongAudioSynthesizeClient } from "@google-cloud/text-to-speech"; +import { google } from "@google-cloud/text-to-speech/build/protos/protos.js"; +import debug from "debug"; +import { writeFile } from "fs/promises"; +import type { ClientOptions } from "google-gax"; +import { join } from "path"; +import pc from "picocolors"; +import { Languages, Voices } from "./voices"; + +const log = debug("gcp-tts:synthesizer"); + +type ISynthesizeLongAudioRequest = google.cloud.texttospeech.v1.ISynthesizeLongAudioRequest; +type ISynthesizeSpeechRequest = google.cloud.texttospeech.v1.ISynthesizeSpeechRequest; +type IAudioConfig = google.cloud.texttospeech.v1.IAudioConfig; + +export type GCPConfig = { + clientOptions: ClientOptions; + projectId: string; + bucketId: string; +}; + +export type SynthesizeOptions = { + language: Languages; + voice: Voices; + audioEncoding: IAudioConfig["audioEncoding"]; +}; + +export type SynthesizeDestination = { + folder: string; + filename: string; +}; + +export async function synthesize( + textToRead: string, + gcpConfig: GCPConfig, + options: SynthesizeOptions, + destination: SynthesizeDestination +): Promise { + try { + // Destination path + const destFileName = `${destination.filename}.wav`; + const destFilePath = join(destination.folder, destFileName); + + // Check if it's a short text (ie less than 5000 bytes) + const textLength = Buffer.from(textToRead).length; + const isShortText = textLength < 5000; + log(`Text length in bytes ${pc.gray(pc.italic(textLength))}`); + + // Create the speech synthesizer. + if (isShortText) { + // The limit is 5000 bytes + const client = new TextToSpeechClient(gcpConfig.clientOptions); + const request: ISynthesizeSpeechRequest = { + input: { text: textToRead }, // { text: string; } or { ssml: string ;} + voice: { languageCode: options.language, name: options.voice }, + audioConfig: { audioEncoding: options.audioEncoding }, + }; + + // Start the synthesizer and wait for a result. + log(`Use TextToSpeechClient with the destination ${destFilePath}`); + const [response] = await client.synthesizeSpeech(request); + if (response.audioContent !== null && response.audioContent !== undefined) { + console.log("Speech synthesis finished."); + await writeFile(destFilePath, response.audioContent); + } else { + console.log("Speech synthesis failed."); + } + } else { + // The limit is 1M bytes + log(`Use TextToSpeechLongAudioSynthesizeClient with the destination gs://${gcpConfig.bucketId}/${destFileName}`); + const client = new TextToSpeechLongAudioSynthesizeClient(gcpConfig.clientOptions); + const request: ISynthesizeLongAudioRequest = { + parent: `projects/${gcpConfig.projectId}/locations/global`, + input: { text: textToRead }, // { text: string; } or { ssml: string ;} + voice: { languageCode: options.language, name: options.voice }, + audioConfig: { audioEncoding: options.audioEncoding }, + outputGcsUri: `gs://${gcpConfig.bucketId}/${destFileName}`, + }; + + // Start the synthesizer. + const [operation] = await client.synthesizeLongAudio(request); + + // Polls the operation until complete + await operation.promise(); + + // Creates a Storage client + const storage = new Storage(gcpConfig.clientOptions); + const bucket = storage.bucket(gcpConfig.bucketId); + + // Download the file + log(`Download the file gs://${gcpConfig.bucketId}/${destFileName} to ${destFilePath}`); + await bucket.file(destFileName).download({ destination: destFilePath }); + + // Remove the file from the Bucket + log(`Delete the file gs://${gcpConfig.bucketId}/${destFileName}`); + await bucket.file(destFileName).delete(); + } + + // Return the destination file + return destFilePath; + } catch (err) { + throw new Error(`Can't synthesize the gievn text!`, { cause: err }); + } +} diff --git a/src/voices.ts b/src/voices.ts new file mode 100644 index 0000000..420e014 --- /dev/null +++ b/src/voices.ts @@ -0,0 +1,608 @@ +export enum Voices { + af_ZA_Standard_A = "af-ZA-Standard-A", + am_ET_Standard_A = "am-ET-Standard-A", + am_ET_Standard_B = "am-ET-Standard-B", + am_ET_Wavenet_A = "am-ET-Wavenet-A", + am_ET_Wavenet_B = "am-ET-Wavenet-B", + ar_XA_Standard_A = "ar-XA-Standard-A", + ar_XA_Standard_B = "ar-XA-Standard-B", + ar_XA_Standard_C = "ar-XA-Standard-C", + ar_XA_Standard_D = "ar-XA-Standard-D", + ar_XA_Wavenet_A = "ar-XA-Wavenet-A", + ar_XA_Wavenet_B = "ar-XA-Wavenet-B", + ar_XA_Wavenet_C = "ar-XA-Wavenet-C", + ar_XA_Wavenet_D = "ar-XA-Wavenet-D", + bg_BG_Standard_A = "bg-BG-Standard-A", + bg_BG_Standard_B = "bg-BG-Standard-B", + bn_IN_Standard_A = "bn-IN-Standard-A", + bn_IN_Standard_B = "bn-IN-Standard-B", + bn_IN_Standard_C = "bn-IN-Standard-C", + bn_IN_Standard_D = "bn-IN-Standard-D", + bn_IN_Wavenet_A = "bn-IN-Wavenet-A", + bn_IN_Wavenet_B = "bn-IN-Wavenet-B", + bn_IN_Wavenet_C = "bn-IN-Wavenet-C", + bn_IN_Wavenet_D = "bn-IN-Wavenet-D", + ca_ES_Standard_A = "ca-ES-Standard-A", + ca_ES_Standard_B = "ca-ES-Standard-B", + cmn_CN_Standard_A = "cmn-CN-Standard-A", + cmn_CN_Standard_B = "cmn-CN-Standard-B", + cmn_CN_Standard_C = "cmn-CN-Standard-C", + cmn_CN_Standard_D = "cmn-CN-Standard-D", + cmn_CN_Wavenet_A = "cmn-CN-Wavenet-A", + cmn_CN_Wavenet_B = "cmn-CN-Wavenet-B", + cmn_CN_Wavenet_C = "cmn-CN-Wavenet-C", + cmn_CN_Wavenet_D = "cmn-CN-Wavenet-D", + cmn_TW_Standard_A = "cmn-TW-Standard-A", + cmn_TW_Standard_B = "cmn-TW-Standard-B", + cmn_TW_Standard_C = "cmn-TW-Standard-C", + cmn_TW_Wavenet_A = "cmn-TW-Wavenet-A", + cmn_TW_Wavenet_B = "cmn-TW-Wavenet-B", + cmn_TW_Wavenet_C = "cmn-TW-Wavenet-C", + cs_CZ_Standard_A = "cs-CZ-Standard-A", + cs_CZ_Standard_B = "cs-CZ-Standard-B", + cs_CZ_Wavenet_A = "cs-CZ-Wavenet-A", + da_DK_Neural2_D = "da-DK-Neural2-D", + da_DK_Standard_A = "da-DK-Standard-A", + da_DK_Standard_C = "da-DK-Standard-C", + da_DK_Standard_D = "da-DK-Standard-D", + da_DK_Standard_E = "da-DK-Standard-E", + da_DK_Standard_F = "da-DK-Standard-F", + da_DK_Standard_G = "da-DK-Standard-G", + da_DK_Wavenet_A = "da-DK-Wavenet-A", + da_DK_Wavenet_C = "da-DK-Wavenet-C", + da_DK_Wavenet_D = "da-DK-Wavenet-D", + da_DK_Wavenet_E = "da-DK-Wavenet-E", + de_DE_Journey_D = "de-DE-Journey-D", + de_DE_Journey_F = "de-DE-Journey-F", + de_DE_Journey_O = "de-DE-Journey-O", + de_DE_Neural2_A = "de-DE-Neural2-A", + de_DE_Neural2_B = "de-DE-Neural2-B", + de_DE_Neural2_C = "de-DE-Neural2-C", + de_DE_Neural2_D = "de-DE-Neural2-D", + de_DE_Neural2_F = "de-DE-Neural2-F", + de_DE_Polyglot_1 = "de-DE-Polyglot-1", + de_DE_Standard_A = "de-DE-Standard-A", + de_DE_Standard_B = "de-DE-Standard-B", + de_DE_Standard_C = "de-DE-Standard-C", + de_DE_Standard_D = "de-DE-Standard-D", + de_DE_Standard_E = "de-DE-Standard-E", + de_DE_Standard_F = "de-DE-Standard-F", + de_DE_Standard_G = "de-DE-Standard-G", + de_DE_Standard_H = "de-DE-Standard-H", + de_DE_Studio_B = "de-DE-Studio-B", + de_DE_Studio_C = "de-DE-Studio-C", + de_DE_Wavenet_A = "de-DE-Wavenet-A", + de_DE_Wavenet_B = "de-DE-Wavenet-B", + de_DE_Wavenet_C = "de-DE-Wavenet-C", + de_DE_Wavenet_D = "de-DE-Wavenet-D", + de_DE_Wavenet_E = "de-DE-Wavenet-E", + de_DE_Wavenet_F = "de-DE-Wavenet-F", + de_DE_Wavenet_G = "de-DE-Wavenet-G", + de_DE_Wavenet_H = "de-DE-Wavenet-H", + el_GR_Standard_A = "el-GR-Standard-A", + el_GR_Standard_B = "el-GR-Standard-B", + el_GR_Wavenet_A = "el-GR-Wavenet-A", + en_AU_Journey_D = "en-AU-Journey-D", + en_AU_Journey_F = "en-AU-Journey-F", + en_AU_Neural2_A = "en-AU-Neural2-A", + en_AU_Neural2_B = "en-AU-Neural2-B", + en_AU_Neural2_C = "en-AU-Neural2-C", + en_AU_Neural2_D = "en-AU-Neural2-D", + en_AU_News_E = "en-AU-News-E", + en_AU_News_F = "en-AU-News-F", + en_AU_News_G = "en-AU-News-G", + en_AU_Polyglot_1 = "en-AU-Polyglot-1", + en_AU_Standard_A = "en-AU-Standard-A", + en_AU_Standard_B = "en-AU-Standard-B", + en_AU_Standard_C = "en-AU-Standard-C", + en_AU_Standard_D = "en-AU-Standard-D", + en_AU_Wavenet_A = "en-AU-Wavenet-A", + en_AU_Wavenet_B = "en-AU-Wavenet-B", + en_AU_Wavenet_C = "en-AU-Wavenet-C", + en_AU_Wavenet_D = "en-AU-Wavenet-D", + en_GB_Journey_D = "en-GB-Journey-D", + en_GB_Journey_F = "en-GB-Journey-F", + en_GB_Neural2_A = "en-GB-Neural2-A", + en_GB_Neural2_B = "en-GB-Neural2-B", + en_GB_Neural2_C = "en-GB-Neural2-C", + en_GB_Neural2_D = "en-GB-Neural2-D", + en_GB_Neural2_F = "en-GB-Neural2-F", + en_GB_News_G = "en-GB-News-G", + en_GB_News_H = "en-GB-News-H", + en_GB_News_I = "en-GB-News-I", + en_GB_News_J = "en-GB-News-J", + en_GB_News_K = "en-GB-News-K", + en_GB_News_L = "en-GB-News-L", + en_GB_News_M = "en-GB-News-M", + en_GB_Standard_A = "en-GB-Standard-A", + en_GB_Standard_B = "en-GB-Standard-B", + en_GB_Standard_C = "en-GB-Standard-C", + en_GB_Standard_D = "en-GB-Standard-D", + en_GB_Standard_F = "en-GB-Standard-F", + en_GB_Standard_N = "en-GB-Standard-N", + en_GB_Standard_O = "en-GB-Standard-O", + en_GB_Studio_B = "en-GB-Studio-B", + en_GB_Studio_C = "en-GB-Studio-C", + en_GB_Wavenet_A = "en-GB-Wavenet-A", + en_GB_Wavenet_B = "en-GB-Wavenet-B", + en_GB_Wavenet_C = "en-GB-Wavenet-C", + en_GB_Wavenet_D = "en-GB-Wavenet-D", + en_GB_Wavenet_F = "en-GB-Wavenet-F", + en_IN_Journey_D = "en-IN-Journey-D", + en_IN_Journey_F = "en-IN-Journey-F", + en_IN_Neural2_A = "en-IN-Neural2-A", + en_IN_Neural2_B = "en-IN-Neural2-B", + en_IN_Neural2_C = "en-IN-Neural2-C", + en_IN_Neural2_D = "en-IN-Neural2-D", + en_IN_Standard_A = "en-IN-Standard-A", + en_IN_Standard_B = "en-IN-Standard-B", + en_IN_Standard_C = "en-IN-Standard-C", + en_IN_Standard_D = "en-IN-Standard-D", + en_IN_Standard_E = "en-IN-Standard-E", + en_IN_Standard_F = "en-IN-Standard-F", + en_IN_Wavenet_A = "en-IN-Wavenet-A", + en_IN_Wavenet_B = "en-IN-Wavenet-B", + en_IN_Wavenet_C = "en-IN-Wavenet-C", + en_IN_Wavenet_D = "en-IN-Wavenet-D", + en_IN_Wavenet_E = "en-IN-Wavenet-E", + en_IN_Wavenet_F = "en-IN-Wavenet-F", + en_US_Casual_K = "en-US-Casual-K", + en_US_Journey_D = "en-US-Journey-D", + en_US_Journey_F = "en-US-Journey-F", + en_US_Journey_O = "en-US-Journey-O", + en_US_Neural2_A = "en-US-Neural2-A", + en_US_Neural2_C = "en-US-Neural2-C", + en_US_Neural2_D = "en-US-Neural2-D", + en_US_Neural2_E = "en-US-Neural2-E", + en_US_Neural2_F = "en-US-Neural2-F", + en_US_Neural2_G = "en-US-Neural2-G", + en_US_Neural2_H = "en-US-Neural2-H", + en_US_Neural2_I = "en-US-Neural2-I", + en_US_Neural2_J = "en-US-Neural2-J", + en_US_News_K = "en-US-News-K", + en_US_News_L = "en-US-News-L", + en_US_News_N = "en-US-News-N", + en_US_Polyglot_1 = "en-US-Polyglot-1", + en_US_Standard_A = "en-US-Standard-A", + en_US_Standard_B = "en-US-Standard-B", + en_US_Standard_C = "en-US-Standard-C", + en_US_Standard_D = "en-US-Standard-D", + en_US_Standard_E = "en-US-Standard-E", + en_US_Standard_F = "en-US-Standard-F", + en_US_Standard_G = "en-US-Standard-G", + en_US_Standard_H = "en-US-Standard-H", + en_US_Standard_I = "en-US-Standard-I", + en_US_Standard_J = "en-US-Standard-J", + en_US_Studio_O = "en-US-Studio-O", + en_US_Studio_Q = "en-US-Studio-Q", + en_US_Wavenet_A = "en-US-Wavenet-A", + en_US_Wavenet_B = "en-US-Wavenet-B", + en_US_Wavenet_C = "en-US-Wavenet-C", + en_US_Wavenet_D = "en-US-Wavenet-D", + en_US_Wavenet_E = "en-US-Wavenet-E", + en_US_Wavenet_F = "en-US-Wavenet-F", + en_US_Wavenet_G = "en-US-Wavenet-G", + en_US_Wavenet_H = "en-US-Wavenet-H", + en_US_Wavenet_I = "en-US-Wavenet-I", + en_US_Wavenet_J = "en-US-Wavenet-J", + es_ES_Journey_D = "es-ES-Journey-D", + es_ES_Journey_F = "es-ES-Journey-F", + es_ES_Neural2_A = "es-ES-Neural2-A", + es_ES_Neural2_B = "es-ES-Neural2-B", + es_ES_Neural2_C = "es-ES-Neural2-C", + es_ES_Neural2_D = "es-ES-Neural2-D", + es_ES_Neural2_E = "es-ES-Neural2-E", + es_ES_Neural2_F = "es-ES-Neural2-F", + es_ES_Polyglot_1 = "es-ES-Polyglot-1", + es_ES_Standard_A = "es-ES-Standard-A", + es_ES_Standard_B = "es-ES-Standard-B", + es_ES_Standard_C = "es-ES-Standard-C", + es_ES_Standard_D = "es-ES-Standard-D", + es_ES_Standard_E = "es-ES-Standard-E", + es_ES_Standard_F = "es-ES-Standard-F", + es_ES_Studio_C = "es-ES-Studio-C", + es_ES_Studio_F = "es-ES-Studio-F", + es_ES_Wavenet_B = "es-ES-Wavenet-B", + es_ES_Wavenet_C = "es-ES-Wavenet-C", + es_ES_Wavenet_D = "es-ES-Wavenet-D", + es_ES_Wavenet_E = "es-ES-Wavenet-E", + es_ES_Wavenet_F = "es-ES-Wavenet-F", + es_US_Journey_D = "es-US-Journey-D", + es_US_Journey_F = "es-US-Journey-F", + es_US_Neural2_A = "es-US-Neural2-A", + es_US_Neural2_B = "es-US-Neural2-B", + es_US_Neural2_C = "es-US-Neural2-C", + es_US_News_D = "es-US-News-D", + es_US_News_E = "es-US-News-E", + es_US_News_F = "es-US-News-F", + es_US_News_G = "es-US-News-G", + es_US_Polyglot_1 = "es-US-Polyglot-1", + es_US_Standard_A = "es-US-Standard-A", + es_US_Standard_B = "es-US-Standard-B", + es_US_Standard_C = "es-US-Standard-C", + es_US_Studio_B = "es-US-Studio-B", + es_US_Wavenet_A = "es-US-Wavenet-A", + es_US_Wavenet_B = "es-US-Wavenet-B", + es_US_Wavenet_C = "es-US-Wavenet-C", + et_EE_Standard_A = "et-EE-Standard-A", + eu_ES_Standard_A = "eu-ES-Standard-A", + eu_ES_Standard_B = "eu-ES-Standard-B", + fi_FI_Standard_A = "fi-FI-Standard-A", + fi_FI_Standard_B = "fi-FI-Standard-B", + fi_FI_Wavenet_A = "fi-FI-Wavenet-A", + fil_PH_Standard_A = "fil-PH-Standard-A", + fil_PH_Standard_B = "fil-PH-Standard-B", + fil_PH_Standard_C = "fil-PH-Standard-C", + fil_PH_Standard_D = "fil-PH-Standard-D", + fil_PH_Wavenet_A = "fil-PH-Wavenet-A", + fil_PH_Wavenet_B = "fil-PH-Wavenet-B", + fil_PH_Wavenet_C = "fil-PH-Wavenet-C", + fil_PH_Wavenet_D = "fil-PH-Wavenet-D", + fil_ph_Neural2_A = "fil-ph-Neural2-A", + fil_ph_Neural2_D = "fil-ph-Neural2-D", + fr_CA_Journey_D = "fr-CA-Journey-D", + fr_CA_Journey_F = "fr-CA-Journey-F", + fr_CA_Neural2_A = "fr-CA-Neural2-A", + fr_CA_Neural2_B = "fr-CA-Neural2-B", + fr_CA_Neural2_C = "fr-CA-Neural2-C", + fr_CA_Neural2_D = "fr-CA-Neural2-D", + fr_CA_Standard_A = "fr-CA-Standard-A", + fr_CA_Standard_B = "fr-CA-Standard-B", + fr_CA_Standard_C = "fr-CA-Standard-C", + fr_CA_Standard_D = "fr-CA-Standard-D", + fr_CA_Wavenet_A = "fr-CA-Wavenet-A", + fr_CA_Wavenet_B = "fr-CA-Wavenet-B", + fr_CA_Wavenet_C = "fr-CA-Wavenet-C", + fr_CA_Wavenet_D = "fr-CA-Wavenet-D", + fr_FR_Journey_D = "fr-FR-Journey-D", + fr_FR_Journey_F = "fr-FR-Journey-F", + fr_FR_Neural2_A = "fr-FR-Neural2-A", + fr_FR_Neural2_B = "fr-FR-Neural2-B", + fr_FR_Neural2_C = "fr-FR-Neural2-C", + fr_FR_Neural2_D = "fr-FR-Neural2-D", + fr_FR_Neural2_E = "fr-FR-Neural2-E", + fr_FR_Polyglot_1 = "fr-FR-Polyglot-1", + fr_FR_Standard_A = "fr-FR-Standard-A", + fr_FR_Standard_B = "fr-FR-Standard-B", + fr_FR_Standard_C = "fr-FR-Standard-C", + fr_FR_Standard_D = "fr-FR-Standard-D", + fr_FR_Standard_E = "fr-FR-Standard-E", + fr_FR_Standard_F = "fr-FR-Standard-F", + fr_FR_Standard_G = "fr-FR-Standard-G", + fr_FR_Studio_A = "fr-FR-Studio-A", + fr_FR_Studio_D = "fr-FR-Studio-D", + fr_FR_Wavenet_A = "fr-FR-Wavenet-A", + fr_FR_Wavenet_B = "fr-FR-Wavenet-B", + fr_FR_Wavenet_C = "fr-FR-Wavenet-C", + fr_FR_Wavenet_D = "fr-FR-Wavenet-D", + fr_FR_Wavenet_E = "fr-FR-Wavenet-E", + fr_FR_Wavenet_F = "fr-FR-Wavenet-F", + fr_FR_Wavenet_G = "fr-FR-Wavenet-G", + gl_ES_Standard_A = "gl-ES-Standard-A", + gl_ES_Standard_B = "gl-ES-Standard-B", + gu_IN_Standard_A = "gu-IN-Standard-A", + gu_IN_Standard_B = "gu-IN-Standard-B", + gu_IN_Standard_C = "gu-IN-Standard-C", + gu_IN_Standard_D = "gu-IN-Standard-D", + gu_IN_Wavenet_A = "gu-IN-Wavenet-A", + gu_IN_Wavenet_B = "gu-IN-Wavenet-B", + gu_IN_Wavenet_C = "gu-IN-Wavenet-C", + gu_IN_Wavenet_D = "gu-IN-Wavenet-D", + he_IL_Standard_A = "he-IL-Standard-A", + he_IL_Standard_B = "he-IL-Standard-B", + he_IL_Standard_C = "he-IL-Standard-C", + he_IL_Standard_D = "he-IL-Standard-D", + he_IL_Wavenet_A = "he-IL-Wavenet-A", + he_IL_Wavenet_B = "he-IL-Wavenet-B", + he_IL_Wavenet_C = "he-IL-Wavenet-C", + he_IL_Wavenet_D = "he-IL-Wavenet-D", + hi_IN_Neural2_A = "hi-IN-Neural2-A", + hi_IN_Neural2_B = "hi-IN-Neural2-B", + hi_IN_Neural2_C = "hi-IN-Neural2-C", + hi_IN_Neural2_D = "hi-IN-Neural2-D", + hi_IN_Standard_A = "hi-IN-Standard-A", + hi_IN_Standard_B = "hi-IN-Standard-B", + hi_IN_Standard_C = "hi-IN-Standard-C", + hi_IN_Standard_D = "hi-IN-Standard-D", + hi_IN_Standard_E = "hi-IN-Standard-E", + hi_IN_Standard_F = "hi-IN-Standard-F", + hi_IN_Wavenet_A = "hi-IN-Wavenet-A", + hi_IN_Wavenet_B = "hi-IN-Wavenet-B", + hi_IN_Wavenet_C = "hi-IN-Wavenet-C", + hi_IN_Wavenet_D = "hi-IN-Wavenet-D", + hi_IN_Wavenet_E = "hi-IN-Wavenet-E", + hi_IN_Wavenet_F = "hi-IN-Wavenet-F", + hu_HU_Standard_A = "hu-HU-Standard-A", + hu_HU_Standard_B = "hu-HU-Standard-B", + hu_HU_Wavenet_A = "hu-HU-Wavenet-A", + id_ID_Standard_A = "id-ID-Standard-A", + id_ID_Standard_B = "id-ID-Standard-B", + id_ID_Standard_C = "id-ID-Standard-C", + id_ID_Standard_D = "id-ID-Standard-D", + id_ID_Wavenet_A = "id-ID-Wavenet-A", + id_ID_Wavenet_B = "id-ID-Wavenet-B", + id_ID_Wavenet_C = "id-ID-Wavenet-C", + id_ID_Wavenet_D = "id-ID-Wavenet-D", + is_IS_Standard_A = "is-IS-Standard-A", + is_IS_Standard_B = "is-IS-Standard-B", + it_IT_Journey_D = "it-IT-Journey-D", + it_IT_Journey_F = "it-IT-Journey-F", + it_IT_Journey_O = "it-IT-Journey-O", + it_IT_Neural2_A = "it-IT-Neural2-A", + it_IT_Neural2_C = "it-IT-Neural2-C", + it_IT_Standard_A = "it-IT-Standard-A", + it_IT_Standard_B = "it-IT-Standard-B", + it_IT_Standard_C = "it-IT-Standard-C", + it_IT_Standard_D = "it-IT-Standard-D", + it_IT_Standard_E = "it-IT-Standard-E", + it_IT_Standard_F = "it-IT-Standard-F", + it_IT_Wavenet_A = "it-IT-Wavenet-A", + it_IT_Wavenet_B = "it-IT-Wavenet-B", + it_IT_Wavenet_C = "it-IT-Wavenet-C", + it_IT_Wavenet_D = "it-IT-Wavenet-D", + ja_JP_Neural2_B = "ja-JP-Neural2-B", + ja_JP_Neural2_C = "ja-JP-Neural2-C", + ja_JP_Neural2_D = "ja-JP-Neural2-D", + ja_JP_Standard_A = "ja-JP-Standard-A", + ja_JP_Standard_B = "ja-JP-Standard-B", + ja_JP_Standard_C = "ja-JP-Standard-C", + ja_JP_Standard_D = "ja-JP-Standard-D", + ja_JP_Wavenet_A = "ja-JP-Wavenet-A", + ja_JP_Wavenet_B = "ja-JP-Wavenet-B", + ja_JP_Wavenet_C = "ja-JP-Wavenet-C", + ja_JP_Wavenet_D = "ja-JP-Wavenet-D", + kn_IN_Standard_A = "kn-IN-Standard-A", + kn_IN_Standard_B = "kn-IN-Standard-B", + kn_IN_Standard_C = "kn-IN-Standard-C", + kn_IN_Standard_D = "kn-IN-Standard-D", + kn_IN_Wavenet_A = "kn-IN-Wavenet-A", + kn_IN_Wavenet_B = "kn-IN-Wavenet-B", + kn_IN_Wavenet_C = "kn-IN-Wavenet-C", + kn_IN_Wavenet_D = "kn-IN-Wavenet-D", + ko_KR_Neural2_A = "ko-KR-Neural2-A", + ko_KR_Neural2_B = "ko-KR-Neural2-B", + ko_KR_Neural2_C = "ko-KR-Neural2-C", + ko_KR_Standard_A = "ko-KR-Standard-A", + ko_KR_Standard_B = "ko-KR-Standard-B", + ko_KR_Standard_C = "ko-KR-Standard-C", + ko_KR_Standard_D = "ko-KR-Standard-D", + ko_KR_Wavenet_A = "ko-KR-Wavenet-A", + ko_KR_Wavenet_B = "ko-KR-Wavenet-B", + ko_KR_Wavenet_C = "ko-KR-Wavenet-C", + ko_KR_Wavenet_D = "ko-KR-Wavenet-D", + lt_LT_Standard_A = "lt-LT-Standard-A", + lt_LT_Standard_B = "lt-LT-Standard-B", + lv_LV_Standard_A = "lv-LV-Standard-A", + lv_LV_Standard_B = "lv-LV-Standard-B", + ml_IN_Standard_A = "ml-IN-Standard-A", + ml_IN_Standard_B = "ml-IN-Standard-B", + ml_IN_Standard_C = "ml-IN-Standard-C", + ml_IN_Standard_D = "ml-IN-Standard-D", + ml_IN_Wavenet_A = "ml-IN-Wavenet-A", + ml_IN_Wavenet_B = "ml-IN-Wavenet-B", + ml_IN_Wavenet_C = "ml-IN-Wavenet-C", + ml_IN_Wavenet_D = "ml-IN-Wavenet-D", + mr_IN_Standard_A = "mr-IN-Standard-A", + mr_IN_Standard_B = "mr-IN-Standard-B", + mr_IN_Standard_C = "mr-IN-Standard-C", + mr_IN_Wavenet_A = "mr-IN-Wavenet-A", + mr_IN_Wavenet_B = "mr-IN-Wavenet-B", + mr_IN_Wavenet_C = "mr-IN-Wavenet-C", + ms_MY_Standard_A = "ms-MY-Standard-A", + ms_MY_Standard_B = "ms-MY-Standard-B", + ms_MY_Standard_C = "ms-MY-Standard-C", + ms_MY_Standard_D = "ms-MY-Standard-D", + ms_MY_Wavenet_A = "ms-MY-Wavenet-A", + ms_MY_Wavenet_B = "ms-MY-Wavenet-B", + ms_MY_Wavenet_C = "ms-MY-Wavenet-C", + ms_MY_Wavenet_D = "ms-MY-Wavenet-D", + nb_NO_Standard_A = "nb-NO-Standard-A", + nb_NO_Standard_B = "nb-NO-Standard-B", + nb_NO_Standard_C = "nb-NO-Standard-C", + nb_NO_Standard_D = "nb-NO-Standard-D", + nb_NO_Standard_E = "nb-NO-Standard-E", + nb_NO_Standard_F = "nb-NO-Standard-F", + nb_NO_Standard_G = "nb-NO-Standard-G", + nb_NO_Wavenet_A = "nb-NO-Wavenet-A", + nb_NO_Wavenet_B = "nb-NO-Wavenet-B", + nb_NO_Wavenet_C = "nb-NO-Wavenet-C", + nb_NO_Wavenet_D = "nb-NO-Wavenet-D", + nb_NO_Wavenet_E = "nb-NO-Wavenet-E", + nl_BE_Standard_A = "nl-BE-Standard-A", + nl_BE_Standard_B = "nl-BE-Standard-B", + nl_BE_Standard_C = "nl-BE-Standard-C", + nl_BE_Standard_D = "nl-BE-Standard-D", + nl_BE_Wavenet_A = "nl-BE-Wavenet-A", + nl_BE_Wavenet_B = "nl-BE-Wavenet-B", + nl_NL_Standard_A = "nl-NL-Standard-A", + nl_NL_Standard_B = "nl-NL-Standard-B", + nl_NL_Standard_C = "nl-NL-Standard-C", + nl_NL_Standard_D = "nl-NL-Standard-D", + nl_NL_Standard_E = "nl-NL-Standard-E", + nl_NL_Standard_F = "nl-NL-Standard-F", + nl_NL_Standard_G = "nl-NL-Standard-G", + nl_NL_Wavenet_A = "nl-NL-Wavenet-A", + nl_NL_Wavenet_B = "nl-NL-Wavenet-B", + nl_NL_Wavenet_C = "nl-NL-Wavenet-C", + nl_NL_Wavenet_D = "nl-NL-Wavenet-D", + nl_NL_Wavenet_E = "nl-NL-Wavenet-E", + pa_IN_Standard_A = "pa-IN-Standard-A", + pa_IN_Standard_B = "pa-IN-Standard-B", + pa_IN_Standard_C = "pa-IN-Standard-C", + pa_IN_Standard_D = "pa-IN-Standard-D", + pa_IN_Wavenet_A = "pa-IN-Wavenet-A", + pa_IN_Wavenet_B = "pa-IN-Wavenet-B", + pa_IN_Wavenet_C = "pa-IN-Wavenet-C", + pa_IN_Wavenet_D = "pa-IN-Wavenet-D", + pl_PL_Standard_A = "pl-PL-Standard-A", + pl_PL_Standard_B = "pl-PL-Standard-B", + pl_PL_Standard_C = "pl-PL-Standard-C", + pl_PL_Standard_D = "pl-PL-Standard-D", + pl_PL_Standard_E = "pl-PL-Standard-E", + pl_PL_Standard_F = "pl-PL-Standard-F", + pl_PL_Standard_G = "pl-PL-Standard-G", + pl_PL_Wavenet_A = "pl-PL-Wavenet-A", + pl_PL_Wavenet_B = "pl-PL-Wavenet-B", + pl_PL_Wavenet_C = "pl-PL-Wavenet-C", + pl_PL_Wavenet_D = "pl-PL-Wavenet-D", + pl_PL_Wavenet_E = "pl-PL-Wavenet-E", + pt_BR_Neural2_A = "pt-BR-Neural2-A", + pt_BR_Neural2_B = "pt-BR-Neural2-B", + pt_BR_Neural2_C = "pt-BR-Neural2-C", + pt_BR_Standard_A = "pt-BR-Standard-A", + pt_BR_Standard_B = "pt-BR-Standard-B", + pt_BR_Standard_C = "pt-BR-Standard-C", + pt_BR_Standard_D = "pt-BR-Standard-D", + pt_BR_Standard_E = "pt-BR-Standard-E", + pt_BR_Wavenet_A = "pt-BR-Wavenet-A", + pt_BR_Wavenet_B = "pt-BR-Wavenet-B", + pt_BR_Wavenet_C = "pt-BR-Wavenet-C", + pt_BR_Wavenet_D = "pt-BR-Wavenet-D", + pt_BR_Wavenet_E = "pt-BR-Wavenet-E", + pt_PT_Standard_A = "pt-PT-Standard-A", + pt_PT_Standard_B = "pt-PT-Standard-B", + pt_PT_Standard_C = "pt-PT-Standard-C", + pt_PT_Standard_D = "pt-PT-Standard-D", + pt_PT_Standard_E = "pt-PT-Standard-E", + pt_PT_Standard_F = "pt-PT-Standard-F", + pt_PT_Wavenet_A = "pt-PT-Wavenet-A", + pt_PT_Wavenet_B = "pt-PT-Wavenet-B", + pt_PT_Wavenet_C = "pt-PT-Wavenet-C", + pt_PT_Wavenet_D = "pt-PT-Wavenet-D", + ro_RO_Standard_A = "ro-RO-Standard-A", + ro_RO_Standard_B = "ro-RO-Standard-B", + ro_RO_Wavenet_A = "ro-RO-Wavenet-A", + ru_RU_Standard_A = "ru-RU-Standard-A", + ru_RU_Standard_B = "ru-RU-Standard-B", + ru_RU_Standard_C = "ru-RU-Standard-C", + ru_RU_Standard_D = "ru-RU-Standard-D", + ru_RU_Standard_E = "ru-RU-Standard-E", + ru_RU_Wavenet_A = "ru-RU-Wavenet-A", + ru_RU_Wavenet_B = "ru-RU-Wavenet-B", + ru_RU_Wavenet_C = "ru-RU-Wavenet-C", + ru_RU_Wavenet_D = "ru-RU-Wavenet-D", + ru_RU_Wavenet_E = "ru-RU-Wavenet-E", + sk_SK_Standard_A = "sk-SK-Standard-A", + sk_SK_Standard_B = "sk-SK-Standard-B", + sk_SK_Wavenet_A = "sk-SK-Wavenet-A", + sr_RS_Standard_A = "sr-RS-Standard-A", + sv_SE_Standard_A = "sv-SE-Standard-A", + sv_SE_Standard_B = "sv-SE-Standard-B", + sv_SE_Standard_C = "sv-SE-Standard-C", + sv_SE_Standard_D = "sv-SE-Standard-D", + sv_SE_Standard_E = "sv-SE-Standard-E", + sv_SE_Standard_F = "sv-SE-Standard-F", + sv_SE_Standard_G = "sv-SE-Standard-G", + sv_SE_Wavenet_A = "sv-SE-Wavenet-A", + sv_SE_Wavenet_B = "sv-SE-Wavenet-B", + sv_SE_Wavenet_C = "sv-SE-Wavenet-C", + sv_SE_Wavenet_D = "sv-SE-Wavenet-D", + sv_SE_Wavenet_E = "sv-SE-Wavenet-E", + ta_IN_Standard_A = "ta-IN-Standard-A", + ta_IN_Standard_B = "ta-IN-Standard-B", + ta_IN_Standard_C = "ta-IN-Standard-C", + ta_IN_Standard_D = "ta-IN-Standard-D", + ta_IN_Wavenet_A = "ta-IN-Wavenet-A", + ta_IN_Wavenet_B = "ta-IN-Wavenet-B", + ta_IN_Wavenet_C = "ta-IN-Wavenet-C", + ta_IN_Wavenet_D = "ta-IN-Wavenet-D", + te_IN_Standard_A = "te-IN-Standard-A", + te_IN_Standard_B = "te-IN-Standard-B", + te_IN_Standard_C = "te-IN-Standard-C", + te_IN_Standard_D = "te-IN-Standard-D", + th_TH_Neural2_C = "th-TH-Neural2-C", + th_TH_Standard_A = "th-TH-Standard-A", + tr_TR_Standard_A = "tr-TR-Standard-A", + tr_TR_Standard_B = "tr-TR-Standard-B", + tr_TR_Standard_C = "tr-TR-Standard-C", + tr_TR_Standard_D = "tr-TR-Standard-D", + tr_TR_Standard_E = "tr-TR-Standard-E", + tr_TR_Wavenet_A = "tr-TR-Wavenet-A", + tr_TR_Wavenet_B = "tr-TR-Wavenet-B", + tr_TR_Wavenet_C = "tr-TR-Wavenet-C", + tr_TR_Wavenet_D = "tr-TR-Wavenet-D", + tr_TR_Wavenet_E = "tr-TR-Wavenet-E", + uk_UA_Standard_A = "uk-UA-Standard-A", + uk_UA_Wavenet_A = "uk-UA-Wavenet-A", + ur_IN_Standard_A = "ur-IN-Standard-A", + ur_IN_Standard_B = "ur-IN-Standard-B", + ur_IN_Wavenet_A = "ur-IN-Wavenet-A", + ur_IN_Wavenet_B = "ur-IN-Wavenet-B", + vi_VN_Neural2_A = "vi-VN-Neural2-A", + vi_VN_Neural2_D = "vi-VN-Neural2-D", + vi_VN_Standard_A = "vi-VN-Standard-A", + vi_VN_Standard_B = "vi-VN-Standard-B", + vi_VN_Standard_C = "vi-VN-Standard-C", + vi_VN_Standard_D = "vi-VN-Standard-D", + vi_VN_Wavenet_A = "vi-VN-Wavenet-A", + vi_VN_Wavenet_B = "vi-VN-Wavenet-B", + vi_VN_Wavenet_C = "vi-VN-Wavenet-C", + vi_VN_Wavenet_D = "vi-VN-Wavenet-D", + yue_HK_Standard_A = "yue-HK-Standard-A", + yue_HK_Standard_B = "yue-HK-Standard-B", + yue_HK_Standard_C = "yue-HK-Standard-C", + yue_HK_Standard_D = "yue-HK-Standard-D", +} + +export enum Languages { + af_ZA = "af-ZA", + am_ET = "am-ET", + ar_XA = "ar-XA", + bg_BG = "bg-BG", + bn_IN = "bn-IN", + ca_ES = "ca-ES", + cmn_CN = "cmn-CN", + cmn_TW = "cmn-TW", + cs_CZ = "cs-CZ", + da_DK = "da-DK", + de_DE = "de-DE", + el_GR = "el-GR", + en_AU = "en-AU", + en_GB = "en-GB", + en_IN = "en-IN", + en_US = "en-US", + es_ES = "es-ES", + es_US = "es-US", + et_EE = "et-EE", + eu_ES = "eu-ES", + fi_FI = "fi-FI", + fil_PH = "fil-PH", + fr_CA = "fr-CA", + fr_FR = "fr-FR", + gl_ES = "gl-ES", + gu_IN = "gu-IN", + he_IL = "he-IL", + hi_IN = "hi-IN", + hu_HU = "hu-HU", + id_ID = "id-ID", + is_IS = "is-IS", + it_IT = "it-IT", + ja_JP = "ja-JP", + kn_IN = "kn-IN", + ko_KR = "ko-KR", + lt_LT = "lt-LT", + lv_LV = "lv-LV", + ml_IN = "ml-IN", + mr_IN = "mr-IN", + ms_MY = "ms-MY", + nb_NO = "nb-NO", + nl_BE = "nl-BE", + nl_NL = "nl-NL", + pa_IN = "pa-IN", + pl_PL = "pl-PL", + pt_BR = "pt-BR", + pt_PT = "pt-PT", + ro_RO = "ro-RO", + ru_RU = "ru-RU", + sk_SK = "sk-SK", + sr_RS = "sr-RS", + sv_SE = "sv-SE", + ta_IN = "ta-IN", + te_IN = "te-IN", + th_TH = "th-TH", + tr_TR = "tr-TR", + uk_UA = "uk-UA", + ur_IN = "ur-IN", + vi_VN = "vi-VN", + yue_HK = "yue-HK", +} diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..87bf552 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "src" + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "**/*.spec.ts"] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..806eb4b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "./node_modules/@tsconfig/node20/tsconfig.json", + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + "outDir": "lib", + "rootDir": ".", + "declaration": true, + "declarationDir": "lib" + }, + "include": ["src/**/*", "tests/**/*", "update-voices.ts"], + "exclude": ["node_modules"], + "ts-node": { + "files": true + } +} diff --git a/update-voices.ts b/update-voices.ts new file mode 100644 index 0000000..423405b --- /dev/null +++ b/update-voices.ts @@ -0,0 +1,48 @@ +import { TextToSpeechClient } from "@google-cloud/text-to-speech"; +import { existsSync, readFileSync, writeFileSync } from "fs"; + +(async function () { + // Check if we have the required environment variables + const { TTS_GCP_CREDENTIALS } = process.env; + if (typeof TTS_GCP_CREDENTIALS !== "string") { + throw new Error("Missing GOOGLE_APPLICATION_CREDENTIALS or GOOGLE_APPLICATION_PROJECT_ID environment variable!"); + } + if (!existsSync(TTS_GCP_CREDENTIALS)) { + throw new Error(`${TTS_GCP_CREDENTIALS} doesn't exist!`); + } + const credentials = Object.freeze(JSON.parse(readFileSync(TTS_GCP_CREDENTIALS, { encoding: "utf-8" }))); + + // Create the speech synthesizer. + const client = new TextToSpeechClient({ credentials: credentials }); + + // Create the enum definitions with all available voices. + const [result] = await client.listVoices({}); + const voices = result.voices; + if (voices === null || voices === undefined) { + throw new Error("Can't fetch availables voices!"); + } + const languages = [] as Array; + let enumDef = "export enum Voices {\n"; + for (const voice of voices) { + if (voice.name === null || voice.name === undefined) { + continue; + } + enumDef += ` ${voice.name.replaceAll("-", "_")} = "${voice.name}",\n`; + if (voice.languageCodes !== null && voice.languageCodes !== undefined) { + for (const languageCode of voice.languageCodes) { + if (!languages.includes(languageCode)) { + languages.push(languageCode); + } + } + } + } + enumDef += "}\n\n"; + enumDef += "export enum Languages {\n"; + for (const language of languages) { + enumDef += ` ${language.replaceAll("-", "_")} = "${language}",\n`; + } + enumDef += "}\n"; + + // Write the enum in the src folder + writeFileSync("./src/voices.ts", enumDef, { encoding: "utf-8", mode: 0o664 }); +})();