This commit is contained in:
Zeeshaun
2026-04-23 14:46:06 +00:00
parent 5533fea21f
commit ade58260d7
@@ -11,20 +11,18 @@ namespace LCU.Webapps.Marketing.SPIT.Services
{ {
private readonly SpitConfiguration _spitConfiguration; private readonly SpitConfiguration _spitConfiguration;
private readonly IFileOperationsApiService _fileOperationsApiService; private readonly IFileOperationsApiService _fileOperationsApiService;
private SpeechConfig _speechConfig;
public TextToSpeechService(SpitConfiguration spitConfiguration, IFileOperationsApiService fileOperationsApiService) public TextToSpeechService(SpitConfiguration spitConfiguration, IFileOperationsApiService fileOperationsApiService)
{ {
_spitConfiguration = spitConfiguration; _spitConfiguration = spitConfiguration;
_fileOperationsApiService = fileOperationsApiService; _fileOperationsApiService = fileOperationsApiService;
_speechConfig = BuildSpeechConfig();
} }
public async Task<string> SpeechToFile(TextToSpeech textToSpeech) public async Task<string> SpeechToFile(TextToSpeech textToSpeech)
{ {
try try
{ {
_speechConfig.SpeechSynthesisVoiceName = textToSpeech.SelectedVoiceShortName; var speechConfig = BuildSpeechConfig();
speechConfig.SpeechSynthesisVoiceName = textToSpeech.SelectedVoiceShortName;
var finalDestination = (_spitConfiguration.FileDestination ?? string.Empty).Trim(); var finalDestination = (_spitConfiguration.FileDestination ?? string.Empty).Trim();
if (string.IsNullOrWhiteSpace(finalDestination)) if (string.IsNullOrWhiteSpace(finalDestination))
{ {
@@ -41,7 +39,7 @@ namespace LCU.Webapps.Marketing.SPIT.Services
finalDestination, finalDestination,
textToSpeech.Ssml?.Length ?? 0); textToSpeech.Ssml?.Length ?? 0);
using var synth = new SpeechSynthesizer(_speechConfig); using var synth = new SpeechSynthesizer(speechConfig, audioConfig: null);
using var result = await synth.SpeakSsmlAsync(textToSpeech.Ssml); using var result = await synth.SpeakSsmlAsync(textToSpeech.Ssml);
if (result.Reason != ResultReason.SynthesizingAudioCompleted) if (result.Reason != ResultReason.SynthesizingAudioCompleted)
{ {
@@ -107,11 +105,24 @@ namespace LCU.Webapps.Marketing.SPIT.Services
private SpeechConfig BuildSpeechConfig() private SpeechConfig BuildSpeechConfig()
{ {
var synthesisEndpoint = _spitConfiguration.ResolveSpeechSynthesisEndpointUri(); var speechEndpoint = _spitConfiguration.ResolveSpeechEndpointUri();
if (synthesisEndpoint != null) if (speechEndpoint != null)
{
var endpointPath = speechEndpoint.AbsolutePath?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(endpointPath) || endpointPath == "/")
{ {
Log.Information( Log.Information(
"Using speech synthesis endpoint {SpeechSynthesisEndpoint} for file generation.", "Using speech host {SpeechHost} for file generation.",
speechEndpoint);
return SpeechConfig.FromHost(
speechEndpoint,
_spitConfiguration.SpeechApiKey);
}
var synthesisEndpoint = _spitConfiguration.ResolveSpeechSynthesisEndpointUri();
Log.Information(
"Using explicit speech endpoint {SpeechSynthesisEndpoint} for file generation.",
synthesisEndpoint); synthesisEndpoint);
return SpeechConfig.FromEndpoint( return SpeechConfig.FromEndpoint(