You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace HuizhongLibrary
{
public class SoundVoice
{
private AutoResetEvent AutoReset = new AutoResetEvent(false);
//public event Action<FormModel> PlayFront;
Queue<Voice> ListVoice = new Queue<Voice>();
private bool IsRun = false;
public SoundVoice()
{
}
#region 播放Wav
public void PlayWav(string FileName, bool IsSync)
{
if (IsSync == false)
{
SystemInfo.W32_PlaySound(FileName, IntPtr.Zero, SystemInfo.SoundFlag.SND_SYNC + SystemInfo.SoundFlag.SND_FILENAME);
}
else
{
SystemInfo.W32_PlaySound(FileName, IntPtr.Zero, SystemInfo.SoundFlag.SND_ASYNC + SystemInfo.SoundFlag.SND_FILENAME);
}
}
#endregion
#region 播放Wav流
public void PlayWav(byte[] source, bool IsSync)
{
if (IsSync == false)
{
SystemInfo.W32_PlaySound(source, IntPtr.Zero, SystemInfo.SoundFlag.SND_SYNC + SystemInfo.SoundFlag.SND_MEMORY);
}
else
{
SystemInfo.W32_PlaySound(source, IntPtr.Zero, SystemInfo.SoundFlag.SND_ASYNC + SystemInfo.SoundFlag.SND_MEMORY);
}
}
#endregion
#region 播放文件合并
public byte[] WavUnion(List<string> files)
{
try
{
MemoryStream msm = new MemoryStream();
byte[] fsbyte = null;
for (int i = 0; i < files.Count; i++)
{
string filePath = files[i] + ".wav";
if (File.Exists(filePath))
{
FileInfo fi = new FileInfo(filePath);
FileStream fs = fi.OpenRead();
if (i == 0)
{
fsbyte = new byte[fs.Length];
}
else
{
fsbyte = new byte[fs.Length - 44];
fs.Seek(44, SeekOrigin.Begin);
}
fs.Read(fsbyte, 0, fsbyte.Length);
msm.Write(fsbyte, 0, fsbyte.Length);
fs.Close();
}
}
if (files.Count > 1)
{
msm.Seek(4, SeekOrigin.Begin);
msm.Write(BitConverter.GetBytes(msm.Length - 8), 0, 4);
msm.Seek(40, SeekOrigin.Begin);
msm.Write(BitConverter.GetBytes(msm.Length - 44), 0, 4);
}
byte[] soundbyte = new byte[msm.Length];
msm.Seek(0, SeekOrigin.Begin);
msm.Read(soundbyte, 0, soundbyte.Length);
msm.Close();
return soundbyte;
}
catch
{
return null;
}
}
#endregion
#region 播放文件合并
public byte[] WavUnion(string fileName)
{
List<string> list = new List<string>();
list.Add(fileName);
return WavUnion(list);
}
#endregion
#region 停止播放WAV
public void StopWav()
{
ClearVoice();
SystemInfo.W32_PlaySound((string)null, IntPtr.Zero, SystemInfo.SoundFlag.SND_FILENAME);
}
#endregion
#region 播放文本语音
/// <summary>
/// 播放文本语音
/// </summary>
/// <param name="ptext">文本</param>
/// <param name="val">朗读语速</param>
/// <param name="vol">朗读音量</param>
public void PlayVoice(string ptext, Int32 val, Int32 vol, bool IsSync)
{
//try
//{
// if (IsSync == false) { SystemInfo.Tts_Play(ptext, val, vol, 0xAF18); }
// else { SystemInfo.Tts_Play_A(ptext, val, vol, 0xAF18); }
//}
//catch { }
}
#endregion
#region 停止语音播报
public void StopPlayVoice()
{
try
{
ClearVoice();
//SystemInfo.Tts_Stop();
}
catch { }
}
#endregion
#region 转换金额
public string MoneyToCHN(double dbFee)
{
if (dbFee == 2) return "两元";
int iLoop = 0, iCur = 0, iCount = 0;
string strTemp = "", strMoney = "", strCur = "", strZero = "";
string[] strDigital = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string[] strUnit = new string[] { "分", "角", "元", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万" };
strTemp = Convert.ToString(System.Math.Round(System.Math.Abs(dbFee) * 100, 0));
if (strTemp.Equals("") || strTemp.Equals("0"))
return "0元";
iCount = strTemp.Length;
if (iCount > strUnit.GetUpperBound(0) + 1)
return "";
for (iLoop = 0; iLoop < iCount; iLoop++)
{
iCur = Convert.ToInt32(strTemp.Substring(iLoop, 1)) + 1;
strCur = strUnit[iCount - iLoop - 1];
if (iCur == 1)
{
if (strCur.Equals("元") || strCur.Equals("亿"))
{
strMoney = strMoney + strCur;
strZero = "";
}
else if (strCur.Equals("万"))
{
if (strMoney.Length < 2 || strMoney.Substring(strMoney.Length - 2, 2) != "亿")
{
strMoney = strMoney + strCur;
strZero = "";
}
}
else
{
strZero = "0";
}
}
else
{
strMoney = strMoney + strZero + strDigital[iCur - 1] + strCur;
strZero = "";
}
}
if (strMoney.Substring(strMoney.Length - 1, 1) != "分")
{
strMoney = strMoney + "";
}
return strMoney;
}
#endregion
#region 数字转换
public static string IntToCHN(long n, bool fang)
{
string strn = n.ToString();
string str = "";
string nn = "零壹贰叁肆伍陆柒捌玖";
string ln = "零一二三四五六七八九";
string mm = " 拾佰仟萬拾佰仟亿拾佰仟萬兆拾佰仟萬亿";
string lm = " 十百千万十百千亿十百千万兆十百千万亿";
int i = 0;
while (i < strn.Length)//>>>>>>>>>>>>>>>>出现空格
{
int m = int.Parse(strn.Substring(i, 1));
if (fang)//返回繁体字
{
str += nn.Substring(m, 1);
if (lm.Substring(strn.Length - i, 1) != " ")
{ str += mm.Substring(strn.Length - i, 1); }
}
else//返回简体字
{
str += ln.Substring(m, 1);
if (lm.Substring(strn.Length - i, 1) != " ")
{
str += lm.Substring(strn.Length - i, 1);
}
}
i++;
}
if (str.Substring(str.Length - 1) == "零")
{ str = str.Substring(0, str.Length - 1); }
if (str.Length > 1 && str.Substring(0, 2) == "一十")
{ str = str.Substring(1); }
if (str.Length > 1 && str.Substring(0, 2) == "壹拾")
{ str = str.Substring(1); }
return str;
}
#endregion
#region 设置金额文件列表
public void SetMoney(ref List<string> ListModel, string dir, string Money)
{
string buy1 = Money;
string buy3 = "";
int i;
int k;
//string CurrentPath = "";// "ResidentFlash";
//string wavdir = "";// "Wav";
//buy4 = qz + ".wav" + "#";
string[] strUnit = new string[] { "分", "角", "元", "十", "百", "千", "万", "亿" };
for (i = 6; i >= 0; i--)
{
k = buy1.IndexOf(strUnit[i]);
if (k == -1) continue;
buy3 = buy1.Substring(0, k + 1);
buy1 = buy1.Remove(0, k + 1);
if (k <= 1)
{
ListModel.Add(dir + buy3);
}
else
{
ListModel.Add(dir + buy3.Substring(0, 1));
ListModel.Add(dir + buy3.Substring(1, k));
}
}
}
#endregion
#region 新增语音
public void CheckIn(Voice item)
{
lock (ListVoice)
{
ListVoice.Enqueue(item);
}
}
#endregion
#region 读取语音
public Voice CheckOut()
{
lock (ListVoice)
{
if (ListVoice.Count == 0) return null;
return ListVoice.Dequeue();
}
}
#endregion
#region 清除语音
public void Clear()
{
lock (ListVoice)
{
ListVoice.Clear();
}
}
#endregion
#region 语音切断
public void ClearVoice()
{
while (true)
{
Voice model = CheckOut();
if (model == null) break;
}
}
#endregion
#region 播放线程
void OnStart()
{
IsRun = true;
while (IsRun == true)
{
Voice model = CheckOut();
if (model != null)
{
if (model.VoiceType == 0)
{
PlayWav(model.Bytes, model.IsSync);
}
else
{
PlayVoice(model.Source, 0, 0, model.IsSync);
}
}
AutoReset.WaitOne(500, false);
}
}
#endregion
#region 开始执行播放
public void Start()
{
Thread t = new Thread(this.OnStart);
t.Start();
}
#endregion
#region 停止播放
public void Stop()
{
IsRun = false;
}
#endregion
}
public class Voice
{
public byte[] Bytes;
public string Source = "";
public int VoiceType = 0;
public bool IsSync = false;
}
}