c# Azure 文本转语音 中文汉字多音字处理 英文按指定音标生成音频
private async void english2AudioService()
{
string subscriptionKey = "";
string region = "eastus";
var config = SpeechConfig.FromSubscription(subscriptionKey, region);
//config.SpeechSynthesisVoiceName = "en-US-JennyNeural";
config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3); // 设置输出格式为MP3
using (var synthesizer = new SpeechSynthesizer(config, null))
{
//en-GB-AdaMultilingualNeural en
//config.SpeechSynthesisVoiceName = "en-US-AvaMultilingualNeural";
//en-US-AndrewMultilingualNeural
// 使用SSML定义语音合成的内容
//onfig.SpeechSynthesisVoiceName = "en-GB-AdaMultilingualNeural";
string ssml = "<speak version = '1.0' xmlns = 'http://www.w3.org/2001/10/synthesis' xml:lang = 'en-US' > " +
"<voice name='en-GB-AdaMultilingualNeural'>Listen to me carefully,Mr Zhang," +
"<prosody rate='-20%' pitch='+10%'><phoneme alphabet='ipa' ph='təˈmɑːtəʊ'>tomato</phoneme></prosody>" +
"</voice>" +
"<voice name='en-US-AvaMultilingualNeural'>" +
"<prosody rate='-20%' pitch='+10%'><phoneme alphabet='ipa' ph='təˈmeɪtoʊ'>tomato</phoneme></prosody>" +
"</voice></speak>";
//zh-CN-YunjianNeural
string savePath = Path.Combine(Application.StartupPath, "en-US-JennyNeural");
if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
// 合成语音
var result = await synthesizer.SpeakSsmlAsync(ssml);
// 检查结果
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
var stream = AudioDataStream.FromResult(result);
string fn = Path.Combine(Application.StartupPath, "2.mp3");
if (File.Exists(fn))
File.Delete(fn);
await stream.SaveToWaveFileAsync(fn); //文件名不以用中文
Process.Start(fn);
}
}
private async void convertService20260603()
{
string key = "";
string region = "eastus";
var config = SpeechConfig.FromSubscription(key, region);
config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);
string ssml = @"
<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='zh-CN'>
<voice name='zh-CN-XiaoxiaoNeural'>
注意听注意听注意听注意听注意听:你说
<phoneme alphabet='x-microsoft-sapi' ph='bo 2'>重</phoneme>,
我说
<phoneme alphabet='x-microsoft-sapi' ph='bao 2'>重</phoneme>。
</voice>
</speak>";
string savefn = @"D:\test2.mp3";
if (File.Exists(savefn)) File.Delete(savefn);
using (var synth = new SpeechSynthesizer(config))
{
var result = await synth.SpeakSsmlAsync(ssml);
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
await AudioDataStream.FromResult(result).SaveToWaveFileAsync(savefn);
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}"); // 取消原因
if (cancellation.Reason == CancellationReason.Error) // 如果是错误导致的取消
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}"); // ⭐ 关键:错误代码
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}"); // ⭐ 关键:详细信息
// 修改下面这行,打印出你实际的错误信息
// Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
}
}
}
Process.Start(savefn);
}