From 36e04c945fcaf61f9afe37b34310c6707e831953 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Wed, 11 Jan 2023 19:22:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BD=95=E9=9F=B3=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++- CMakeLists.txt | 48 ++++++++++++++++++++++++++++++---------- dep/iflytopcpp | 2 +- sh/envsetup_rk3328.sh | 24 ++++++++++++++++---- sh/iflytool.sh | 2 +- sh/packet.sh | 2 +- src/audio_recorder.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 50 +++++++++++------------------------------- src/zmain.hpp | 46 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 180 insertions(+), 56 deletions(-) create mode 100644 src/audio_recorder.cpp create mode 100644 src/zmain.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index dde7f69..e92d171 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -86,6 +86,7 @@ "__functional_base_03": "cpp", "__hash_table": "cpp", "__tree": "cpp", - "__tuple": "cpp" + "__tuple": "cpp", + "*.ipp": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 094d3c7..5bf9005 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,9 @@ set(CMAKE_INSTALL_PREFIX "./") include(zcmake/zcmake.cmake) project(app) +set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) +set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) + # 设置通用编译选项 set(C_CPP_FLAGS "${C_CPP_FLAGS} ") set(C_CPP_FLAGS @@ -43,23 +46,46 @@ include(dep/zservice_container/module.cmake) include(dep/zwebservice/module.cmake) include(dep/zlinuxcomponents/module.cmake) -zadd_executable( +function(zadd_executable_simple) + set(oneValueArgs NAME TARGET) + set(multiValueArgs SRC) + cmake_parse_arguments(VAR "${options}" "${oneValueArgs}" "${multiValueArgs}" + ${ARGN}) + zadd_executable( + TARGET + ${VAR_TARGET} + INSTALL + ./app + LINK_LIBRARIES + asound + pthread + INCLUDE_DIRECTORIES + ${DEP_INCLUDE} + ./src + DEFINES + ${DEP_DEFINE} + SRC + ${DEP_SRC} + ${VAR_SRC}) +endfunction(zadd_executable_simple) + +zadd_executable_simple( + # TARGET TARGET app.out - INSTALL - ./app - LINK_LIBRARIES - pthread - INCLUDE_DIRECTORIES - ${DEP_INCLUDE} - ./src - DEFINES - ${DEP_DEFINE} + # SRC SRC - ${DEP_SRC} src/main.cpp src/service/device_io_service.cpp src/service/device_io_service_mock.cpp src/service/main_control_service.cpp src/service/light_control_service.cpp src/service/report_service.cpp) + +zadd_executable_simple( + # TARGET + TARGET # + audio_recorder.out + # SRC + SRC + src/audio_recorder.cpp) diff --git a/dep/iflytopcpp b/dep/iflytopcpp index ea4118d..2ae1504 160000 --- a/dep/iflytopcpp +++ b/dep/iflytopcpp @@ -1 +1 @@ -Subproject commit ea4118ddeb8238ccc065a2c11237ad7be056f89e +Subproject commit 2ae150407bb2beb45b2d3155a114992bdb59798e diff --git a/sh/envsetup_rk3328.sh b/sh/envsetup_rk3328.sh index 8b4f7cc..a2ba88c 100755 --- a/sh/envsetup_rk3328.sh +++ b/sh/envsetup_rk3328.sh @@ -1,11 +1,27 @@ #!/bin/bash # 下载编译器 cd ${PROJECT_PATH}/build -wget -c "ftp://zftpuser:zftpzwsd@192.168.1.2/zwsd/gnu_gcc_release/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.gz" -tar -xvf "gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.gz" + +wget -c "192.168.1.2:8021/sdk_firefly_rk3328_v2.5.1/buildtools/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.gz" +rm -rf gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu +tar -xvf gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.gz + +BUILD_TOOLS_PATH=${PROJECT_PATH}/build/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/ + +# alsa 支持 +wget -c "192.168.1.2:8021/sdk_firefly_rk3328_v2.5.1/buildtools/libasound2_dev1.1.3-5ubuntu0.6.tar.gz" +rm -rf libasound2_dev1.1.3-5ubuntu0.6 +tar -xvf libasound2_dev1.1.3-5ubuntu0.6.tar.gz +mkdir -p ${BUILD_TOOLS_PATH}/pkgconfig/ +mkdir -p ${BUILD_TOOLS_PATH}/include/sys/ + +mv libasound2_dev1.1.3-5ubuntu0.6/unpack/usr/lib/aarch64-linux-gnu/libasound.* ${BUILD_TOOLS_PATH}/lib/ +mv libasound2_dev1.1.3-5ubuntu0.6/unpack/usr/lib/aarch64-linux-gnu/pkgconfig/alsa.pc ${BUILD_TOOLS_PATH}/pkgconfig/ +mv libasound2_dev1.1.3-5ubuntu0.6/unpack/usr/include/alsa ${BUILD_TOOLS_PATH}/include/ +mv libasound2_dev1.1.3-5ubuntu0.6/unpack/usr/include/sys/* ${BUILD_TOOLS_PATH}/include/sys/ # 构造build.sh echo "cmake ../ \\" >${PROJECT_PATH}/build/build.sh -echo "-DCMAKE_C_COMPILER=${PROJECT_PATH}/build/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc \\" >>${PROJECT_PATH}/build/build.sh -echo "-DCMAKE_CXX_COMPILER=${PROJECT_PATH}/build/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ \\" >>${PROJECT_PATH}/build/build.sh +echo "-DCMAKE_C_COMPILER=ccache ${PROJECT_PATH}/build/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc \\" >>${PROJECT_PATH}/build/build.sh +echo "-DCMAKE_CXX_COMPILER=ccache ${PROJECT_PATH}/build/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ \\" >>${PROJECT_PATH}/build/build.sh chmod +x ${PROJECT_PATH}/build/build.sh diff --git a/sh/iflytool.sh b/sh/iflytool.sh index b5ef844..4a79928 100755 --- a/sh/iflytool.sh +++ b/sh/iflytool.sh @@ -38,7 +38,7 @@ function do_flashapp_to_targetboard() { ssh ${TARGET_USER}@${IP} "killall app.out" set -e # 拷贝目标程序 - scp -r ${PROJECT_PATH}/build/app/app.out ${TARGET_USER}@${IP}:/app/ + scp -r ${PROJECT_PATH}/build/app/*.out ${TARGET_USER}@${IP}:/app/ } function do_flashwebapp_to_targetboard() { diff --git a/sh/packet.sh b/sh/packet.sh index cef5be2..c3672d6 100755 --- a/sh/packet.sh +++ b/sh/packet.sh @@ -43,7 +43,7 @@ cd $PROJECT_PATH/build # ./build.sh && make -j8 && make install #2.打包APP -cp $PROJECT_PATH/build/app/app.out $PACKET_DIR/ +cp $PROJECT_PATH/build/app/*.out $PACKET_DIR/ #3.3 打包webapp cp -rf $PROJECT_PATH/webapp $PACKET_DIR/ diff --git a/src/audio_recorder.cpp b/src/audio_recorder.cpp new file mode 100644 index 0000000..a2dc77e --- /dev/null +++ b/src/audio_recorder.cpp @@ -0,0 +1,59 @@ +#include "zmain.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/audio/audio_recoder.hpp" +#include "zlinuxcomponents/rootfs_auto_update.hpp" +#include "zservice_container/zservice_container.hpp" +// +using namespace iflytop; +using namespace core; +using namespace std; +using namespace clipp; + +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +ZMAIN(); +/** + * @brief ctrl+C 回调 + */ +ofstream outputwav("audio_recorder.wav", ios::binary | ios::trunc); +void Main::onSIGINT() { + logger->info("save audio to {}", "audio_recorder.wav"); + exit(0); +} + +int Main::main(int argc, char *argv[]) { + string g_host_server_ip; + string g_device_id; + + int channel = 0; + int rate = 0; +#if 1 + auto cli = ( // + (required("-c") & value("c", channel)).doc("channel"), // + (required("-r") & value("r", rate)).doc("rate") // + ); + if (!parse(argc, argv, cli)) { + cout << make_man_page(cli, argv[0]); + exit(-1); + } +#endif + + shared_ptr audioRecoder(new AudioRecoder()); + audioRecoder->onRecordData.connect([&](shared_ptr audioclip) { + logger->info("on audio {}", audioclip->toString()); + // 保存语音到文件 + outputwav.write((char *)audioclip->getVoice(), audioclip->getVoiceLen()); + }); + + audioRecoder->initialize("recorder", channel, rate, SND_PCM_FORMAT_S16_LE); + audioRecoder->startRecord(); + logger->info("system setup end."); + while (true) sleep(1000); +} diff --git a/src/main.cpp b/src/main.cpp index e1d9530..28f910f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ +#include "zmain.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 "zclipp/include/clipp.h" + #include "zlinuxcomponents/rootfs_auto_update.hpp" #include "zservice_container/zservice_container.hpp" #include "zwebservice/zwebservice.hpp" @@ -14,47 +16,19 @@ #include "service/main_control_service.hpp" #include "service/report_service.hpp" // -#include "dirent.h" +#include "zlinuxcomponents/audio/audio_recoder.hpp" + +// using namespace iflytop; using namespace core; using namespace std; using namespace clipp; -class Main { - ENABLE_LOGGER(Main); - - private: - unique_ptr thread; - - private: - int main(int argc, char *argv[]); - - public: - Main(/* args */) {} - ~Main() {} - void run(int argc, char *argv[]) { - thread.reset(new Thread("main", [&]() { exit(main(argc, argv)); })); - while (true) sleep(1000); - } -}; -int main(int argc, char *argv[]) { - Main main; - main.run(argc, argv); -} -#define BUILD_AND_REG_SERRVICE(type, ...) \ - logger->info("build {}.....", #type); \ - shared_ptr type##_val(new type(__VA_ARGS__)); \ - ServiceContrainer::get().regService(type##_val); - -#define BUILD_AND_REG_MOCK_SERRVICE(type, mocktype, ...) \ - logger->info("build {}.....", #type); \ - shared_ptr type##_val(new mocktype(__VA_ARGS__)); \ - ServiceContrainer::get().regService(type##_val); - +ZMAIN(); /*********************************************************************************************************************** * =======================================================Main======================================================== * ***********************************************************************************************************************/ - +void Main::onSIGINT() { exit(0); } int Main::main(int argc, char *argv[]) { string g_host_server_ip; string g_device_id; @@ -81,13 +55,12 @@ int Main::main(int argc, char *argv[]) { GET_SERVICE(Config)->initialize(); if (!g_device_id.empty()) GET_SERVICE(Config)->set_deviceId(g_device_id); - /** * @brief 比较rootfs目录中的文件和系统中对应路径的文件是否一致,如果不一致则替换 - * + * * 例如 * ./rootfs/etc/asound.conf 和 /etc/asound.conf 不一致,则替换 - * + * */ auto rootfsAutoUpdate = make_shared(); rootfsAutoUpdate->compareAndReplace(); @@ -98,6 +71,9 @@ int Main::main(int argc, char *argv[]) { // system("reboot"); } + // AudioRecoder::get().initialize(); + shared_ptr audioRecoder(new AudioRecoder()); + #if 0 BUILD_AND_REG_SERRVICE(DeviceIOService); #else diff --git a/src/zmain.hpp b/src/zmain.hpp new file mode 100644 index 0000000..4312580 --- /dev/null +++ b/src/zmain.hpp @@ -0,0 +1,46 @@ + +#include +#include +#include +#include +#include + +#include "zclipp/include/clipp.h" + +#define ZMAIN() \ + class Main { \ + ENABLE_LOGGER(Main); \ + \ + private: \ + unique_ptr thread; \ + \ + private: \ + int main(int argc, char *argv[]); \ + \ + public: \ + Main(/* args */) {} \ + ~Main() {} \ + void run(int argc, char *argv[]) { \ + thread.reset(new Thread("main", [&]() { exit(main(argc, argv)); })); \ + while (true) sleep(1000); \ + } \ + void onSIGINT(); \ + }; \ + Main *g_main; \ + void onsignal(int signo) { g_main->onSIGINT(); } \ + int main(int argc, char *argv[]) { \ + Main main; \ + g_main = &main; \ + ::signal(SIGINT, onsignal); \ + main.run(argc, argv); \ + } + +#define BUILD_AND_REG_SERRVICE(type, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new type(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val); + +#define BUILD_AND_REG_MOCK_SERRVICE(type, mocktype, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new mocktype(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val);