0

我正在使用 azure-speech 识别来自Speech_recognition_samples.cpp的音频流,来自 RecognitionResult类我只能获取 Text 和 m_duration,但是如何获取语音结果的开始时间和结束时间?我知道e.Result->Offset()可以返回偏移量,但我仍然对此感到困惑,我的代码是

void recognizeSpeech() {
std::shared_ptr<SpeechConfig> config = SpeechConfig::FromSubscription("****", "****");
config->RequestWordLevelTimestamps();
auto pushStream = AudioInputStream::CreatePushStream();
std::cout << "created push\n" << std::endl;
auto audioInput = AudioConfig::FromStreamInput(pushStream);
auto recognizer = SpeechRecognizer::FromConfig(config, audioInput);

promise<void> recognitionEnd;

recognizer->Recognizing.Connect([](const SpeechRecognitionEventArgs& e)
{
    cout << "Recognizing:" << e.Result->Text << std::endl
            << "  Offset=" << e.Result->Offset() << std::endl
            << "  Duration=" << e.Result->Duration() << std::endl;
});

recognizer->Recognized.Connect([](const SpeechRecognitionEventArgs& e)
{
    if (e.Result->Reason == ResultReason::RecognizedSpeech)
    {
        cout << "RECOGNIZED: Text=" << e.Result->Text << std::endl
            << "  Offset=" << e.Result->Offset() << std::endl
            << "  Duration=" << e.Result->Duration() << std::endl;
    }
    else if (e.Result->Reason == ResultReason::NoMatch)
    {
        cout << "NOMATCH: Speech could not be recognized." << std::endl;
    }
});

recognizer->Canceled.Connect([&recognitionEnd](const SpeechRecognitionCanceledEventArgs& e)
{
    switch (e.Reason)
    {
    case CancellationReason::EndOfStream:
        cout << "CANCELED: Reach the end of the file." << std::endl;
        break;

    case CancellationReason::Error:
        cout << "CANCELED: ErrorCode=" << (int)e.ErrorCode << std::endl;
        cout << "CANCELED: ErrorDetails=" << e.ErrorDetails << std::endl;
        recognitionEnd.set_value();
        break;

    default:
        cout << "CANCELED: received unknown reason." << std::endl;
    }

});

recognizer->SessionStopped.Connect([&recognitionEnd](const SessionEventArgs& e)
{
    cout << "Session stopped.";
    recognitionEnd.set_value(); // Notify to stop recognition.
});

WavFileReader reader(FILE_NAME);

vector<uint8_t> buffer(1000);
recognizer->StartContinuousRecognitionAsync().wait();

int readSamples = 0;
while((readSamples = reader.Read(buffer.data(), (uint32_t)buffer.size())) != 0)
{
    pushStream->Write(buffer.data(), readSamples);
}

pushStream->Close();
recognitionEnd.get_future().get();

recognizer->StopContinuousRecognitionAsync().get();
}

结果是

 Recognizing:my
 Offset=6800000
 Duration=2700000
 Recognizing:my voice is
 Offset=6800000
 Duration=8500000
 Recognizing:my voice is my
 Offset=6800000
 Duration=9800000
 Recognizing:my voice is my passport
 Offset=6800000
 Duration=14400000
 Recognizing:my voice is my passport verify me
 Offset=6800000
 Duration=26100000
 RECOGNIZED: Text=My voice is my passport, verify me.
  Offset=6800000
 Duration=28100000
 CANCELED: Reach the end of the file.

为什么每次结果的偏移量总是6800000?我认为应该是不断增加的,比如:“my”的开始偏移量为0,“my”的结束偏移量为100000,“my voice is”的开始偏移量为0,“my”的结束偏移量voice is" 200000。那么我可以得到句子中“my voice is”的开始时间和结束时间。但是现在我怎样才能得到每个结果的句子中的开始时间和结束时间呢?

4

1 回答 1

1

如果您仔细查看输出 - 有两个事件:

认可认可

识别:接收作为中间识别结果的识别信号事件。

已识别 :事件已识别表示收到最终识别结果。

所以你看到的偏移量是完整的句子(识别事件 - 通常在第一次暂停之前):我的声音是我的护照,验证我。因此,对于所有识别(中间)事件,偏移量将相同。因此,如果您有另一个已识别的事件,您会看到顺序偏移。因此,如果您在音频中有另一个句子 - 您可能会看到额外的识别事件和偏移量 - 像您预期的那样增长。

更新 :

附加说明: 每个已识别事件的持续时间从零开始增长。持续时间计数从零遍历到完整识别事件的持续时间。

所以例如

Recognizing:my
 Offset=6800000
 Duration=2700000
 Recognizing:my voice is
 Offset=6800000
 Duration=8500000

所以如果你想要一个偏移量:My Voice is- 你可以添加前一个的初始偏移量和持续时间 6800000 + 2700000 (开始时间)和结束时间将是 6800000 + 8500000 (当前持续时间)

更新 2

RECOGNIZED: Text=My voice is my passport, verify me.
  Offset=6800000
 Duration=28100000

它们在 100 纳秒内(10 ^-7 秒)

所以让我们来看看你的案子

您的偏移量是 6800000,即 0.68 秒

这意味着句子(或该音频流的完整识别事件)已在整个音频的第 0.68 秒开始。

说出(“我的声音是我的护照,请验证我。”)的持续时间或完成时间为 2.8(28100000) 秒。

第二句(识别的事件)的偏移量将大于此持续时间。

持续时间可以小于或大于 offset :

在整个音频的第 3 秒,我可以不间断地发出 4 秒长的音频流。

偏移量为 3 秒,持续时间为 4 秒

于 2021-01-25T19:28:26.557 回答