【问题标题】:How can I get begin time and end time for the conversion result of stream audio?如何获取流音频转换结果的开始时间和结束时间?
【发布时间】:2021-04-29 06:12:56
【问题描述】:

我正在使用 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, “我的声音是”的开始偏移量是 0,“我的声音是”的结束偏移量是 200000。 然后我可以得到句子中“我的声音是”的开始时间和结束时间。但是现在我怎样才能得到每个结果的句子中的开始时间和结束时间呢?

【问题讨论】:

    标签: azure speech azure-speech


    【解决方案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) 秒。

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

    持续时间可以小于或大于偏移量:

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

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

    【讨论】:

    • 谢谢,有没有办法获得单词或短语的偏移量?现在偏移量是完整的句子。
    • 添加了一个可以回答您问题的附加说明。
    • 谢谢,但我还是对时间感到困惑,offset是给句子的,但是为什么duration time比offset长,它们不应该相等吗?比如我的结果:Offset=6800000 Duration=8500000,
    • 持续时间可以小于或大于偏移量:在整个音频的第 3 秒,我可以不间断地发出 4 秒长的音频流。偏移量为 3 秒,持续时间为 4 秒
    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多