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 30, 2021
1 parent bef4fea commit 2a81604
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 228 deletions.
210 changes: 0 additions & 210 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 13 additions & 15 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ export class GBConversationalService {
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
}

public static async getAudioBufferFromText(speechKey, cloudRegion, text, locale): Promise<string> {
public static async getAudioBufferFromText( text): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
const name = GBAdminService.getRndReadableIdentifier();

const waveFilename = `work/tmp${name}.pcm`;
try {

const textToSpeech = new TextToSpeechV1({
Expand All @@ -307,27 +306,26 @@ export class GBConversationalService {

const params = {
text: text,
accept: 'audio/l16; rate=44100',
voice: 'pt-BR_IsabelaV3Voice'
accept: 'audio/ogg;codecs=vorbis', // ; rate=44100
voice: 'pt-BR_IsabelaVoice'
};

// Migrated to IBM from MSFT, as it own package do not compile on Azure Web App.

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

// Converts to OGG.

let res = await textToSpeech.synthesize(params);
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 body = '';
res.result.on('data',(chunck) => {
body += chunck;
});

res.result.on('end', () => {
fs.writeFileSync(oggFilename, body);
res.statusCode = 200;
});

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

Expand Down
4 changes: 1 addition & 3 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,7 @@ export class WhatsappDirectLine extends GBService {
public async sendTextAsAudioToDevice(to, msg) {

const url = await GBConversationalService.getAudioBufferFromText(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
msg, this.locale
msg
);

await this.sendFileToDevice(to, url, 'Audio', msg);
Expand Down
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export class GBServer {
const azureDeployer: AzureDeployerService = new AzureDeployerService(deployer);
const adminService: GBAdminService = new GBAdminService(core);



if (process.env.NODE_ENV === 'development') {
const proxy = GBConfigService.get('REVERSE_PROXY');
if (proxy !== undefined) {
Expand Down

0 comments on commit 2a81604

Please sign in to comment.