Created
September 5, 2020 20:17
A sample console app using the Azure Speech SDK to get text from speech in a recording.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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