语音和文本互转
安装Baidu.AI
Install-Package Baidu.AI
将文本合成语音
将文本合成为语音文件主要使用百度云API中的Tts类,该类是语音合成的交互类,为使用语音合成的开发人员提供了一系列的交互方法。
Tts 类中提供了一个Synthnesis 方法,哟过来将文本合成语音,其语法如下:
参数 |
类型 |
描述 |
text |
String |
合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节 |
option |
Dictionary |
可选参数,用来指定合成语音的一些信息,如语速,男声、女生、音量 |
spd |
String |
语速,0-9,默认为5,中语速 |
pit |
String |
语调,取值0-9,默认5 中语调 |
vol |
String |
音量,取值0-15,默认为5 中音量 |
per |
string |
发音人选择,0为女声 1为男声 3为情感合成 度逍遥 4 为情感合成 度YY |
guid |
string |
用户唯一标识,用来区分用户,用来设置机器MAC地址或IMEI码,长度为60以内 |
语音识别
将语音识别为文本主要使用百度云API中的Asr类,该类是语音识别的交互类,为使用语音识别的开发人员提供了一系列的交互方法。
Asr 类中提供了一个Recognize方法,用来向远程服务上传整段语音进行识别,其语法如下:
参数 |
类型 |
描述 |
data |
byte[] |
语音二进制数据,语音文件的格式,pcm或者wav或者amr, 不区分大小写 |
format |
string |
语音文件的格式,pcm或者wav或者amr 不区分大小写,推荐pcm文件 |
rate |
int |
采样率,16000,固定值 |
Recognize方法返回值
参数 |
类型 |
是否一定输出 |
描述 |
err_no |
int |
是 |
错误码 |
err_msg |
int |
是 |
错误描述 |
sn |
int |
是 |
语音数据唯一标识,系统内部产生,用于debug |
result |
int |
是 |
识别结果数组,提供1~5个候选结果,string 类型为识别的字符串 UTF-8编码 |
Code
using Baidu.Aip.Speech;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace VoiceToWord
{
public partial class Form1 : UIForm
{
private readonly Tts _ttsClient;
private readonly Asr _asrClient;
string APPID = "11079594";
string API_KEY = "fMA2S0U0dGPHbdbn3EmtRGfz";
string SECRET_KEY = "2d9bbfc2a45bde1056d0c1fd272fd5f2";
public Form1()
{
_ttsClient = new Tts(API_KEY, SECRET_KEY);
_asrClient = new Asr(APPID, API_KEY, SECRET_KEY);
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnYuYinHeCheng_Click(object sender, EventArgs e)
{
var option = new Dictionary<string, object>();
if (rbtnGirl.Checked)
{
option = new Dictionary<string, object>() {
{"spd",5 },
{ "vol",7},
{ "per",0}
};
}
else if (rBtnBoy.Checked)
{
option = new Dictionary<string, object>()
{
{"spd",5 },
{ "vol",7},
{ "per",1}
};
}
else if (rbtnD1.Checked)
{
option = new Dictionary<string, object>() {
{"spd",5 },
{ "vol",7},
{"per",3 }
};
}
else if (rbtnD2.Checked) {
option = new Dictionary<string, object>() {
{ "spd",5},
{"vol",7 },
{ "per",4}
};
}
var result = _ttsClient.Synthesis(txtContent.Text, option);
string filename = DateTime.Now.ToLongDateString()+ ".mp3";
if (result.Success) {
File.WriteAllBytes(filename, result.Data);
}
System.Diagnostics.Process.Start(filename);
}
private void btnSelectFile_Click(object sender, EventArgs e)
{
txtNeiRong.Text = "";
OpenFileDialog file = new OpenFileDialog();
file.Filter = "音频文件|*.pcm;*.wav;*.amr;*.mp3";
if (file.ShowDialog() == DialogResult.OK) {
txtVoiceFile.Text = file.FileName;
System.Threading.ThreadPool.QueueUserWorkItem(
(p_temp) => {
string cmd=@"ffmpeg-i"+txtVoiceFile.Text+" -ar 16000 ac 1 -f wav temp.wav";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmd+"&exit");
p.StandardInput.AutoFlush = true;
p.WaitForExit();
p.Close();
System.Threading.Thread.Sleep(1000);
var data = File.ReadAllBytes(txtVoiceFile.Text);
var result = _asrClient.Recognize(data, "pcm", 16000);
txtNeiRong.Text = "识别内容:" + Environment.NewLine + result + Environment.NewLine + "\n\n提取的文字" +
Environment.NewLine;
if (result["err_msg"].ToString() == "success.") {
txtNeiRong.Text += result["result"];
}
}
);
}
}
}
}
运行效果:


【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2023-06-01 Dev cxDBGrid 增加操作按钮