An Azure service that integrates speech processing into apps and services.
Hi @Chirravuri Venkata, Bharani Chakravarthy,
I create a speech service and using below code, it works fine. And I also verified the generated ogg file, it's correct.
Test Code
// See https://aka.ms/new-console-template for more information
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
private static readonly string apiKey = "Your_Api_Key";
private static readonly string endpoint = "https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1";
//https://%SPEECH_REGION%.tts.speech.microsoft.com/cognitiveservices/v1
static async Task Main(string[] args)
{
string text = "First Floor";
string voice = "en-US-JennyNeural";
string audioFormat = "ogg-16khz-16bit-mono-opus";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
request.Content = new StringContent(GenerateSsml(text, voice), Encoding.UTF8, "application/ssml+xml");
request.Headers.Add("X-Microsoft-OutputFormat", audioFormat);
request.Headers.Add("User-Agent", "ConsoleApp");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
using (Stream responseStream = await response.Content.ReadAsStreamAsync())
using (FileStream fileStream = new FileStream("output.ogg", FileMode.Create, FileAccess.Write, FileShare.None))
{
await responseStream.CopyToAsync(fileStream);
Console.WriteLine("Audio file saved successfully.");
}
}
}
static string GenerateSsml(string text, string voice)
{
return $@"
<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'>
<voice name='{voice}'>{text}</voice>
</speak>";
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards
Jason