FaceAccess/Record/RkRecorder.cpp

226 lines
7.1 KiB
C++
Raw Normal View History

2024-06-18 14:27:48 +08:00
#include "BoostLog.h"
#include <fstream>
#include <rkmedia/rkmedia_api.h>
#include <signal.h>
#include <thread>
#define VQEFILE "/data/sdcard/RKAP_3A_Para.bin"
#define ALSA_PATH "default" // get from "arecord -L"
static bool quit = false;
static void sigterm_handler(int sig) {
fprintf(stderr, "signal %d\n", sig);
quit = true;
}
std::shared_ptr<std::ofstream> ofs;
void run() {
ofs = std::make_shared<std::ofstream>("/data/sdcard/test.pcm", std::ofstream::binary);
while (!quit) {
auto mediaBuffer = RK_MPI_SYS_GetMediaBuffer(RK_ID_AI, 0, -1);
if (!mediaBuffer) {
LOG(error) << "RK_MPI_SYS_GetMediaBuffer() failed.";
continue;
}
LOG(info) << "get frame, timestamp: " << RK_MPI_MB_GetTimestamp(mediaBuffer)
<< ", size: " << RK_MPI_MB_GetSize(mediaBuffer);
ofs->write((const char *)RK_MPI_MB_GetPtr(mediaBuffer), RK_MPI_MB_GetSize(mediaBuffer));
RK_MPI_MB_ReleaseBuffer(mediaBuffer);
}
ofs.reset();
}
void rkDemo() {
AI_CHN_ATTR_S ai_attr;
ai_attr.pcAudioNode = "default";
ai_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
ai_attr.u32NbSamples = 16 * 20;
ai_attr.u32SampleRate = 16000;
ai_attr.u32Channels = 2;
ai_attr.enAiLayout = AI_LAYOUT_REF_MIC;
int status = RK_MPI_AI_SetChnAttr(0, &ai_attr);
status |= RK_MPI_AI_EnableChn(0);
if (status) {
LOG(error) << "enable AI[0] failed, status = " << status;
return;
}
AI_TALKVQE_CONFIG_S config = {0};
strcpy(config.aParamFilePath, VQEFILE);
config.s32WorkSampleRate = 16000;
config.s32FrameSample = 16 * 20;
config.u32OpenMask = AI_TALKVQE_MASK_AEC | AI_TALKVQE_MASK_ANR | AI_TALKVQE_MASK_AGC;
status = RK_MPI_AI_SetTalkVqeAttr(0, &config);
if (status) {
LOG(error) << "RK_MPI_AI_SetTalkVqeAttr() failed, status: " << status;
return;
}
status = RK_MPI_AI_EnableVqe(0);
if (status) {
LOG(error) << "RK_MPI_AI_EnableVqe() failed, status: " << status;
return;
}
std::thread thread(&run);
status = RK_MPI_AI_StartStream(0);
if (status) {
LOG(info) << "start AI failed, status: " << status;
return;
}
signal(SIGINT, sigterm_handler);
while (!quit) {
usleep(500000);
}
if (thread.joinable()) {
thread.join();
}
RK_MPI_AI_DisableChn(0);
}
static RK_U32 g_enWorkSampleRate = 8000;
static RK_U32 g_s32VqeFrameSample = 320; // 20ms;
// #define VQEFILE "/data/sdcard/3a.bin"
int AI_VqeProcess_AO() {
AI_TALKVQE_CONFIG_S stAiVqeTalkAttr;
AI_RECORDVQE_CONFIG_S stAiVqeRecordAttr;
AO_VQE_CONFIG_S stAoVqeAttr;
MPP_CHN_S mpp_chn_ai, mpp_chn_ao;
memset(&stAiVqeTalkAttr, 0, sizeof(AI_TALKVQE_CONFIG_S));
stAiVqeTalkAttr.s32WorkSampleRate = g_enWorkSampleRate;
stAiVqeTalkAttr.s32FrameSample = g_s32VqeFrameSample;
strcpy(stAiVqeTalkAttr.aParamFilePath, VQEFILE);
stAiVqeTalkAttr.u32OpenMask = AI_TALKVQE_MASK_AEC | AI_TALKVQE_MASK_ANR | AI_TALKVQE_MASK_AGC;
// stAiVqeTalkAttr.u32OpenMask = AI_TALKVQE_MASK_AEC ;
memset(&stAoVqeAttr, 0, sizeof(AO_VQE_CONFIG_S));
stAoVqeAttr.s32WorkSampleRate = g_enWorkSampleRate;
stAoVqeAttr.s32FrameSample = g_s32VqeFrameSample;
strcpy(stAoVqeAttr.aParamFilePath, VQEFILE);
stAoVqeAttr.u32OpenMask = AO_VQE_MASK_ANR | AO_VQE_MASK_AGC;
mpp_chn_ai.enModId = RK_ID_AI;
mpp_chn_ai.s32ChnId = 0;
mpp_chn_ao.enModId = RK_ID_AO;
mpp_chn_ao.s32ChnId = 0;
AI_CHN_ATTR_S ai_attr;
ai_attr.pcAudioNode = "default";
ai_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
ai_attr.u32NbSamples = 320;
ai_attr.u32SampleRate = g_enWorkSampleRate;
ai_attr.u32Channels = 1;
ai_attr.enAiLayout = AI_LAYOUT_MIC_REF; // remove ref channel, and output mic mono
AO_CHN_ATTR_S ao_attr;
ao_attr.pcAudioNode = "default";
ao_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
ao_attr.u32NbSamples = 320;
ao_attr.u32SampleRate = g_enWorkSampleRate;
ao_attr.u32Channels = 1;
// 1. create AI
RK_MPI_AI_SetChnAttr(mpp_chn_ai.s32ChnId, &ai_attr);
RK_MPI_AI_EnableChn(mpp_chn_ai.s32ChnId);
RK_MPI_AI_SetTalkVqeAttr(mpp_chn_ai.s32ChnId, &stAiVqeTalkAttr);
RK_MPI_AI_EnableVqe(mpp_chn_ai.s32ChnId);
// 2. create AO
RK_MPI_AO_SetChnAttr(mpp_chn_ao.s32ChnId, &ao_attr);
RK_MPI_AO_EnableChn(mpp_chn_ao.s32ChnId);
RK_MPI_AO_SetVqeAttr(mpp_chn_ao.s32ChnId, &stAoVqeAttr);
RK_MPI_AO_EnableVqe(mpp_chn_ao.s32ChnId);
// 3. bind AI-AO
RK_MPI_SYS_Bind(&mpp_chn_ai, &mpp_chn_ao);
printf("%s initial finish\n", __func__);
signal(SIGINT, sigterm_handler);
while (!quit) {
usleep(500000);
}
printf("%s exit!\n", __func__);
RK_MPI_SYS_UnBind(&mpp_chn_ai, &mpp_chn_ao);
RK_MPI_AI_DisableChn(mpp_chn_ai.s32ChnId);
RK_MPI_AO_DisableChn(mpp_chn_ao.s32ChnId);
return RK_SUCCESS;
}
int AI_VqeProcess_AO1() {
AI_TALKVQE_CONFIG_S stAiVqeTalkAttr;
AI_RECORDVQE_CONFIG_S stAiVqeRecordAttr;
AO_VQE_CONFIG_S stAoVqeAttr;
MPP_CHN_S mpp_chn_ai, mpp_chn_ao;
memset(&stAiVqeTalkAttr, 0, sizeof(AI_TALKVQE_CONFIG_S));
stAiVqeTalkAttr.s32WorkSampleRate = g_enWorkSampleRate;
stAiVqeTalkAttr.s32FrameSample = g_s32VqeFrameSample;
strcpy(stAiVqeTalkAttr.aParamFilePath, VQEFILE);
stAiVqeTalkAttr.u32OpenMask = AI_TALKVQE_MASK_AEC | AI_TALKVQE_MASK_ANR | AI_TALKVQE_MASK_AGC;
memset(&stAoVqeAttr, 0, sizeof(AO_VQE_CONFIG_S));
stAoVqeAttr.s32WorkSampleRate = g_enWorkSampleRate;
stAoVqeAttr.s32FrameSample = g_s32VqeFrameSample;
strcpy(stAoVqeAttr.aParamFilePath, VQEFILE);
stAoVqeAttr.u32OpenMask = AO_VQE_MASK_ANR | AO_VQE_MASK_AGC;
RK_MPI_SYS_Init();
mpp_chn_ai.enModId = RK_ID_AI;
mpp_chn_ai.s32ChnId = 0;
mpp_chn_ao.enModId = RK_ID_AO;
mpp_chn_ao.s32ChnId = 0;
AI_CHN_ATTR_S ai_attr;
ai_attr.pcAudioNode = ALSA_PATH;
ai_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
ai_attr.u32NbSamples = 640;
ai_attr.u32SampleRate = g_enWorkSampleRate;
ai_attr.u32Channels = 1;
ai_attr.enAiLayout = AI_LAYOUT_MIC_REF; // remove ref channel, and output mic mono
AO_CHN_ATTR_S ao_attr;
ao_attr.pcAudioNode = ALSA_PATH;
ao_attr.enSampleFormat = RK_SAMPLE_FMT_S16;
ao_attr.u32NbSamples = 640;
ao_attr.u32SampleRate = g_enWorkSampleRate;
ao_attr.u32Channels = 1;
// 1. create AI
RK_MPI_AI_SetChnAttr(mpp_chn_ai.s32ChnId, &ai_attr);
RK_MPI_AI_EnableChn(mpp_chn_ai.s32ChnId);
RK_MPI_AI_SetTalkVqeAttr(mpp_chn_ai.s32ChnId, &stAiVqeTalkAttr);
RK_MPI_AI_EnableVqe(mpp_chn_ai.s32ChnId);
// 2. create AO
RK_MPI_AO_SetChnAttr(mpp_chn_ao.s32ChnId, &ao_attr);
RK_MPI_AO_EnableChn(mpp_chn_ao.s32ChnId);
RK_MPI_AO_SetVqeAttr(mpp_chn_ao.s32ChnId, &stAoVqeAttr);
RK_MPI_AO_EnableVqe(mpp_chn_ao.s32ChnId);
// 3. bind AI-AO
RK_MPI_SYS_Bind(&mpp_chn_ai, &mpp_chn_ao);
printf("%s initial finish\n", __func__);
signal(SIGINT, sigterm_handler);
while (!quit) {
usleep(500000);
}
printf("%s exit!\n", __func__);
RK_MPI_SYS_UnBind(&mpp_chn_ai, &mpp_chn_ao);
RK_MPI_AI_DisableChn(mpp_chn_ai.s32ChnId);
RK_MPI_AO_DisableChn(mpp_chn_ao.s32ChnId);
return RK_SUCCESS;
}