作业要求

1.短语音识别:选择音频文件,识别并显示结果
2.语音合成:播放合成语音

具体实现如下图

在VS里安装SDK,NuGet搜索:Baidu.AI

 先在百度AI开放平台注册,然后申领免费调用额度。

Form1.cs:

using Baidu.Aip.Speech;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PiggyVoiceT
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        //选择文件按钮,可以自行修改
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "选择文件";
            fdlg.FilterIndex = 2;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                filePath.Text = System.IO.Path.GetFullPath(fdlg.FileName);
                int start = filePath.Text.LastIndexOf(".") + 1;
                int end = filePath.Text.Length;
                voiceType.Text = filePath.Text.Substring(start);
            }
        }
        //开始识别按钮,可以自行修改
        private void button2_Click(object sender, EventArgs e)
        {
            string value = this.voiceType.Text;
            String filePath = this.filePath.Text;
            // 设置APPID/AK/SK,修改为自己的百度APPID/AK/SK
            String APP_ID = "xxxx";
            String API_KEY = "xxxxxx";
            String SECRET_KEY = "xxxxxxx";
            var client = new Asr(APP_ID, API_KEY, SECRET_KEY);
           // client.Timeout = 60000;  // 修改超时时间
            client.Timeout = 120000; // 若语音较长,建议设置更大的超时时间. ms
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] buffur = new byte[fs.Length];
            try
            {
                fs.Read(buffur, 0, (int)fs.Length);

            }
            catch (Exception ex)
            {
                Console.Write(ex.StackTrace);
            }
            finally
            {
                if (fs != null)
                {
                    //关闭资源  
                    fs.Close();
                }
            }
            var result = client.Recognize(buffur, value, 16000);
            Convert.ToString(result);
            JToken resultStr = null;
            result.TryGetValue("result", out resultStr);
            Console.WriteLine("aToken===>" + resultStr);
            voiceResult.Text = Convert.ToString(resultStr);
            Console.Write(result);
        }

        //合成语音
        //开始合成按钮,可以自行修改
        private void button3_Click(object sender, EventArgs e)
        {
           // String APP_ID = "31494867";
            String API_KEY = "WjhbDaPGirczVjxIKcf9Uog3";
            String SECRET_KEY = "xcg1c2Bt44db9KAY5q2e3tIYRZkRrbV5";
            // 获取输入框的值
            String value = this.Speech_Synthesis.Text;
            // 将 value 转成语音文件存放到本地
            var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY);
            // 可选参数
            var option = new Dictionary<string, object>()
    {
        {"spd", 5}, // 语速
        {"vol", 7}, // 音量
        {"per", 3}  // 发音人
    };
            var result = client.Synthesis(value, option);
            try
            {
                if (result.ErrorCode == 0)
                { //可自己选保存的目录
                    File.WriteAllBytes("C:tmp.mp3", result.Data);
                    outputdir.Text = "语音合成已完成,文件已保存至C:tmp.mp3。";
                }
                else
                {
                    outputdir.Text = "语音合成失败。";
                }

            }
            catch (Exception ex) { Console.Write(ex.StackTrace); }
             
            //播放合成的文件          
            MP3Player mp3 = new MP3Player();
            //设置要播放的文件   
            mp3.FilePath = "C:tmp.mp3";
            //播放   
            mp3.Play();
            //暂停   
          //  mp3.Pause();
            //停止   
         //   mp3.Stop();

        }
    }
    //定义播放器
    public class MP3Player
    {
        //文件地址     
        public string FilePath;

        // 播放   
        public void Play()
        {
            mciSendString("close all", "", 0, 0);
            mciSendString("open " + FilePath + " alias media", "", 0, 0);
            mciSendString("play media", "", 0, 0);
        }
        // 暂停         
        public void Pause()
        {
            mciSendString("pause media", "", 0, 0);
        }
        // 停止   
        public void Stop()
        {
            mciSendString("close media", "", 0, 0);
        }

        // API函数    
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        private static extern int mciSendString(
         string lpstrCommand,
         string lpstrReturnString,
         int uReturnLength,
         int hwndCallback
        );
    }
}

界面设计代码自行修改

更多推荐