Skip to content

Commit

Permalink
Added a script
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiipsa committed Oct 3, 2023
1 parent 77223f1 commit 6e7522a
Show file tree
Hide file tree
Showing 5 changed files with 5,719 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/google-cloud/gcTextToSpeechClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import GcTextToSpeech, { type TextToSpeechClient } from '@google-cloud/text-to-speech'
import config from '../config'
import type { CredentialBody } from 'google-auth-library/build/src/auth/credentials'
import type { google } from '@google-cloud/text-to-speech/build/protos/protos'

export interface TextToSpeechParams {
text: string
languageCode: string
ssmlGender?: 'MALE' | 'FEMALE'
voiceName?: string
ssmlGender?: google.cloud.texttospeech.v1.SsmlVoiceGender | keyof typeof google.cloud.texttospeech.v1.SsmlVoiceGender | null
voiceName?: string | null
}

class GcTextToSpeechClient {
Expand Down Expand Up @@ -35,6 +36,11 @@ class GcTextToSpeechClient {

return response.audioContent
}

async listVoices (): Promise<google.cloud.texttospeech.v1.IVoice[] | null | undefined> {
const response = await this._client.listVoices()
return response[0].voices
}
}

const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8'))
Expand Down
2 changes: 1 addition & 1 deletion src/modules/text-to-speech/commandConfigList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TextToSpeechParams } from '../../google-cloud/gcTextToSpeechClient'

interface CommandConfigItem {
export interface CommandConfigItem {
command: string
gcParams: Omit<TextToSpeechParams, 'text'>
}
Expand Down
118 changes: 118 additions & 0 deletions src/modules/text-to-speech/generateConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import * as fs from 'fs'
import * as path from 'path'
import { gcTextToSpeedClient } from '../../google-cloud/gcTextToSpeechClient'
import type { google } from '@google-cloud/text-to-speech/build/protos/protos'
import type { CommandConfigItem } from './commandConfigList'

async function main (): Promise<void> {
const voices = await gcTextToSpeedClient.listVoices()

if (!voices) {
console.log('### no voices')
return
}

const getShortLangCode = (voice: google.cloud.texttospeech.v1.IVoice): string => {
const code = voice.languageCodes?.[0]

if (!code) {
return ''
}

return code.split('-')[0]
}

const getFullLangCode = (voice: google.cloud.texttospeech.v1.IVoice): string => {
const code = voice.languageCodes?.[0]

if (!code) {
return ''
}

return code
}

const configMap: Record<string, Record<string, CommandConfigItem[]>> = {}

console.log('### voices.length', voices.length)

for (const voice of voices) {
// console.log('### voice', getFullLangCode(voice), getShortLangCode(voice), voice)

const shortLangCode = getShortLangCode(voice)
const fullLangCode = getFullLangCode(voice)
const commandGender = voice.ssmlGender === 'MALE' ? 'm' : 'f'

if (!configMap[shortLangCode]) {
configMap[shortLangCode] = {}
}

const langGroupMap = configMap[shortLangCode]

if (!langGroupMap) {
continue
}

const command = `v${shortLangCode}${commandGender}`

if (!voice.ssmlGender) {
continue
}

if (!langGroupMap[fullLangCode]) {
langGroupMap[fullLangCode] = []
}

const genderGroupSet = langGroupMap[fullLangCode]

if (!genderGroupSet) {
console.log('### error')
return
}

genderGroupSet.push({
command,
gcParams: {
languageCode: fullLangCode,
ssmlGender: voice.ssmlGender,
voiceName: voice.name
}
})
}
// const jsonContent = JSON.stringify(mapToObject(configMap), null, 4)
const jsonContent = JSON.stringify(configMap, null, 4)

const filepath = path.join(__dirname, 'voices.json')

fs.writeFileSync(filepath, jsonContent)

const result = []
for (const key in configMap) {
const langMap = configMap[key]
if (Object.values(langMap).length === 1) {
const male = Object.values(langMap)[0].find((item) => item.gcParams.ssmlGender === 'MALE')
const female = Object.values(langMap)[0].find((item) => item.gcParams.ssmlGender === 'FEMALE')

if (!male) {
console.log('### does not have a male voice', key)
}

if (!female) {
console.log('### does not have a female voice', key)
}

result.push(male, female)
continue
}

console.log('### Please choose manually: ', key, Object.keys(langMap))
}

const simpleLangs = JSON.stringify(result.filter(Boolean), null, 4)

const filepath2 = path.join(__dirname, 'voices-simple.json')

fs.writeFileSync(filepath2, simpleLangs)
}

main().then(() => { console.log('### finish') }).catch(console.log)
Loading

0 comments on commit 6e7522a

Please sign in to comment.