main.cpp (5.7 KB)
在ai子系统按照其提示配置好讯飞语音大模型后,在其应用ai助手尝试语音输入显示鉴权错误。是不是配置语音模型部分还有些问题啊。各位大佬有遇到过类似的问题么?都是怎么处理的欸。
运行用来测试麒麟AI SDK语音识别的程序后输出错误日志如下:
开始测试语音识别...
Error connecting to D-Bus address unix:path=/tmp/.kylin-ai-runtime-unix/0/core-speech.sock: Could not connect: No such file or directory
Error creating core speech server proxy: Server proxy connection is unavailable.
语音识别会话初始化失败!错误码:3
(process:38458): GLib-GIO-CRITICAL **: 22:56:44.070: g_dbus_proxy_call_sync_internal: assertion 'G_IS_DBUS_PROXY (proxy)' failed
Error creating core speech server proxy: Server proxy connection is unavailable.
源程序如下:
#include <filesystem>
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <gio/gio.h>
#include <coreai/speech/recognizer.h>
#include <coreai/speech/synthesizer.h>
#include <coreai/speech/audioconfig.h>
// 设置 PCM 文件路径为当前目录的文件
const char *PCM_FILE_PATH = "./audio_input.pcm";
// 全局变量用于语音识别结果
std::string recognizedText;
bool recognitionDone = false;
std::mutex mutex_lock;
std::condition_variable cv;
// 工具函数:打印错误信息
void handleError(GError *error, const std::string &context) {
if (error) {
std::cerr << "Error in " << context << ": " << error->message << std::endl;
g_error_free(error);
}
}
// 工具函数:读取 PCM 文件音频数据
std::vector<uint8_t> readAudioData(const std::string &filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::cerr << "无法打开文件: " << filePath << std::endl;
return {};
}
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> audioData(fileSize);
file.read(reinterpret_cast<char *>(audioData.data()), fileSize);
std::cout << "成功读取音频文件: " << filePath << ",大小: " << fileSize << " 字节" << std::endl;
return audioData;
}
// 测试 D-Bus 通信
void testDbusConnection() {
GError *error = nullptr;
// 创建 GDBusProxy
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(
G_BUS_TYPE_SESSION, // 使用会话总线
G_DBUS_PROXY_FLAGS_NONE, // 无特殊标志
nullptr, // 无接口
"org.freedesktop.DBus", // D-Bus 服务名
"/org/freedesktop/DBus", // 对象路径
"org.freedesktop.DBus", // 接口名
nullptr, // 无自定义 Cancellable
&error // 错误信息
);
if (!proxy) {
handleError(error, "创建 D-Bus 代理失败");
return;
}
std::cout << "D-Bus proxy created successfully!" << std::endl;
// 调用 ListNames 方法
GVariant *result = g_dbus_proxy_call_sync(
proxy,
"ListNames", // 方法名
nullptr, // 参数 (无)
G_DBUS_CALL_FLAGS_NONE, // 无特殊标志
-1, // 超时时间 (ms)
nullptr, // 无自定义 Cancellable
&error // 错误信息
);
if (!result) {
handleError(error, "调用 ListNames 方法失败");
} else {
// 打印返回结果
char *result_str = g_variant_print(result, true);
std::cout << "D-Bus 服务列表: " << result_str << std::endl;
g_free(result_str);
g_variant_unref(result);
}
// 确保释放代理后不再使用
g_object_unref(proxy);
}
// 语音识别回调函数
void recognitionCallback(SpeechRecognitionResult *result, void *user_data) {
std::cout << "回调函数被调用了!" << std::endl;
if (!result) {
std::cerr << "语音识别回调未返回有效结果。" << std::endl;
return;
}
const char *text = speech_recognition_result_get_text(result);
if (text) {
std::cout << "语音识别结果: " << text << std::endl;
{
std::lock_guard<std::mutex> lock(mutex_lock);
recognizedText = text;
recognitionDone = true;
}
cv.notify_one();
} else {
std::cerr << "语音识别回调中未返回有效文本。" << std::endl;
}
}
// 测试语音识别和语音合成
void testSpeechRecognitionAndSynthesis() {
if (!std::filesystem::exists(PCM_FILE_PATH)) {
std::cerr << "文件不存在: " << PCM_FILE_PATH << std::endl;
return;
}
std::vector<uint8_t> audioData = readAudioData(PCM_FILE_PATH);
if (audioData.empty()) {
std::cerr << "读取 PCM 文件失败。" << std::endl;
return;
}
// 初始化语音识别
SpeechRecognitionSession *recogSession = speech_recognizer_create_session();
SpeechModelConfig *recogModelConfig = speech_model_config_create();
speech_model_config_set_name(recogModelConfig, "讯飞-语音大模型");
speech_model_config_set_deploy_type(recogModelConfig, ModelDeployType::PublicCloud);
speech_recognizer_set_model_config(recogSession, recogModelConfig);
AudioConfig *recogAudioConfig = audio_config_create_once_audio_input_from_pcm_data(audioData.data(), audioData.size());
speech_recognizer_set_audio_config(recogSession, recogAudioConfig);
// 设置回调函数
speech_recognizer_result_set_callback(recogSession, recognitionCallback, nullptr);
speech_recognizer_init_session(recogSession);
// 异步语音识别
std::cout << "语音识别中..." << std::endl;
speech_recognizer_recognize_once_async(recogSession);
// 等待语音识别完成,设置超时为 30 秒
{
std::unique_lock<std::mutex> lock(mutex_lock);
if (!cv.wait_for(lock, std::chrono::seconds(30), [] { return recognitionDone; })) {
std::cerr << "语音识别超时。" << std::endl;
}
}
// 检查语音识别结果
if (!recognizedText.empty()) {
std::cout << "识别到的文本: " << recognizedText << std::endl;
} else {
std::cerr << "语音识别失败或无结果。" << std::endl;
}
// 清理资源
speech_recognizer_destroy_session(&recogSession);
speech_model_config_destroy(&recogModelConfig);
audio_config_destroy(&recogAudioConfig);
}
int main() {
std::cout << "开始测试 D-Bus 通信..." << std::endl;
testDbusConnection();
std::cout << "\n开始测试语音识别和语音合成..." << std::endl;
testSpeechRecognitionAndSynthesis();
return 0;
}