Created
July 24, 2016 15:50
-
-
Save ksasao/911b4dfe4ef4115a628c996c7c77655a to your computer and use it in GitHub Desktop.
C#による音声ファイル(.wav)→テキスト変換。参照>アセンブリで、System.Speech を参照に追加する。
This file contains hidden or 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.Speech.Recognition; | |
using System.Text; | |
namespace Wav2Text | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length == 0) | |
{ | |
Console.WriteLine("usage: wav2text target_file.wav"); | |
return; | |
} | |
string filename = args[0]; | |
string result = WaveFileToText(filename); | |
Console.WriteLine(result); | |
} | |
static string WaveFileToText(string filename) | |
{ | |
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(); | |
Grammar gr = new DictationGrammar(); | |
sre.LoadGrammar(gr); | |
sre.SetInputToWaveFile(filename); | |
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue); | |
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue); | |
sre.EndSilenceTimeout = new TimeSpan(100000000); | |
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000); | |
StringBuilder sb = new StringBuilder(); | |
while (true) | |
{ | |
try | |
{ | |
var recText = sre.Recognize(); | |
if (recText == null) | |
{ | |
break; | |
} | |
sb.Append(recText.Text); | |
} | |
catch (Exception ex) | |
{ | |
//handle exception | |
//... | |
break; | |
} | |
} | |
return sb.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment