From 7a91876cd7c359d363fd149017623e6aa18afe71 Mon Sep 17 00:00:00 2001 From: Quan HL Date: Wed, 9 Aug 2023 17:56:38 +0700 Subject: [PATCH] support self hosted microsoft --- lib/synth-audio.js | 15 ++++++++++++--- lib/utils.js | 8 +++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/synth-audio.js b/lib/synth-audio.js index 40d7059..cb61f28 100644 --- a/lib/synth-audio.js +++ b/lib/synth-audio.js @@ -14,7 +14,7 @@ const { CancellationDetails, SpeechSynthesisOutputFormat } = sdk; -const {makeSynthKey, createNuanceClient, createKryptonClient, createRivaClient, noopLogger} = require('./utils'); +const {makeSynthKey, createNuanceClient, createKryptonClient, createRivaClient, noopLogger, isValidHttpOrWsUrl} = require('./utils'); const getNuanceAccessToken = require('./get-nuance-access-token'); const { SynthesisRequest, @@ -309,10 +309,19 @@ const synthMicrosoft = async(logger, { const {api_key: apiKey, region, use_custom_tts, custom_tts_endpoint} = credentials; const trimSilence = filePath.endsWith('.r8'); let content = text; - const speechConfig = SpeechConfig.fromSubscription(apiKey, region); + let speechConfig; + if (use_custom_tts && isValidHttpOrWsUrl(custom_tts_endpoint)) { + if (apiKey) { + speechConfig = SpeechConfig.fromEndpoint(custom_tts_endpoint, apiKey); + } else { + speechConfig = SpeechConfig.fromEndpoint(custom_tts_endpoint); + } + } else { + speechConfig = SpeechConfig.fromSubscription(apiKey, region); + } speechConfig.speechSynthesisLanguage = language; speechConfig.speechSynthesisVoiceName = voice; - if (use_custom_tts && custom_tts_endpoint) { + if (use_custom_tts && !isValidHttpOrWsUrl(custom_tts_endpoint)) { speechConfig.endpointId = custom_tts_endpoint; /** diff --git a/lib/utils.js b/lib/utils.js index ba91d27..e6e7616 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -106,6 +106,11 @@ const createRivaClient = async(rivaUri) => { return client; }; +const isValidHttpOrWsUrl = (url) => { + const pattern = new RegExp('^(https?:\/\/|wss?:\/\/)'); + return !!url.match(pattern); +} + module.exports = { makeSynthKey, @@ -117,5 +122,6 @@ module.exports = { createRivaClient, makeBasicAuthHeader, NUANCE_AUTH_ENDPOINT, - noopLogger + noopLogger, + isValidHttpOrWsUrl };