diff --git a/ASRT_SpeechClient_WPF/ASRT_SpeechClient_WPF.csproj b/ASRT_SpeechClient_WPF/ASRT_SpeechClient_WPF.csproj
index e347915..2d8f1a3 100644
--- a/ASRT_SpeechClient_WPF/ASRT_SpeechClient_WPF.csproj
+++ b/ASRT_SpeechClient_WPF/ASRT_SpeechClient_WPF.csproj
@@ -8,11 +8,12 @@
WinExe
ASRT_SpeechClient_WPF
ASRT_SpeechClient_WPF
- v4.6.1
+ v4.7.2
512
{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
4
true
+
AnyCPU
@@ -34,9 +35,6 @@
4
-
- ..\ailemon.asrt\bin\Release\ailemon.asrt.dll
-
@@ -96,5 +94,14 @@
+
+
+
+
+
+ {7aedf08a-4fe9-45d8-b547-4a6e5db0671d}
+ ailemon.asrt
+
+
\ No newline at end of file
diff --git a/ASRT_SpeechClient_WPF/App.config b/ASRT_SpeechClient_WPF/App.config
index 731f6de..ecdcf8a 100644
--- a/ASRT_SpeechClient_WPF/App.config
+++ b/ASRT_SpeechClient_WPF/App.config
@@ -1,6 +1,6 @@
-
+
-
+
-
\ No newline at end of file
+
diff --git a/ASRT_SpeechClient_WPF/App.xaml.cs b/ASRT_SpeechClient_WPF/App.xaml.cs
index daaecee..b9335e7 100644
--- a/ASRT_SpeechClient_WPF/App.xaml.cs
+++ b/ASRT_SpeechClient_WPF/App.xaml.cs
@@ -13,5 +13,16 @@ namespace ASRT_SpeechClient_WPF
///
public partial class App : Application
{
+ /*
+ protected override void OnStartup(StartupEventArgs e)
+ {
+
+ SplashScreen s = new SplashScreen("SplashScreen.png");
+ s.Show(false);
+ s.Close(new TimeSpan(0, 0, 3));
+
+ base.OnStartup(e);
+ }
+ */
}
}
diff --git a/ASRT_SpeechClient_WPF/MainWindow.xaml b/ASRT_SpeechClient_WPF/MainWindow.xaml
index 663f88b..d6fa6f6 100644
--- a/ASRT_SpeechClient_WPF/MainWindow.xaml
+++ b/ASRT_SpeechClient_WPF/MainWindow.xaml
@@ -5,15 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ASRT_SpeechClient_WPF"
mc:Ignorable="d"
- Title="ASRT_SpeechClient_WPF" Height="700" Width="1000" MinWidth="900" MinHeight="600" Margin="0" WindowStartupLocation="CenterScreen">
+ Title="ASRT语音识别系统客户端WPF版" Height="700" Width="1000" MinWidth="900" MinHeight="600" Margin="0" WindowStartupLocation="CenterScreen">
-
+
-
+
+
+
diff --git a/ASRT_SpeechClient_WPF/MainWindow.xaml.cs b/ASRT_SpeechClient_WPF/MainWindow.xaml.cs
index bcf6a0a..0b316e5 100644
--- a/ASRT_SpeechClient_WPF/MainWindow.xaml.cs
+++ b/ASRT_SpeechClient_WPF/MainWindow.xaml.cs
@@ -12,7 +12,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
-using ailemon.asrt;
+using Ailemon.Asrt;
namespace ASRT_SpeechClient_WPF
{
@@ -21,64 +21,98 @@ namespace ASRT_SpeechClient_WPF
///
public partial class MainWindow : Window
{
- SpeechRecognizer asr;
+ AsrtClientProxy _clientProxy;
- string token = "qwertasd";
string filename_conf = "conf.txt";
+ string host = "127.0.0.1";
+ string port = "20001";
+ string protocol = "http";
+
+ string textBuffer = "";
public MainWindow()
{
InitializeComponent();
MessageBox.Visibility = Visibility.Collapsed;
+ System.Threading.Thread.Sleep(1000); //延迟启动1秒,以便人们看清闪屏上的字
-
- string url = "http://127.0.0.1:20000/";
-
if (System.IO.File.Exists(filename_conf))
{
- //url = System.IO.File.ReadAllText(filename_url);
string[] conf_lines = System.IO.File.ReadAllLines(filename_conf);
- url = conf_lines[0];
- token = conf_lines[1];
- textbox_url.Text = url;
+ protocol = conf_lines[0];
+ host = conf_lines[1];
+ port = conf_lines[2];
+ textbox_url.Text = protocol + "/" + host + "/" + port;
}
- asr = new SpeechRecognizer(url, token);
- asr.OnReceiveText += SpeechRecognizer_OnReceiveText;
+ _clientProxy = new AsrtClientProxy(host, port, protocol);
+ _clientProxy.SetRecorderDevice(0);
+ _clientProxy.OnReceiveText += SpeechRecognizer_OnReceiveText;
+ Console.WriteLine("MainWindow运行");
}
private void btn_start_speech_input_Click(object sender, RoutedEventArgs e)
{
- asr.Start();
+ //asr.Start();
+ _clientProxy.Start();
MessageBox.Visibility = Visibility.Visible;
}
private void btn_end_speech_input_Click(object sender, RoutedEventArgs e)
{
- asr.StopAsync();
+ _clientProxy.StopAsync();
MessageBox.Visibility = Visibility.Collapsed;
}
- private async void btn_change_url_Click(object sender, RoutedEventArgs e)
+ private void btn_change_url_Click(object sender, RoutedEventArgs e)
{
- //text_note.Text += await asr.RecogniteFromFile("1.wav");
- //text_note.ScrollToEnd();
- if (!asr.isRecognizing)
+ if (!_clientProxy.IsRecognizing)
{
string url_new = textbox_url.Text;
- asr = new SpeechRecognizer(url_new, token);
- asr.OnReceiveText += SpeechRecognizer_OnReceiveText;
+ string[] config_arr = url_new.Split('/');
+ protocol = config_arr[0];
+ host = config_arr[1];
+ port = config_arr[2];
+ _clientProxy = new AsrtClientProxy(host, port, protocol);
+ _clientProxy.OnReceiveText += SpeechRecognizer_OnReceiveText;
- System.IO.File.WriteAllText(filename_conf, url_new + "\n" + token);
-
+ System.IO.File.WriteAllText(filename_conf, protocol + "\n" + host + "\n" + port);
}
}
- private void SpeechRecognizer_OnReceiveText(object sender, string text)
+ private void SpeechRecognizer_OnReceiveText(object sender, AsrtResult result)
{
//事件处理方法
- text_note.Text += text;
+ if (result.Confirm)
+ {
+ textBuffer += result.Text;
+ text_note.Text = textBuffer;
+ }
+ else
+ {
+ text_note.Text = textBuffer + result.Text;
+ }
+
text_note.ScrollToEnd();
+ Console.WriteLine("recv: {0}, {1}", result.Confirm.ToString(), result.Text);
+ }
+
+ private async void btn_recognite_file_Click(object sender, RoutedEventArgs e)
+ {
+ string filename = "";
+ Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
+ openFileDialog.Filter = "WAVE音频文件|*.wav";
+ openFileDialog.DefaultExt = "WAVE音频文件|*.wav";
+ if (openFileDialog.ShowDialog() == true)
+ {
+ filename = openFileDialog.FileName;
+
+ Ailemon.Asrt.BaseSpeechRecognizer sr = Ailemon.Asrt.SDK.GetSpeechRecognizer(host, port, protocol);
+ Ailemon.Asrt.AsrtApiResponse rsp = (Ailemon.Asrt.AsrtApiResponse)await sr.RecogniteFile(filename);
+ System.Console.WriteLine((string)rsp.Result);
+ AsrtResult result = new AsrtResult("\n" + (string)rsp.Result + "\n", true, rsp.StatusCode, rsp.StatusMessage);
+ SpeechRecognizer_OnReceiveText(sender, result);
+ }
}
}
}
diff --git a/ASRT_SpeechClient_WPF/Properties/Resources.Designer.cs b/ASRT_SpeechClient_WPF/Properties/Resources.Designer.cs
index b251a66..ba42bf5 100644
--- a/ASRT_SpeechClient_WPF/Properties/Resources.Designer.cs
+++ b/ASRT_SpeechClient_WPF/Properties/Resources.Designer.cs
@@ -1,69 +1,61 @@
//------------------------------------------------------------------------------
//
// 此代码由工具生成。
-// 运行时版本: 4.0.30319.42000
+// 运行时版本:4.0.30319.42000
//
-// 对此文件的更改可能导致不正确的行为,如果
-// 重新生成代码,则所做更改将丢失。
+// 对此文件的更改可能会导致不正确的行为,并且如果
+// 重新生成代码,这些更改将会丢失。
//
//------------------------------------------------------------------------------
-namespace ASRT_SpeechClient_WPF.Properties
-{
-
-
+namespace ASRT_SpeechClient_WPF.Properties {
+ using System;
+
+
///
- /// 强类型资源类,用于查找本地化字符串等。
+ /// 一个强类型的资源类,用于查找本地化的字符串等。
///
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
- // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
+ // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
- internal class Resources
- {
-
+ internal class Resources {
+
private static global::System.Resources.ResourceManager resourceMan;
-
+
private static global::System.Globalization.CultureInfo resourceCulture;
-
+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
- internal Resources()
- {
+ internal Resources() {
}
-
+
///
- /// 返回此类使用的缓存 ResourceManager 实例。
+ /// 返回此类使用的缓存的 ResourceManager 实例。
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Resources.ResourceManager ResourceManager
- {
- get
- {
- if ((resourceMan == null))
- {
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ASRT_SpeechClient_WPF.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
-
+
///
- /// 覆盖当前线程的 CurrentUICulture 属性
- /// 使用此强类型的资源类的资源查找。
+ /// 重写当前线程的 CurrentUICulture 属性,对
+ /// 使用此强类型资源类的所有资源查找执行重写。
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Globalization.CultureInfo Culture
- {
- get
- {
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
return resourceCulture;
}
- set
- {
+ set {
resourceCulture = value;
}
}
diff --git a/ASRT_SpeechClient_WPF/Properties/Settings.Designer.cs b/ASRT_SpeechClient_WPF/Properties/Settings.Designer.cs
index cc974f3..2f3e86e 100644
--- a/ASRT_SpeechClient_WPF/Properties/Settings.Designer.cs
+++ b/ASRT_SpeechClient_WPF/Properties/Settings.Designer.cs
@@ -1,28 +1,24 @@
//------------------------------------------------------------------------------
//
-// This code was generated by a tool.
-// Runtime Version:4.0.30319.42000
+// 此代码由工具生成。
+// 运行时版本:4.0.30319.42000
//
-// Changes to this file may cause incorrect behavior and will be lost if
-// the code is regenerated.
+// 对此文件的更改可能会导致不正确的行为,并且如果
+// 重新生成代码,这些更改将会丢失。
//
//------------------------------------------------------------------------------
-namespace ASRT_SpeechClient_WPF.Properties
-{
-
-
+namespace ASRT_SpeechClient_WPF.Properties {
+
+
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
- internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
- {
-
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")]
+ internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
+
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
-
- public static Settings Default
- {
- get
- {
+
+ public static Settings Default {
+ get {
return defaultInstance;
}
}
diff --git a/ASRT_SpeechClient_WPF/SplashScreen.png b/ASRT_SpeechClient_WPF/SplashScreen.png
new file mode 100644
index 0000000..3d702cf
Binary files /dev/null and b/ASRT_SpeechClient_WPF/SplashScreen.png differ
diff --git a/ailemon.asrt/AsrtClientProxy.cs b/ailemon.asrt/AsrtClientProxy.cs
new file mode 100644
index 0000000..6c3a9ed
--- /dev/null
+++ b/ailemon.asrt/AsrtClientProxy.cs
@@ -0,0 +1,127 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Threading;
+using System.IO;
+
+namespace Ailemon.Asrt
+{
+ ///
+ /// ASRT语音识别调用调用SDK客户端代理
+ ///
+ public class AsrtClientProxy
+ {
+ ///
+ /// 定义委托
+ ///
+ ///
+ ///
+ public delegate void ReceiveText(object sender, AsrtResult result);
+ ///
+ /// 当收到语音识别后的文本的时候触发事件
+ ///
+ public event ReceiveText OnReceiveText;
+
+ private string _host = "";
+ private string _port = "";
+ private string _protocol = "";
+
+ DispatcherTimer timer;//定义定时器
+ NAudioRecorder _audioRecorder;
+ ///
+ /// 返回是否正在录音识别
+ ///
+ public bool IsRecognizing
+ {
+ get { return _isRecognizing; }
+ }
+ private bool _isRecognizing = false;
+ private BaseSpeechRecognizer _speechRecognizer = null;
+
+ public AsrtClientProxy(string host, string port, string protocol)
+ {
+ this._host = host;
+ this._port = port;
+ this._protocol = protocol;
+ _speechRecognizer = SDK.GetSpeechRecognizer(host, port, protocol);
+
+ if (NAudioRecorder.DeviceCount < 1)
+ {
+ throw new Exception("There is no record device found");
+ }
+ }
+
+ ///
+ /// 指定录音设备ID,默认为0
+ ///
+ ///
+ public void SetRecorderDevice(int deviceNumber=0)
+ {
+ _audioRecorder = new NAudioRecorder(deviceNumber, 16000, 1, DataAvailableHandler);
+ }
+
+ private void DataAvailableHandler(float value)
+ {
+ //Console.WriteLine("data available handler value: {0}", value);
+ }
+
+ ///
+ /// 启动并开始客户端调用ASRT语音识别
+ ///
+ public void Start()
+ {
+ if(_audioRecorder != null)
+ {
+ timer = new DispatcherTimer();
+ timer.Interval = new TimeSpan(0, 0, 6);
+ timer.Tick += Timer_Tick;//每6秒触发这个事件,以刷新指针
+ timer.Start();
+
+ _isRecognizing = true;
+ //启动录音
+ _audioRecorder.Start();
+ Console.WriteLine("Ailemon.Asrt.AsrtClientProxy: 已启动录音识别");
+ }
+ }
+
+ private async void Timer_Tick(object sender, object e)
+ {
+ Console.WriteLine("Ailemon.Asrt.AsrtClientProxy: 录音周期流式传输");
+ // 定时从缓存中读取wave数据,并送去语音识别
+ Stream waveMemStream = _audioRecorder.PopMemoryStream();
+ WaveData wav = SDK.ReadWaveDatas(waveMemStream);
+ AsrtApiResponse rsp = (AsrtApiResponse)await _speechRecognizer.RecogniteAsync(wav.byteWavs, wav.sampleRate, wav.channels, wav.byteWidth);
+ AsrtResult result = new AsrtResult((string)rsp.Result, true, rsp.StatusCode, rsp.StatusMessage);
+ OnReceiveText(this, result);
+ }
+
+ ///
+ /// 异步停止客户端调用ASRT语音识别
+ ///
+ public async void StopAsync()
+ {
+ if (_isRecognizing)
+ {
+ timer.Stop();
+ Console.WriteLine("Ailemon.Asrt.AsrtClientProxy: 停止录音识别");
+
+ try
+ {
+ // 从缓存中读取wave数据,并送去语音识别
+ Stream waveMemStream = _audioRecorder.Stop();
+ WaveData wav = SDK.ReadWaveDatas(waveMemStream);
+ AsrtApiResponse rsp = (AsrtApiResponse)await _speechRecognizer.RecogniteAsync(wav.byteWavs, wav.sampleRate, wav.channels, wav.byteWidth);
+ AsrtResult result = new AsrtResult((string)rsp.Result, true, rsp.StatusCode, rsp.StatusMessage);
+ OnReceiveText(this, result); //产生事件
+ }
+ catch (Exception ex)
+ {
+ string str = ex.Message;
+ }
+ _isRecognizing = false;
+ }
+ }
+ }
+}
diff --git a/ailemon.asrt/AudioRecorder.cs b/ailemon.asrt/AudioRecorder.cs
index 90daaed..87e6a5a 100644
--- a/ailemon.asrt/AudioRecorder.cs
+++ b/ailemon.asrt/AudioRecorder.cs
@@ -5,7 +5,7 @@
using System.Threading.Tasks;
using System.Runtime.InteropServices;
-namespace ailemon
+namespace ailemon.Compat
{
public class WavRecorder
{
diff --git a/ailemon.asrt/BaseSpeechRecognizer.cs b/ailemon.asrt/BaseSpeechRecognizer.cs
new file mode 100644
index 0000000..28143cc
--- /dev/null
+++ b/ailemon.asrt/BaseSpeechRecognizer.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.IO;
+
+namespace Ailemon.Asrt
+{
+ ///
+ /// ASRT语音识别功能C#接口
+ ///
+ public interface ISpeechRecognizer
+ {
+ Task