|
|
using HuizhongLibrary.Network;
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO.Ports;
|
|
|
using System.Linq;
|
|
|
using System.Media;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Speech.Synthesis;
|
|
|
using System.Runtime.InteropServices;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using HuizhongLibrary.Log;
|
|
|
|
|
|
namespace HuizhongLibrary
|
|
|
{
|
|
|
public class VoicePortSound
|
|
|
{
|
|
|
SerialPortService ser = new SerialPortService();
|
|
|
SpeechSynthesizer synth = new SpeechSynthesizer();
|
|
|
|
|
|
public string PortName { get; set; }
|
|
|
public int BaudRate { get; set; }
|
|
|
public int DataBits { get; set; }
|
|
|
/// <summary>
|
|
|
/// 0不使用语音呼叫1使用微软语音呼叫2使用精灵SmartRead语音3使用外置串口语音呼叫
|
|
|
/// </summary>
|
|
|
public int SoundType { get; set; }
|
|
|
public int SendSleep = 500;
|
|
|
Queue<TtsVoice> ListVoice = new Queue<TtsVoice>();
|
|
|
|
|
|
public PlayState Status;
|
|
|
|
|
|
private AutoResetEvent AutoReset = new AutoResetEvent(false);
|
|
|
private bool IsRun = false;
|
|
|
public VoicePortSound()
|
|
|
{
|
|
|
this.BaudRate = 9600;
|
|
|
this.DataBits = 8;
|
|
|
ser.MaxSendNumber = 1;
|
|
|
ser.RefshSecond = 60;
|
|
|
ser.ReceiveData += ser_ReceiveData;
|
|
|
ser.OverMaxSend += new Action<SocketMessage>(ser_OverMaxSend);
|
|
|
//this.HardwareInit();
|
|
|
}
|
|
|
#region 启动监控
|
|
|
public void Start()
|
|
|
{
|
|
|
if (SoundType == 0) return;
|
|
|
if (SoundType == 1)
|
|
|
{
|
|
|
var list = synth.GetInstalledVoices();
|
|
|
if (list.Count > 0)
|
|
|
{
|
|
|
foreach (InstalledVoice item in list)
|
|
|
{
|
|
|
if (item.VoiceInfo.Name == "Microsoft Huihui Desktop") synth.SelectVoice("Microsoft Huihui Desktop");
|
|
|
if (item.VoiceInfo.Name == "Microsoft Huihui") synth.SelectVoice("Microsoft Huihui");
|
|
|
if (item.VoiceInfo.Name == "Microsoft Lili Desktop") synth.SelectVoice("Microsoft Lili Desktop");
|
|
|
if (item.VoiceInfo.Name == "Microsoft Lili") synth.SelectVoice("Microsoft Lili");
|
|
|
ErrorFollow.TraceWrite("语音库名称", "", item.VoiceInfo.Name);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
synth.SelectVoice("Microsoft Lili");
|
|
|
}
|
|
|
}
|
|
|
if (SoundType == 2)
|
|
|
{
|
|
|
//SmartReadSDK.SmartRead_InitialAuth(0, 0, 0, 0, "support@smartysoft.com", "88888888", "111-111-111-111");
|
|
|
}
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
ser.Start(PortName, this.BaudRate, this.DataBits, Parity.None, StopBits.One);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Thread t = new Thread(this.OnStart);
|
|
|
t.Start();
|
|
|
}
|
|
|
if (SoundType == 4) synth.SelectVoice("Microsoft Lili");
|
|
|
}
|
|
|
#endregion
|
|
|
#region 停止
|
|
|
public void Stop()
|
|
|
{
|
|
|
IsRun = false;
|
|
|
AutoReset.Set();
|
|
|
ser.Stop();
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 超出最大发送次数
|
|
|
void ser_OverMaxSend(SocketMessage obj)
|
|
|
{
|
|
|
ser.ListMessage.Clear();
|
|
|
}
|
|
|
#endregion
|
|
|
#region 读取数据
|
|
|
void ser_ReceiveData(SerialPort model)
|
|
|
{
|
|
|
switch (ser.Data[0])
|
|
|
{
|
|
|
case 0x4A:
|
|
|
//PlaySound._HardwareSuccessful = true;
|
|
|
break;
|
|
|
case 0x41:
|
|
|
Status = PlayState.RecvCorrectCommand;
|
|
|
//ser.EndFirstMsg();
|
|
|
break;
|
|
|
case 0x45:
|
|
|
Status = PlayState.RecvErrorCommand;
|
|
|
break;
|
|
|
case 0x4E:
|
|
|
Status = PlayState.Busy;
|
|
|
break;
|
|
|
case 0x4F:
|
|
|
Status = PlayState.Idle;
|
|
|
ser.EndFirstMsg();
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
#endregion
|
|
|
#region 停止正在播放的语音
|
|
|
public void HardwareStop()
|
|
|
{
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
byte[] b = new byte[4];
|
|
|
b[0] = 0xFD;
|
|
|
b[1] = 0x00;
|
|
|
b[2] = 0x01;
|
|
|
b[3] = 0x02;
|
|
|
ser.Send(b);
|
|
|
}
|
|
|
if (SoundType == 1 || SoundType == 4) synth.Pause();
|
|
|
//if (SoundType == 2) SmartReadSDK.SmartRead_Stop();
|
|
|
}
|
|
|
#endregion
|
|
|
#region 暂停语音
|
|
|
public void HardwarePause()
|
|
|
{
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
byte[] b = new byte[4];
|
|
|
b[0] = 0xFD;
|
|
|
b[1] = 0x00;
|
|
|
b[2] = 0x01;
|
|
|
b[3] = 0x03;
|
|
|
ser.Send(b);
|
|
|
}
|
|
|
if (SoundType == 1 || SoundType == 4) synth.Pause();
|
|
|
//if (SoundType == 2) SmartReadSDK.SmartRead_PauseORContinue();
|
|
|
}
|
|
|
#endregion
|
|
|
#region 恢复暂停的语音
|
|
|
public void HardwareResume()
|
|
|
{
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
byte[] b = new byte[4];
|
|
|
b[0] = 0xFD;
|
|
|
b[1] = 0x00;
|
|
|
b[2] = 0x01;
|
|
|
b[3] = 0x04;
|
|
|
ser.Send(b);
|
|
|
}
|
|
|
if (SoundType == 1 || SoundType == 4) synth.Resume();
|
|
|
//if (SoundType == 2) SmartReadSDK.SmartRead_PauseORContinue();
|
|
|
}
|
|
|
#endregion
|
|
|
#region 读取状态
|
|
|
public void HardwareStatus()
|
|
|
{
|
|
|
byte[] b = new byte[4];
|
|
|
b[0] = 0xFD;
|
|
|
b[1] = 0x00;
|
|
|
b[2] = 0x01;
|
|
|
b[3] = 0x04;
|
|
|
ser.Send(b);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 播放语音
|
|
|
public void HardwareSpeak(string text)
|
|
|
{
|
|
|
Regex reg = new Regex(@"\[@暂停=\d+\]", RegexOptions.Multiline);
|
|
|
MatchCollection matches = reg.Matches(text);
|
|
|
if (matches.Count == 0)
|
|
|
{
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
byte[] b = Encoding.GetEncoding("gb2312").GetBytes(text);
|
|
|
byte[] bytes = new byte[b.Length + 5];
|
|
|
Int16 len = (Int16)(b.Length + 2);
|
|
|
bytes[0] = 0xFD;
|
|
|
bytes[1] = (byte)(len >> 8 & 0xFF);
|
|
|
bytes[2] = (byte)(len & 0xFF);
|
|
|
bytes[3] = 0x01;
|
|
|
bytes[4] = 0x00;
|
|
|
Array.Copy(b, 0, bytes, 5, b.Length);
|
|
|
SocketMessage msg = new SocketMessage();
|
|
|
msg.Bytes = bytes;
|
|
|
msg.SendLength = msg.Bytes.Length;
|
|
|
msg.IsRevert = true;
|
|
|
msg.SequenceID = "4F";
|
|
|
msg.FunNo = "4F";
|
|
|
msg.IsLock = true;
|
|
|
ser.ListMessage.Add(msg);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
lock (ListVoice)
|
|
|
{
|
|
|
TtsVoice m = new TtsVoice();
|
|
|
m.VoiceText = text;
|
|
|
ListVoice.Enqueue(m);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string[] ss = reg.Split(text);
|
|
|
int index = 0;
|
|
|
int sleep = 0;
|
|
|
int len2 = ss.Length - 1;
|
|
|
foreach (string s in ss)
|
|
|
{
|
|
|
if (index < len2) sleep = Convert.ToInt32(matches[index].Value.Substring(5, matches[index].Value.Length - 6));
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
byte[] b = Encoding.GetEncoding("gb2312").GetBytes(s);
|
|
|
byte[] bytes = new byte[b.Length + 5];
|
|
|
Int16 len = (Int16)(b.Length + 2);
|
|
|
bytes[0] = 0xFD;
|
|
|
bytes[1] = (byte)(len >> 8 & 0xFF);
|
|
|
bytes[2] = (byte)(len & 0xFF);
|
|
|
bytes[3] = 0x01;
|
|
|
bytes[4] = 0x00;
|
|
|
Array.Copy(b, 0, bytes, 5, b.Length);
|
|
|
|
|
|
SocketMessage msg = new SocketMessage();
|
|
|
msg.Bytes = bytes;
|
|
|
msg.SendLength = msg.Bytes.Length;
|
|
|
msg.IsRevert = true;
|
|
|
msg.SequenceID = "4F";
|
|
|
msg.FunNo = "4F";
|
|
|
msg.IsLock = true;
|
|
|
ser.ListMessage.Add(msg);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
TtsVoice m = new TtsVoice();
|
|
|
m.VoiceText = s;
|
|
|
m.SendSleep = sleep;
|
|
|
ListVoice.Enqueue(m);
|
|
|
}
|
|
|
sleep = 0;
|
|
|
index++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 清除语音队列
|
|
|
public void Clear()
|
|
|
{
|
|
|
if (SoundType == 3)
|
|
|
{
|
|
|
ser.ListMessage.Clear();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
lock (ListVoice)
|
|
|
{
|
|
|
ListVoice.Clear();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
#region 读取语音
|
|
|
TtsVoice CheckOut()
|
|
|
{
|
|
|
lock (ListVoice)
|
|
|
{
|
|
|
if (ListVoice.Count == 0) return null;
|
|
|
return ListVoice.Dequeue();
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
#region 语音朗读
|
|
|
void PlayVoice(string text)
|
|
|
{
|
|
|
if (SoundType == 1 || SoundType == 4) synth.Speak(text);
|
|
|
if (SoundType == 2)
|
|
|
{
|
|
|
//SmartReadSDK.SmartRead_Speak(text, "", 1, 13, 50, 100, 1, -1, "http://www.smartysoft.com/cn/");
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
#region 播放线程
|
|
|
void OnStart()
|
|
|
{
|
|
|
IsRun = true;
|
|
|
while (IsRun == true)
|
|
|
{
|
|
|
TtsVoice model = CheckOut();
|
|
|
if (model != null) PlayVoice(model.VoiceText);
|
|
|
if (model == null || model.SendSleep == 0) { AutoReset.WaitOne(this.SendSleep, false); }
|
|
|
else { AutoReset.WaitOne(model.SendSleep, false); }
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
|
|
|
|
|
|
public enum PlayState
|
|
|
{
|
|
|
InitializationSuccessful,
|
|
|
RecvCorrectCommand,
|
|
|
RecvErrorCommand,
|
|
|
Busy,
|
|
|
Idle
|
|
|
}
|
|
|
|
|
|
public class TtsVoice
|
|
|
{
|
|
|
public string VoiceText = "";
|
|
|
public int SendSleep = 0;
|
|
|
}
|
|
|
|
|
|
#region SmartReadSDK
|
|
|
public class SmartReadSDK
|
|
|
{
|
|
|
////初始化认证函数,使用语音功能前必须首先调用,进行认证。
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_InitialAuth")]
|
|
|
//public static extern int SmartRead_InitialAuth(int hwndEditText, int hwndMessage, int hwndProgress, int hwndToShow, string chMailBox, string chPassword, string chValidateCode);
|
|
|
|
|
|
////关闭函数,推出程序前必须调用
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Close")]
|
|
|
//public static extern int SmartRead_Close();
|
|
|
|
|
|
////朗读函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Speak")]
|
|
|
//public static extern int SmartRead_Speak(string lpStr, string lpToShow, int iStyle, int iSpeech, int iRate, int iVolume, int iPunctuation, int iSelVoiceDevice, string lpLink);
|
|
|
|
|
|
////打开语音设置对话框,用于改变中英文语音,音量,语速,可自定义下载连接
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SetDialog")]
|
|
|
//public static extern int SmartRead_SetDialog(int hwndFather, string lpDownPage);
|
|
|
|
|
|
////停止朗读函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Stop")]
|
|
|
//public static extern int SmartRead_Stop();
|
|
|
|
|
|
////将文本朗读到WAVE文件的函数,可设置输出的文件
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SpeakToWave")]
|
|
|
//public static extern int SmartRead_SpeakToWave(string lpStr, string lpToShow, string lpWaveFile, int iStyle, int iSpeech, int iRate, int iVolume, int iFormat, int iPunctuation, string lpLink);
|
|
|
|
|
|
////获得朗读的位置
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetLocationInfo")]
|
|
|
//public static extern int SmartRead_GetLocationInfo(ref int iLocationInfo);
|
|
|
|
|
|
////在朗读过程中,改变音量,范围在0-100之间,100为最高音.
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SetVolume")]
|
|
|
//public static extern int SmartRead_SetVolume(int iVolume);
|
|
|
|
|
|
////在朗读过程中,改变语速,范围在0-100直接,100为最快速度.
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SetSpeed")]
|
|
|
//public static extern int SmartRead_SetSpeed(int iRate);
|
|
|
|
|
|
////在朗读过程中,获得音量,范围在0-100之间,100为最高音.
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetVolume")]
|
|
|
//public static extern int SmartRead_GetVolume(ref int iVolume);
|
|
|
|
|
|
////在朗读过程中,获得语速,范围在0-100直接,100为最快速度.
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetSpeed")]
|
|
|
//public static extern int SmartRead_GetSpeed(ref int iRate);
|
|
|
|
|
|
////在朗读过程中,实现暂停/继续功能.
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_PauseORContinue")]
|
|
|
//public static extern int SmartRead_PauseORContinue();
|
|
|
|
|
|
////返回值为本台电脑上声卡的数目
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetVoiceDeviceNum")]
|
|
|
//public static extern int SmartRead_GetVoiceDeviceNum(ref int iDeviceNum);
|
|
|
|
|
|
////语音开发包版本信息,开在任何时候调用
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Version")]
|
|
|
//public static extern int SmartRead_Version(ref int iMaxVer, ref int iMinVer, ref int iYear, ref int iMonth, ref int iDay);
|
|
|
|
|
|
////朗读模式设置函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SetMode")]
|
|
|
//public static extern int SmartRead_SetMode(int iMode);
|
|
|
|
|
|
////返回错误代码函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetErrCode")]
|
|
|
//public static extern int SmartRead_GetErrCode();
|
|
|
|
|
|
////返回错误信息函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_GetErrMsg")]
|
|
|
//public static extern int SmartRead_GetErrMsg(StringBuilder szErrMsg);
|
|
|
|
|
|
////队列播放,获得已经朗读/总朗读文本信息数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_QueueStatistics")]
|
|
|
//public static extern int SmartRead_QueueStatistics(ref int iReaded, ref int iTotal);
|
|
|
|
|
|
////队列播放,朗读下一条函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_QueueNext")]
|
|
|
//public static extern int SmartRead_QueueNext();
|
|
|
|
|
|
////隐藏消息对话框
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_HideMessageBox")]
|
|
|
//public static extern int SmartRead_HideMessageBox();
|
|
|
|
|
|
////参数设置函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_SetParameter")]
|
|
|
//public static extern int SmartRead_SetParameter(int iParaName, int iParaNum);
|
|
|
|
|
|
|
|
|
////打开跟踪功能函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Trace_ON")]
|
|
|
//public static extern int SmartRead_Trace_ON();
|
|
|
|
|
|
|
|
|
////关闭跟踪功能函数
|
|
|
//[DllImport("smartread7.dll", EntryPoint = "SmartRead_Trace_OFF")]
|
|
|
//public static extern int SmartRead_Trace_OFF();
|
|
|
|
|
|
|
|
|
//public static int SmartRead_iMultiReadTimes = 0;//重复或者多次朗读的次数
|
|
|
//public static int SmartRead_iMultiRead = 0; //是否是重复或者多次朗读
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
}
|