Skip to content

Instantly share code, notes, and snippets.

@mahiya
Created December 1, 2023 07:41
Show Gist options
  • Save mahiya/63d8333632e5ab3c28d21a39fc7919d5 to your computer and use it in GitHub Desktop.
Save mahiya/63d8333632e5ab3c28d21a39fc7919d5 to your computer and use it in GitHub Desktop.
Azure AI Speech でリアルタイム音声認識を行うコード
// dotnet add package Microsoft.CognitiveServices.Speech --version 1.33.0
// dotnet add package NAudio --version 2.2.1
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using NAudio.CoreAudioApi;
namespace RealtimeAudioRecognitionByAzure
{
class Program
{
static async Task Main()
{
// AI Speech Service の設定を行う
const string key = "";
const string region = "japaneast";
// オーディオデバイスを取得
var enumerator = new MMDeviceEnumerator();
var deviceType = DataFlow.Render; // オーディオデバイス
//var deviceType = DataFlow.Capture; // マイクデバイス
var devices = enumerator.EnumerateAudioEndPoints(deviceType, DeviceState.Active).ToList();
if(!devices.Any())
{
Console.WriteLine("Audio device is not found.");
return;
}
// 利用可能なオーディオデバイスを表示
foreach(var (d, i) in devices.Select((d, i) => (d, i)))
{
Console.WriteLine($"{i}: {d.FriendlyName}");
}
// ユーザに使用するオーディオを選択させる
Console.WriteLine("Select audio device to record:");
var inputed = Console.ReadKey();
if(!int.TryParse(inputed.KeyChar.ToString(), out var inputedIndex))
{
Console.WriteLine("Invalid input.");
return;
}
var device = devices[inputedIndex];
// AI Speech Service に接続して音声認識を開始する
var config = SpeechConfig.FromSubscription(key, region);
//config.SetProperty("ConversationTranscriptionInRoomAndOnline", "true");
//config.SetProperty("DifferentiateGuestSpeakers", "true");
//config.SetServiceProperty("transcriptionMode", "RealTimeAndAsync", ServicePropertyChannel.UriQueryParameter);
var input = deviceType == DataFlow.Render
? AudioConfig.FromSpeakerOutput(device.ID)
: AudioConfig.FromMicrophoneInput(device.ID);
var recognizer = new SpeechRecognizer(config, "ja-JP", input);
// 音声認識のイベントを設定する
recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("STARTING on {0}", e);
};
recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("ClOSING on {0}", e);
};
recognizer.Canceled += (s, e) =>
{
Console.WriteLine("ClOSING on {0}", e);
};
recognizer.Recognized += (s, e) =>
{
Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
};
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};
// 音声認識を開始する
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
// 音声認識を停止する
Console.WriteLine("Press any key to stop");
Console.ReadLine();
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment