Home › Artificial Intelligence (AI) / Machine Learning (ML) › Azure Cognitive Services Speech to Text Hello World in C#
Artificial Intelligence (AI) / Machine Learning (ML) · Aug 17, 2018

Azure Cognitive Services Speech to Text Hello World in C#

In this post we will create a Windows console app that integrates with Azure Cognitive Services Speech Service to convert to text what we are speaking.

In Visual Studio, create a new console app:

You will see:

Add Microsoft.CognitiveServices.Speech through NuGet:

Click on the Solution->Configuration Manager:

Change the platform to your current platform:

Add the code. This is based on this sample code here. For yourapikey, use the key created here. For the region, use your region such as westus.

[sourcecode language=”CSharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;

namespace Carl.SpeechRecognitionHelloWorld
{
class Program
{
public static async Task RecognizeSpeechAsync()
{
var factory = SpeechFactory.FromSubscription(yourapikey, yourregion);

using (var recognizer = factory.CreateSpeechRecognizer())
{
Console.WriteLine("Say something…");

var result = await recognizer.RecognizeAsync();
if (result.RecognitionStatus != RecognitionStatus.Recognized)
{
Console.WriteLine($"Recognition status: {result.RecognitionStatus.ToString()}");
if (result.RecognitionStatus == RecognitionStatus.Canceled)
Console.WriteLine($"There was an error, reason: {result.RecognitionFailureReason}");
else
Console.WriteLine("No speech could be recognized.\n");
}
else
Console.WriteLine($"We recognized: {result.Text}");
}
}

static void Main()
{
RecognizeSpeechAsync().Wait();
Console.WriteLine("Please press a key to continue.");
Console.ReadLine();
}
}
}
[/sourcecode]

Press F5 to run the application. You will see “Say something”. After saying something, it will appear in text, i.e. below I said “Testing 123” and it converted it to text and displayed it on the screen:

Once we get a response from the Speech API, we can use this to look perform any number of actions.

In our next post we will use voice input to look up the weather.

 

One Response to Azure Cognitive Services Speech to Text Hello World in C#

  1. Hi Carl,
    SpeechFactory is deprecated or not. Because I try your advice and SpeechFactory not exist.
    If deprecated how I must customize your code?

    Thanks in advance.
    Necdet

Leave a Reply

Your email address will not be published. Required fields are marked *

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.