<
speak
version
=
'1.0'
xml:lang
'en-US'
><
voice
'ta-IN'
xml:gender
'Female'
name
'Microsoft Server Speech Text to Speech Voice (ta-IN, Valluvar)'
>
நன்றி
</
></
using
Microsoft.AspNetCore.Mvc.Rendering;
System.Collections.Generic;
System.ComponentModel;
namespace
TextToSpeechApp.Models
{
public
class
SpeechModel
string
Content {
get
;
set
; }
SubscriptionKey {
; } =
"< Subscription Key >"
[DisplayName(
"Language Selection :"
)]
LanguageCode {
"NA"
List<SelectListItem> LanguagePreference {
new
List<SelectListItem>
SelectListItem { Value =
, Text =
"-Select-"
},
"en-US"
"English (United States)"
"en-IN"
"English (India)"
"ta-IN"
"Tamil (India)"
"hi-IN"
"Hindi (India)"
"te-IN"
"Telugu (India)"
}
};
System.Threading.Tasks;
TextToSpeechApp.BusinessLayer.Interface
interface
ITextToSpeech
Task<
byte
[]> TranslateText(
token,
key,
content,
lang);
///
<summary>
/// Translate text to speech
/// </summary>
/// <param name="token">Authentication token</param>
/// <param name="key">Azure subscription key</param>
/// <param name="content">Text content for speech</param>
/// <param name="lang">Speech conversion language</param>
/// <returns></returns>
async Task<
lang)
//Request url for the speech api.
uri =
"https://westus.tts.speech.microsoft.com/cognitiveservices/v1"
//Generate Speech Synthesis Markup Language (SSML)
var requestBody =
this
.GenerateSsml(lang,
"Female"
,
.ServiceName(lang), content);
(var client =
HttpClient())
(var request =
HttpRequestMessage())
request.Method = HttpMethod.Post;
request.RequestUri =
Uri(uri);
request.Headers.Add(
"Ocp-Apim-Subscription-Key"
, key);
request.Headers.Authorization =
AuthenticationHeaderValue(
"Bearer"
, token);
"X-Microsoft-OutputFormat"
"audio-16khz-64kbitrate-mono-mp3"
);
request.Content =
StringContent(requestBody, Encoding.UTF8,
"text/plain"
request.Content.Headers.Remove(
"Content-Type"
request.Content.Headers.Add(
"application/ssml+xml"
"User-Agent"
"TexttoSpeech"
var response = await client.SendAsync(request);
var httpStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(
false
Stream receiveStream = httpStream;
[] buffer =
[32768];
(Stream stream = httpStream)
(MemoryStream ms =
MemoryStream())
[] waveBytes =
null
int
count = 0;
do
[] buf =
[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
while
(stream.CanRead && count > 0);
waveBytes = ms.ToArray();
return
waveBytes;
It's recommended to read more articles related to ASP.NET Core & Azure App Service: