⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speech.cs

📁 2001年度.net程序大赛清华大学作品 网上订票系统
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using SpeechLib;

namespace SpeechExt
{
	[Serializable] 
	public struct CmdInfo
	{
		public bool bSuccess;
		public string destination;
		public string time;	
		public int method;
		public int count;
	};
	

	public enum PROPERTYID : int
	{
		Lid_Place  = 11,
		Lid_Count  =12,
		Lid_Ticket =13,
		Lid_Time   =14,
	};


	/// <summary>
	/// Summary description for Speech.
	/// </summary>
	public class Speech : MarshalByRefObject
	{

		private const int grammarId=11;

		public CmdInfo cmdInfo;

		public static readonly string[] Destination = new string[] {
																	   "the united states",
																	   "the united kingdom",
																	   "mexico",
																	   "china",
																	   "japan",
																	   "los angles",
																	   "washington",
																	   "seattle",
																	   "san francisco",
																	   "new york",
																	   "chicago"
															
																   };
		public static readonly string[] Time = new string[] {
																"today ",
																"tomorrow ",
																"the day after tomorrow",
																"two days after tomorrow",
																"three days after tomorrow",
																"four days after tomorrow",
																"five days after tomorrow",
																"this afternoon",
																"this morning",
																"tonight",
																"tomorrow morning",
																"tomorrow afternoon",
																"tomorrow evening",
																"sunday",
																"monday",
																"tuesday",
																"wednesday",
																"thuresday",
																"friday",
																"saturday",
		};


		private bool bComplete;

		public Speech()
		{
			Console.WriteLine( "Create Speech" );
		}


		~Speech( )
		{
			Console.WriteLine( "Release Speech" );
		}



		void RsContext_FalseRecognition(
			int StreamNumber, 
			object StreamPosition, 
			ISpeechRecoResult Result )
		{			
			bComplete = true;
			cmdInfo.bSuccess = false;
			Console.WriteLine("failed recognization :");
			Console.WriteLine( Result.PhraseInfo.GetText(0,-1,true) );
			
		}

		void ReContext_RecognitionForOtherContext(int StreamNumber, object StreamPosition )
		{
			bComplete = true;
			cmdInfo.bSuccess = false;
			Console.WriteLine("false recognization :");

		}
				


		void RsContext_Recognition( int StreamNumber, 
			object StreamPosition, 
			SpeechRecognitionType RecognitionType,
			ISpeechRecoResult Result )
		{
			cmdInfo.bSuccess = true;
			cmdInfo.method = -1;

			SpeechLib.ISpeechPhraseProperties properties = Result.PhraseInfo.Properties ;
			if( properties != null )
			{
				for ( int i=0;i<properties.Count;i++)
				{
					switch ( (PROPERTYID)(properties.Item(i).Id) )
					{
						case PROPERTYID.Lid_Place: 
							cmdInfo.destination = Destination[(int)(properties.Item(i).Value)];
							break;
						case PROPERTYID.Lid_Count:
							cmdInfo.count = (int)properties.Item(i).Value;
							break;
						case PROPERTYID.Lid_Ticket:
							cmdInfo.method =(int)(properties.Item(i).Value);
							break;
						case PROPERTYID.Lid_Time:
							cmdInfo.time = Time[(int)(properties.Item(i).Value)];
							break;
					}
				}	
			}
			Console.WriteLine("success recognization :");

			Console.WriteLine( Result.PhraseInfo.GetText(0,-1,true) );

			bComplete = true;
		}

		[DllImport("Ole32.dll", EntryPoint="CoCreateInstance",
			 ExactSpelling=false,CallingConvention=CallingConvention.StdCall)]
		static extern uint CoCreateInstance(ref System.Guid guid, 
			uint unOuter,
			uint unClsContext,
			ref System.Guid refiid,
			out System.IntPtr pSelf );

		public CmdInfo VoiceToCommand( byte[] voice )
		{
			Console.WriteLine( "start " );

			bComplete = false;

			System.Guid clsGuid = new System.Guid( "73AD6842-ACE0-45E8-A4DD-8795881A2C2A" );
			System.Guid iid = new System.Guid( "580AA49D-7E1E-4809-B8E2-57DA806104B8" );
			System.IntPtr pSelf;
			uint unResult = CoCreateInstance( ref clsGuid, 0, 1, ref iid, out pSelf );
			SpInProcRecoContext spContext = (SpInProcRecoContext)Marshal.GetTypedObjectForIUnknown( pSelf, typeof(SpInProcRecoContext) );

			spContext.Recognition += new  _ISpeechRecoContextEvents_RecognitionEventHandler( RsContext_Recognition );
			spContext.FalseRecognition += new _ISpeechRecoContextEvents_FalseRecognitionEventHandler( RsContext_FalseRecognition );
			spContext.RecognitionForOtherContext += new _ISpeechRecoContextEvents_RecognitionForOtherContextEventHandler( ReContext_RecognitionForOtherContext );

			try
			{
				SpMemoryStream spStream = new SpMemoryStream( );
				spStream.SetData(voice);
				
				spStream.Format.Type  = SpeechLib.SpeechAudioFormatType.SAFT11kHz8BitMono;
				spContext.Recognizer.AudioInputStream = (ISpeechBaseStream)spStream;

				ISpeechRecoGrammar grammar = spContext.CreateGrammar( grammarId );
				grammar.CmdLoadFromFile( "test.xml", SpeechLoadOption.SLOStatic );
				grammar.CmdSetRuleState("",SpeechLib.SpeechRuleState.SGDSActive);
				while( !bComplete )
				{
					Application.DoEvents( );
				}
			}
			catch( Exception ae )
			{
				string str = ae.Message;
				Console.WriteLine( ae.Message );
			}

			Console.WriteLine( "Work Complete" );

			return cmdInfo;
		}

		public byte[] TextToVoice( string text )
		{
			SpVoice spVoice = new SpVoice();
			SpMemoryStream spStream = new SpMemoryStream();
			spVoice.AudioOutputStream = (ISpeechBaseStream)spStream;
			spVoice.Speak(text,SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
			object obj = spStream.GetData();
			return (byte[])obj;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -