Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Created September 5, 2020 20:17
Show Gist options
  • Save chrisobriensp/7a4c88088bbe00037d2c6b9720124484 to your computer and use it in GitHub Desktop.
Save chrisobriensp/7a4c88088bbe00037d2c6b9720124484 to your computer and use it in GitHub Desktop.
A sample console app using the Azure Speech SDK to get text from speech in a recording.
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace COB.SpeechAPI
{
class Program
{
private static string WAV_FILEPATH = @"C:\Users\chris.obrien\Documents\Voicemeeter\Recording-_17_.wav";
private static async Task getTextFromSpeech()
{
var speechConfig = SpeechConfig.FromSubscription("[AZURE SPEECH KEY]", "[REGION]");
// OPTION 1: use pre-recorded WAV file..
var audioConfig = AudioConfig.FromWavFileInput(WAV_FILEPATH);
var recognizer = new SpeechRecognizer(speechConfig, audioConfig);
// OPTION 2: record from microphone..
//var recognizer = new SpeechRecognizer(speechConfig);
//var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
var result = await recognizer.RecognizeOnceAsync();
switch (result.Reason)
{
case ResultReason.RecognizedSpeech:
Console.WriteLine($"RECOGNIZED TEXT: '{result.Text}'");
break;
case ResultReason.NoMatch:
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
break;
case ResultReason.Canceled:
var cancellation = CancellationDetails.FromResult(result);
Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}
break;
}
}
static void Main(string[] args)
{
getTextFromSpeech().Wait();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment