Exemple #1
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Please put in \"key,region\" for Azure Cognitive Services [leave blank for local]:");
            var config = Console.ReadLine().Trim();

            using (var recognizer = CreateRecognizer(config))
            {
                var recognitions       = new Dictionary <string, ConsoleText>();
                ISpeechRecognizer inst = recognizer;
                inst.SpeechRecognized += (snd, evt) =>
                {
                    var tmp = $"\r[{evt.Result.Offset:hh\\:mm\\:ss}]: {evt.Result.Text}\r\n";
                    if (recognitions.TryGetValue(evt.ResultId, out var text))
                    {
                        UpdateDisplay(tmp, text);
                        recognitions.Remove(evt.ResultId);
                    }
                };

                inst.SpeechPredicted += (snd, evt) =>
                {
                    string tmp = $"\r[{evt.Result.Offset:hh\\:mm\\:ss}]: {evt.Result.Text}\r\n";
                    if (!recognitions.TryGetValue(evt.ResultId, out var text))
                    {
                        text = new ConsoleText
                        {
                            Text     = tmp,
                            Location = new Location
                            {
                                Column = Console.CursorLeft,
                                Row    = Console.CursorTop
                            }
                        };
                    }

                    UpdateDisplay(tmp, text);

                    recognitions[evt.ResultId] = text;
                };

                await inst.StartAsync();

                while (true)
                {
                    Console.ReadLine();
                    break;
                }

                await inst.StopAsync();
            }
        }