Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active January 15, 2020 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksasao/799b9eae7b61ad1817bf13a0672172f1 to your computer and use it in GitHub Desktop.
Save ksasao/799b9eae7b61ad1817bf13a0672172f1 to your computer and use it in GitHub Desktop.
Windows 10 のオンライン連続音声認識が不定期に止まってしまうのを回避したコード
// Windows 10 の Windows.Media.SpeechRecognition で連続音声認識をします。
// 標準的な使い方では、音声認識が不定期に動作しなくなることが知られていますが、
// https://social.msdn.microsoft.com/Forums/en-US/5afbf9fa-d660-4a7d-b24e-e9e673282244/uwpccontinuous-speech-recognition-is-not-continuous-it-stops-randomly?forum=wpdevelop
// https://social.msdn.microsoft.com/Forums/windowsapps/en-US/1af5f3d1-d5b2-4a0c-956c-53f550e8f1d1/uwpdesktop-bridgesend-speech-recognition-argsresult-as-parameter-in-uwp-desktopbridge-package?forum=wpdevelop#96188ce3-47bd-4537-b1af-fb4c6a362c84
// それを回避しています。
//
// ビルドするためには
// [参照]で右クリック > NuGetパッケージの管理 > 右上の歯車マーク(設定)で右クリック >
// NuGet パッケージマネージャー > 全般 > 既定のパッケージの管理 を PackageReference に
// に変更してから、プレスリリース版を含める、にチェックを入れ、Microsoft.Windows.SDK.Contracts を検索して追加してください。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Media.SpeechRecognition;
using Windows.UI.Core;
namespace VoiceRecognition
{
public partial class Form1 : Form
{
private SpeechRecognizer contSpeechRecognizer;
private async Task InitializeSpeechRecognizer()
{
if(contSpeechRecognizer != null)
{
contSpeechRecognizer.HypothesisGenerated -= ContSpeechRecognizer_HypothesisGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed;
contSpeechRecognizer.StateChanged -= ContSpeechRecognizer_StateChanged;
contSpeechRecognizer.Dispose();
contSpeechRecognizer = null;
}
//初期化
contSpeechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();
// Apply the dictation topic constraint to optimize for dictated freeform speech.
// 日本語ではこれはコメントアウトしたほうが良い結果が得られる
// var dictationConstraint = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "dictation");
// contSpeechRecognizer.Constraints.Add(dictationConstraint);
await contSpeechRecognizer.CompileConstraintsAsync();
//認識中の処理定義
contSpeechRecognizer.HypothesisGenerated += ContSpeechRecognizer_HypothesisGenerated;
contSpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
// 音声入力ないままタイムアウト(20秒位)した場合の処理
contSpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
contSpeechRecognizer.StateChanged += ContSpeechRecognizer_StateChanged;
//認識開始
await contSpeechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private void ContSpeechRecognizer_StateChanged(SpeechRecognizer sender, SpeechRecognizerStateChangedEventArgs args)
{
Console.WriteLine(args.State.ToString());
}
private async void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
Console.WriteLine("タイムアウトしました");
await InitializeSpeechRecognizer();
}
private void ContSpeechRecognizer_HypothesisGenerated(SpeechRecognizer sender, SpeechRecognitionHypothesisGeneratedEventArgs args)
{
this.Invoke(new Action(()=>{
this.textBox1.Text = args.Hypothesis.Text;
}));
//認識途中に画面表示
Console.Write("\r" + args.Hypothesis.Text);
}
private void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
this.Invoke(new Action(() => {
this.textBox1.Text = args.Result.Text+"。";
}));
//認識完了後に画面に表示
Console.WriteLine("\r" + args.Result.Text + "。");
}
public Form1()
{
InitializeComponent();
Task.Run(()=> InitializeSpeechRecognizer());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment