using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Media;
using System.Runtime.InteropServices;
namespace audio
{
class main_audio
{
private const string my_appid="appid = 5e09465f, work_dir = .";
public const string session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding =gb2312";
static void Main(string[] args) {
if (init_audio()) {
audio_iat("audio_source/cn_word.wav", session_begin_params);
}
Console.ReadLine();
}
private static bool init_audio() {
int res = mscDLL.MSPLogin(null, null, my_appid);
if (res != (int)Errors.MSP_SUCCESS) {
Console.WriteLine("登陸失敗!");
return false;
}
Console.WriteLine("登陸成功!");
return true;
}
private static bool audio_iat(string audio_path,string session_begin_params) {
if (audio_path == null || audio_path == "") return false;
IntPtr session_id;
StringBuilder result = new StringBuilder();
var aud_stat=AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
var ep_stat =EpStatus.MSP_EP_LOOKING_FOR_SPEECH;
var rec_stat=RecogStatus.MSP_REC_STATUS_SUCCESS;
int errcode =(int) Errors.MSP_SUCCESS;
byte[] audio_content;
int totalLength = 0;
try
{
audio_content = File.ReadAllBytes(audio_path);
SoundPlayer player = new SoundPlayer(audio_path);
player.Play();
}
catch(Exception e) {
Console.WriteLine(e);
audio_content = null;
}
if (audio_content == null) {
Console.WriteLine("沒有讀取到任何內容");
return false;
}
Console.WriteLine("開始進行語音聽寫.......");
session_id = mscDLL.QISRSessionBegin(null,session_begin_params,ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS) {
Console.WriteLine("開始一次語音識別失敗!");
return false;
}
int res = mscDLL.QISRAudioWrite(session_id,audio_content,(uint)audio_content.Length,aud_stat,ref ep_stat,ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS) {
Console.WriteLine("寫入識別的音頻失敗!"+res);
return false;
}
res = mscDLL.QISRAudioWrite(session_id,null,0,AudioStatus.MSP_AUDIO_SAMPLE_LAST,ref ep_stat,ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS) {
Console.WriteLine("寫入音頻失敗!"+res);
return false;
}
while (RecogStatus.MSP_REC_STATUS_COMPLETE != rec_stat) {
IntPtr now_result= mscDLL.QISRGetResult(session_id,ref rec_stat,0,ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS) {
Console.WriteLine("獲取結果失敗:"+errcode);
return false;
}
if (now_result != null) {
int length = now_result.ToString().Length;
totalLength += length;
if (totalLength > 4096) {
Console.WriteLine("快取空間不夠" + totalLength);
return false;
}
result.Append(Marshal.PtrToStringAnsi(now_result));
}
Thread.Sleep(150);//防止頻繁占用cpu
}
Console.WriteLine("語音聽寫結束");
Console.WriteLine("結果:\n");
Console.WriteLine(result);
return true;
}
}
}
uj5u.com熱心網友回復:
這是語音識別系統部分代碼 求大神幫我轉成表單應用程式uj5u.com熱心網友回復:
你只需要把方法提取出來,然后在表單程式中呼叫,控制臺的輸入,可以做成表單程式的輸入框textbox之類的,表現形式不一樣只能提取uj5u.com熱心網友回復:
沒啥需要轉換的:1.新建一個winform程式,拖入一個按鈕
2.把你原先的main函式改名為Test
3.在按鈕回應中呼叫Test即可
uj5u.com熱心網友回復:
樓上正解啊
uj5u.com熱心網友回復:
如果輸出內容也是放在textbox里面呢 具體怎么實作
uj5u.com熱心網友回復:
能不能幫幫忙 弄了一天了 弄不好
uj5u.com熱心網友回復:
你這是完全不會表單哦
uj5u.com熱心網友回復:
純新手啊 幫幫忙啊 大佬
uj5u.com熱心網友回復:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsFormsApp10
{
public partial class Form1 : Form
{
private const string my_appid = "appid = 5e09465f, work_dir = .";
public const string session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding =gb2312";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
if (init_audio())
{
audio_iat("audio_source/cn_word.wav", session_begin_params);
}
});
}
private bool init_audio()
{
int res = mscDLL.MSPLogin(null, null, my_appid);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("登陸失敗!");
return false;
}
appendLine("登陸成功!");
return true;
}
void appendLine(string text)
{
this.Invoke((MethodInvoker)delegate
{
this.richTextBox1.AppendText(text + "\n");
});
}
private bool audio_iat(string audio_path, string session_begin_params)
{
if (audio_path == null || audio_path == "") return false;
IntPtr session_id;
StringBuilder result = new StringBuilder();
var aud_stat = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
var ep_stat = EpStatus.MSP_EP_LOOKING_FOR_SPEECH;
var rec_stat = RecogStatus.MSP_REC_STATUS_SUCCESS;
int errcode = (int)Errors.MSP_SUCCESS;
byte[] audio_content;
int totalLength = 0;
try
{
audio_content = File.ReadAllBytes(audio_path);
SoundPlayer player = new SoundPlayer(audio_path);
player.Play();
}
catch (Exception e)
{
appendLine(e.Message);
audio_content = null;
}
if (audio_content == null)
{
appendLine("沒有讀取到任何內容");
return false;
}
appendLine("開始進行語音聽寫.......");
session_id = mscDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
{
appendLine("開始一次語音識別失敗!");
return false;
}
int res = mscDLL.QISRAudioWrite(session_id, audio_content, (uint)audio_content.Length, aud_stat, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("寫入識別的音頻失敗!" + res);
return false;
}
res = mscDLL.QISRAudioWrite(session_id, null, 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("寫入音頻失敗!" + res);
return false;
}
while (RecogStatus.MSP_REC_STATUS_COMPLETE != rec_stat)
{
IntPtr now_result = mscDLL.QISRGetResult(session_id, ref rec_stat, 0, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
{
appendLine("獲取結果失敗:" + errcode);
return false;
}
if (now_result != null)
{
int length = now_result.ToString().Length;
totalLength += length;
if (totalLength > 4096)
{
appendLine("快取空間不夠" + totalLength);
return false;
}
result.Append(Marshal.PtrToStringAnsi(now_result));
}
Thread.Sleep(150);//防止頻繁占用cpu
}
appendLine("語音聽寫結束");
appendLine("結果:\n");
appendLine(result.ToString());
return true;
}
}
}
uj5u.com熱心網友回復:
看到樓上給你回復了,如果還是不會,聯系我晚上回去幫你弄
uj5u.com熱心網友回復:
上樓正確,其實樓主可以先建一個工程,代碼copy,然后把報錯的注釋掉,然后在控制元件上顯示執行結果就好了。uj5u.com熱心網友回復:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsFormsApp10
{
public partial class Form1 : Form
{
private const string my_appid = "appid = 5e09465f, work_dir = .";
public const string session_begin_params = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding =gb2312";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
if (init_audio())
{
audio_iat("audio_source/cn_word.wav", session_begin_params);
}
});
}
private bool init_audio()
{
int res = mscDLL.MSPLogin(null, null, my_appid);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("登陸失敗!");
return false;
}
appendLine("登陸成功!");
return true;
}
void appendLine(string text)
{
this.Invoke((MethodInvoker)delegate
{
this.richTextBox1.AppendText(text + "\n");
});
}
private bool audio_iat(string audio_path, string session_begin_params)
{
if (audio_path == null || audio_path == "") return false;
IntPtr session_id;
StringBuilder result = new StringBuilder();
var aud_stat = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
var ep_stat = EpStatus.MSP_EP_LOOKING_FOR_SPEECH;
var rec_stat = RecogStatus.MSP_REC_STATUS_SUCCESS;
int errcode = (int)Errors.MSP_SUCCESS;
byte[] audio_content;
int totalLength = 0;
try
{
audio_content = File.ReadAllBytes(audio_path);
SoundPlayer player = new SoundPlayer(audio_path);
player.Play();
}
catch (Exception e)
{
appendLine(e.Message);
audio_content = null;
}
if (audio_content == null)
{
appendLine("沒有讀取到任何內容");
return false;
}
appendLine("開始進行語音聽寫.......");
session_id = mscDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
{
appendLine("開始一次語音識別失敗!");
return false;
}
int res = mscDLL.QISRAudioWrite(session_id, audio_content, (uint)audio_content.Length, aud_stat, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("寫入識別的音頻失敗!" + res);
return false;
}
res = mscDLL.QISRAudioWrite(session_id, null, 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
{
appendLine("寫入音頻失敗!" + res);
return false;
}
while (RecogStatus.MSP_REC_STATUS_COMPLETE != rec_stat)
{
IntPtr now_result = mscDLL.QISRGetResult(session_id, ref rec_stat, 0, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
{
appendLine("獲取結果失敗:" + errcode);
return false;
}
if (now_result != null)
{
int length = now_result.ToString().Length;
totalLength += length;
if (totalLength > 4096)
{
appendLine("快取空間不夠" + totalLength);
return false;
}
result.Append(Marshal.PtrToStringAnsi(now_result));
}
Thread.Sleep(150);//防止頻繁占用cpu
}
appendLine("語音聽寫結束");
appendLine("結果:\n");
appendLine(result.ToString());
return true;
}
}
}
void appendLine(string text)
{
this.Invoke((MethodInvoker)delegate
{
this.richTextBox1.AppendText(text + "\n");
});
}
這一段有點問題 VS說object未包含AppendText定義
uj5u.com熱心網友回復:
richTextBox1是個控制元件
uj5u.com熱心網友回復:
richTextBox1是個控制元件
大佬你能把你當時給我改的時候 設計源檔案也給我嗎 我知道richtextbox 我拖進去我不知道怎么把他們連接起來
uj5u.com熱心網友回復:
namespace WindowsFormsApp10
{
partial class Form1
{
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 表單設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要修改
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(122, 66);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(557, 318);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/81419.html
標籤:C#
上一篇:library支持.net standard 那么library支持.net framework嗎
下一篇:c#后臺程式實作Echarts圖
