Whisper.cpp 0基础搭建音频文件转文字(Windows下C++)
#include <iostream>
#include <speechapi_cxx.h>
int main() {
std::string key = "您的Speech服务密钥";
std::string region = "您的Speech服务区域";
auto config = SpeechConfig::FromSubscription(key, region);
auto audioInput = AudioConfig::FromWavFileInput("16k_test.wav");
auto recognizer = SpeechRecognizer::FromConfig(config, audioInput);
recognizer->Recognized.Connect([](const SpeechRecognitionEventArgs& e) {
std::cout << "收到识别结果:" << e.Result->Text << std::endl;
});
recognizer->RecognitionErrorOccurred.Connect([](const SpeechRecognitionErrorEventArgs& e)
{
std::cerr << "发生识别错误:" << e.ErrorDetails << std::endl;
});
std::cout << "说话开始..." << std::endl;
recognizer->StartContinuousRecognitionAsync().get();
// 等待一段时间接收识别结果,例如10秒
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << "说话结束,停止识别..." << std::endl;
recognizer->StopContinuousRecognitionAsync().get();
return 0;
}
这段代码示例展示了如何使用Microsoft Cognitive Speech SDK来识别音频文件中的文本内容。代码首先配置了SpeechConfig,并从音频文件中读取了输入。然后创建了一个SpeechRecognizer实例,并连接了识别结果和错误事件。接着,使用异步方法开始连续识别,并等待一段时间来接收结果。最后,停止连续识别。这个例子简单直观地展示了如何将音频文件中的语音转换为文本。
评论已关闭