Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile
SpeechServerSource/SpeechServer.c@4:c4dfbf6b7f9f, 2014-12-09 (annotated)
- Committer:
- dudanian
- Date:
- Tue Dec 09 15:52:59 2014 +0000
- Revision:
- 4:c4dfbf6b7f9f
- Child:
- 5:ba94b62d86c9
Added SpeechServer Source code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudanian | 4:c4dfbf6b7f9f | 1 | // THIS IS ACTUALLY A .cs FILE! Mbed only supports .c and .cpp files in repositories |
dudanian | 4:c4dfbf6b7f9f | 2 | /* |
dudanian | 4:c4dfbf6b7f9f | 3 | using System; |
dudanian | 4:c4dfbf6b7f9f | 4 | using System.IO; |
dudanian | 4:c4dfbf6b7f9f | 5 | using System.Net; |
dudanian | 4:c4dfbf6b7f9f | 6 | using System.Net.Sockets; |
dudanian | 4:c4dfbf6b7f9f | 7 | using System.Text; |
dudanian | 4:c4dfbf6b7f9f | 8 | using System.Speech.Recognition; |
dudanian | 4:c4dfbf6b7f9f | 9 | using System.Speech.Recognition.SrgsGrammar; |
dudanian | 4:c4dfbf6b7f9f | 10 | using System.Collections.Generic; |
dudanian | 4:c4dfbf6b7f9f | 11 | using System.Timers; |
dudanian | 4:c4dfbf6b7f9f | 12 | using System.Speech.Synthesis; |
dudanian | 4:c4dfbf6b7f9f | 13 | using System.Speech.AudioFormat; |
dudanian | 4:c4dfbf6b7f9f | 14 | |
dudanian | 4:c4dfbf6b7f9f | 15 | namespace ServerTest |
dudanian | 4:c4dfbf6b7f9f | 16 | { |
dudanian | 4:c4dfbf6b7f9f | 17 | class Program |
dudanian | 4:c4dfbf6b7f9f | 18 | { |
dudanian | 4:c4dfbf6b7f9f | 19 | static Timer timeout; |
dudanian | 4:c4dfbf6b7f9f | 20 | static bool responseSent; |
dudanian | 4:c4dfbf6b7f9f | 21 | static TcpClient client; |
dudanian | 4:c4dfbf6b7f9f | 22 | static SpeechSynthesizer synth; |
dudanian | 4:c4dfbf6b7f9f | 23 | |
dudanian | 4:c4dfbf6b7f9f | 24 | static void Main(string[] args) |
dudanian | 4:c4dfbf6b7f9f | 25 | { |
dudanian | 4:c4dfbf6b7f9f | 26 | timeout = new Timer(2000); |
dudanian | 4:c4dfbf6b7f9f | 27 | timeout.AutoReset = false; |
dudanian | 4:c4dfbf6b7f9f | 28 | timeout.Elapsed += OnSpeechRecognitionFailed; |
dudanian | 4:c4dfbf6b7f9f | 29 | responseSent = false; |
dudanian | 4:c4dfbf6b7f9f | 30 | |
dudanian | 4:c4dfbf6b7f9f | 31 | Grammar g = new Grammar(new SrgsDocument("grammar.xml")); |
dudanian | 4:c4dfbf6b7f9f | 32 | |
dudanian | 4:c4dfbf6b7f9f | 33 | // Recognizing Speech |
dudanian | 4:c4dfbf6b7f9f | 34 | SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); |
dudanian | 4:c4dfbf6b7f9f | 35 | recognizer.LoadGrammarAsync(g); |
dudanian | 4:c4dfbf6b7f9f | 36 | recognizer.SpeechRecognized += |
dudanian | 4:c4dfbf6b7f9f | 37 | new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); |
dudanian | 4:c4dfbf6b7f9f | 38 | recognizer.SpeechRecognitionRejected += |
dudanian | 4:c4dfbf6b7f9f | 39 | new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected); |
dudanian | 4:c4dfbf6b7f9f | 40 | // Synthesizing Speech |
dudanian | 4:c4dfbf6b7f9f | 41 | synth = new SpeechSynthesizer(); |
dudanian | 4:c4dfbf6b7f9f | 42 | synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted); |
dudanian | 4:c4dfbf6b7f9f | 43 | |
dudanian | 4:c4dfbf6b7f9f | 44 | TcpListener server = null; |
dudanian | 4:c4dfbf6b7f9f | 45 | try { |
dudanian | 4:c4dfbf6b7f9f | 46 | Int32 port = 13000; |
dudanian | 4:c4dfbf6b7f9f | 47 | //Console.WriteLine("What IP Address?"); |
dudanian | 4:c4dfbf6b7f9f | 48 | //String ip = Console.ReadLine(); |
dudanian | 4:c4dfbf6b7f9f | 49 | //IPAddress hostaddr = IPAddress.Parse(ip); |
dudanian | 4:c4dfbf6b7f9f | 50 | IPHostEntry host = Dns.GetHostEntry("laptop-pc"); |
dudanian | 4:c4dfbf6b7f9f | 51 | IPAddress hostaddr = null; |
dudanian | 4:c4dfbf6b7f9f | 52 | foreach (IPAddress ipaddr in host.AddressList) { |
dudanian | 4:c4dfbf6b7f9f | 53 | if (ipaddr.AddressFamily == AddressFamily.InterNetwork) |
dudanian | 4:c4dfbf6b7f9f | 54 | { |
dudanian | 4:c4dfbf6b7f9f | 55 | hostaddr = ipaddr; |
dudanian | 4:c4dfbf6b7f9f | 56 | } |
dudanian | 4:c4dfbf6b7f9f | 57 | } |
dudanian | 4:c4dfbf6b7f9f | 58 | Console.WriteLine("Listening on Address: {0}", hostaddr.ToString()); |
dudanian | 4:c4dfbf6b7f9f | 59 | |
dudanian | 4:c4dfbf6b7f9f | 60 | server = new TcpListener(hostaddr, port); |
dudanian | 4:c4dfbf6b7f9f | 61 | server.Start(); |
dudanian | 4:c4dfbf6b7f9f | 62 | |
dudanian | 4:c4dfbf6b7f9f | 63 | Byte[] bytes = new Byte[1024]; |
dudanian | 4:c4dfbf6b7f9f | 64 | |
dudanian | 4:c4dfbf6b7f9f | 65 | while (true) |
dudanian | 4:c4dfbf6b7f9f | 66 | { |
dudanian | 4:c4dfbf6b7f9f | 67 | Console.Write("Waiting for a connection..."); |
dudanian | 4:c4dfbf6b7f9f | 68 | client = server.AcceptTcpClient(); |
dudanian | 4:c4dfbf6b7f9f | 69 | Console.WriteLine("Connected!"); |
dudanian | 4:c4dfbf6b7f9f | 70 | recognizer.SetInputToNull(); |
dudanian | 4:c4dfbf6b7f9f | 71 | |
dudanian | 4:c4dfbf6b7f9f | 72 | NetworkStream stream = client.GetStream(); |
dudanian | 4:c4dfbf6b7f9f | 73 | int i; |
dudanian | 4:c4dfbf6b7f9f | 74 | |
dudanian | 4:c4dfbf6b7f9f | 75 | using (FileStream fs = File.Open("test.wav", FileMode.Create, FileAccess.Write, FileShare.None)) |
dudanian | 4:c4dfbf6b7f9f | 76 | { |
dudanian | 4:c4dfbf6b7f9f | 77 | int sum = 0; |
dudanian | 4:c4dfbf6b7f9f | 78 | while (sum < 110296) |
dudanian | 4:c4dfbf6b7f9f | 79 | { |
dudanian | 4:c4dfbf6b7f9f | 80 | i = stream.Read(bytes, 0, bytes.Length); |
dudanian | 4:c4dfbf6b7f9f | 81 | sum += i; |
dudanian | 4:c4dfbf6b7f9f | 82 | fs.Write(bytes, 0, i); |
dudanian | 4:c4dfbf6b7f9f | 83 | } |
dudanian | 4:c4dfbf6b7f9f | 84 | } |
dudanian | 4:c4dfbf6b7f9f | 85 | |
dudanian | 4:c4dfbf6b7f9f | 86 | recognizer.SetInputToWaveFile("test.wav"); |
dudanian | 4:c4dfbf6b7f9f | 87 | recognizer.RecognizeAsync(RecognizeMode.Single); |
dudanian | 4:c4dfbf6b7f9f | 88 | timeout.Start(); |
dudanian | 4:c4dfbf6b7f9f | 89 | |
dudanian | 4:c4dfbf6b7f9f | 90 | while (!responseSent) |
dudanian | 4:c4dfbf6b7f9f | 91 | { |
dudanian | 4:c4dfbf6b7f9f | 92 | System.Threading.Thread.Sleep(100); |
dudanian | 4:c4dfbf6b7f9f | 93 | } |
dudanian | 4:c4dfbf6b7f9f | 94 | responseSent = false; |
dudanian | 4:c4dfbf6b7f9f | 95 | System.Threading.Thread.Sleep(1000); |
dudanian | 4:c4dfbf6b7f9f | 96 | client.Close(); |
dudanian | 4:c4dfbf6b7f9f | 97 | } |
dudanian | 4:c4dfbf6b7f9f | 98 | } |
dudanian | 4:c4dfbf6b7f9f | 99 | catch (SocketException e) |
dudanian | 4:c4dfbf6b7f9f | 100 | { |
dudanian | 4:c4dfbf6b7f9f | 101 | Console.WriteLine("SocketException: {0}", e); |
dudanian | 4:c4dfbf6b7f9f | 102 | } |
dudanian | 4:c4dfbf6b7f9f | 103 | finally |
dudanian | 4:c4dfbf6b7f9f | 104 | { |
dudanian | 4:c4dfbf6b7f9f | 105 | server.Stop(); |
dudanian | 4:c4dfbf6b7f9f | 106 | } |
dudanian | 4:c4dfbf6b7f9f | 107 | Console.WriteLine("\nHit enter to continue..."); |
dudanian | 4:c4dfbf6b7f9f | 108 | Console.Read(); |
dudanian | 4:c4dfbf6b7f9f | 109 | } |
dudanian | 4:c4dfbf6b7f9f | 110 | |
dudanian | 4:c4dfbf6b7f9f | 111 | static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) |
dudanian | 4:c4dfbf6b7f9f | 112 | { |
dudanian | 4:c4dfbf6b7f9f | 113 | timeout.Stop(); |
dudanian | 4:c4dfbf6b7f9f | 114 | String toSend = e.Result.Semantics["command"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 115 | String minute = "0"; |
dudanian | 4:c4dfbf6b7f9f | 116 | String hour = "0"; |
dudanian | 4:c4dfbf6b7f9f | 117 | String period = "0"; |
dudanian | 4:c4dfbf6b7f9f | 118 | String timezone = "0"; |
dudanian | 4:c4dfbf6b7f9f | 119 | |
dudanian | 4:c4dfbf6b7f9f | 120 | // The following code illustrates some of the information available |
dudanian | 4:c4dfbf6b7f9f | 121 | // in the recognition result. |
dudanian | 4:c4dfbf6b7f9f | 122 | Console.WriteLine("Recognition result summary:"); |
dudanian | 4:c4dfbf6b7f9f | 123 | Console.WriteLine( |
dudanian | 4:c4dfbf6b7f9f | 124 | " Recognized phrase: {0}\n" + |
dudanian | 4:c4dfbf6b7f9f | 125 | " Confidence score {1}\n" + |
dudanian | 4:c4dfbf6b7f9f | 126 | " Grammar used: {2}\n", |
dudanian | 4:c4dfbf6b7f9f | 127 | e.Result.Text, e.Result.Confidence, e.Result.Grammar.Name); |
dudanian | 4:c4dfbf6b7f9f | 128 | |
dudanian | 4:c4dfbf6b7f9f | 129 | // Display the semantic values in the recognition result. |
dudanian | 4:c4dfbf6b7f9f | 130 | Console.WriteLine(" Semantic results:"); |
dudanian | 4:c4dfbf6b7f9f | 131 | foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics) |
dudanian | 4:c4dfbf6b7f9f | 132 | { |
dudanian | 4:c4dfbf6b7f9f | 133 | Console.WriteLine(" The {0} value is {1}", |
dudanian | 4:c4dfbf6b7f9f | 134 | child.Key, child.Value.Value ?? "null"); |
dudanian | 4:c4dfbf6b7f9f | 135 | } |
dudanian | 4:c4dfbf6b7f9f | 136 | if (e.Result.Semantics.ContainsKey("time")) |
dudanian | 4:c4dfbf6b7f9f | 137 | { |
dudanian | 4:c4dfbf6b7f9f | 138 | Console.WriteLine(" In Time:"); |
dudanian | 4:c4dfbf6b7f9f | 139 | foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics["time"]) |
dudanian | 4:c4dfbf6b7f9f | 140 | { |
dudanian | 4:c4dfbf6b7f9f | 141 | Console.WriteLine(" The {0} value is {1}", |
dudanian | 4:c4dfbf6b7f9f | 142 | child.Key, child.Value.Value ?? "null"); |
dudanian | 4:c4dfbf6b7f9f | 143 | } |
dudanian | 4:c4dfbf6b7f9f | 144 | hour = e.Result.Semantics["time"]["hour"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 145 | minute = e.Result.Semantics["time"]["minute"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 146 | period = e.Result.Semantics["time"]["period"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 147 | } |
dudanian | 4:c4dfbf6b7f9f | 148 | else if (e.Result.Semantics.ContainsKey("length")) |
dudanian | 4:c4dfbf6b7f9f | 149 | { |
dudanian | 4:c4dfbf6b7f9f | 150 | Console.WriteLine(" In Length:"); |
dudanian | 4:c4dfbf6b7f9f | 151 | foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics["length"]) |
dudanian | 4:c4dfbf6b7f9f | 152 | { |
dudanian | 4:c4dfbf6b7f9f | 153 | Console.WriteLine(" The {0} value is {1}", |
dudanian | 4:c4dfbf6b7f9f | 154 | child.Key, child.Value.Value ?? "null"); |
dudanian | 4:c4dfbf6b7f9f | 155 | } |
dudanian | 4:c4dfbf6b7f9f | 156 | hour = e.Result.Semantics["length"]["hour"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 157 | minute = e.Result.Semantics["length"]["minute"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 158 | } |
dudanian | 4:c4dfbf6b7f9f | 159 | else if (e.Result.Semantics.ContainsKey("zone")) |
dudanian | 4:c4dfbf6b7f9f | 160 | { |
dudanian | 4:c4dfbf6b7f9f | 161 | timezone = e.Result.Semantics["zone"].Value.ToString(); |
dudanian | 4:c4dfbf6b7f9f | 162 | } |
dudanian | 4:c4dfbf6b7f9f | 163 | toSend += " " + hour + " " + minute + " " + period + " " + timezone; |
dudanian | 4:c4dfbf6b7f9f | 164 | Console.WriteLine("To Send: \"{0}\"", toSend); |
dudanian | 4:c4dfbf6b7f9f | 165 | Console.WriteLine(); |
dudanian | 4:c4dfbf6b7f9f | 166 | |
dudanian | 4:c4dfbf6b7f9f | 167 | byte[] msg = System.Text.Encoding.ASCII.GetBytes(toSend); |
dudanian | 4:c4dfbf6b7f9f | 168 | |
dudanian | 4:c4dfbf6b7f9f | 169 | client.GetStream().Write(msg, 0, msg.Length); |
dudanian | 4:c4dfbf6b7f9f | 170 | responseSent = true; |
dudanian | 4:c4dfbf6b7f9f | 171 | } |
dudanian | 4:c4dfbf6b7f9f | 172 | |
dudanian | 4:c4dfbf6b7f9f | 173 | static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e) |
dudanian | 4:c4dfbf6b7f9f | 174 | { |
dudanian | 4:c4dfbf6b7f9f | 175 | Console.WriteLine("Speech input was rejected."); |
dudanian | 4:c4dfbf6b7f9f | 176 | foreach (RecognizedPhrase phrase in e.Result.Alternates) |
dudanian | 4:c4dfbf6b7f9f | 177 | { |
dudanian | 4:c4dfbf6b7f9f | 178 | Console.WriteLine(" Rejected phrase: " + phrase.Text); |
dudanian | 4:c4dfbf6b7f9f | 179 | Console.WriteLine(" Confidence score: " + phrase.Confidence); |
dudanian | 4:c4dfbf6b7f9f | 180 | } |
dudanian | 4:c4dfbf6b7f9f | 181 | } |
dudanian | 4:c4dfbf6b7f9f | 182 | |
dudanian | 4:c4dfbf6b7f9f | 183 | private static void OnSpeechRecognitionFailed(Object source, ElapsedEventArgs e) |
dudanian | 4:c4dfbf6b7f9f | 184 | { |
dudanian | 4:c4dfbf6b7f9f | 185 | Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); |
dudanian | 4:c4dfbf6b7f9f | 186 | |
dudanian | 4:c4dfbf6b7f9f | 187 | byte[] msg = System.Text.Encoding.ASCII.GetBytes("noCommand 0 0 0 0"); |
dudanian | 4:c4dfbf6b7f9f | 188 | |
dudanian | 4:c4dfbf6b7f9f | 189 | client.GetStream().Write(msg, 0, msg.Length); |
dudanian | 4:c4dfbf6b7f9f | 190 | |
dudanian | 4:c4dfbf6b7f9f | 191 | responseSent = true; |
dudanian | 4:c4dfbf6b7f9f | 192 | |
dudanian | 4:c4dfbf6b7f9f | 193 | //synth.SetOutputToWaveFile("response.wav", new SpeechAudioFormatInfo(11025, AudioBitsPerSample.Sixteen, AudioChannel.Mono)); |
dudanian | 4:c4dfbf6b7f9f | 194 | //synth.SpeakAsync("testing this"); |
dudanian | 4:c4dfbf6b7f9f | 195 | } |
dudanian | 4:c4dfbf6b7f9f | 196 | |
dudanian | 4:c4dfbf6b7f9f | 197 | static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e) |
dudanian | 4:c4dfbf6b7f9f | 198 | { |
dudanian | 4:c4dfbf6b7f9f | 199 | synth.SetOutputToNull(); |
dudanian | 4:c4dfbf6b7f9f | 200 | Console.WriteLine("Speaking Complete"); |
dudanian | 4:c4dfbf6b7f9f | 201 | } |
dudanian | 4:c4dfbf6b7f9f | 202 | } |
dudanian | 4:c4dfbf6b7f9f | 203 | } |
dudanian | 4:c4dfbf6b7f9f | 204 | */ |