Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
speech service demo: STT
using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
namespace SpeechServiceConsole
{
class Program
{
private const string SubscriptionKey = "????????????"; // TODO: input !!
private const string Region = "??????????????????"; // TODO: input !!
private const ConsoleColor DefaultColor = ConsoleColor.White;
private const ConsoleColor ProcessingColor = ConsoleColor.Yellow;
private const ConsoleColor AppMessageColor = ConsoleColor.Green;
static async Task Main(string[] args)
{
ConsoleKey inputKey;
do
{
ShowMenu();
inputKey = Console.ReadKey().Key;
switch (inputKey)
{
case ConsoleKey.D1:
await RunContinuousSpeechRecognitionAsync();
break;
case ConsoleKey.D2:
await RunSpeechTranslationAsync();
break;
default:
Console.WriteLine();
Console.WriteLine("入力不正です。メニューから数字を選んでください。");
ShowMenu();
break;
}
} while (inputKey != ConsoleKey.D0);
}
private static void ShowMenu()
{
Console.WriteLine();
Console.WriteLine("メニューの数字を選んでください。");
Console.WriteLine("1. ダラダラと会話をテキスト化する");
Console.WriteLine("2. ダラダラと会話を翻訳する");
Console.WriteLine("0. 終了");
}
private static readonly TaskCompletionSource<int> StopRecognition = new TaskCompletionSource<int>();
private static async Task RunContinuousSpeechRecognitionAsync()
{
var language = SelectLanguage("話す言語を数字で選んでください");
var config = SpeechConfig.FromSubscription(SubscriptionKey, Region);
config.SpeechRecognitionLanguage = language;
var recognizer = new SpeechRecognizer(config);
SetEventHandler(recognizer);
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
Task.WaitAny(StopRecognition.Task);
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
}
private static async Task RunSpeechTranslationAsync()
{
// 未実装ですよー
throw new NotImplementedException();
}
#region Language Selection
private static string SelectLanguage(string message)
{
Console.WriteLine();
Console.WriteLine(message);
ShowLanguageMenu();
Console.WriteLine();
while (true)
{
var input = Console.ReadKey().Key;
// 参考: 言語コードリスト https://docs.microsoft.com/ja-jp/azure/cognitive-services/speech-service/language-support
switch (input)
{
case ConsoleKey.D1: return "ja-jp";
case ConsoleKey.D2: return "en-us";
default:
Console.ForegroundColor = DefaultColor;
Console.WriteLine();
Console.WriteLine("Oh No! メニューから数字を選んでください。");
ShowLanguageMenu();
break;
}
}
}
private static void ShowLanguageMenu()
{
Console.WriteLine("1. 日本語");
Console.WriteLine("2. English");
}
#endregion
private static void SetEventHandler(SpeechRecognizer recognizer)
{
recognizer.SessionStarted += (s, e) =>
{
Console.ForegroundColor = AppMessageColor;
Console.WriteLine();
Console.WriteLine("SESSION-STARTED.: 独り言をマイクに向かって呟いてみましょう!「ストップ」というと停止します。");
Console.WriteLine("----------------------------------");
};
recognizer.Recognizing += (s, e) =>
{
Console.ForegroundColor = ProcessingColor;
Console.WriteLine($"RECOGNIZING: {e.Result.Text}");
};
recognizer.Recognized += (s, e) =>
{
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
Console.ForegroundColor = DefaultColor;
Console.WriteLine($"RECOGNIZED: {e.Result.Text}");
if (IsStopWord(e.Result.Text))
{
StopRecognition.TrySetResult(0);
}
}
else
{
Console.ForegroundColor = AppMessageColor;
Console.WriteLine($"NOMATCH: 認識できませんでした。");
}
Console.WriteLine();
};
recognizer.Canceled += (s, e) =>
{
Console.ForegroundColor = AppMessageColor;
Console.WriteLine($"CANCELED: Reason={e.Reason}");
Console.WriteLine();
if (e.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}; ErrorDetails={e.ErrorDetails};");
}
StopRecognition.TrySetResult(0);
};
recognizer.SessionStopped += (s, e) =>
{
StopRecognition.TrySetResult(0);
Console.WriteLine("SESSION-STOPPED: executed SessionStopped event handler.");
};
}
private static bool IsStopWord(string message)
{
return message == "ストップ。" || message == "Stop.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.