Browse Source

update zlinuxcomponents

master
zhaohe 2 years ago
parent
commit
4af621289c
  1. 1
      CMakeLists.txt
  2. 2
      cmakepc.cmake
  3. 2
      dep/zlinuxcomponents
  4. 43
      src/service/voiceprocess/asr_service.cpp
  5. 22
      src/service/voiceprocess/asr_service.hpp
  6. 67
      src/test_asr_main.cpp

1
CMakeLists.txt

@ -104,3 +104,4 @@ zadd_executable_simple(TARGET alsaplayer_main.out SRC
dep/zlinuxcomponents/alsaplayer/alsaplayer_main.cpp)
zadd_executable_simple(TARGET audio_recorder_main.out SRC
dep/zlinuxcomponents/audio/audio_recorder_main.cpp)
zadd_executable_simple(TARGET test_asr_main.out SRC dep/zlinuxcomponents/aiui_ws/aiui_main.c)

2
cmakepc.cmake

@ -59,3 +59,5 @@ zadd_executable_simple(TARGET alsaplayer_main.out SRC
dep/zlinuxcomponents/alsaplayer/alsaplayer_main.cpp)
zadd_executable_simple(TARGET audio_recorder_main.out SRC
dep/zlinuxcomponents/audio/audio_recorder_main.cpp)
zadd_executable_simple(TARGET test_asr_main.out SRC src/test_asr_main.cpp)
zadd_executable_simple(TARGET aiui_main.out SRC dep/zlinuxcomponents/aiui_ws/aiui_main.c)

2
dep/zlinuxcomponents

@ -1 +1 @@
Subproject commit cc20701d9b4e719524821a98fb7b0a8334fbe3f9
Subproject commit 2344b60385743afbe743660579ee72e8e9760552

43
src/service/voiceprocess/asr_service.cpp

@ -0,0 +1,43 @@
#include "asr_service.hpp"
#include "zlinuxcomponents/aiui_ws/aiui.h"
using namespace iflytop;
using namespace core;
using namespace std;
static AiuiService *thisClass = nullptr;
void aiui_message_cb(const char *data, int len) {
if (thisClass) {
thisClass->call_aiui_message_cb(data, len);
}
}
void aiui_error_cb(int code, const char *str) {
if (thisClass) {
thisClass->call_aiui_error_cb(code, str);
}
}
AiuiService::AiuiService() { thisClass = this; }
int AiuiService::aiuiInit(const char *appid, const char *key, const char *param) {
thisClass = this;
return aiui_init(appid, key, param, aiui_message_cb, aiui_error_cb);
}
int AiuiService::aiuiWrite(const char *audio, int len) { return aiui_write(audio, len); }
int AiuiService::aiuiFinished() { return aiui_finished(); }
void AiuiService::aiuiDestroy() {
aiui_destroy();
thisClass = nullptr;
}
void AiuiService::call_aiui_message_cb(const char *data, int len) {
try {
json j = json::parse(data);
onAsrResult(j);
} catch (const std::exception &e) {
logger->error("AiuiService::call_aiui_message_cb error: {}", e.what());
}
}
void AiuiService::call_aiui_error_cb(int code, const char *str) {
logger->error("AiuiService::call_aiui_error_cb error: {} => {}", code, str);
onError(code, str);
}

22
src/service/voiceprocess/asr_service.hpp

@ -34,6 +34,25 @@ namespace iflytop {
using namespace std;
using namespace core;
using namespace nlohmann;
class AiuiService : public enable_shared_from_this<AsrService> {
ENABLE_LOGGER(AiuiService);
public:
nod::signal<void(json &asrResult)> onAsrResult;
nod::signal<void(int code, const char *str)> onError;
AiuiService();
int aiuiInit(const char *appid, const char *key, const char *param);
int aiuiWrite(const char *audio, int len);
int aiuiFinished();
void aiuiDestroy();
void call_aiui_message_cb(const char *data, int len);
void call_aiui_error_cb(int code, const char *str);
};
class AsrService : public enable_shared_from_this<AsrService> {
ENABLE_LOGGER(AsrService);
@ -47,7 +66,10 @@ class AsrService : public enable_shared_from_this<AsrService> {
AsrService(){};
void initialize(string appid, string key);
void startRequest();
void endRequest();
void writeVoice(uint16_t *voice, size_t voiceLen);
void writeVoice(string text);
void writeVoiceEnd();
};
} // namespace iflytop

67
src/test_asr_main.cpp

@ -0,0 +1,67 @@
#include "zlinuxcomponents/zmainhelper.hpp"
//
#include "configs/config.hpp"
#include "iflytopcpp/core/spdlogfactory/logger.hpp"
#include "iflytopcpp/core/thread/thread.hpp"
#include "spdlog/spdlog.h"
#include "version.hpp"
#include "zlinuxcomponents/rootfs_auto_update/rootfs_auto_update.hpp"
#include "zservice_container/zservice_container.hpp"
#include "zwebservice/zwebservice.hpp"
//
#include "service/device_io_service.hpp"
#include "service/device_io_service_mock.hpp"
#include "service/light_control_service.hpp"
#include "service/main_control_service.hpp"
#include "service/report_service.hpp"
//
#include "zlinuxcomponents/alsaplayer/AudioPlayerAlsaImpl.hpp"
// #include "zlinuxcomponents/audio/audio_recoder.hpp"
#include <curl/curl.h>
#include <iostream>
#include <string>
#include "zlinuxcomponents/aiui_ws/aiui.h"
//
using namespace iflytop;
using namespace core;
using namespace std;
using namespace clipp;
ZMAIN();
/***********************************************************************************************************************
* =======================================================Main======================================================== *
***********************************************************************************************************************/
static void onmessage(const char *data, int len) {}
static void onerror(int code, const char *str) { printf("aiui_init onerror %d => %s\n", code, str); }
void Main::onSIGINT() { exit(0); }
int Main::main(int argc, char *argv[]) {
string g_host_server_ip;
string g_device_id;
spdlog::flush_on(spdlog::level::debug);
const char *appid = "5938b7c7"; // 应用ID,在AIUI开放平台创建并设置
const char *key = "19c1f7becc78eedc7826b485aabe30de"; // 接口密钥,在AIUI开放平台查看
const char *param =
"{\"result_level\":\"plain\",\"auth_id\":\"ac30105366ea460f9ff08ddac0c4f71e\",\"data_"
"type\":\"text\","
"\"scene\":\"main_box\",\"sample_rate\":\"16000\", "
"\"context\":\"{\\\"sdk_support\\\":[\\\"nlp\\\",\\\"tts\\\"]}\"}";
aiui_init(appid, key, param, onmessage, onerror);
const char *text = "播放音乐";
aiui_write(text, strlen(text));
aiui_finished();
aiui_destroy();
while (true) sleep(1000);
}
Loading…
Cancel
Save