From 5533fea21f50cde145be333a2d11758975c5cbca Mon Sep 17 00:00:00 2001 From: Zeeshaun Date: Thu, 23 Apr 2026 14:16:17 +0000 Subject: [PATCH] Updating --- .../Model/SpitConfiguration.cs | 56 +++++++++++++++++++ .../Services/TextToSpeechService.cs | 22 +++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Model/SpitConfiguration.cs b/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Model/SpitConfiguration.cs index 9cb574e..f641efb 100644 --- a/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Model/SpitConfiguration.cs +++ b/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Model/SpitConfiguration.cs @@ -5,6 +5,8 @@ namespace LCU.Webapps.Marketing.SPIT.Model { public class SpitConfiguration { + private const string DefaultSpeechSynthesisPath = "/tts/cognitiveservices/v1"; + public string AppName { get; set; } public string SeqApiKey { get; set; } public string SeqUrl { get; set; } @@ -36,6 +38,45 @@ namespace LCU.Webapps.Marketing.SPIT.Model return new Uri(SpeechEndpoint.Trim(), UriKind.Absolute); } + public Uri ResolveSpeechSynthesisEndpointUri() + { + var endpoint = ResolveSpeechEndpointUri(); + if (endpoint == null) + { + return null; + } + + var endpointPath = endpoint.AbsolutePath?.Trim() ?? string.Empty; + if (string.IsNullOrWhiteSpace(endpointPath) || endpointPath == "/") + { + return new Uri(endpoint, ResolveSpeechSynthesisPath()); + } + + if (endpointPath.EndsWith("/websocket/v1", StringComparison.OrdinalIgnoreCase)) + { + return new UriBuilder(endpoint) + { + Scheme = Uri.UriSchemeHttps, + Port = -1, + Path = endpointPath.Replace("/websocket/v1", "/v1", StringComparison.OrdinalIgnoreCase), + Query = string.Empty + }.Uri; + } + + if (endpointPath.EndsWith("/voices/list", StringComparison.OrdinalIgnoreCase)) + { + return new UriBuilder(endpoint) + { + Scheme = Uri.UriSchemeHttps, + Port = -1, + Path = endpointPath.Replace("/voices/list", "/v1", StringComparison.OrdinalIgnoreCase), + Query = string.Empty + }.Uri; + } + + return endpoint; + } + public Uri ResolveSpeechTokenUri() { return new Uri(ResolveSpeechEndpointUri(), FetchTokenUri); @@ -45,5 +86,20 @@ namespace LCU.Webapps.Marketing.SPIT.Model { return new Uri(ResolveSpeechEndpointUri(), VoicesUrl); } + + private string ResolveSpeechSynthesisPath() + { + if (!string.IsNullOrWhiteSpace(WebsocketUrl)) + { + return WebsocketUrl.Replace("/websocket/v1", "/v1", StringComparison.OrdinalIgnoreCase); + } + + if (!string.IsNullOrWhiteSpace(VoicesUrl)) + { + return VoicesUrl.Replace("/voices/list", "/v1", StringComparison.OrdinalIgnoreCase); + } + + return DefaultSpeechSynthesisPath; + } } } diff --git a/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Services/TextToSpeechService.cs b/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Services/TextToSpeechService.cs index a7d27f3..b4ae8e1 100644 --- a/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Services/TextToSpeechService.cs +++ b/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/LCU.Webapps.Marketing.SPIT/Services/TextToSpeechService.cs @@ -107,9 +107,25 @@ namespace LCU.Webapps.Marketing.SPIT.Services private SpeechConfig BuildSpeechConfig() { - return SpeechConfig.FromEndpoint( - _spitConfiguration.ResolveSpeechEndpointUri(), - _spitConfiguration.SpeechApiKey); + var synthesisEndpoint = _spitConfiguration.ResolveSpeechSynthesisEndpointUri(); + if (synthesisEndpoint != null) + { + Log.Information( + "Using speech synthesis endpoint {SpeechSynthesisEndpoint} for file generation.", + synthesisEndpoint); + + return SpeechConfig.FromEndpoint( + synthesisEndpoint, + _spitConfiguration.SpeechApiKey); + } + + Log.Information( + "SpeechEndpoint is not configured for file generation. Falling back to region {Region}.", + _spitConfiguration.Region); + + return SpeechConfig.FromSubscription( + _spitConfiguration.SpeechApiKey, + _spitConfiguration.Region); } } }