C#winform窗口基于百度云实现语音识别,MP3格式转WAV

添加的DLL库

首先是将录音文件保存在本地,我直接调用的傲瑞科技公司的样例,可以实现混音、声卡同时录制,并且以MP3格式保存在本地。如果只是声卡录制,只能保存电脑内部的声音,所以需要用混音。

大致流程

1.调用傲瑞公司的声卡、混音录制样例,以MP3格式保存在本地。
2.个人花时间相对多的部分:将MP3格式的录音文件转化为WAV格式,参数是:16000(8000可能会出现3301错误:语音模糊)因为百度云语音识别只接受WAV、Pcm格式的录音文件。
3.调用百度云语音识别:获取密钥,转化为文本。

代码部分

using ESBasic;
using NAudio.Wave;
using Newtonsoft.Json.Linq;
using Oraycn.MCapture;
using Oraycn.MFile;
using System;
using System.IO;
using System.Net;
using System.Speech.Synthesis;
using System.Text;
using System.Windows.Forms;
using WaveFormat = NAudio.Wave.WaveFormat;

namespace 测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

        Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
        Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }        
    private int audioCount = 0;
    private string strJSON;
    private ISoundcardCapturer soundcardCapturer;
    private IMicrophoneCapturer microphoneCapturer;
    private IAudioMixter audioMixter;
    private AudioFileMaker audioFileMaker;
    private SpeechSynthesizer speechSyn;
    private volatile bool isRecording = false;
    //开始声卡采集、录制 
    void capturer_CaptureError(Exception obj)
    {

    }
    void audioMixter_AudioMixed(byte[] audioData)
    {
        if (this.isRecording)
        {
            this.audioFileMaker.AddAudioFrame(audioData);
            ++this.audioCount;
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            this.audioCount = 0;

            //声卡采集器 【目前声卡采集仅支持vista以及以上系统】
            this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();

            //麦克风采集器
            this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);

            //混音器
            this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer, SoundcardMode4Mix.DoubleChannel, true);

            this.audioMixter.AudioMixed += new CbGeneric<byte[]>(audioMixter_AudioMixed);
            this.microphoneCapturer.CaptureError += new CbGeneric<Exception>(capturer_CaptureError);
            this.soundcardCapturer.CaptureError += new CbGeneric<Exception>(capturer_CaptureError);

            //开始采集
            this.microphoneCapturer.Start();
            this.soundcardCapturer.Start();

            //录制组件
            this.audioFileMaker = new AudioFileMaker();
            this.audioFileMaker.Initialize("test.mp3", AudioCodecType.MP3, this.audioMixter.SampleRate, this.audioMixter.ChannelCount);
            this.isRecording = true;                
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }
   //停止声卡采集、停止录制
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            this.Cursor = Cursors.WaitCursor;
            CbGeneric cb = new CbGeneric(this.StopRecordAsyn);
            cb.BeginInvoke(null, null);
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }
    private void StopRecordAsyn()
    {
        this.isRecording = false;
        this.microphoneCapturer.Stop();
        this.microphoneCapturer.Dispose();
        this.soundcardCapturer.Stop();
        this.soundcardCapturer.Dispose(); //释放声卡采集器
        this.audioMixter.Dispose();
        this.audioFileMaker.Close(true);
        this.audioFileMaker.Dispose();
        this.audioFileMaker = null;
        this.AfterStopRecord();
    }
    private void AfterStopRecord()
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new CbGeneric(this.AfterStopRecord));
        }
        else
        {
            this.Cursor = Cursors.Default;
            MessageBox.Show(Get());
        }
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (this.isRecording)
        {
            MessageBox.Show("正在录制视频,请先停止!");
            e.Cancel = true;
            return;
        }
    }
    public static string Convert2Wav()
    {
        string filePath = AppDomain.CurrentDomain.BaseDirectory + @"\test.Mp3";
        string directoryName = Path.GetDirectoryName(filePath);
        string fileName = Path.GetFileName(filePath);
        string tempDir = directoryName + "\\temp";//+ DateTime.Now.ToString("yyyyMMddHHmmss") + "\\";
        if (!Directory.Exists(tempDir))
        {
            Directory.CreateDirectory(tempDir);

        }
        if (filePath.EndsWith(".wav", StringComparison.CurrentCultureIgnoreCase))
        {
            using (var reader = new WaveFileReader(filePath))
            {
                var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit
                using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
                {
                    WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream);
                }
            }
        }
        else if (filePath.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase))
        {
            using (Mp3FileReader reader = new Mp3FileReader(filePath))
            {
                var newFormat = new WaveFormat(16000, 16, 1); // 8kHz, 8bit
                using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
                {
                    WaveFileWriter.CreateWaveFile(tempDir + "yyy.WAV", conversionStream);
                }
            }
        }
        return tempDir + "yyy.WAV";
    }
    public string GetStrText(string ch, string pal, string pai, string fbl, string gs)
    {
        string strText = null;
        string error = null;
        string fff = Convert2Wav();
        FileInfo fi = new FileInfo(fff);
        FileStream fs = new FileStream(fff, FileMode.Open);
        byte[] voice = new byte[fs.Length];
        fs.Read(voice, 0, voice.Length);
        fs.Close();
        string getTextUrl = "http://vop.baidu.com/server_api?lan=" + "&cuid=" + "" + pai + "" + "&token=" + "" + ch + "";
        HttpWebRequest getTextRequst = WebRequest.Create(getTextUrl) as HttpWebRequest;
        getTextRequst.Proxy = null;
        getTextRequst.ServicePoint.Expect100Continue = false;
        getTextRequst.ServicePoint.UseNagleAlgorithm = false;
        getTextRequst.ServicePoint.ConnectionLimit = 65500;
        getTextRequst.AllowWriteStreamBuffering = false;
        getTextRequst.ContentType = "audio /" + "" + gs + "" + ";rate=" + "" + fbl + "";
        getTextRequst.ContentLength = fi.Length;
        getTextRequst.Method = "post";
        getTextRequst.Accept = "*/*";
        getTextRequst.KeepAlive = true;
        getTextRequst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
        getTextRequst.Timeout = 30000;//30秒连接不成功就中断 
        using (Stream writeStream = getTextRequst.GetRequestStream())
        {
            writeStream.Write(voice, 0, voice.Length);
        }
        HttpWebResponse getTextResponse = getTextRequst.GetResponse() as HttpWebResponse;
        using (StreamReader strHttpText = new StreamReader(getTextResponse.GetResponseStream(), Encoding.UTF8))
        {
            strJSON = strHttpText.ReadToEnd();
        }
        JObject jsons = JObject.Parse(strJSON);//解析JSON
        if (jsons["err_msg"].Value<string>() == "success.")
        {
            strText = jsons["result"][0].ToString();
            return strText;
        }
        else
        {

            error = jsons["err_no"].Value<string>() + jsons["err_msg"].Value<string>();
            return error;
        }
    }
    public string Get()
    {
        string access_html = null;
        string access_token = null;
        string bbb = "15208407";
        string aaa = "1537";
        //参数16000  8000可能会语音模糊
        string ccc = "16000";
        string ddd = "wav";
        string getAccessUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
       "&client_id=" + "TUNRWWLgvYGVTq2Hw1BGQ0Fe" + "&client_secret=" + "G9HNiYVPHsj6bblL6OQUg4LNxMXPMYnb";
        try
        {
            HttpWebRequest getAccessRequest = WebRequest.Create(getAccessUrl) as HttpWebRequest;
            //getAccessRequest.Proxy = null;
            getAccessRequest.ContentType = "multipart/form-data";
            // getAccessRequest.Accept = "*/*";
            getAccessRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
            getAccessRequest.Timeout = 100000;//100秒连接不成功就中断 
            getAccessRequest.Method = "post";

            HttpWebResponse response = getAccessRequest.GetResponse() as HttpWebResponse;
            using (StreamReader strHttpComback = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                access_html = strHttpComback.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        JObject jo = JObject.Parse(access_html);
        access_token = jo["access_token"].ToString();//得到返回的toke
        textBox1.Text = GetStrText(access_token, aaa, bbb, ccc, ddd);
        return access_token;
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }
    private void button3_Click(object sender, EventArgs e)
    {

    }
}

}
代码demo下载,可直接使用应用程序

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Speech Recognition This sample demonstrates the capabilities of the Speech API, which comes standard with Microsoft Windows XP Tablet PC Edition 2005. Although SAPI is less programmed for than other APIs, it's extremely rich and similar enough to the handwriting recognition APIs that it can be easily learned by those programming Microsoft Windows XP Tablet PC Edition 2005. You must create an Interop wrapper for the SAPI DLL. This can be done by using the "Add Reference..." capability, selecting the COM tab, and selecting the "Microsoft Speech Object Library." Once that's done, and the SpeechLib namespace is imported, things are quite similar to programming handwriting recognition using the Tablet PC Platform SDK. A recognizer is created (in this case, an in-process SpInProcRecognizer) and generates a context (type SpInProcRecoContext) that is associated with the particular recognition. Events associated with the recognition process are the Hypothesis, Recognition, and EndStream events. A large number of Hypothesis events will be generated while only one Recognition and EndStream events will be associated with a particular recognition attempt. The sample provides a standard file dialog to open a user-supplied .WAV file. Recognition occurs against this file, with the results placed in a RichTextEdit box. The technique for iterating over alternative possibilities is also shown, although that output is directed to the Debug console. This sample requires: Visual Studio 2005 Microsoft Windows XP Tablet PC Edition 2005
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值