Skip to content

Commit

Permalink
fix(all): Added MSFT cognitive stack again to test if it is working n…
Browse files Browse the repository at this point in the history
…ow on Azure.
  • Loading branch information
rodrigorodriguez committed Dec 28, 2021
1 parent dcb1c47 commit 4452a31
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 7 deletions.
210 changes: 210 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"js-beautify": "1.13.13",
"luxon": "2.0.2",
"marked": "2.0.7",
"microsoft-cognitiveservices-speech-sdk": "^1.19.0",
"momentjs": "2.0.0",
"ms-rest-azure": "3.0.0",
"nexmo": "2.9.1",
Expand Down
47 changes: 47 additions & 0 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,53 @@ export class GBConversationalService {
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
}

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

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

var audioConfig = sdk.AudioConfig.fromAudioFileOutput(waveFilename);
var speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, cloudRegion);

var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);

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']
});

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

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

public static async getTextFromAudioBuffer(speechKey, cloudRegion, buffer, locale): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
try {
Expand Down
Loading

0 comments on commit 4452a31

Please sign in to comment.