Merge pull request #8559 from ivandrofly/text-to-speech

Implement IDisposable pattern on HttpClient usage
This commit is contained in:
Nikolaj Olsson 2024-06-23 07:44:23 +02:00 committed by GitHub
commit 7603bdfda9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -933,7 +933,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(nikseComboBoxVoice.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? nikseComboBoxVoice.Text : "http://localhost:5002/api/tts"); httpClient.BaseAddress = new Uri(nikseComboBoxVoice.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? nikseComboBoxVoice.Text : "http://localhost:5002/api/tts");
progressBar1.Value = 0; progressBar1.Value = 0;
@ -977,10 +978,12 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private bool GenerateParagraphAudioAllTalk(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioAllTalk(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://127.0.0.1:7851"); httpClient.BaseAddress = new Uri("http://127.0.0.1:7851");
progressBar1.Value = 0; progressBar1.Value = 0;
@ -1054,6 +1057,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private bool GenerateParagraphAudioElevenLabs(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioElevenLabs(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
@ -1064,7 +1068,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return false; return false;
} }
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("xi-api-key", nikseTextBoxApiKey.Text.Trim()); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("xi-api-key", nikseTextBoxApiKey.Text.Trim());
@ -1131,6 +1136,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private List<AzureVoiceModel> GetAzureVoices(bool useCache) private List<AzureVoiceModel> GetAzureVoices(bool useCache)
{ {
@ -1233,7 +1239,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return false; return false;
} }
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "ssml+xml"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "ssml+xml");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Microsoft-OutputFormat", "audio-16khz-32kbitrate-mono-mp3"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Microsoft-OutputFormat", "audio-16khz-32kbitrate-mono-mp3");
@ -1301,6 +1308,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private void buttonOK_Click(object sender, EventArgs e) private void buttonOK_Click(object sender, EventArgs e)
{ {
@ -1806,7 +1814,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
var url = "https://huggingface.co/rhasspy/piper-voices/resolve/main/voices.json?download=true"; var url = "https://huggingface.co/rhasspy/piper-voices/resolve/main/voices.json?download=true";
@ -1823,6 +1832,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {
@ -1903,7 +1913,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
@ -1927,6 +1938,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {
@ -1991,7 +2003,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using(var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
@ -2010,6 +2023,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {