Last active
September 22, 2019 09:08
-
-
Save xErik/ddc80052ae098cb5d40fc04f0d61df82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --- CALL FROM ANGULAR | |
import { Component, OnInit } from '@angular/core'; | |
import { AngularFireFunctions } from '@angular/fire/functions'; | |
@Component({ | |
selector: 'app-functiontest' | |
}) | |
export class FunctiontestComponent implements OnInit { | |
constructor(private func: AngularFireFunctions) { } | |
ngOnInit() { | |
let payload = { | |
text: this.phrase, | |
lang: 'vi-VN', | |
lang2: 'Wavenet-A' | |
}; | |
this.func.httpsCallable('textToSpeechRequest')(payload) | |
.toPromise() | |
.then(resp => { | |
console.log(resp.file); | |
}) | |
.catch(err => { | |
console.error({ err }); | |
}); | |
} | |
} | |
// --- RESPOND WITH FIREBASE MICROSERVICE | |
import * as functions from 'firebase-functions'; | |
// const functions = require('firebase-functions'); | |
const textToSpeech = require('@google-cloud/text-to-speech'); // Imports the Google Cloud client library | |
const admin = require('firebase-admin'); | |
const path = require('path'); | |
const os = require('os'); | |
const fs = require('fs'); | |
const client = new textToSpeech.TextToSpeechClient(); | |
const audioType = 'mp3'; | |
const gender = 'female'; | |
admin.initializeApp(); | |
const bucket = admin.storage().bucket(); | |
const buckMeta = { | |
destination: 'SET-PATH-IN-CODE', | |
contentType: 'audio/mpeg', | |
metadata: { | |
source: 'Google Text-to-Speech' | |
} | |
}; | |
export const textToSpeechRequest = functions.https.onCall((data: any, context: any) => { | |
return new Promise((resolve, reject) => { | |
const text = data.text.trim(); //sanitize! | |
const lang = data.lang; //sanitize! | |
const lang2 = data.lang2; //sanitize! | |
const destFile: string = text.replace(/ /gi, '_') + '.' + audioType | |
const tempFile = path.join(os.tmpdir(), destFile); | |
buckMeta.destination = 'audio/' + lang + '/' + gender + '/' + destFile; | |
const request = { | |
input: { text: text }, | |
voice: { | |
languageCode: lang, | |
ssmlGender: gender, | |
name:lang + '-' + lang2 | |
}, | |
audioConfig: { audioEncoding: 'MP3' } | |
}; | |
return client.synthesizeSpeech(request, (err1: any, response: any) => { | |
if (err1) { | |
console.log(err1); | |
reject(`error_synth: ${JSON.stringify(err1)}`); | |
} | |
console.log(response.audioContent); | |
fs.writeFile(tempFile, response.audioContent, 'binary', function(err2: any) { | |
if (err2) { | |
console.log(err2); | |
fs.unlink(tempFile); | |
reject(`error_write_tmp: ${JSON.stringify(err2)}`); | |
} | |
return bucket.upload(tempFile, buckMeta) | |
.then(() => { | |
fs.unlink(tempFile); | |
resolve({ | |
file: buckMeta.destination | |
}); | |
}).catch((err3: any) => { | |
fs.unlink(tempFile); | |
reject(`Failed to upload: ${JSON.stringify(err3)}`); | |
}); | |
}); | |
}) | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment