記得上高中時,給人當會計,幫忙結算月度工資;用的就是帶語音功能的計算器! 當時用起來倍兒爽,於是速度加倍,效率加速;結果讓老闆賠了不少錢! 就是因為這,我才對語音計算器有了深刻印象!可能是這貨坑了我!哼~! 好吧,閑言少敘,直入正題吧! 最近在做一個項目,有個簡單的功能,就是將文本轉換成語音。 研究 ...
記得上高中時,給人當會計,幫忙結算月度工資;用的就是帶語音功能的計算器! 當時用起來倍兒爽,於是速度加倍,效率加速;結果讓老闆賠了不少錢!
就是因為這,我才對語音計算器有了深刻印象!可能是這貨坑了我!哼~!
好吧,閑言少敘,直入正題吧!
最近在做一個項目,有個簡單的功能,就是將文本轉換成語音。
研究了這個功能後,抽空順帶做了個語音計算器!
“來銀啊,上代碼!”
“老大,木有銀,上不了”
“哎呀我去,非逼我說粗話,來個貨,把代碼碼上來!”
“好的,老大!”
哈哈哈,俺就是那個帶點兒逗比的搬磚員兒!好吧,代碼來了,小主,等急了吧?
using System; using System.Speech.Synthesis; using System.Collections.Generic; namespace ReadTxt { class Program { static string num1 = "", firchar = "", lastchar = "", tempRe = ""; static void Main(string[] args) { Console.Title = "語音計算器"; bool jump = true; do { ConsoleKeyInfo info = Console.ReadKey(); switch (info.Key) { case ConsoleKey.Escape: Environment.Exit(0); jump = false; break; case ConsoleKey.NumPad0: GetKeyRead("0"); break; case ConsoleKey.NumPad1: GetKeyRead("1"); break; case ConsoleKey.NumPad2: GetKeyRead("2"); break; case ConsoleKey.NumPad3: GetKeyRead("3"); break; case ConsoleKey.NumPad4: GetKeyRead("4"); break; case ConsoleKey.NumPad5: GetKeyRead("5"); break; case ConsoleKey.NumPad6: GetKeyRead("6"); break; case ConsoleKey.NumPad7: GetKeyRead("7"); break; case ConsoleKey.NumPad8: GetKeyRead("8"); break; case ConsoleKey.NumPad9: GetKeyRead("9"); break; case ConsoleKey.Add: GetKeyRead("加"); break; case ConsoleKey.Subtract: GetKeyRead("減"); break; case ConsoleKey.Multiply: GetKeyRead("乘"); break; case ConsoleKey.Divide: GetKeyRead("除"); break; case ConsoleKey.Enter: if (!string.IsNullOrEmpty(num1) && GetSignIsTrue(num1)) { SetValue(num1); num1 = ""; } else { num1 = ""; if (!string.IsNullOrEmpty(num1)) { Console.Beep(); Console.WriteLine("Error."); } } break; default: break; } } while (jump); Console.Read(); } //判斷用戶輸入的內容是否合法 static void GetKeyRead(string str) { SpeechSynthesizer spvoice = new SpeechSynthesizer(); spvoice.Rate = 1; spvoice.Volume = 100; if (!string.IsNullOrEmpty(num1)) { firchar = num1.Substring(0, 1); lastchar = num1.Substring(num1.Length - 1, 1); } switch (str) { case "加": if (firchar != "加" && lastchar != "加") { if (lastchar != "減" && lastchar != "乘" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "減": if (firchar != "減" && lastchar != "減") { if (lastchar != "加" && lastchar != "乘" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "乘": if (firchar != "乘" && lastchar != "乘") { if (lastchar != "加" && lastchar != "減" && lastchar != "除") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; case "除": if (firchar != "除" && lastchar != "除") { if (lastchar != "加" && lastchar != "減" && lastchar != "乘") { num1 += str; } else { num1 = num1.Remove(num1.Length - 1); num1 += str; } } break; default: num1 += str; break; } spvoice.SpeakAsync(str); Console.Clear(); Console.Write(tempRe + num1.Replace('加', '+').Replace('減', '-').Replace('乘', '*').Replace('除', '/').Replace("等於", "=")); } static bool GetSignIsTrue(string num1) { if (!string.IsNullOrEmpty(num1)) { firchar = num1.Substring(0, 1); lastchar = num1.Substring(num1.Length - 1, 1); } return firchar != "加" && lastchar != "加" && firchar != "減" && lastchar != "減" && firchar != "乘" && lastchar != "乘" && firchar != "除" && lastchar != "除"; } //計算 static void SetValue(string num1) { List<double> array = new List<double>(); List<string> sign = new List<string>(); num1 = num1.Replace('加', '+').Replace('減', '-').Replace('乘', '*').Replace('除', '/'); string tempCap = ""; char[] MyChar = num1.ToCharArray(); for (int i = 0; i < MyChar.Length; i++) { if (MyChar[i].ToString() == "+" || MyChar[i].ToString() == "-" || MyChar[i].ToString() == "*" || MyChar[i].ToString() == "/") { array.Add(Convert.ToDouble(tempCap)); tempCap = ""; sign.Add(MyChar[i].ToString()); } else { if (i == MyChar.Length - 1) { tempCap += MyChar[i].ToString(); array.Add(Convert.ToDouble(tempCap)); } else { tempCap += MyChar[i].ToString(); } } } double resultTemp = 0; for (int i = 0; i < sign.ToArray().Length; i++) { if (sign[i] == "*") { resultTemp = array[i] * array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } else if (sign[i] == "/") { resultTemp = array[i] / array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } } for (int i = 0; i < sign.ToArray().Length; i++) { if (sign[i] == "+") { resultTemp = array[i] + array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } else if (sign[i] == "-") { resultTemp = array[i] - array[i + 1]; array[i] = resultTemp; array.Remove(array[i + 1]); sign.Remove(sign[i]); resultTemp = 0; i--; } } double answ = array[0]; array.Clear(); sign.Clear(); tempRe += num1.Replace('加', '+').Replace('減', '-').Replace('乘', '*').Replace('除', '/') + "= " + answ + "\n"; GetKeyRead("等於" + answ); } } }
ok,這就是全部代碼了,請容在下粗略的介紹下實現步驟:
1.既然是語音計算器,首先要解決的就是如何實現語音?
其實,語音功能實現起來並不複雜,微軟早就封裝好了一個,將文本轉換成語音功能的類庫,現在你要做的就是:找到它,然後把它引用到你的項目里!
什麼?這個類庫叫啥?
表急嘛,心急吃不了“熱豆腐”,有可能“豆腐”還跑了呢,要耐心點兒,才能約到手!是吧?
好吧,介紹下這個類庫,它叫“System.Speech.dll”。.net FrameWork 的安裝目錄里就有它的身影!裝了.net FrameWork 的童鞋,可以通過以下目錄去尋覓它!
大概是這個:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5
尋尋覓覓,冷冷清清,凄凄慘慘戚戚!什麼?沒尋覓到?
好吧,教你一招
打開C盤,直接搜索!
沒裝的,那就去網上搜吧,自己下崽兒!哈哈~!願意裝的也可以裝一下!
找到的,直接引用到項目里就行了哈!
2.其次,別忘了引用下這兩個命名空間:
using System.Speech.Synthesis; //語音類庫
using System.Collections.Generic; //數組集合類庫
3.最後,簡單介紹下文本轉換成語音的功能!
其實,也就幾句話!
SpeechSynthesizer spvoice = new SpeechSynthesizer(); //語音類 spvoice.Rate = 1; //使用 spvoice 設置朗讀頻率 [範圍 -10 ~ 10] spvoice.Volume = 80; //使用 spvoice 設置朗讀音量 [範圍 0 ~ 100] spvoice.SpeakAsync("大家好,我是博主小白!"); //開始讀
好了,其他的代碼就要看小主的內力了,在下就不多說了!
看小主骨骼驚奇,“英語非凡”,想是定能習得搬磚精髓,打遍天下需求,殺掉世間bug;然後走上淫僧巔峰,贏取大白美眉的!哈哈哈~!
拙文一篇,望各位海涵!
技術討論群:225443677 有意者歡迎騷擾,謝謝!