Как упомянул Вольфганг выше, для непрерывной речи вы можете подписаться на событие Recognizing
, чтобы получать периодические c обновления прогнозируемого речевого текста. Событие Recognized
сработает, когда Azure Speech Service определит, что пользователь прекратил говорить.
Пример:
var microphone = string.IsNullOrEmpty(file);
var audio = microphone
? AudioConfig.FromDefaultMicrophoneInput()
: AudioConfig.FromWavFileInput(file);
var config = SpeechConfig.FromSubscription(key, region);
var recognizer = new SpeechRecognizer(config);
recognizer.SessionStarted += SessionStarted;
recognizer.SessionStopped += SessionStopped;
recognizer.Recognizing += Recognizing;
recognizer.Recognized += Recognized;
recognizer.Canceled += Canceled;
recognizer.StartContinuousRecognitionAsync().Wait();
if (microphone) { Console.WriteLine("Listening; press ENTER to stop ...\n"); }
var timeout = _values.GetOrDefault("recognize.timeout", _microphone ? 30000 : int.MaxValue);
WaitForContinuousStopCancelKeyOrTimeout(recognizer, timeout);
recognizer.StopContinuousRecognitionAsync().Wait();
с такими обработчиками событий:
private void Recognizing(object sender, SpeechRecognitionEventArgs e)
{
Console.WriteLine($"RECOGNIZING: {e.Result.Text}");
}
private void Recognized(object sender, SpeechRecognitionEventArgs e)
{
var result = e.Result;
if (result.Reason == ResultReason.RecognizedSpeech && result.Text.Length != 0)
{
Console.WriteLine($"RECOGNIZED: {result.Text}");
Console.WriteLine();
}
else if (result.Reason == ResultReason.NoMatch && _verbose)
{
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
Console.WriteLine();
}
}
При запуске я произнес фразу «Меня зовут Роб Чамберс, и это тест распознавания речи ", вывод появляется очень быстро (в пределах 700-1000 мс каждого слова, которое я произнес):
Listening; press ENTER to stop ...
RECOGNIZING: my
RECOGNIZING: my name
RECOGNIZING: my name is
RECOGNIZING: my name
RECOGNIZING: my name is
RECOGNIZING: my name is rob
RECOGNIZING: my name is rob chambers
RECOGNIZING: my name is rob chambers and
RECOGNIZING: my name is rob chambers and this
RECOGNIZING: my name is rob chambers and this
RECOGNIZING: my name is rob chambers and this is
RECOGNIZING: my name is rob chambers and this is
RECOGNIZING: my name is rob chambers and this is a
RECOGNIZING: my name is rob chambers and this is a test
RECOGNIZING: my name is rob chambers and this is a test of
RECOGNIZING: my name is rob chambers and this is a test of speech
RECOGNIZING: my name is rob chambers and this is a test of
RECOGNIZING: my name is rob chambers and this is a test of speech
RECOGNIZING: my name is rob chambers and this is a test of speech recognition
RECOGNIZED: My name is Rob Chambers and this is a test of speech recognition.
Когда я произнес почти одну и ту же фразу, но в виде двух предложений с очень короткой паузой между ними вывод выглядит следующим образом:
Listening; press ENTER to stop ...
RECOGNIZING: my
RECOGNIZING: my name
RECOGNIZING: my name is
RECOGNIZING: my name is
RECOGNIZING: my name is rob
RECOGNIZING: my name is rob chambers
RECOGNIZED: My name is Rob Chambers.
RECOGNIZING: this
RECOGNIZING: this is a
RECOGNIZING: this is a test
RECOGNIZING: this is a test of
RECOGNIZING: this is a test of speech
RECOGNIZING: this is a test of speech recognition
RECOGNIZED: This is a test of speech recognition.