using System; using NAudio.Wave; class Program { static WaveInEvent waveSource; static void Main(string[] args) { // Set up microphone input waveSource = new WaveInEvent(); waveSource.WaveFormat = new WaveFormat(44100, 16, 1); waveSource.DataAvailable += WaveSource_DataAvailable; // Start capturing audio waveSource.StartRecording(); Console.WriteLine("Press any key to stop recording..."); Console.ReadKey(); // Stop capturing audio and release resources waveSource.StopRecording(); waveSource.Dispose(); } private static void WaveSource_DataAvailable(object sender, WaveInEventArgs e) { // Do something with the captured audio data Console.WriteLine($"Received {e.BytesRecorded} bytes of audio data..."); } }
using System; using System.Speech.Recognition; class Program { static SpeechRecognitionEngine speechEngine; static void Main(string[] args) { // Set up SpeechRecognitionEngine object speechEngine = new SpeechRecognitionEngine(); // Start recognizing speech speechEngine.SetInputToDefaultAudioDevice(); speechEngine.SpeechRecognized += SpeechEngine_SpeechRecognized; speechEngine.RecognizeAsync(RecognizeMode.Multiple); Console.WriteLine("Press any key to stop recognizing speech..."); Console.ReadKey(); // Stop recognizing speech and release resources speechEngine.RecognizeAsyncCancel(); speechEngine.Dispose(); } private static void SpeechEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { // Do something with the recognized speech Console.WriteLine($"Recognized speech: {e.Result.Text}"); } }This code sets up speech recognition using the .NET Speech API and starts recognizing speech input from the default audio device. The `SpeechEngine_SpeechRecognized` method is called when a speech is recognized, and in this example, it just prints out the recognized text. When a key is pressed on the console, the `speechEngine` object calls its `RecognizeAsyncCancel` method to end the speech recognition, and then it releases its resources. The first example likely uses the NAudio library, which provides a way to capture and playback audio using the .NET Framework. The second example likely uses the System.Speech library, which provides a way to do speech recognition using the .NET Framework.