c# Azure 文本转语音 中文汉字多音字处理 英文按指定音标生成音频

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);

}

文件名精灵2025年最新版 机器翻译 AI智能
...
批量修改文件名称、文件夹名称

免费使用 功能无限制

全面接入机器自动翻译功能,主流AI大模型智能改名

方便快捷,支持正则表达式、无需安装功能齐全、支持定制无广告、无插件,放心使用。

点击下载 点击下载
本站中所有的计算器的计算结果仅供参考,本站对此结果的准确性不承担任何责任,实际数额以银行/保险公司/国家相关机构确认的结果为准。
在线客服QQ:543690914,备案号: 苏ICP备15037649号-27。东海县白塔埠镇佳诚电脑经营部版权所有。