Skip to content

Commit

Permalink
fix(core.gbapp): Added IBM for TTS and removed MSFT while it does not…
Browse files Browse the repository at this point in the history
… compile on AZR.
  • Loading branch information
rodrigorodriguez committed Dec 29, 2021
1 parent c12a7d7 commit 4dbc1eb
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const prism = require('prism-media');
const request = require('request-promise-native');
const fs = require('fs');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const marked = require('marked');
const { Translate } = require('@google-cloud/translate').v2;
Expand Down Expand Up @@ -256,7 +257,7 @@ export class GBConversationalService {
public async sendEvent(min: GBMinInstance, step: GBDialogStep, name: string, value: Object): Promise<any> {
if (!this.userMobile(step)) {
GBLog.info(`Sending event ${name}:${typeof value === 'object' ? JSON.stringify(value) :
value?value:''} to client...`);
value ? value : ''} to client...`);
const msg = MessageFactory.text('');
msg.value = value;
msg.type = 'event';
Expand Down Expand Up @@ -287,7 +288,7 @@ export class GBConversationalService {
});
}

public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId ) {
public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId) {
GBLog.info(`Sending message ${message} to ${mobile}...`);
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
}
Expand All @@ -297,42 +298,39 @@ export class GBConversationalService {
const name = GBAdminService.getRndReadableIdentifier();

const waveFilename = `work/tmp${name}.pcm`;
const sdk = require('microsoft-cognitiveservices-speech-sdk');
sdk.Recognizer.enableTelemetry(false);
try {

var audioConfig = sdk.AudioConfig.fromAudioFileOutput(waveFilename);
var speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, cloudRegion);
const textToSpeech = new TextToSpeechV1({
authenticator: new IamAuthenticator({ apikey: process.env.WATSON_TTS_KEY }),
url: process.env.WATSON_STT_URL
});

var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
const params = {
text: text,
accept: 'audio/l16; rate=44100',
voice: 'pt-BR_IsabelaV3Voice'
};

try {
speechConfig.speechSynthesisLanguage = locale;
speechConfig.speechSynthesisVoiceName = 'pt-BR-FranciscaNeural';

synthesizer.speakTextAsync(text, result => {
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
let raw = Buffer.from(result.audioData);
fs.writeFileSync(waveFilename, raw);
GBLog.info(`Audio data byte size: ${result.audioData.byteLength}.`);
const oggFilenameOnly = `tmp${name}.ogg`;
const oggFilename = `work/${oggFilenameOnly}`;

const output = fs.createWriteStream(oggFilename);
const transcoder = new prism.FFmpeg({
args: ['-analyzeduration', '0', '-loglevel', '0', '-f', 'opus', '-ar', '16000', '-ac', '1']
});
// Migrated to IBM from MSFT, as it own package do not compile on Azure Web App.

fs.createReadStream(waveFilename).pipe(transcoder).pipe(output);
let buffer = await textToSpeech.synthesize(params);
fs.writeFileSync(waveFilename, buffer);
GBLog.info(`Audio data byte size: ${buffer.byteLength}.`);

let url = urlJoin(GBServer.globals.publicAddress, 'audios', oggFilenameOnly);
resolve(url);
} else {
const error = 'Speech synthesis canceled, ' + result.errorDetails;
reject(error);
}
synthesizer.close();
synthesizer = undefined;
// Converts to OGG.

const oggFilenameOnly = `tmp${name}.ogg`;
const oggFilename = `work/${oggFilenameOnly}`;
const output = fs.createWriteStream(oggFilename);
const transcoder = new prism.FFmpeg({
args: ['-analyzeduration', '0', '-loglevel', '0', '-f', 'opus', '-ar', '16000', '-ac', '1']
});

fs.createReadStream(waveFilename).pipe(transcoder).pipe(output);

let url = urlJoin(GBServer.globals.publicAddress, 'audios', oggFilenameOnly);
resolve(url);

} catch (error) {
reject(error);
}
Expand Down Expand Up @@ -481,7 +479,7 @@ export class GBConversationalService {
answer: string
) {
const locale = step.context.activity.locale;

html = html.replace(/src\=\"kb\//gi, `src=\"../kb/`);
await this.sendEvent(min, step, 'play', {
playerType: 'markdown',
Expand Down

0 comments on commit 4dbc1eb

Please sign in to comment.